HATEOAS-first backend framework.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

113 Zeilen
3.1 KiB

  1. import {
  2. describe,
  3. beforeAll,
  4. afterAll,
  5. it,
  6. expect,
  7. Mock,
  8. } from 'vitest';
  9. import {
  10. statusCodes
  11. } from '@modal-sh/yasumi';
  12. import {
  13. DataSource,
  14. Server,
  15. } from '@modal-sh/yasumi/backend';
  16. import {Client} from '@modal-sh/yasumi/client';
  17. import {client} from '@modal-sh/yasumi-extender-http/client';
  18. import {ResourceItemFetchedResponse} from '@modal-sh/yasumi-recipe-resource';
  19. import {createDummyDataSource} from './fixtures/data-source';
  20. import {setupApp} from '../src/setup';
  21. const connectionParams = {
  22. port: 3001,
  23. };
  24. describe('default', () => {
  25. let theClient: Client;
  26. let theServer: Server;
  27. let dataSource: Record<keyof DataSource, Mock>;
  28. beforeAll(() => {
  29. dataSource = createDummyDataSource();
  30. });
  31. afterAll(() => {
  32. dataSource.getById.mockReset();
  33. });
  34. beforeAll(async () => {
  35. const {
  36. app: theApp,
  37. server: createdServer,
  38. } = setupApp(dataSource);
  39. theServer = createdServer;
  40. await theServer.serve(connectionParams);
  41. theClient = client({
  42. app: theApp,
  43. });
  44. await theClient.connect(connectionParams);
  45. });
  46. afterAll(async () => {
  47. await theClient.disconnect();
  48. await theServer.close();
  49. });
  50. it('works', async () => {
  51. const theEndpoint = theClient.app.endpoints.get('users');
  52. const theOperation = theClient.app.operations.get('fetch');
  53. // TODO create wrapper for fetch's Response here
  54. //
  55. // should we create a helper object to process client-side received response from server's sent response?
  56. //
  57. // the motivation is to remove the manual deserialization from the client (provide serialization on the response
  58. // object so as the client is not limited to .text(), .json(), .arrayBuffer() etc)
  59. const responseRaw = await theClient
  60. .at(theEndpoint)
  61. .makeRequest(
  62. theOperation
  63. .search({
  64. foo: 'bar',
  65. })
  66. );
  67. const response = ResourceItemFetchedResponse.fromFetchResponse(responseRaw);
  68. const body = await response['deserialize']();
  69. expect(response).toHaveProperty('statusCode', statusCodes.HTTP_STATUS_OK);
  70. expect(response).toHaveProperty('statusMessage', 'Resource Collection Fetched');
  71. expect(body).toEqual([]);
  72. });
  73. it('works for items', async () => {
  74. const theEndpoint = theClient.app.endpoints.get('users');
  75. const theOperation = theClient.app.operations.get('fetch');
  76. // TODO create wrapper for fetch's Response here
  77. //
  78. // should we create a helper object to process client-side received response from server's sent response?
  79. //
  80. // the motivation is to remove the manual deserialization from the client (provide serialization on the response
  81. // object so as the client is not limited to .text(), .json(), .arrayBuffer() etc)
  82. const responseRaw = await theClient
  83. .at(theEndpoint, { resourceId: 3 })
  84. // TODO how to inject extra data (e.g. headers, body) in the operation (e.g. auth)?
  85. .makeRequest(
  86. theOperation
  87. .search({
  88. foo: 'bar',
  89. }) // allow multiple calls of .search() to add to search params
  90. );
  91. const response = ResourceItemFetchedResponse.fromFetchResponse(responseRaw);
  92. expect(response).toHaveProperty('statusCode', statusCodes.HTTP_STATUS_OK);
  93. expect(response).toHaveProperty('statusMessage', 'Resource Item Fetched');
  94. });
  95. });