Prechádzať zdrojové kódy

test(core): Split test plugins into separate files

Michael Bromley 5 rokov pred
rodič
commit
803769ba2b

+ 0 - 263
packages/core/e2e/fixtures/test-plugins.ts

@@ -1,263 +0,0 @@
-import { Controller, Get, Injectable } from '@nestjs/common';
-import { Query, Resolver } from '@nestjs/graphql';
-import { MessagePattern } from '@nestjs/microservices';
-import { Permission } from '@vendure/common/lib/generated-shop-types';
-import { LanguageCode } from '@vendure/common/lib/generated-types';
-import {
-    Allow,
-    ConfigModule,
-    ConfigService,
-    InternalServerError,
-    Logger,
-    OnVendureBootstrap,
-    OnVendureClose,
-    OnVendureWorkerBootstrap,
-    OnVendureWorkerClose,
-    PluginCommonModule,
-    ProcessContext,
-    VendurePlugin,
-    WorkerMessage,
-    WorkerService,
-} from '@vendure/core';
-import gql from 'graphql-tag';
-import { of } from 'rxjs';
-
-export class TestPluginWithAllLifecycleHooks
-    implements OnVendureBootstrap, OnVendureWorkerBootstrap, OnVendureClose, OnVendureWorkerClose {
-    private static onConstructorFn: any;
-    private static onBootstrapFn: any;
-    private static onWorkerBootstrapFn: any;
-    private static onCloseFn: any;
-    private static onWorkerCloseFn: any;
-    static init(
-        constructorFn: any,
-        bootstrapFn: any,
-        workerBootstrapFn: any,
-        closeFn: any,
-        workerCloseFn: any,
-    ) {
-        this.onConstructorFn = constructorFn;
-        this.onBootstrapFn = bootstrapFn;
-        this.onWorkerBootstrapFn = workerBootstrapFn;
-        this.onCloseFn = closeFn;
-        this.onWorkerCloseFn = workerCloseFn;
-        return this;
-    }
-    constructor() {
-        TestPluginWithAllLifecycleHooks.onConstructorFn();
-    }
-    onVendureBootstrap(): void | Promise<void> {
-        TestPluginWithAllLifecycleHooks.onBootstrapFn();
-    }
-    onVendureWorkerBootstrap(): void | Promise<void> {
-        TestPluginWithAllLifecycleHooks.onWorkerBootstrapFn();
-    }
-    onVendureClose(): void | Promise<void> {
-        TestPluginWithAllLifecycleHooks.onCloseFn();
-        this.resetSpies();
-    }
-    onVendureWorkerClose(): void | Promise<void> {
-        TestPluginWithAllLifecycleHooks.onWorkerCloseFn();
-        this.resetSpies();
-    }
-
-    /**
-     * This is required because on the first run, the Vendure server will be bootstrapped twice -
-     * once to populate the database and the second time forthe actual tests. Thus the call counts
-     * for the plugin lifecycles will be doubled. This method resets them after the initial
-     * (population) run.
-     */
-    private resetSpies() {
-        TestPluginWithAllLifecycleHooks.onConstructorFn.mockClear();
-        TestPluginWithAllLifecycleHooks.onBootstrapFn.mockClear();
-        TestPluginWithAllLifecycleHooks.onWorkerBootstrapFn.mockClear();
-    }
-}
-
-@Resolver()
-export class TestAdminPluginResolver {
-    @Query()
-    foo() {
-        return ['bar'];
-    }
-
-    @Query()
-    barList() {
-        return {
-            items: [{ id: 1, name: 'Test' }],
-            totalItems: 1,
-        };
-    }
-}
-
-@Resolver()
-export class TestShopPluginResolver {
-    @Query()
-    baz() {
-        return ['quux'];
-    }
-}
-
-@VendurePlugin({
-    shopApiExtensions: {
-        resolvers: [TestShopPluginResolver],
-        schema: gql`
-            extend type Query {
-                baz: [String]!
-            }
-        `,
-    },
-    adminApiExtensions: {
-        resolvers: [TestAdminPluginResolver],
-        schema: gql`
-            extend type Query {
-                foo: [String]!
-                barList(options: BarListOptions): BarList!
-            }
-
-            input BarListOptions
-            type Bar implements Node {
-                id: ID!
-                name: String!
-            }
-            type BarList implements PaginatedList {
-                items: [Bar!]!
-                totalItems: Int!
-            }
-        `,
-    },
-})
-export class TestAPIExtensionPlugin {}
-
-@Resolver()
-export class TestLazyResolver {
-    @Query()
-    lazy() {
-        return 'sleeping';
-    }
-}
-
-@VendurePlugin({
-    shopApiExtensions: {
-        resolvers: () => [TestLazyResolver],
-        schema: () => gql`
-            extend type Query {
-                lazy: String!
-            }
-        `,
-    },
-})
-export class TestLazyExtensionPlugin {}
-
-@Injectable()
-export class NameService {
-    getNames(): string[] {
-        return ['seon', 'linda', 'hong'];
-    }
-}
-
-@Resolver()
-export class TestResolverWithInjection {
-    constructor(private nameService: NameService) {}
-
-    @Query()
-    names() {
-        return this.nameService.getNames();
-    }
-}
-
-@VendurePlugin({
-    providers: [NameService],
-    shopApiExtensions: {
-        resolvers: [TestResolverWithInjection],
-        schema: gql`
-            extend type Query {
-                names: [String]!
-            }
-        `,
-    },
-})
-export class TestPluginWithProvider {}
-
-@VendurePlugin({
-    imports: [ConfigModule],
-    configuration: (config) => {
-        // tslint:disable-next-line:no-non-null-assertion
-        config.defaultLanguageCode = LanguageCode.zh;
-        return config;
-    },
-})
-export class TestPluginWithConfigAndBootstrap implements OnVendureBootstrap, OnVendureClose {
-    private static boostrapWasCalled: any;
-    static setup(boostrapWasCalled: (arg: any) => void) {
-        TestPluginWithConfigAndBootstrap.boostrapWasCalled = boostrapWasCalled;
-        return TestPluginWithConfigAndBootstrap;
-    }
-    constructor(private configService: ConfigService) {}
-
-    onVendureBootstrap() {
-        TestPluginWithConfigAndBootstrap.boostrapWasCalled(this.configService);
-    }
-
-    onVendureClose() {
-        TestPluginWithConfigAndBootstrap.boostrapWasCalled.mockClear();
-    }
-}
-
-@Controller('test')
-export class TestController {
-    @Get('public')
-    publicRoute() {
-        return 'success';
-    }
-
-    @Allow(Permission.Authenticated)
-    @Get('restricted')
-    restrictedRoute() {
-        return 'success';
-    }
-
-    @Get('bad')
-    badRoute() {
-        throw new InternalServerError('uh oh!');
-    }
-}
-
-@VendurePlugin({
-    controllers: [TestController],
-})
-export class TestRestPlugin {}
-
-class TestWorkerMessage extends WorkerMessage<string, boolean> {
-    static readonly pattern = 'TestWorkerMessage';
-}
-
-@Controller('process-context')
-export class TestProcessContextController {
-    constructor(private processContext: ProcessContext, private workerService: WorkerService) {}
-
-    @Get('server')
-    isServer() {
-        return this.processContext.isServer;
-    }
-
-    @Get('worker')
-    isWorker() {
-        return this.workerService.send(new TestWorkerMessage('hello'));
-    }
-}
-@Controller()
-export class TestProcessContextWorkerController {
-    constructor(private processContext: ProcessContext) {}
-
-    @MessagePattern(TestWorkerMessage.pattern)
-    isWorker() {
-        return of(this.processContext.isWorker);
-    }
-}
-@VendurePlugin({
-    imports: [PluginCommonModule],
-    controllers: [TestProcessContextController],
-    workers: [TestProcessContextWorkerController],
-})
-export class TestProcessContextPlugin {}

