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

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