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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  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. IdTest1,
  10. IdTest2,
  11. IdTest3,
  12. IdTest4,
  13. IdTest5,
  14. IdTest6,
  15. IdTest7,
  16. IdTest8,
  17. LanguageCode,
  18. } from './graphql/generated-e2e-admin-types';
  19. import { sortById } from './utils/test-order-utils';
  20. describe('EntityIdStrategy', () => {
  21. const { server, adminClient, shopClient } = createTestEnvironment(testConfig());
  22. beforeAll(async () => {
  23. await server.init({
  24. initialData,
  25. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  26. customerCount: 1,
  27. });
  28. await adminClient.asSuperAdmin();
  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.items.sort(sortById)).toEqual([
  44. { id: 'T_1' },
  45. { id: 'T_2' },
  46. { id: 'T_3' },
  47. { id: 'T_4' },
  48. { id: 'T_5' },
  49. ]);
  50. });
  51. it('Does not doubly-encode ids from resolved properties', async () => {
  52. const { products } = await shopClient.query<IdTest2.Query>(gql`
  53. query IdTest2 {
  54. products(options: { take: 1 }) {
  55. items {
  56. id
  57. variants {
  58. id
  59. options {
  60. id
  61. name
  62. }
  63. }
  64. }
  65. }
  66. }
  67. `);
  68. expect(products.items[0].id).toBe('T_1');
  69. expect(products.items[0].variants[0].id).toBe('T_1');
  70. expect(products.items[0].variants[0].options.map(o => o.id).sort()).toEqual(['T_1', 'T_3']);
  71. });
  72. it('decodes embedded argument', async () => {
  73. const { product } = await shopClient.query<IdTest3.Query>(gql`
  74. query IdTest3 {
  75. product(id: "T_1") {
  76. id
  77. }
  78. }
  79. `);
  80. expect(product).toEqual({
  81. id: 'T_1',
  82. });
  83. });
  84. it('decodes embedded nested id', async () => {
  85. const { updateProduct } = await adminClient.query<IdTest4.Mutation>(gql`
  86. mutation IdTest4 {
  87. updateProduct(input: { id: "T_1", featuredAssetId: "T_3" }) {
  88. id
  89. featuredAsset {
  90. id
  91. }
  92. }
  93. }
  94. `);
  95. expect(updateProduct).toEqual({
  96. id: 'T_1',
  97. featuredAsset: {
  98. id: 'T_3',
  99. },
  100. });
  101. });
  102. it('decodes embedded nested object id', async () => {
  103. const { updateProduct } = await adminClient.query<IdTest5.Mutation>(gql`
  104. mutation IdTest5 {
  105. updateProduct(
  106. input: { id: "T_1", translations: [{ id: "T_1", languageCode: en, name: "changed" }] }
  107. ) {
  108. id
  109. name
  110. }
  111. }
  112. `);
  113. expect(updateProduct).toEqual({
  114. id: 'T_1',
  115. name: 'changed',
  116. });
  117. });
  118. it('decodes argument as variable', async () => {
  119. const { product } = await shopClient.query<IdTest6.Query, IdTest6.Variables>(
  120. gql`
  121. query IdTest6($id: ID!) {
  122. product(id: $id) {
  123. id
  124. }
  125. }
  126. `,
  127. { id: 'T_1' },
  128. );
  129. expect(product).toEqual({
  130. id: 'T_1',
  131. });
  132. });
  133. it('decodes nested id as variable', async () => {
  134. const { updateProduct } = await adminClient.query<IdTest7.Mutation, IdTest7.Variables>(
  135. gql`
  136. mutation IdTest7($input: UpdateProductInput!) {
  137. updateProduct(input: $input) {
  138. id
  139. featuredAsset {
  140. id
  141. }
  142. }
  143. }
  144. `,
  145. {
  146. input: {
  147. id: 'T_1',
  148. featuredAssetId: 'T_2',
  149. },
  150. },
  151. );
  152. expect(updateProduct).toEqual({
  153. id: 'T_1',
  154. featuredAsset: {
  155. id: 'T_2',
  156. },
  157. });
  158. });
  159. it('decodes nested object id as variable', async () => {
  160. const { updateProduct } = await adminClient.query<IdTest8.Mutation, IdTest8.Variables>(
  161. gql`
  162. mutation IdTest8($input: UpdateProductInput!) {
  163. updateProduct(input: $input) {
  164. id
  165. name
  166. }
  167. }
  168. `,
  169. {
  170. input: {
  171. id: 'T_1',
  172. translations: [{ id: 'T_1', languageCode: LanguageCode.en, name: 'changed again' }],
  173. },
  174. },
  175. );
  176. expect(updateProduct).toEqual({
  177. id: 'T_1',
  178. name: 'changed again',
  179. });
  180. });
  181. it('encodes ids in fragment', async () => {
  182. const { products } = await shopClient.query<IdTest1.Query>(gql`
  183. query IdTest9 {
  184. products(options: { take: 1 }) {
  185. items {
  186. ...ProdFragment
  187. }
  188. }
  189. }
  190. fragment ProdFragment on Product {
  191. id
  192. featuredAsset {
  193. id
  194. }
  195. }
  196. `);
  197. expect(products).toEqual({
  198. items: [
  199. {
  200. id: 'T_1',
  201. featuredAsset: {
  202. id: 'T_2',
  203. },
  204. },
  205. ],
  206. });
  207. });
  208. it('encodes ids in doubly-nested fragment', async () => {
  209. const { products } = await shopClient.query<IdTest1.Query>(gql`
  210. query IdTest10 {
  211. products(options: { take: 1 }) {
  212. items {
  213. ...ProdFragment1
  214. }
  215. }
  216. }
  217. fragment ProdFragment1 on Product {
  218. ...ProdFragment2
  219. }
  220. fragment ProdFragment2 on Product {
  221. id
  222. featuredAsset {
  223. id
  224. }
  225. }
  226. `);
  227. expect(products).toEqual({
  228. items: [
  229. {
  230. id: 'T_1',
  231. featuredAsset: {
  232. id: 'T_2',
  233. },
  234. },
  235. ],
  236. });
  237. });
  238. it('encodes ids in triply-nested fragment', async () => {
  239. const { products } = await shopClient.query<IdTest1.Query>(gql`
  240. query IdTest11 {
  241. products(options: { take: 1 }) {
  242. items {
  243. ...ProdFragment1_1
  244. }
  245. }
  246. }
  247. fragment ProdFragment1_1 on Product {
  248. ...ProdFragment2_1
  249. }
  250. fragment ProdFragment2_1 on Product {
  251. ...ProdFragment3_1
  252. }
  253. fragment ProdFragment3_1 on Product {
  254. id
  255. featuredAsset {
  256. id
  257. }
  258. }
  259. `);
  260. expect(products).toEqual({
  261. items: [
  262. {
  263. id: 'T_1',
  264. featuredAsset: {
  265. id: 'T_2',
  266. },
  267. },
  268. ],
  269. });
  270. });
  271. });