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

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