lifecycle.e2e-spec.ts 3.1 KB

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