test-plugins.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import { Injectable, OnApplicationBootstrap, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
  2. import { Query, Resolver } from '@nestjs/graphql';
  3. import { LanguageCode } from '@vendure/common/lib/generated-types';
  4. import gql from 'graphql-tag';
  5. import { VendureConfig } from '../../src/config';
  6. import { ConfigModule } from '../../src/config/config.module';
  7. import { ConfigService } from '../../src/config/config.service';
  8. import {
  9. OnVendureBootstrap,
  10. OnVendureClose,
  11. OnVendureWorkerBootstrap,
  12. OnVendureWorkerClose,
  13. VendurePlugin,
  14. } from '../../src/plugin/vendure-plugin';
  15. export class TestPluginWithAllLifecycleHooks
  16. implements OnVendureBootstrap, OnVendureWorkerBootstrap, OnVendureClose, OnVendureWorkerClose {
  17. private static onConstructorFn: any;
  18. private static onBootstrapFn: any;
  19. private static onWorkerBootstrapFn: any;
  20. private static onCloseFn: any;
  21. private static onWorkerCloseFn: any;
  22. static init(
  23. constructorFn: any,
  24. bootstrapFn: any,
  25. workerBootstrapFn: any,
  26. closeFn: any,
  27. workerCloseFn: any,
  28. ) {
  29. this.onConstructorFn = constructorFn;
  30. this.onBootstrapFn = bootstrapFn;
  31. this.onWorkerBootstrapFn = workerBootstrapFn;
  32. this.onCloseFn = closeFn;
  33. this.onWorkerCloseFn = workerCloseFn;
  34. return this;
  35. }
  36. constructor() {
  37. TestPluginWithAllLifecycleHooks.onConstructorFn();
  38. }
  39. onVendureBootstrap(): void | Promise<void> {
  40. TestPluginWithAllLifecycleHooks.onBootstrapFn();
  41. }
  42. onVendureWorkerBootstrap(): void | Promise<void> {
  43. TestPluginWithAllLifecycleHooks.onWorkerBootstrapFn();
  44. }
  45. onVendureClose(): void | Promise<void> {
  46. TestPluginWithAllLifecycleHooks.onCloseFn();
  47. this.resetSpies();
  48. }
  49. onVendureWorkerClose(): void | Promise<void> {
  50. TestPluginWithAllLifecycleHooks.onWorkerCloseFn();
  51. this.resetSpies();
  52. }
  53. /**
  54. * This is required because on the first run, the Vendure server will be bootstrapped twice -
  55. * once to populate the database and the second time forthe actual tests. Thus the call counts
  56. * for the plugin lifecycles will be doubled. This method resets them after the initial
  57. * (population) run.
  58. */
  59. private resetSpies() {
  60. TestPluginWithAllLifecycleHooks.onConstructorFn.mockClear();
  61. TestPluginWithAllLifecycleHooks.onBootstrapFn.mockClear();
  62. TestPluginWithAllLifecycleHooks.onWorkerBootstrapFn.mockClear();
  63. }
  64. }
  65. @Resolver()
  66. export class TestAdminPluginResolver {
  67. @Query()
  68. foo() {
  69. return ['bar'];
  70. }
  71. @Query()
  72. barList() {
  73. return {
  74. items: [{ id: 1, name: 'Test' }],
  75. totalItems: 1,
  76. };
  77. }
  78. }
  79. @Resolver()
  80. export class TestShopPluginResolver {
  81. @Query()
  82. baz() {
  83. return ['quux'];
  84. }
  85. }
  86. @VendurePlugin({
  87. shopApiExtensions: {
  88. resolvers: [TestShopPluginResolver],
  89. schema: gql`
  90. extend type Query {
  91. baz: [String]!
  92. }
  93. `,
  94. },
  95. adminApiExtensions: {
  96. resolvers: [TestAdminPluginResolver],
  97. schema: gql`
  98. extend type Query {
  99. foo: [String]!
  100. barList(options: BarListOptions): BarList!
  101. }
  102. input BarListOptions
  103. type Bar implements Node {
  104. id: ID!
  105. name: String!
  106. }
  107. type BarList implements PaginatedList {
  108. items: [Bar!]!
  109. totalItems: Int!
  110. }
  111. `,
  112. },
  113. })
  114. export class TestAPIExtensionPlugin {}
  115. @Resolver()
  116. export class TestLazyResolver {
  117. @Query()
  118. lazy() {
  119. return 'sleeping';
  120. }
  121. }
  122. @VendurePlugin({
  123. shopApiExtensions: {
  124. resolvers: () => [TestLazyResolver],
  125. schema: () => gql`
  126. extend type Query {
  127. lazy: String!
  128. }
  129. `,
  130. },
  131. })
  132. export class TestLazyExtensionPlugin {}
  133. @Injectable()
  134. export class NameService {
  135. getNames(): string[] {
  136. return ['seon', 'linda', 'hong'];
  137. }
  138. }
  139. @Resolver()
  140. export class TestResolverWithInjection {
  141. constructor(private nameService: NameService) {}
  142. @Query()
  143. names() {
  144. return this.nameService.getNames();
  145. }
  146. }
  147. @VendurePlugin({
  148. providers: [NameService],
  149. shopApiExtensions: {
  150. resolvers: [TestResolverWithInjection],
  151. schema: gql`
  152. extend type Query {
  153. names: [String]!
  154. }
  155. `,
  156. },
  157. })
  158. export class TestPluginWithProvider {}
  159. @VendurePlugin({
  160. imports: [ConfigModule],
  161. configuration: config => {
  162. // tslint:disable-next-line:no-non-null-assertion
  163. config.defaultLanguageCode = LanguageCode.zh;
  164. return config;
  165. },
  166. })
  167. export class TestPluginWithConfigAndBootstrap implements OnVendureBootstrap, OnVendureClose {
  168. private static boostrapWasCalled: any;
  169. static setup(boostrapWasCalled: (arg: any) => void) {
  170. TestPluginWithConfigAndBootstrap.boostrapWasCalled = boostrapWasCalled;
  171. return TestPluginWithConfigAndBootstrap;
  172. }
  173. constructor(private configService: ConfigService) {}
  174. onVendureBootstrap() {
  175. TestPluginWithConfigAndBootstrap.boostrapWasCalled(this.configService);
  176. }
  177. onVendureClose() {
  178. TestPluginWithConfigAndBootstrap.boostrapWasCalled.mockClear();
  179. }
  180. }