plugin.e2e-spec.ts 4.0 KB

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