+ 0 - 0
packages/core/e2e/fixtures/list-query-plugin.ts → packages/core/e2e/fixtures/test-plugins/list-query-plugin.ts


+ 64 - 0
packages/core/e2e/fixtures/test-plugins/with-all-lifecycle-hooks.ts

@@ -0,0 +1,64 @@
+import {
+    OnVendureBootstrap,
+    OnVendureClose,
+    OnVendureWorkerBootstrap,
+    OnVendureWorkerClose,
+} from '@vendure/core';
+
+export class TestPluginWithAllLifecycleHooks
+    implements OnVendureBootstrap, OnVendureWorkerBootstrap, OnVendureClose, OnVendureWorkerClose {
+    private static onConstructorFn: any;
+    private static onBootstrapFn: any;
+    private static onWorkerBootstrapFn: any;
+    private static onCloseFn: any;
+    private static onWorkerCloseFn: any;
+
+    static init(
+        constructorFn: any,
+        bootstrapFn: any,
+        workerBootstrapFn: any,
+        closeFn: any,
+        workerCloseFn: any,
+    ) {
+        this.onConstructorFn = constructorFn;
+        this.onBootstrapFn = bootstrapFn;
+        this.onWorkerBootstrapFn = workerBootstrapFn;
+        this.onCloseFn = closeFn;
+        this.onWorkerCloseFn = workerCloseFn;
+        return this;
+    }
+
+    constructor() {
+        TestPluginWithAllLifecycleHooks.onConstructorFn();
+    }
+
+    onVendureBootstrap(): void | Promise<void> {
+        TestPluginWithAllLifecycleHooks.onBootstrapFn();
+    }
+
+    onVendureWorkerBootstrap(): void | Promise<void> {
+        TestPluginWithAllLifecycleHooks.onWorkerBootstrapFn();
+    }
+
+    onVendureClose(): void | Promise<void> {
+        TestPluginWithAllLifecycleHooks.onCloseFn();
+        this.resetSpies();
+    }
+
+    onVendureWorkerClose(): void | Promise<void> {
+        TestPluginWithAllLifecycleHooks.onWorkerCloseFn();
+        this.resetSpies();
+    }
+
+    /**
+     * This is required because on the first run, the Vendure server will be bootstrapped twice -
+     * once to populate the database and the second time forthe actual tests. Thus the call counts
+     * for the plugin lifecycles will be doubled. This method resets them after the initial
+     * (population) run.
+     */
+    private resetSpies() {
+        TestPluginWithAllLifecycleHooks.onConstructorFn.mockClear();
+        TestPluginWithAllLifecycleHooks.onBootstrapFn.mockClear();
+        TestPluginWithAllLifecycleHooks.onWorkerBootstrapFn.mockClear();
+    }
+}

