HATEOAS-first backend framework.
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

112 líneas
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. expect(response).toHaveProperty('statusCode', statusCodes.HTTP_STATUS_OK);
  69. expect(response).toHaveProperty('statusMessage', 'Resource Collection Fetched');
  70. console.log(responseRaw.headers);
  71. });
  72. it('works for items', async () => {
  73. const theEndpoint = theClient.app.endpoints.get('users');
  74. const theOperation = theClient.app.operations.get('fetch');
  75. // TODO create wrapper for fetch's Response here
  76. //
  77. // should we create a helper object to process client-side received response from server's sent response?
  78. //
  79. // the motivation is to remove the manual deserialization from the client (provide serialization on the response
  80. // object so as the client is not limited to .text(), .json(), .arrayBuffer() etc)
  81. const responseRaw = await theClient
  82. .at(theEndpoint, { resourceId: 3 })
  83. // TODO how to inject extra data (e.g. headers, body) in the operation (e.g. auth)?
  84. .makeRequest(
  85. theOperation
  86. .search({
  87. foo: 'bar',
  88. }) // allow multiple calls of .search() to add to search params
  89. );
  90. const response = ResourceItemFetchedResponse.fromFetchResponse(responseRaw);
  91. expect(response).toHaveProperty('statusCode', statusCodes.HTTP_STATUS_OK);
  92. expect(response).toHaveProperty('statusMessage', 'Resource Item Fetched');
  93. });
  94. });