populate.e2e-spec.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. import { INestApplication } from '@nestjs/common';
  2. import { DefaultLogger, User } from '@vendure/core';
  3. import { populate } from '@vendure/core/cli';
  4. import { createTestEnvironment, E2E_DEFAULT_CHANNEL_TOKEN } from '@vendure/testing';
  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 { InitialData } from '../src/index';
  9. import {
  10. ChannelFragment,
  11. CreateChannelMutation,
  12. CreateChannelMutationVariables,
  13. CurrencyCode,
  14. GetAssetListQuery,
  15. GetCollectionsQuery,
  16. GetProductListQuery,
  17. LanguageCode,
  18. } from './graphql/generated-e2e-admin-types';
  19. import {
  20. CREATE_CHANNEL,
  21. GET_ASSET_LIST,
  22. GET_COLLECTIONS,
  23. GET_PRODUCT_LIST,
  24. } from './graphql/shared-definitions';
  25. describe('populate() function', () => {
  26. let channel2: ChannelFragment;
  27. const { server, adminClient } = createTestEnvironment({
  28. ...testConfig(),
  29. // logger: new DefaultLogger(),
  30. customFields: {
  31. Product: [
  32. { type: 'string', name: 'pageType' },
  33. {
  34. name: 'owner',
  35. public: true,
  36. nullable: true,
  37. type: 'relation',
  38. entity: User,
  39. eager: true,
  40. },
  41. {
  42. name: 'keywords',
  43. public: true,
  44. nullable: true,
  45. type: 'string',
  46. list: true,
  47. },
  48. {
  49. name: 'localName',
  50. type: 'localeString',
  51. },
  52. ],
  53. ProductVariant: [{ type: 'int', name: 'weight' }],
  54. },
  55. });
  56. beforeAll(async () => {
  57. await server.init({
  58. initialData: { ...initialData, collections: [] },
  59. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-empty.csv'),
  60. customerCount: 1,
  61. });
  62. await adminClient.asSuperAdmin();
  63. const { createChannel } = await adminClient.query<
  64. CreateChannelMutation,
  65. CreateChannelMutationVariables
  66. >(CREATE_CHANNEL, {
  67. input: {
  68. code: 'Channel 2',
  69. token: 'channel-2',
  70. currencyCode: CurrencyCode.EUR,
  71. defaultLanguageCode: LanguageCode.en,
  72. defaultShippingZoneId: 'T_1',
  73. defaultTaxZoneId: 'T_2',
  74. pricesIncludeTax: true,
  75. },
  76. });
  77. channel2 = createChannel as ChannelFragment;
  78. await server.destroy();
  79. }, TEST_SETUP_TIMEOUT_MS);
  80. describe('populating default channel', () => {
  81. let app: INestApplication;
  82. beforeAll(async () => {
  83. const initialDataForPopulate: InitialData = {
  84. defaultLanguage: initialData.defaultLanguage,
  85. defaultZone: initialData.defaultZone,
  86. taxRates: [],
  87. shippingMethods: [],
  88. paymentMethods: [],
  89. countries: [],
  90. collections: [{ name: 'Collection 1', filters: [] }],
  91. };
  92. const csvFile = path.join(__dirname, 'fixtures', 'product-import.csv');
  93. app = await populate(
  94. async () => {
  95. await server.bootstrap();
  96. return server.app;
  97. },
  98. initialDataForPopulate,
  99. csvFile,
  100. );
  101. }, TEST_SETUP_TIMEOUT_MS);
  102. afterAll(async () => {
  103. await app.close();
  104. });
  105. it('populates products', async () => {
  106. await adminClient.asSuperAdmin();
  107. const { products } = await adminClient.query<GetProductListQuery>(GET_PRODUCT_LIST);
  108. expect(products.totalItems).toBe(4);
  109. expect(products.items.map(i => i.name).sort()).toEqual([
  110. 'Artists Smock',
  111. 'Giotto Mega Pencils',
  112. 'Mabef M/02 Studio Easel',
  113. 'Perfect Paper Stretcher',
  114. ]);
  115. });
  116. it('populates assets', async () => {
  117. const { assets } = await adminClient.query<GetAssetListQuery>(GET_ASSET_LIST);
  118. expect(assets.items.map(i => i.name).sort()).toEqual([
  119. 'box-of-12.jpg',
  120. 'box-of-8.jpg',
  121. 'pps1.jpg',
  122. 'pps2.jpg',
  123. ]);
  124. });
  125. it('populates collections', async () => {
  126. const { collections } = await adminClient.query<GetCollectionsQuery>(GET_COLLECTIONS);
  127. expect(collections.items.map(i => i.name).sort()).toEqual(['Collection 1']);
  128. });
  129. });
  130. describe('populating a non-default channel', () => {
  131. let app: INestApplication;
  132. beforeAll(async () => {
  133. const initialDataForPopulate: InitialData = {
  134. defaultLanguage: initialData.defaultLanguage,
  135. defaultZone: initialData.defaultZone,
  136. taxRates: [],
  137. shippingMethods: [],
  138. paymentMethods: [],
  139. countries: [],
  140. collections: [{ name: 'Collection 2', filters: [] }],
  141. };
  142. const csvFile = path.join(__dirname, 'fixtures', 'product-import-channel.csv');
  143. app = await populate(
  144. async () => {
  145. await server.bootstrap();
  146. return server.app;
  147. },
  148. initialDataForPopulate,
  149. csvFile,
  150. channel2.token,
  151. );
  152. });
  153. afterAll(async () => {
  154. await app.close();
  155. });
  156. it('populates products', async () => {
  157. await adminClient.asSuperAdmin();
  158. await adminClient.setChannelToken(channel2.token);
  159. const { products } = await adminClient.query<GetProductListQuery>(GET_PRODUCT_LIST);
  160. expect(products.totalItems).toBe(1);
  161. expect(products.items.map(i => i.name).sort()).toEqual(['Model Hand']);
  162. });
  163. it('populates assets', async () => {
  164. const { assets } = await adminClient.query<GetAssetListQuery>(GET_ASSET_LIST);
  165. expect(assets.items.map(i => i.name).sort()).toEqual(['vincent-botta-736919-unsplash.jpg']);
  166. });
  167. it('populates collections', async () => {
  168. const { collections } = await adminClient.query<GetCollectionsQuery>(GET_COLLECTIONS);
  169. expect(collections.items.map(i => i.name).sort()).toEqual(['Collection 2']);
  170. });
  171. it('product also assigned to default channel', async () => {
  172. await adminClient.setChannelToken(E2E_DEFAULT_CHANNEL_TOKEN);
  173. const { products } = await adminClient.query<GetProductListQuery>(GET_PRODUCT_LIST);
  174. expect(products.items.map(i => i.name).includes('Model Hand')).toBe(true);
  175. });
  176. });
  177. });