entity-id-strategy.e2e-spec.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* tslint:disable:no-non-null-assertion */
  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 { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  7. import {
  8. IdTest1,
  9. IdTest2,
  10. IdTest3,
  11. IdTest4,
  12. IdTest5,
  13. IdTest6,
  14. IdTest7,
  15. IdTest8,
  16. LanguageCode,
  17. } from './graphql/generated-e2e-admin-types';
  18. describe('EntityIdStrategy', () => {
  19. const { server, adminClient, shopClient } = createTestEnvironment(testConfig);
  20. beforeAll(async () => {
  21. await server.init({
  22. dataDir: path.join(__dirname, '__data__'),
  23. initialData,
  24. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  25. customerCount: 1,
  26. });
  27. await adminClient.asSuperAdmin();
  28. }, TEST_SETUP_TIMEOUT_MS);
  29. afterAll(async () => {
  30. await server.destroy();
  31. });
  32. it('encodes ids', async () => {
  33. const { products } = await shopClient.query<IdTest1.Query>(gql`
  34. query IdTest1 {
  35. products(options: { take: 5 }) {
  36. items {
  37. id
  38. }
  39. }
  40. }
  41. `);
  42. expect(products).toEqual({
  43. items: [{ id: 'T_1' }, { id: 'T_2' }, { id: 'T_3' }, { id: 'T_4' }, { id: 'T_5' }],
  44. });
  45. });
  46. it('Does not doubly-encode ids from resolved properties', async () => {
  47. const { products } = await shopClient.query<IdTest2.Query>(gql`
  48. query IdTest2 {
  49. products(options: { take: 1 }) {
  50. items {
  51. id
  52. variants {
  53. id
  54. options {
  55. id
  56. name
  57. }
  58. }
  59. }
  60. }
  61. }
  62. `);
  63. expect(products.items[0].id).toBe('T_1');
  64. expect(products.items[0].variants[0].id).toBe('T_1');
  65. expect(products.items[0].variants[0].options[0].id).toBe('T_1');
  66. });
  67. it('decodes embedded argument', async () => {
  68. const { product } = await shopClient.query<IdTest3.Query>(gql`
  69. query IdTest3 {
  70. product(id: "T_1") {
  71. id
  72. }
  73. }
  74. `);
  75. expect(product).toEqual({
  76. id: 'T_1',
  77. });
  78. });
  79. it('decodes embedded nested id', async () => {
  80. const { updateProduct } = await adminClient.query<IdTest4.Mutation>(gql`
  81. mutation IdTest4 {
  82. updateProduct(input: { id: "T_1", featuredAssetId: "T_3" }) {
  83. id
  84. featuredAsset {
  85. id
  86. }
  87. }
  88. }
  89. `);
  90. expect(updateProduct).toEqual({
  91. id: 'T_1',
  92. featuredAsset: {
  93. id: 'T_3',
  94. },
  95. });
  96. });
  97. it('decodes embedded nested object id', async () => {
  98. const { updateProduct } = await adminClient.query<IdTest5.Mutation>(gql`
  99. mutation IdTest5 {
  100. updateProduct(
  101. input: { id: "T_1", translations: [{ id: "T_1", languageCode: en, name: "changed" }] }
  102. ) {
  103. id
  104. name
  105. }
  106. }
  107. `);
  108. expect(updateProduct).toEqual({
  109. id: 'T_1',
  110. name: 'changed',
  111. });
  112. });
  113. it('decodes argument as variable', async () => {
  114. const { product } = await shopClient.query<IdTest6.Query, IdTest6.Variables>(
  115. gql`
  116. query IdTest6($id: ID!) {
  117. product(id: $id) {
  118. id
  119. }
  120. }
  121. `,
  122. { id: 'T_1' },
  123. );
  124. expect(product).toEqual({
  125. id: 'T_1',
  126. });
  127. });
  128. it('decodes nested id as variable', async () => {
  129. const { updateProduct } = await adminClient.query<IdTest7.Mutation, IdTest7.Variables>(
  130. gql`
  131. mutation IdTest7($input: UpdateProductInput!) {
  132. updateProduct(input: $input) {
  133. id
  134. featuredAsset {
  135. id
  136. }
  137. }
  138. }
  139. `,
  140. {
  141. input: {
  142. id: 'T_1',
  143. featuredAssetId: 'T_2',
  144. },
  145. },
  146. );
  147. expect(updateProduct).toEqual({
  148. id: 'T_1',
  149. featuredAsset: {
  150. id: 'T_2',
  151. },
  152. });
  153. });
  154. it('decodes nested object id as variable', async () => {
  155. const { updateProduct } = await adminClient.query<IdTest8.Mutation, IdTest8.Variables>(
  156. gql`
  157. mutation IdTest8($input: UpdateProductInput!) {
  158. updateProduct(input: $input) {
  159. id
  160. name
  161. }
  162. }
  163. `,
  164. {
  165. input: {
  166. id: 'T_1',
  167. translations: [{ id: 'T_1', languageCode: LanguageCode.en, name: 'changed again' }],
  168. },
  169. },
  170. );
  171. expect(updateProduct).toEqual({
  172. id: 'T_1',
  173. name: 'changed again',
  174. });
  175. });
  176. it('encodes ids in fragment', async () => {
  177. const { products } = await shopClient.query<IdTest1.Query>(gql`
  178. query IdTest9 {
  179. products(options: { take: 1 }) {
  180. items {
  181. ...ProdFragment
  182. }
  183. }
  184. }
  185. fragment ProdFragment on Product {
  186. id
  187. featuredAsset {
  188. id
  189. }
  190. }
  191. `);
  192. expect(products).toEqual({
  193. items: [
  194. {
  195. id: 'T_1',
  196. featuredAsset: {
  197. id: 'T_2',
  198. },
  199. },
  200. ],
  201. });
  202. });
  203. });