plugin.e2e-spec.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. initialData,
  41. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  42. customerCount: 1,
  43. });
  44. await adminClient.asSuperAdmin();
  45. }, TEST_SETUP_TIMEOUT_MS);
  46. afterAll(async () => {
  47. await server.destroy();
  48. });
  49. it('constructs one instance for each process', () => {
  50. expect(onConstructorFn).toHaveBeenCalledTimes(2);
  51. });
  52. it('calls onVendureBootstrap', () => {
  53. expect(onBootstrapFn).toHaveBeenCalledTimes(1);
  54. });
  55. it('calls onWorkerVendureBootstrap', () => {
  56. expect(onWorkerBootstrapFn).toHaveBeenCalledTimes(1);
  57. });
  58. it('can modify the config in configure()', () => {
  59. expect(bootstrapMockFn).toHaveBeenCalledTimes(1);
  60. const configService: ConfigService = bootstrapMockFn.mock.calls[0][0];
  61. expect(configService instanceof ConfigService).toBe(true);
  62. expect(configService.defaultLanguageCode).toBe(LanguageCode.zh);
  63. });
  64. it('extends the admin API', async () => {
  65. const result = await adminClient.query(gql`
  66. query {
  67. foo
  68. }
  69. `);
  70. expect(result.foo).toEqual(['bar']);
  71. });
  72. it('extends the shop API', async () => {
  73. const result = await shopClient.query(gql`
  74. query {
  75. baz
  76. }
  77. `);
  78. expect(result.baz).toEqual(['quux']);
  79. });
  80. it('allows lazy evaluation of API extension', async () => {
  81. const result = await shopClient.query(gql`
  82. query {
  83. lazy
  84. }
  85. `);
  86. expect(result.lazy).toEqual('sleeping');
  87. });
  88. it('generates ListQueryOptions for api extensions', async () => {
  89. const result = await adminClient.query(gql`
  90. query {
  91. barList(options: { skip: 0, take: 1 }) {
  92. items {
  93. id
  94. name
  95. }
  96. totalItems
  97. }
  98. }
  99. `);
  100. expect(result.barList).toEqual({
  101. items: [{ id: 'T_1', name: 'Test' }],
  102. totalItems: 1,
  103. });
  104. });
  105. it('DI works with defined providers', async () => {
  106. const result = await shopClient.query(gql`
  107. query {
  108. names
  109. }
  110. `);
  111. expect(result.names).toEqual(['seon', 'linda', 'hong']);
  112. });
  113. describe('on app close', () => {
  114. beforeAll(async () => {
  115. await server.destroy();
  116. });
  117. it('calls onVendureClose', () => {
  118. expect(onCloseFn).toHaveBeenCalled();
  119. });
  120. it('calls onWorkerVendureClose', () => {
  121. expect(onWorkerCloseFn).toHaveBeenCalled();
  122. });
  123. });
  124. });