plugin.e2e-spec.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { LanguageCode } from '@vendure/common/lib/generated-types';
  2. import { ConfigService } from '@vendure/core';
  3. import { createTestEnvironment } from '@vendure/testing';
  4. import gql from 'graphql-tag';
  5. import path from 'path';
  6. import { initialData } from '../../../e2e-common/e2e-initial-data';
  7. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  8. import {
  9. TestAPIExtensionPlugin,
  10. TestLazyExtensionPlugin,
  11. TestPluginWithAllLifecycleHooks,
  12. TestPluginWithConfigAndBootstrap,
  13. TestPluginWithProvider,
  14. } from './fixtures/test-plugins';
  15. describe('Plugins', () => {
  16. const bootstrapMockFn = jest.fn();
  17. const onConstructorFn = jest.fn();
  18. const onBootstrapFn = jest.fn();
  19. const onWorkerBootstrapFn = jest.fn();
  20. const onCloseFn = jest.fn();
  21. const onWorkerCloseFn = jest.fn();
  22. const { server, adminClient, shopClient } = createTestEnvironment({
  23. ...testConfig,
  24. plugins: [
  25. TestPluginWithAllLifecycleHooks.init(
  26. onConstructorFn,
  27. onBootstrapFn,
  28. onWorkerBootstrapFn,
  29. onCloseFn,
  30. onWorkerCloseFn,
  31. ),
  32. TestPluginWithConfigAndBootstrap.setup(bootstrapMockFn),
  33. TestAPIExtensionPlugin,
  34. TestPluginWithProvider,
  35. TestLazyExtensionPlugin,
  36. ],
  37. });
  38. beforeAll(async () => {
  39. await server.init({
  40. dataDir: path.join(__dirname, '__data__'),
  41. initialData,
  42. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  43. customerCount: 1,
  44. });
  45. await adminClient.asSuperAdmin();
  46. }, TEST_SETUP_TIMEOUT_MS);
  47. afterAll(async () => {
  48. await server.destroy();
  49. });
  50. it('constructs one instance for each process', () => {
  51. expect(onConstructorFn).toHaveBeenCalledTimes(2);
  52. });
  53. it('calls onVendureBootstrap', () => {
  54. expect(onBootstrapFn).toHaveBeenCalledTimes(1);
  55. });
  56. it('calls onWorkerVendureBootstrap', () => {
  57. expect(onWorkerBootstrapFn).toHaveBeenCalledTimes(1);
  58. });
  59. it('can modify the config in configure()', () => {
  60. expect(bootstrapMockFn).toHaveBeenCalledTimes(1);
  61. const configService: ConfigService = bootstrapMockFn.mock.calls[0][0];
  62. expect(configService instanceof ConfigService).toBe(true);
  63. expect(configService.defaultLanguageCode).toBe(LanguageCode.zh);
  64. });
  65. it('extends the admin API', async () => {
  66. const result = await adminClient.query(gql`
  67. query {
  68. foo
  69. }
  70. `);
  71. expect(result.foo).toEqual(['bar']);
  72. });
  73. it('extends the shop API', async () => {
  74. const result = await shopClient.query(gql`
  75. query {
  76. baz
  77. }
  78. `);
  79. expect(result.baz).toEqual(['quux']);
  80. });
  81. it('allows lazy evaluation of API extension', async () => {
  82. const result = await shopClient.query(gql`
  83. query {
  84. lazy
  85. }
  86. `);
  87. expect(result.lazy).toEqual('sleeping');
  88. });
  89. it('generates ListQueryOptions for api extensions', async () => {
  90. const result = await adminClient.query(gql`
  91. query {
  92. barList(options: { skip: 0, take: 1 }) {
  93. items {
  94. id
  95. name
  96. }
  97. totalItems
  98. }
  99. }
  100. `);
  101. expect(result.barList).toEqual({
  102. items: [{ id: 'T_1', name: 'Test' }],
  103. totalItems: 1,
  104. });
  105. });
  106. it('DI works with defined providers', async () => {
  107. const result = await shopClient.query(gql`
  108. query {
  109. names
  110. }
  111. `);
  112. expect(result.names).toEqual(['seon', 'linda', 'hong']);
  113. });
  114. describe('on app close', () => {
  115. beforeAll(async () => {
  116. await server.destroy();
  117. });
  118. it('calls onVendureClose', () => {
  119. expect(onCloseFn).toHaveBeenCalled();
  120. });
  121. it('calls onWorkerVendureClose', () => {
  122. expect(onWorkerCloseFn).toHaveBeenCalled();
  123. });
  124. });
  125. });