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

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