lifecycle.e2e-spec.ts 3.4 KB

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