entity-hydrator.e2e-spec.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  2. import { mergeConfig, Order, Product, ProductVariant } from '@vendure/core';
  3. import { createErrorResultGuard, createTestEnvironment, ErrorResultGuard } from '@vendure/testing';
  4. import gql from 'graphql-tag';
  5. import path from 'path';
  6. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  7. import { initialData } from '../../../e2e-common/e2e-initial-data';
  8. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  9. import { HydrationTestPlugin } from './fixtures/test-plugins/hydration-test-plugin';
  10. import { UpdateChannelMutation, UpdateChannelMutationVariables } from './graphql/generated-e2e-admin-types';
  11. import {
  12. AddItemToOrderMutation,
  13. AddItemToOrderMutationVariables,
  14. UpdatedOrderFragment,
  15. } from './graphql/generated-e2e-shop-types';
  16. import { UPDATE_CHANNEL } from './graphql/shared-definitions';
  17. import { ADD_ITEM_TO_ORDER } from './graphql/shop-definitions';
  18. const orderResultGuard: ErrorResultGuard<UpdatedOrderFragment> = createErrorResultGuard(
  19. input => !!input.lines,
  20. );
  21. describe('Entity hydration', () => {
  22. const { server, adminClient, shopClient } = createTestEnvironment(
  23. mergeConfig(testConfig(), {
  24. plugins: [HydrationTestPlugin],
  25. }),
  26. );
  27. beforeAll(async () => {
  28. await server.init({
  29. initialData,
  30. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  31. customerCount: 1,
  32. });
  33. await adminClient.asSuperAdmin();
  34. }, TEST_SETUP_TIMEOUT_MS);
  35. afterAll(async () => {
  36. await server.destroy();
  37. });
  38. it('includes existing relations', async () => {
  39. const { hydrateProduct } = await adminClient.query<HydrateProductQuery>(GET_HYDRATED_PRODUCT, {
  40. id: 'T_1',
  41. });
  42. expect(hydrateProduct.facetValues).toBeDefined();
  43. expect(hydrateProduct.facetValues.length).toBe(2);
  44. });
  45. it('hydrates top-level single relation', async () => {
  46. const { hydrateProduct } = await adminClient.query<HydrateProductQuery>(GET_HYDRATED_PRODUCT, {
  47. id: 'T_1',
  48. });
  49. expect(hydrateProduct.featuredAsset.name).toBe('derick-david-409858-unsplash.jpg');
  50. });
  51. it('hydrates top-level array relation', async () => {
  52. const { hydrateProduct } = await adminClient.query<HydrateProductQuery>(GET_HYDRATED_PRODUCT, {
  53. id: 'T_1',
  54. });
  55. expect(hydrateProduct.assets.length).toBe(1);
  56. expect(hydrateProduct.assets[0].asset.name).toBe('derick-david-409858-unsplash.jpg');
  57. });
  58. it('hydrates nested single relation', async () => {
  59. const { hydrateProduct } = await adminClient.query<HydrateProductQuery>(GET_HYDRATED_PRODUCT, {
  60. id: 'T_1',
  61. });
  62. expect(hydrateProduct.variants[0].product.id).toBe('T_1');
  63. });
  64. it('hydrates nested array relation', async () => {
  65. const { hydrateProduct } = await adminClient.query<HydrateProductQuery>(GET_HYDRATED_PRODUCT, {
  66. id: 'T_1',
  67. });
  68. expect(hydrateProduct.variants[0].options.length).toBe(2);
  69. });
  70. it('translates top-level translatable', async () => {
  71. const { hydrateProduct } = await adminClient.query<HydrateProductQuery>(GET_HYDRATED_PRODUCT, {
  72. id: 'T_1',
  73. });
  74. expect(hydrateProduct.variants.map(v => v.name).sort()).toEqual([
  75. 'Laptop 13 inch 16GB',
  76. 'Laptop 13 inch 8GB',
  77. 'Laptop 15 inch 16GB',
  78. 'Laptop 15 inch 8GB',
  79. ]);
  80. });
  81. it('translates nested translatable', async () => {
  82. const { hydrateProduct } = await adminClient.query<HydrateProductQuery>(GET_HYDRATED_PRODUCT, {
  83. id: 'T_1',
  84. });
  85. expect(
  86. getVariantWithName(hydrateProduct, 'Laptop 13 inch 8GB')
  87. .options.map(o => o.name)
  88. .sort(),
  89. ).toEqual(['13 inch', '8GB']);
  90. });
  91. it('translates nested translatable 2', async () => {
  92. const { hydrateProduct } = await adminClient.query<HydrateProductQuery>(GET_HYDRATED_PRODUCT, {
  93. id: 'T_1',
  94. });
  95. expect(hydrateProduct.assets[0].product.name).toBe('Laptop');
  96. });
  97. it('populates ProductVariant price data', async () => {
  98. const { hydrateProduct } = await adminClient.query<HydrateProductQuery>(GET_HYDRATED_PRODUCT, {
  99. id: 'T_1',
  100. });
  101. expect(getVariantWithName(hydrateProduct, 'Laptop 13 inch 8GB').price).toBe(129900);
  102. expect(getVariantWithName(hydrateProduct, 'Laptop 13 inch 8GB').priceWithTax).toBe(155880);
  103. expect(getVariantWithName(hydrateProduct, 'Laptop 13 inch 16GB').price).toBe(219900);
  104. expect(getVariantWithName(hydrateProduct, 'Laptop 13 inch 16GB').priceWithTax).toBe(263880);
  105. expect(getVariantWithName(hydrateProduct, 'Laptop 15 inch 8GB').price).toBe(139900);
  106. expect(getVariantWithName(hydrateProduct, 'Laptop 15 inch 8GB').priceWithTax).toBe(167880);
  107. expect(getVariantWithName(hydrateProduct, 'Laptop 15 inch 16GB').price).toBe(229900);
  108. expect(getVariantWithName(hydrateProduct, 'Laptop 15 inch 16GB').priceWithTax).toBe(275880);
  109. });
  110. // https://github.com/vendure-ecommerce/vendure/issues/1153
  111. it('correctly handles empty array relations', async () => {
  112. // Product T_5 has no asset defined
  113. const { hydrateProductAsset } = await adminClient.query<{ hydrateProductAsset: Product }>(
  114. GET_HYDRATED_PRODUCT_ASSET,
  115. {
  116. id: 'T_5',
  117. },
  118. );
  119. expect(hydrateProductAsset.assets).toEqual([]);
  120. });
  121. // https://github.com/vendure-ecommerce/vendure/issues/1324
  122. it('correctly handles empty nested array relations', async () => {
  123. const { hydrateProductWithNoFacets } = await adminClient.query<{
  124. hydrateProductWithNoFacets: Product;
  125. }>(GET_HYDRATED_PRODUCT_NO_FACETS);
  126. expect(hydrateProductWithNoFacets.facetValues).toEqual([]);
  127. });
  128. // https://github.com/vendure-ecommerce/vendure/issues/1161
  129. it('correctly expands missing relations', async () => {
  130. const { hydrateProductVariant } = await adminClient.query<{ hydrateProductVariant: ProductVariant }>(
  131. GET_HYDRATED_VARIANT,
  132. { id: 'T_1' },
  133. );
  134. expect(hydrateProductVariant.product.id).toBe('T_1');
  135. expect(hydrateProductVariant.product.facetValues.map(fv => fv.id).sort()).toEqual(['T_1', 'T_2']);
  136. });
  137. // https://github.com/vendure-ecommerce/vendure/issues/1172
  138. it('can hydrate entity with getters (Order)', async () => {
  139. const { addItemToOrder } = await shopClient.query<
  140. AddItemToOrderMutation,
  141. AddItemToOrderMutationVariables
  142. >(ADD_ITEM_TO_ORDER, {
  143. productVariantId: 'T_1',
  144. quantity: 1,
  145. });
  146. orderResultGuard.assertSuccess(addItemToOrder);
  147. const { hydrateOrder } = await adminClient.query<{ hydrateOrder: Order }>(GET_HYDRATED_ORDER, {
  148. id: addItemToOrder.id,
  149. });
  150. expect(hydrateOrder.id).toBe('T_1');
  151. expect(hydrateOrder.payments).toEqual([]);
  152. });
  153. // https://github.com/vendure-ecommerce/vendure/issues/1229
  154. it('deep merges existing properties', async () => {
  155. await shopClient.asAnonymousUser();
  156. const { addItemToOrder } = await shopClient.query<
  157. AddItemToOrderMutation,
  158. AddItemToOrderMutationVariables
  159. >(ADD_ITEM_TO_ORDER, {
  160. productVariantId: 'T_1',
  161. quantity: 2,
  162. });
  163. orderResultGuard.assertSuccess(addItemToOrder);
  164. const { hydrateOrderReturnQuantities } = await adminClient.query<{
  165. hydrateOrderReturnQuantities: number[];
  166. }>(GET_HYDRATED_ORDER_QUANTITIES, {
  167. id: addItemToOrder.id,
  168. });
  169. expect(hydrateOrderReturnQuantities).toEqual([2]);
  170. });
  171. // https://github.com/vendure-ecommerce/vendure/issues/1284
  172. it('hydrates custom field relations', async () => {
  173. await adminClient.query<UpdateChannelMutation, UpdateChannelMutationVariables>(UPDATE_CHANNEL, {
  174. input: {
  175. id: 'T_1',
  176. customFields: {
  177. thumbId: 'T_2',
  178. },
  179. },
  180. });
  181. const { hydrateChannel } = await adminClient.query<{
  182. hydrateChannel: any;
  183. }>(GET_HYDRATED_CHANNEL, {
  184. id: 'T_1',
  185. });
  186. expect(hydrateChannel.customFields.thumb).toBeDefined();
  187. expect(hydrateChannel.customFields.thumb.id).toBe('T_2');
  188. });
  189. });
  190. function getVariantWithName(product: Product, name: string) {
  191. return product.variants.find(v => v.name === name)!;
  192. }
  193. type HydrateProductQuery = { hydrateProduct: Product };
  194. const GET_HYDRATED_PRODUCT = gql`
  195. query GetHydratedProduct($id: ID!) {
  196. hydrateProduct(id: $id)
  197. }
  198. `;
  199. const GET_HYDRATED_PRODUCT_NO_FACETS = gql`
  200. query GetHydratedProductWithNoFacets {
  201. hydrateProductWithNoFacets
  202. }
  203. `;
  204. const GET_HYDRATED_PRODUCT_ASSET = gql`
  205. query GetHydratedProductAsset($id: ID!) {
  206. hydrateProductAsset(id: $id)
  207. }
  208. `;
  209. const GET_HYDRATED_VARIANT = gql`
  210. query GetHydratedVariant($id: ID!) {
  211. hydrateProductVariant(id: $id)
  212. }
  213. `;
  214. const GET_HYDRATED_ORDER = gql`
  215. query GetHydratedOrder($id: ID!) {
  216. hydrateOrder(id: $id)
  217. }
  218. `;
  219. const GET_HYDRATED_ORDER_QUANTITIES = gql`
  220. query GetHydratedOrderQuantities($id: ID!) {
  221. hydrateOrderReturnQuantities(id: $id)
  222. }
  223. `;
  224. const GET_HYDRATED_CHANNEL = gql`
  225. query GetHydratedChannel($id: ID!) {
  226. hydrateChannel(id: $id)
  227. }
  228. `;