plugin.e2e-spec.ts 3.9 KB

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