HATEOAS-first backend framework.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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