- import {
- describe,
- beforeAll,
- afterAll,
- it,
- expect,
- Mock,
- } from 'vitest';
-
- import {
- statusCodes
- } from '@modal-sh/yasumi';
- import {
- DataSource,
- Server,
- } from '@modal-sh/yasumi/backend';
- import {Client} from '@modal-sh/yasumi/client';
- import {client} from '@modal-sh/yasumi-extender-http/client';
- import {ResourceItemFetchedResponse} from '@modal-sh/yasumi-recipe-resource';
- import {createDummyDataSource} from './fixtures/data-source';
-
- import {setupApp} from '../src/setup';
-
- const connectionParams = {
- port: 3001,
- };
-
- describe('default', () => {
- let theClient: Client;
- let theServer: Server;
- let dataSource: Record<keyof DataSource, Mock>;
-
- beforeAll(() => {
- dataSource = createDummyDataSource();
- });
-
- afterAll(() => {
- dataSource.getById.mockReset();
- });
-
- beforeAll(async () => {
- const {
- app: theApp,
- server: createdServer,
- } = setupApp(dataSource);
- theServer = createdServer;
-
- await theServer.serve(connectionParams);
-
- theClient = client({
- app: theApp,
- });
-
- await theClient.connect(connectionParams);
- });
-
- afterAll(async () => {
- await theClient.disconnect();
- await theServer.close();
- });
-
- it('works', async () => {
- const theEndpoint = theClient.app.endpoints.get('users');
- const theOperation = theClient.app.operations.get('fetch');
- // 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(theEndpoint)
- .makeRequest(
- theOperation
- .search({
- foo: 'bar',
- })
- );
-
- const response = ResourceItemFetchedResponse.fromFetchResponse(responseRaw);
-
- expect(response).toHaveProperty('statusCode', statusCodes.HTTP_STATUS_OK);
- expect(response).toHaveProperty('statusMessage', 'Resource Collection Fetched');
- console.log(responseRaw.headers);
- });
-
- it('works for items', async () => {
- const theEndpoint = theClient.app.endpoints.get('users');
- const theOperation = theClient.app.operations.get('fetch');
- // 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(theEndpoint, { 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', statusCodes.HTTP_STATUS_OK);
- expect(response).toHaveProperty('statusMessage', 'Resource Item Fetched');
- });
- });
|