lifecycle.e2e-spec.ts 3.1 KB

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