HATEOAS-first backend framework.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.0 KiB

  1. import {
  2. describe,
  3. beforeAll,
  4. afterAll,
  5. it,
  6. expect,
  7. } from 'vitest';
  8. import {
  9. app,
  10. Endpoint,
  11. Operation,
  12. } from '../../src/common';
  13. import {Server} from '../../src/backend';
  14. import {Client} from '../../src/client';
  15. import {server} from '../../src/extenders/http/backend';
  16. import {client} from '../../src/extenders/http/client';
  17. import {composeRecipes} from '../../src/common/recipe';
  18. import {addResourceRecipe, YesResponse} from '../../src/recipes/resource';
  19. describe('default', () => {
  20. let theClient: Client;
  21. let theServer: Server;
  22. let theRawEndpoint: Endpoint;
  23. let theOperation: Operation;
  24. beforeAll(async () => {
  25. const theRawApp = app({
  26. name: 'default' as const,
  27. });
  28. const {
  29. app: theApp,
  30. operations,
  31. backend: theBackend,
  32. } = composeRecipes([
  33. addResourceRecipe({ endpointName: 'users' }),
  34. addResourceRecipe({ endpointName: 'posts' })
  35. ])({
  36. app: theRawApp,
  37. });
  38. theRawEndpoint = Array.from(theApp.endpoints).find((e) => e.name === 'users');
  39. theOperation = operations.fetch;
  40. theServer = server({
  41. backend: theBackend,
  42. });
  43. const connectionParams = {
  44. port: 3001,
  45. };
  46. await theServer.serve(connectionParams);
  47. theClient = client({
  48. app: theApp,
  49. });
  50. await theClient.connect(connectionParams);
  51. });
  52. afterAll(async () => {
  53. await theClient.disconnect();
  54. await theServer.close();
  55. });
  56. it('works', async () => {
  57. // TODO create wrapper for fetch's Response here
  58. //
  59. // should we create a helper object to process client-side received response from server's sent response?
  60. //
  61. // the motivation is to remove the manual deserialization from the client (provide serialization on the response
  62. // object so as the client is not limited to .text(), .json(), .arrayBuffer() etc)
  63. const responseRaw = await theClient
  64. .at(theRawEndpoint)
  65. .makeRequest(theOperation, new URLSearchParams({
  66. foo: 'bar',
  67. }));
  68. const response = YesResponse.fromFetchResponse(responseRaw);
  69. expect(response).toHaveProperty('statusCode', 204);
  70. expect(response).toHaveProperty('statusMessage', 'Yes');
  71. });
  72. });