product.e2e-spec.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import {
  2. CreateProduct,
  3. CreateProductVariables,
  4. GetProductList,
  5. GetProductListVariables,
  6. GetProductWithVariants,
  7. GetProductWithVariantsVariables,
  8. LanguageCode,
  9. SortOrder,
  10. } from 'shared/generated-types';
  11. import { CREATE_PRODUCT } from '../../admin-ui/src/app/data/mutations/product-mutations';
  12. import {
  13. GET_PRODUCT_LIST,
  14. GET_PRODUCT_WITH_VARIANTS,
  15. } from '../../admin-ui/src/app/data/queries/product-queries';
  16. import { TestClient } from './test-client';
  17. import { TestServer } from './test-server';
  18. describe('Product resolver', () => {
  19. const client = new TestClient();
  20. const server = new TestServer();
  21. beforeAll(async () => {
  22. await server.init({
  23. productCount: 20,
  24. customerCount: 1,
  25. });
  26. }, 30000);
  27. afterAll(async () => {
  28. await server.destroy();
  29. });
  30. describe('products list query', () => {
  31. it('returns all products when no options passed', async () => {
  32. const result = await client.query<GetProductList, GetProductListVariables>(GET_PRODUCT_LIST, {
  33. languageCode: LanguageCode.en,
  34. });
  35. expect(result.products.items.length).toBe(20);
  36. expect(result.products.totalItems).toBe(20);
  37. });
  38. it('limits result set with skip & take', async () => {
  39. const result = await client.query<GetProductList, GetProductListVariables>(GET_PRODUCT_LIST, {
  40. languageCode: LanguageCode.en,
  41. options: {
  42. skip: 0,
  43. take: 3,
  44. },
  45. });
  46. expect(result.products.items.length).toBe(3);
  47. expect(result.products.totalItems).toBe(20);
  48. });
  49. it('filters by name', async () => {
  50. const result = await client.query<GetProductList, GetProductListVariables>(GET_PRODUCT_LIST, {
  51. languageCode: LanguageCode.en,
  52. options: {
  53. filter: {
  54. name: {
  55. contains: 'fish',
  56. },
  57. },
  58. },
  59. });
  60. expect(result.products.items.length).toBe(1);
  61. expect(result.products.items[0].name).toBe('en Practical Frozen Fish');
  62. });
  63. it('sorts by name', async () => {
  64. const result = await client.query<GetProductList, GetProductListVariables>(GET_PRODUCT_LIST, {
  65. languageCode: LanguageCode.en,
  66. options: {
  67. sort: {
  68. name: SortOrder.ASC,
  69. },
  70. },
  71. });
  72. expect(result.products.items.map(p => p.name)).toEqual([
  73. 'en Fantastic Granite Salad',
  74. 'en Fantastic Rubber Sausages',
  75. 'en Generic Metal Keyboard',
  76. 'en Generic Wooden Sausages',
  77. 'en Handcrafted Granite Shirt',
  78. 'en Handcrafted Plastic Gloves',
  79. 'en Handmade Cotton Salad',
  80. 'en Incredible Metal Shirt',
  81. 'en Incredible Steel Cheese',
  82. 'en Intelligent Frozen Ball',
  83. 'en Intelligent Wooden Car',
  84. 'en Licensed Cotton Shirt',
  85. 'en Licensed Frozen Chair',
  86. 'en Practical Frozen Fish',
  87. 'en Refined Fresh Bacon',
  88. 'en Rustic Steel Salad',
  89. 'en Rustic Wooden Hat',
  90. 'en Small Granite Chicken',
  91. 'en Small Steel Cheese',
  92. 'en Tasty Soft Gloves',
  93. ]);
  94. });
  95. });
  96. describe('product query', () => {
  97. it('returns expected properties', async () => {
  98. const result = await client.query<GetProductWithVariants, GetProductWithVariantsVariables>(
  99. GET_PRODUCT_WITH_VARIANTS,
  100. {
  101. languageCode: LanguageCode.en,
  102. id: '2',
  103. },
  104. );
  105. if (!result.product) {
  106. fail('Product not found');
  107. return;
  108. }
  109. expect(result.product).toEqual(
  110. expect.objectContaining({
  111. description: 'en Ut nulla quam ipsam nobis cupiditate sed dignissimos debitis incidunt.',
  112. id: '2',
  113. image: 'http://lorempixel.com/640/480',
  114. languageCode: 'en',
  115. name: 'en Incredible Metal Shirt',
  116. optionGroups: [
  117. {
  118. code: 'size',
  119. id: '1',
  120. languageCode: 'en',
  121. name: 'Size',
  122. },
  123. ],
  124. slug: 'en incredible-metal-shirt',
  125. translations: [
  126. {
  127. description:
  128. 'en Ut nulla quam ipsam nobis cupiditate sed dignissimos debitis incidunt.',
  129. languageCode: 'en',
  130. name: 'en Incredible Metal Shirt',
  131. slug: 'en incredible-metal-shirt',
  132. },
  133. {
  134. description:
  135. 'de Ut nulla quam ipsam nobis cupiditate sed dignissimos debitis incidunt.',
  136. languageCode: 'de',
  137. name: 'de Incredible Metal Shirt',
  138. slug: 'de incredible-metal-shirt',
  139. },
  140. ],
  141. }),
  142. );
  143. });
  144. it('returns null when id not found', async () => {
  145. const result = await client.query<GetProductWithVariants, GetProductWithVariantsVariables>(
  146. GET_PRODUCT_WITH_VARIANTS,
  147. {
  148. languageCode: LanguageCode.en,
  149. id: 'bad_id',
  150. },
  151. );
  152. expect(result.product).toBeNull();
  153. });
  154. });
  155. describe('createProduct mutation', () => {
  156. it('creates a new Product', async () => {
  157. const result = await client.query<CreateProduct, CreateProductVariables>(CREATE_PRODUCT, {
  158. input: {
  159. image: 'baked-potato',
  160. translations: [
  161. {
  162. languageCode: LanguageCode.en,
  163. name: 'en Baked Potato',
  164. slug: 'en-baked-potato',
  165. description: 'A baked potato',
  166. },
  167. {
  168. languageCode: LanguageCode.de,
  169. name: 'de Baked Potato',
  170. slug: 'de-baked-potato',
  171. description: 'Eine baked Erdapfel',
  172. },
  173. ],
  174. },
  175. });
  176. expect(result.createProduct).toMatchSnapshot();
  177. });
  178. });
  179. });