|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- import {
- describe,
- beforeAll,
- afterAll,
- it,
- expect,
- vi, Mock,
- } from 'vitest';
-
- import {
- app,
- Endpoint,
- Operation,
- } from '../../src/common';
- import {DataSource, DataSourceQuery, EmplaceDetails, Server} from '../../src/backend';
- import {Client} from '../../src/client';
- import {server} from '../../src/extenders/http/backend';
- import {client} from '../../src/extenders/http/client';
- import {composeRecipes} from '../../src/common/recipe';
- import {addResourceRecipe, ResourceItemFetchedResponse} from '../../src/recipes/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 theRawApp = app({
- name: 'default' as const,
- });
-
- const {
- app: theApp,
- operations,
- backend: theBackend,
- } = composeRecipes([
- addResourceRecipe({ endpointName: 'users', dataSource, }),
- addResourceRecipe({ endpointName: 'posts', dataSource, })
- ])({
- app: theRawApp,
- });
-
- 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, new URLSearchParams({
- 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 })
- .makeRequest(theOperation, new URLSearchParams({
- foo: 'bar',
- }));
-
- const response = ResourceItemFetchedResponse.fromFetchResponse(responseRaw);
-
- expect(response).toHaveProperty('statusCode', 200);
- expect(response).toHaveProperty('statusMessage', 'Resource Item Fetched');
- });
- });
|