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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import {describe, it, expect, vi, Mock, beforeAll, beforeEach} from 'vitest';
  2. import { readFile, writeFile } from 'fs/promises';
  3. import { JsonLinesDataSource } from '../src';
  4. import { resource, validation as v } from '@modal-sh/yasumi';
  5. import {DataSource} from '@modal-sh/yasumi/dist/types/backend';
  6. vi.mock('fs/promises');
  7. describe('prepareResource', () => {
  8. beforeAll(() => {
  9. const mockWriteFile = writeFile as Mock;
  10. mockWriteFile.mockImplementation(() => { /* noop */ })
  11. });
  12. it('works', () => {
  13. const schema = v.object({});
  14. const r = resource(schema);
  15. const ds = new JsonLinesDataSource<typeof schema>();
  16. expect(() => ds.prepareResource(r)).not.toThrow();
  17. });
  18. });
  19. describe('methods', () => {
  20. const dummyItems = [
  21. {
  22. id: 1,
  23. name: 'foo',
  24. },
  25. {
  26. id: 2,
  27. name: 'bar',
  28. },
  29. {
  30. id: 3,
  31. name: 'baz',
  32. },
  33. ];
  34. const schema = v.object({
  35. name: v.string(),
  36. });
  37. let ds: DataSource<v.Output<typeof schema>>;
  38. let mockGenerationStrategy;
  39. beforeEach(() => {
  40. mockGenerationStrategy = vi.fn();
  41. const r = resource(schema)
  42. .id('id', {
  43. generationStrategy: mockGenerationStrategy,
  44. schema: v.any(),
  45. serialize: (id) => id.toString(),
  46. deserialize: (id) => Number(id.toString()),
  47. });
  48. ds = new JsonLinesDataSource<typeof schema>();
  49. ds.prepareResource(r);
  50. });
  51. beforeEach(() => {
  52. const mockReadFile = readFile as Mock;
  53. mockReadFile.mockReturnValueOnce(
  54. dummyItems.map((i) => JSON.stringify(i)).join('\n')
  55. );
  56. });
  57. describe('initialize', () => {
  58. it('works', async () => {
  59. try {
  60. await ds.initialize();
  61. } catch {
  62. expect.fail('Could not initialize data source.');
  63. }
  64. });
  65. });
  66. describe('operations', () => {
  67. beforeEach(async () => {
  68. await ds.initialize();
  69. });
  70. describe('getTotalCount', () => {
  71. it('works', async () => {
  72. if (typeof ds.getTotalCount !== 'function') {
  73. return;
  74. }
  75. const totalCount = await ds.getTotalCount();
  76. expect(totalCount).toBe(dummyItems.length);
  77. });
  78. });
  79. describe('getMultiple', () => {
  80. it('works', async () => {
  81. const items = await ds.getMultiple();
  82. expect(items).toEqual(dummyItems);
  83. });
  84. });
  85. describe('getById', () => {
  86. it('works', async () => {
  87. const item = await ds.getById('2');
  88. const expected = dummyItems.find((i) => i.id === 2);
  89. expect(item).toEqual(expected);
  90. });
  91. });
  92. describe('getSingle', () => {
  93. it('works', async () => {
  94. if (typeof ds.getSingle !== 'function') {
  95. return;
  96. }
  97. const item = await ds.getSingle();
  98. const expected = dummyItems[0];
  99. expect(item).toEqual(expected);
  100. });
  101. });
  102. describe.todo('create');
  103. describe.todo('delete');
  104. describe.todo('emplace');
  105. describe.todo('patch');
  106. describe('newId', () => {
  107. it('works', async () => {
  108. const v = 'newId';
  109. mockGenerationStrategy.mockResolvedValueOnce(v);
  110. const id = await ds.newId();
  111. expect(id).toBe(v);
  112. });
  113. });
  114. });
  115. });