+ 58 - 0
packages/core/e2e/fixtures/test-plugins/with-api-extensions.ts

@@ -0,0 +1,58 @@
+import { Query, Resolver } from '@nestjs/graphql';
+import { VendurePlugin } from '@vendure/core';
+import gql from 'graphql-tag';
+
+@Resolver()
+export class TestAdminPluginResolver {
+    @Query()
+    foo() {
+        return ['bar'];
+    }
+
+    @Query()
+    barList() {
+        return {
+            items: [{ id: 1, name: 'Test' }],
+            totalItems: 1,
+        };
+    }
+}
+
+@Resolver()
+export class TestShopPluginResolver {
+    @Query()
+    baz() {
+        return ['quux'];
+    }
+}
+
+@VendurePlugin({
+    shopApiExtensions: {
+        resolvers: [TestShopPluginResolver],
+        schema: gql`
+            extend type Query {
+                baz: [String]!
+            }
+        `,
+    },
+    adminApiExtensions: {
+        resolvers: [TestAdminPluginResolver],
+        schema: gql`
+            extend type Query {
+                foo: [String]!
+                barList(options: BarListOptions): BarList!
+            }
+
+            input BarListOptions
+            type Bar implements Node {
+                id: ID!
+                name: String!
+            }
+            type BarList implements PaginatedList {
+                items: [Bar!]!
+                totalItems: Int!
+            }
+        `,
+    },
+})
+export class TestAPIExtensionPlugin {}

