lifecycle.e2e-spec.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import {
  2. AutoIncrementIdStrategy,
  3. defaultShippingEligibilityChecker,
  4. Injector,
  5. ProductService,
  6. ShippingEligibilityChecker,
  7. } from '@vendure/core';
  8. import { createTestEnvironment } from '@vendure/testing';
  9. import path from 'path';
  10. import { initialData } from '../../../e2e-common/e2e-initial-data';
  11. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  12. import { TransactionalConnection } from '../src/connection/transactional-connection';
  13. const strategyInitSpy = jest.fn();
  14. const strategyDestroySpy = jest.fn();
  15. const codInitSpy = jest.fn();
  16. const codDestroySpy = jest.fn();
  17. class TestIdStrategy extends AutoIncrementIdStrategy {
  18. async init(injector: Injector) {
  19. const productService = injector.get(ProductService);
  20. const connection = injector.get(TransactionalConnection);
  21. await new Promise(resolve => setTimeout(resolve, 100));
  22. strategyInitSpy(productService.constructor.name, connection.rawConnection.name);
  23. }
  24. async destroy() {
  25. await new Promise(resolve => setTimeout(resolve, 100));
  26. strategyDestroySpy();
  27. }
  28. }
  29. const testShippingEligChecker = new ShippingEligibilityChecker({
  30. code: 'test',
  31. args: {},
  32. description: [],
  33. init: async injector => {
  34. const productService = injector.get(ProductService);
  35. const connection = injector.get(TransactionalConnection);
  36. await new Promise(resolve => setTimeout(resolve, 100));
  37. codInitSpy(productService.constructor.name, connection.rawConnection.name);
  38. },
  39. destroy: async () => {
  40. await new Promise(resolve => setTimeout(resolve, 100));
  41. codDestroySpy();
  42. },
  43. check: order => {
  44. return true;
  45. },
  46. });
  47. describe('lifecycle hooks for configurable objects', () => {
  48. const { server, adminClient } = createTestEnvironment({
  49. ...testConfig(),
  50. entityOptions: { entityIdStrategy: new TestIdStrategy() },
  51. shippingOptions: {
  52. shippingEligibilityCheckers: [defaultShippingEligibilityChecker, testShippingEligChecker],
  53. },
  54. });
  55. beforeAll(async () => {
  56. await server.init({
  57. initialData,
  58. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-empty.csv'),
  59. customerCount: 1,
  60. });
  61. await adminClient.asSuperAdmin();
  62. }, TEST_SETUP_TIMEOUT_MS);
  63. describe('strategy', () => {
  64. it('runs init with Injector', () => {
  65. expect(strategyInitSpy).toHaveBeenCalled();
  66. expect(strategyInitSpy.mock.calls[0][0]).toEqual('ProductService');
  67. expect(strategyInitSpy.mock.calls[0][1]).toBe('default');
  68. });
  69. it('runs destroy', async () => {
  70. await server.destroy();
  71. expect(strategyDestroySpy).toHaveBeenCalled();
  72. });
  73. });
  74. describe('configurable operation', () => {
  75. beforeAll(async () => {
  76. await server.bootstrap();
  77. }, TEST_SETUP_TIMEOUT_MS);
  78. afterAll(async () => {
  79. await server.destroy();
  80. });
  81. it('runs init with Injector', () => {
  82. expect(codInitSpy).toHaveBeenCalled();
  83. expect(codInitSpy.mock.calls[0][0]).toEqual('ProductService');
  84. expect(codInitSpy.mock.calls[0][1]).toBe('default');
  85. });
  86. it('runs destroy', async () => {
  87. await server.destroy();
  88. expect(codDestroySpy).toHaveBeenCalled();
  89. });
  90. });
  91. });