entity-uuid-strategy.e2e-spec.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* tslint:disable:no-non-null-assertion */
  2. import { UuidIdStrategy } from '@vendure/core';
  3. import { createTestEnvironment } from '@vendure/testing';
  4. import path from 'path';
  5. import { initialData } from '../../../e2e-common/e2e-initial-data';
  6. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  7. import '../src/index';
  8. import { GetProductList } from './graphql/generated-e2e-admin-types';
  9. import { GET_PRODUCT_LIST } from './graphql/shared-definitions';
  10. // This import is here to simulate the behaviour of
  11. // the package end-user importing symbols from the
  12. // @vendure/core barrel file. Doing so will then cause the
  13. // recusrsive evaluation of all imported files. This tests
  14. // the resilience of the id strategy implementation to the
  15. // order of file evaluation.
  16. describe('UuidIdStrategy', () => {
  17. const { server, adminClient } = createTestEnvironment({
  18. ...testConfig,
  19. entityIdStrategy: new UuidIdStrategy(),
  20. });
  21. beforeAll(async () => {
  22. await server.init({
  23. dataDir: path.join(__dirname, '__data__'),
  24. initialData,
  25. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  26. customerCount: 1,
  27. });
  28. await adminClient.asSuperAdmin();
  29. }, TEST_SETUP_TIMEOUT_MS);
  30. afterAll(async () => {
  31. await server.destroy();
  32. });
  33. it('uses uuids', async () => {
  34. const { products } = await adminClient.query<GetProductList.Query, GetProductList.Variables>(
  35. GET_PRODUCT_LIST,
  36. {
  37. options: {
  38. take: 1,
  39. },
  40. },
  41. );
  42. expect(isV4Uuid(products.items[0].id)).toBe(true);
  43. });
  44. });
  45. /**
  46. * Returns true if the id string matches the format for a v4 UUID.
  47. */
  48. function isV4Uuid(id: string): boolean {
  49. return /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i.test(id);
  50. }