relations-decorator.e2e-spec.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. import { mergeConfig, Zone } from '@vendure/core';
  2. import { createTestEnvironment } from '@vendure/testing';
  3. import gql from 'graphql-tag';
  4. import path from 'path';
  5. import { initialData } from '../../../e2e-common/e2e-initial-data';
  6. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  7. import {
  8. RelationDecoratorTestService,
  9. RelationsDecoratorTestPlugin,
  10. } from './fixtures/test-plugins/relations-decorator-test-plugin';
  11. describe('Relations decorator', () => {
  12. const { server, adminClient, shopClient } = createTestEnvironment(
  13. mergeConfig(testConfig(), {
  14. customFields: {
  15. Order: [
  16. {
  17. name: 'zone',
  18. entity: Zone,
  19. type: 'relation',
  20. },
  21. ],
  22. },
  23. plugins: [RelationsDecoratorTestPlugin],
  24. }),
  25. );
  26. let testService: RelationDecoratorTestService;
  27. beforeAll(async () => {
  28. await server.init({
  29. initialData,
  30. customerCount: 1,
  31. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  32. });
  33. await adminClient.asSuperAdmin();
  34. testService = server.app.get(RelationDecoratorTestService);
  35. }, TEST_SETUP_TIMEOUT_MS);
  36. afterAll(async () => {
  37. await server.destroy();
  38. });
  39. it('empty relations', async () => {
  40. testService.reset();
  41. await shopClient.query(gql`
  42. {
  43. orders(options: { take: 5 }) {
  44. items {
  45. id
  46. }
  47. }
  48. }
  49. `);
  50. expect(testService.getRelations()).toEqual([]);
  51. });
  52. it('relations specified in query are included', async () => {
  53. testService.reset();
  54. await shopClient.query(gql`
  55. {
  56. orders(options: { take: 5 }) {
  57. items {
  58. customer {
  59. firstName
  60. }
  61. lines {
  62. featuredAsset {
  63. preview
  64. }
  65. }
  66. }
  67. }
  68. }
  69. `);
  70. expect(testService.getRelations()).toEqual(['customer', 'lines', 'lines.featuredAsset']);
  71. });
  72. it('custom field relations are included', async () => {
  73. testService.reset();
  74. await shopClient.query(gql`
  75. {
  76. orders(options: { take: 5 }) {
  77. items {
  78. customFields {
  79. zone {
  80. id
  81. }
  82. }
  83. }
  84. }
  85. }
  86. `);
  87. expect(testService.getRelations()).toEqual(['customFields.zone']);
  88. });
  89. it('relations specified in Calculated decorator are included', async () => {
  90. testService.reset();
  91. await shopClient.query(gql`
  92. {
  93. orders(options: { take: 5 }) {
  94. items {
  95. id
  96. totalQuantity
  97. }
  98. }
  99. }
  100. `);
  101. expect(testService.getRelations()).toEqual(['lines']);
  102. });
  103. it('defaults to a depth of 3', async () => {
  104. testService.reset();
  105. await shopClient.query(gql`
  106. {
  107. orders(options: { take: 5 }) {
  108. items {
  109. lines {
  110. productVariant {
  111. product {
  112. featuredAsset {
  113. preview
  114. }
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121. `);
  122. expect(testService.getRelations()).toEqual([
  123. 'lines',
  124. 'lines.productVariant',
  125. 'lines.productVariant.product',
  126. ]);
  127. });
  128. it('manually set depth of 5', async () => {
  129. testService.reset();
  130. await shopClient.query(gql`
  131. {
  132. ordersWithDepth5(options: { take: 5 }) {
  133. items {
  134. lines {
  135. productVariant {
  136. product {
  137. optionGroups {
  138. options {
  139. name
  140. }
  141. }
  142. }
  143. }
  144. }
  145. }
  146. }
  147. }
  148. `);
  149. expect(testService.getRelations()).toEqual([
  150. 'lines',
  151. 'lines.productVariant',
  152. 'lines.productVariant.product',
  153. 'lines.productVariant.product.optionGroups',
  154. 'lines.productVariant.product.optionGroups.options',
  155. ]);
  156. });
  157. });