entity-hydrator.e2e-spec.ts 9.4 KB

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