plugin.e2e-spec.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. TestPluginWithAllLifecycleHooks,
  9. TestPluginWithConfigAndBootstrap,
  10. TestPluginWithProvider,
  11. } from './fixtures/test-plugins';
  12. import { TestAdminClient, TestShopClient } from './test-client';
  13. import { TestServer } from './test-server';
  14. describe('Plugins', () => {
  15. const adminClient = new TestAdminClient();
  16. const shopClient = new TestShopClient();
  17. const server = new TestServer();
  18. const bootstrapMockFn = jest.fn();
  19. const onBootstrapFn = jest.fn();
  20. const onWorkerBootstrapFn = jest.fn();
  21. const onCloseFn = jest.fn();
  22. const onWorkerCloseFn = jest.fn();
  23. beforeAll(async () => {
  24. const token = await server.init(
  25. {
  26. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  27. customerCount: 1,
  28. },
  29. {
  30. plugins: [
  31. TestPluginWithAllLifecycleHooks.init(
  32. onBootstrapFn,
  33. onWorkerBootstrapFn,
  34. onCloseFn,
  35. onWorkerCloseFn,
  36. ),
  37. TestPluginWithConfigAndBootstrap.setup(bootstrapMockFn),
  38. TestAPIExtensionPlugin,
  39. TestPluginWithProvider,
  40. ],
  41. },
  42. );
  43. await adminClient.init();
  44. await shopClient.init();
  45. }, TEST_SETUP_TIMEOUT_MS);
  46. afterAll(async () => {
  47. await server.destroy();
  48. });
  49. it('calls onVendureBootstrap', () => {
  50. expect(onBootstrapFn).toHaveBeenCalled();
  51. });
  52. it('calls onWorkerVendureBootstrap', () => {
  53. expect(onWorkerBootstrapFn).toHaveBeenCalled();
  54. });
  55. it('can modify the config in configure()', () => {
  56. expect(bootstrapMockFn).toHaveBeenCalled();
  57. const configService: ConfigService = bootstrapMockFn.mock.calls[0][0];
  58. expect(configService instanceof ConfigService).toBe(true);
  59. expect(configService.defaultLanguageCode).toBe(LanguageCode.zh);
  60. });
  61. it('extends the admin API', async () => {
  62. const result = await adminClient.query(gql`
  63. query {
  64. foo
  65. }
  66. `);
  67. expect(result.foo).toEqual(['bar']);
  68. });
  69. it('extends the shop API', async () => {
  70. const result = await shopClient.query(gql`
  71. query {
  72. baz
  73. }
  74. `);
  75. expect(result.baz).toEqual(['quux']);
  76. });
  77. it('DI works with defined providers', async () => {
  78. const result = await shopClient.query(gql`
  79. query {
  80. names
  81. }
  82. `);
  83. expect(result.names).toEqual(['seon', 'linda', 'hong']);
  84. });
  85. describe('on app close', () => {
  86. beforeAll(async () => {
  87. await server.destroy();
  88. });
  89. it('calls onVendureClose', () => {
  90. expect(onCloseFn).toHaveBeenCalled();
  91. });
  92. it('calls onWorkerVendureClose', () => {
  93. expect(onWorkerCloseFn).toHaveBeenCalled();
  94. });
  95. });
  96. });