plugin.e2e-spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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('generates ListQueryOptions for api extensions', async () => {
  78. const result = await adminClient.query(gql`
  79. query {
  80. barList(options: { skip: 0, take: 1 }) {
  81. items {
  82. id
  83. name
  84. }
  85. totalItems
  86. }
  87. }
  88. `);
  89. expect(result.barList).toEqual({
  90. items: [{ id: 'T_1', name: 'Test' }],
  91. totalItems: 1,
  92. });
  93. });
  94. it('DI works with defined providers', async () => {
  95. const result = await shopClient.query(gql`
  96. query {
  97. names
  98. }
  99. `);
  100. expect(result.names).toEqual(['seon', 'linda', 'hong']);
  101. });
  102. describe('on app close', () => {
  103. beforeAll(async () => {
  104. await server.destroy();
  105. });
  106. it('calls onVendureClose', () => {
  107. expect(onCloseFn).toHaveBeenCalled();
  108. });
  109. it('calls onWorkerVendureClose', () => {
  110. expect(onWorkerCloseFn).toHaveBeenCalled();
  111. });
  112. });
  113. });