relations-decorator.e2e-spec.ts 5.1 KB

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