entity-hydrator.e2e-spec.ts 9.5 KB

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