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

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