+ 35 - 0
packages/core/e2e/fixtures/test-plugins/with-config-and-bootstrap.ts

@@ -0,0 +1,35 @@
+import { LanguageCode } from '@vendure/common/lib/generated-types';
+import {
+    ConfigModule,
+    ConfigService,
+    OnVendureBootstrap,
+    OnVendureClose,
+    VendurePlugin,
+} from '@vendure/core';
+
+@VendurePlugin({
+    imports: [ConfigModule],
+    configuration: (config) => {
+        // tslint:disable-next-line:no-non-null-assertion
+        config.defaultLanguageCode = LanguageCode.zh;
+        return config;
+    },
+})
+export class TestPluginWithConfigAndBootstrap implements OnVendureBootstrap, OnVendureClose {
+    private static boostrapWasCalled: any;
+
+    static setup(boostrapWasCalled: (arg: any) => void) {
+        TestPluginWithConfigAndBootstrap.boostrapWasCalled = boostrapWasCalled;
+        return TestPluginWithConfigAndBootstrap;
+    }
+
+    constructor(private configService: ConfigService) {}
+
+    onVendureBootstrap() {
+        TestPluginWithConfigAndBootstrap.boostrapWasCalled(this.configService);
+    }
+
+    onVendureClose() {
+        TestPluginWithConfigAndBootstrap.boostrapWasCalled.mockClear();
+    }
+}

+ 23 - 0
packages/core/e2e/fixtures/test-plugins/with-lazy-api-extensions.ts

@@ -0,0 +1,23 @@
+import { Query, Resolver } from '@nestjs/graphql';
+import { VendurePlugin } from '@vendure/core';
+import gql from 'graphql-tag';
+
+@Resolver()
+export class TestLazyResolver {
+    @Query()
+    lazy() {
+        return 'sleeping';
+    }
+}
+
+@VendurePlugin({
+    shopApiExtensions: {
+        resolvers: () => [TestLazyResolver],
+        schema: () => gql`
+            extend type Query {
+                lazy: String!
+            }
+        `,
+    },
+})
+export class TestLazyExtensionPlugin {}

+ 34 - 0
packages/core/e2e/fixtures/test-plugins/with-provider.ts

@@ -0,0 +1,34 @@
+import { Injectable } from '@nestjs/common';
+import { Query, Resolver } from '@nestjs/graphql';
+import { VendurePlugin } from '@vendure/core';
+import gql from 'graphql-tag';
+
+@Injectable()
+export class NameService {
+    getNames(): string[] {
+        return ['seon', 'linda', 'hong'];
+    }
+}
+
+@Resolver()
+export class TestResolverWithInjection {
+    constructor(private nameService: NameService) {}
+
+    @Query()
+    names() {
+        return this.nameService.getNames();
+    }
+}
+
+@VendurePlugin({
+    providers: [NameService],
+    shopApiExtensions: {
+        resolvers: [TestResolverWithInjection],
+        schema: gql`
+            extend type Query {
+                names: [String]!
+            }
+        `,
+    },
+})
+export class TestPluginWithProvider {}

+ 27 - 0
packages/core/e2e/fixtures/test-plugins/with-rest-controller.ts

