plugin.e2e-spec.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import gql from 'graphql-tag';
  2. import path from 'path';
  3. import { LanguageCode } from '../../shared/generated-types';
  4. import { ConfigService } from '../src/config/config.service';
  5. import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
  6. import {
  7. TestAPIExtensionPlugin,
  8. TestPluginWithConfigAndBootstrap,
  9. TestPluginWithOnClose,
  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 onCloseFn = jest.fn();
  20. beforeAll(async () => {
  21. const token = await server.init(
  22. {
  23. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  24. customerCount: 1,
  25. },
  26. {
  27. plugins: [
  28. new TestPluginWithConfigAndBootstrap(bootstrapMockFn),
  29. new TestAPIExtensionPlugin(),
  30. new TestPluginWithProvider(),
  31. new TestPluginWithOnClose(onCloseFn),
  32. ],
  33. },
  34. );
  35. await adminClient.init();
  36. await shopClient.init();
  37. }, TEST_SETUP_TIMEOUT_MS);
  38. afterAll(async () => {
  39. await server.destroy();
  40. });
  41. it('can modify the config in configure() and inject in onBootstrap()', () => {
  42. expect(bootstrapMockFn).toHaveBeenCalled();
  43. const configService: ConfigService = bootstrapMockFn.mock.calls[0][0];
  44. expect(configService instanceof ConfigService).toBe(true);
  45. expect(configService.defaultLanguageCode).toBe(LanguageCode.zh);
  46. });
  47. it('extends the admin API', async () => {
  48. const result = await adminClient.query(gql`
  49. query {
  50. foo
  51. }
  52. `);
  53. expect(result.foo).toEqual(['bar']);
  54. });
  55. it('extends the shop API', async () => {
  56. const result = await shopClient.query(gql`
  57. query {
  58. baz
  59. }
  60. `);
  61. expect(result.baz).toEqual(['quux']);
  62. });
  63. it('DI works with defined providers', async () => {
  64. const result = await shopClient.query(gql`
  65. query {
  66. names
  67. }
  68. `);
  69. expect(result.names).toEqual(['seon', 'linda', 'hong']);
  70. });
  71. it('calls onClose method when app is closed', async () => {
  72. await server.destroy();
  73. expect(onCloseFn).toHaveBeenCalled();
  74. });
  75. });