plugin.e2e-spec.ts 4.1 KB

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