@@ -0,0 +1,27 @@
+import { Controller, Get } from '@nestjs/common';
+import { Permission } from '@vendure/common/lib/generated-shop-types';
+import { Allow, InternalServerError, VendurePlugin } from '@vendure/core';
+
+@Controller('test')
+export class TestController {
+    @Get('public')
+    publicRoute() {
+        return 'success';
+    }
+
+    @Allow(Permission.Authenticated)
+    @Get('restricted')
+    restrictedRoute() {
+        return 'success';
+    }
+
+    @Get('bad')
+    badRoute() {
+        throw new InternalServerError('uh oh!');
+    }
+}
+
+@VendurePlugin({
+    controllers: [TestController],
+})
+export class TestRestPlugin {}

+ 46 - 0
packages/core/e2e/fixtures/test-plugins/with-worker-controller.ts

@@ -0,0 +1,46 @@
+import { Controller, Get } from '@nestjs/common';
+import { MessagePattern } from '@nestjs/microservices';
+import {
+    PluginCommonModule,
+    ProcessContext,
+    VendurePlugin,
+    WorkerMessage,
+    WorkerService,
+} from '@vendure/core';
+import { of } from 'rxjs';
+
+class TestWorkerMessage extends WorkerMessage<string, boolean> {
+    static readonly pattern = 'TestWorkerMessage';
+}
+
+@Controller('process-context')
+export class TestProcessContextController {
+    constructor(private processContext: ProcessContext, private workerService: WorkerService) {}
+
+    @Get('server')
+    isServer() {
+        return this.processContext.isServer;
+    }
+
+    @Get('worker')
+    isWorker() {
+        return this.workerService.send(new TestWorkerMessage('hello'));
+    }
+}
+
+@Controller()
+export class TestProcessContextWorkerController {
+    constructor(private processContext: ProcessContext) {}
+
+    @MessagePattern(TestWorkerMessage.pattern)
+    isWorker() {
+        return of(this.processContext.isWorker);
+    }
+}
+
+@VendurePlugin({
+    imports: [PluginCommonModule],
+    controllers: [TestProcessContextController],
+    workers: [TestProcessContextWorkerController],
+})
+export class TestProcessContextPlugin {}

+ 1 - 1
packages/core/e2e/list-query-builder.e2e-spec.ts

@@ -6,7 +6,7 @@ import path from 'path';
 import { initialData } from '../../../e2e-common/e2e-initial-data';
 import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
 
-import { ListQueryPlugin } from './fixtures/list-query-plugin';
+import { ListQueryPlugin } from './fixtures/test-plugins/list-query-plugin';
 import { fixPostgresTimezone } from './utils/fix-pg-timezone';
 
 fixPostgresTimezone();

+ 7 - 9
packages/core/e2e/plugin.e2e-spec.ts

@@ -7,15 +7,13 @@ import path from 'path';
 import { initialData } from '../../../e2e-common/e2e-initial-data';
 import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
 
-import {
-    TestAPIExtensionPlugin,
-    TestLazyExtensionPlugin,
-    TestPluginWithAllLifecycleHooks,
-    TestPluginWithConfigAndBootstrap,
-    TestPluginWithProvider,
-    TestProcessContextPlugin,
-    TestRestPlugin,
-} from './fixtures/test-plugins';
+import { TestPluginWithAllLifecycleHooks } from './fixtures/test-plugins/with-all-lifecycle-hooks';
+import { TestAPIExtensionPlugin } from './fixtures/test-plugins/with-api-extensions';
+import { TestPluginWithConfigAndBootstrap } from './fixtures/test-plugins/with-config-and-bootstrap';
+import { TestLazyExtensionPlugin } from './fixtures/test-plugins/with-lazy-api-extensions';
+import { TestPluginWithProvider } from './fixtures/test-plugins/with-provider';
+import { TestRestPlugin } from './fixtures/test-plugins/with-rest-controller';
+import { TestProcessContextPlugin } from './fixtures/test-plugins/with-worker-controller';
 
 describe('Plugins', () => {
     const bootstrapMockFn = jest.fn();