|
- import {
- describe,
- beforeAll,
- afterAll,
- it,
- expect,
- vi, Mock,
- } from 'vitest';
-
- import {
- app,
- Endpoint,
- Operation,
- composeRecipes,
- } from '@modal-sh/yasumi';
- import {DataSource, DataSourceQuery, EmplaceDetails, Server} from '@modal-sh/yasumi/backend';
- import {Client} from '@modal-sh/yasumi/client';
- import {server} from '@modal-sh/yasumi-extender-http/backend';
- import {client} from '@modal-sh/yasumi-extender-http/client';
- import {addResourceRecipe, ResourceItemFetchedResponse} from '@modal-sh/yasumi-recipe-resource';
-
- describe('default', () => {
- let theClient: Client;
- let theServer: Server;
- let theRawEndpoint: Endpoint;
- let theOperation: Operation;
- let dataSource: Record<keyof DataSource, Mock>;
-
- beforeAll(() => {
- dataSource = {
- create: vi.fn(async (data) => data),
- getById: vi.fn(async () => ({})),
- delete: vi.fn(),
- emplace: vi.fn(async () => [{}, { isCreated: false }]),
- getMultiple: vi.fn(async () => []),
- getSingle: vi.fn(async () => ({})),
- getTotalCount: vi.fn(async () => 1),
- newId: vi.fn(async () => 1),
- patch: vi.fn(async (id, data) => ({ ...data, id })),
- initialize: vi.fn(async () => {}),
- };
- });
-
- afterAll(() => {
- dataSource.getById.mockReset();
- });
-
- beforeAll(async () => {
- const {
- app: theApp,
- operations,
- backend: theBackend,
- } = composeRecipes([
- addResourceRecipe({ endpointName: 'users', dataSource, }),
- addResourceRecipe({ endpointName: 'posts', dataSource, })
- ])({
- app: app({
- name: 'default' as const,
- }),
- });
-
- theRawEndpoint = theApp.endpoints.get('users');
- theOperation = operations.fetch;
-
- theServer = server({
- backend: theBackend,
- });
-
- const connectionParams = {
- port: 3001,
- };
-
- await theServer.serve(connectionParams);
-
- theClient = client({
- app: theApp,
- });
-
- await theClient.connect(connectionParams);
- });
-
- afterAll(async () => {
- await theClient.disconnect();
- await theServer.close();
- });
-
- it('works', async () => {
- // TODO create wrapper for fetch's Response here
- //
- // should we create a helper object to process client-side received response from server's sent response?
- //
- // the motivation is to remove the manual deserialization from the client (provide serialization on the response
- // object so as the client is not limited to .text(), .json(), .arrayBuffer() etc)
- const responseRaw = await theClient
- .at(theRawEndpoint)
- .makeRequest(
- theOperation
- .search({
- foo: 'bar',
- })
- );
-
- const response = ResourceItemFetchedResponse.fromFetchResponse(responseRaw);
-
- expect(response).toHaveProperty('statusCode', 200);
- expect(response).toHaveProperty('statusMessage', 'Resource Collection Fetched');
- });
-
- it('works for items', async () => {
- // TODO create wrapper for fetch's Response here
- //
- // should we create a helper object to process client-side received response from server's sent response?
- //
- // the motivation is to remove the manual deserialization from the client (provide serialization on the response
- // object so as the client is not limited to .text(), .json(), .arrayBuffer() etc)
- const responseRaw = await theClient
- .at(theRawEndpoint, { resourceId: 3 })
- // TODO how to inject extra data (e.g. headers, body) in the operation (e.g. auth)?
- .makeRequest(
- theOperation
- .search({
- foo: 'bar',
- }) // allow multiple calls of .search() to add to search params
- );
-
- const response = ResourceItemFetchedResponse.fromFetchResponse(responseRaw);
-
- expect(response).toHaveProperty('statusCode', 200);
- expect(response).toHaveProperty('statusMessage', 'Resource Item Fetched');
- });
- });
|