plugin.e2e-spec.ts 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. TestPluginWithProvider,
  10. } from './fixtures/test-plugins';
  11. import { TestAdminClient, TestShopClient } from './test-client';
  12. import { TestServer } from './test-server';
  13. describe('Plugins', () => {
  14. const adminClient = new TestAdminClient();
  15. const shopClient = new TestShopClient();
  16. const server = new TestServer();
  17. const bootstrapMockFn = jest.fn();
  18. beforeAll(async () => {
  19. const token = await server.init(
  20. {
  21. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  22. customerCount: 1,
  23. },
  24. {
  25. plugins: [
  26. new TestPluginWithConfigAndBootstrap(bootstrapMockFn),
  27. new TestAPIExtensionPlugin(),
  28. new TestPluginWithProvider(),
  29. ],
  30. },
  31. );
  32. await adminClient.init();
  33. await shopClient.init();
  34. }, TEST_SETUP_TIMEOUT_MS);
  35. afterAll(async () => {
  36. await server.destroy();
  37. });
  38. it('can modify the config in configure() and inject in onBootstrap()', () => {
  39. expect(bootstrapMockFn).toHaveBeenCalled();
  40. const configService: ConfigService = bootstrapMockFn.mock.calls[0][0];
  41. expect(configService instanceof ConfigService).toBe(true);
  42. expect(configService.defaultLanguageCode).toBe(LanguageCode.zh);
  43. });
  44. it('extends the admin API', async () => {
  45. const result = await adminClient.query(gql`
  46. query {
  47. foo
  48. }
  49. `);
  50. expect(result.foo).toEqual(['bar']);
  51. });
  52. it('extends the shop API', async () => {
  53. const result = await shopClient.query(gql`
  54. query {
  55. baz
  56. }
  57. `);
  58. expect(result.baz).toEqual(['quux']);
  59. });
  60. it('DI works with defined providers', async () => {
  61. const result = await shopClient.query(gql`
  62. query {
  63. names
  64. }
  65. `);
  66. expect(result.names).toEqual(['seon', 'linda', 'hong']);
  67. });
  68. });