zone.e2e-spec.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import { createTestEnvironment } from '@vendure/testing';
  2. import gql from 'graphql-tag';
  3. import path from 'path';
  4. import { initialData } from '../../../e2e-common/e2e-initial-data';
  5. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  6. import { ZONE_FRAGMENT } from './graphql/fragments';
  7. import {
  8. AddMembersToZone,
  9. CreateZone,
  10. DeleteZone,
  11. DeletionResult,
  12. GetCountryList,
  13. GetZone,
  14. GetZones,
  15. RemoveMembersFromZone,
  16. UpdateChannel,
  17. UpdateZone,
  18. } from './graphql/generated-e2e-admin-types';
  19. import { GET_COUNTRY_LIST, UPDATE_CHANNEL } from './graphql/shared-definitions';
  20. // tslint:disable:no-non-null-assertion
  21. describe('Zone resolver', () => {
  22. const { server, adminClient } = createTestEnvironment(testConfig());
  23. let countries: GetCountryList.Items[];
  24. let zones: Array<{ id: string; name: string }>;
  25. let oceania: { id: string; name: string };
  26. let pangaea: { id: string; name: string; members: any[] };
  27. beforeAll(async () => {
  28. await server.init({
  29. initialData,
  30. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  31. customerCount: 1,
  32. });
  33. await adminClient.asSuperAdmin();
  34. const result = await adminClient.query<GetCountryList.Query>(GET_COUNTRY_LIST, {});
  35. countries = result.countries.items;
  36. }, TEST_SETUP_TIMEOUT_MS);
  37. afterAll(async () => {
  38. await server.destroy();
  39. });
  40. it('zones', async () => {
  41. const result = await adminClient.query<GetZones.Query>(GET_ZONE_LIST);
  42. expect(result.zones.length).toBe(5);
  43. zones = result.zones;
  44. oceania = zones[0];
  45. });
  46. it('zone', async () => {
  47. const result = await adminClient.query<GetZone.Query, GetZone.Variables>(GET_ZONE, {
  48. id: oceania.id,
  49. });
  50. expect(result.zone!.name).toBe('Oceania');
  51. });
  52. it('updateZone', async () => {
  53. const result = await adminClient.query<UpdateZone.Mutation, UpdateZone.Variables>(UPDATE_ZONE, {
  54. input: {
  55. id: oceania.id,
  56. name: 'oceania2',
  57. },
  58. });
  59. expect(result.updateZone.name).toBe('oceania2');
  60. });
  61. it('createZone', async () => {
  62. const result = await adminClient.query<CreateZone.Mutation, CreateZone.Variables>(CREATE_ZONE, {
  63. input: {
  64. name: 'Pangaea',
  65. memberIds: [countries[0].id, countries[1].id],
  66. },
  67. });
  68. pangaea = result.createZone;
  69. expect(pangaea.name).toBe('Pangaea');
  70. expect(pangaea.members.map(m => m.name)).toEqual([countries[0].name, countries[1].name]);
  71. });
  72. it('addMembersToZone', async () => {
  73. const result = await adminClient.query<AddMembersToZone.Mutation, AddMembersToZone.Variables>(
  74. ADD_MEMBERS_TO_ZONE,
  75. {
  76. zoneId: oceania.id,
  77. memberIds: [countries[2].id, countries[3].id],
  78. },
  79. );
  80. expect(!!result.addMembersToZone.members.find(m => m.name === countries[2].name)).toBe(true);
  81. expect(!!result.addMembersToZone.members.find(m => m.name === countries[3].name)).toBe(true);
  82. });
  83. it('removeMembersFromZone', async () => {
  84. const result = await adminClient.query<
  85. RemoveMembersFromZone.Mutation,
  86. RemoveMembersFromZone.Variables
  87. >(REMOVE_MEMBERS_FROM_ZONE, {
  88. zoneId: oceania.id,
  89. memberIds: [countries[0].id, countries[2].id],
  90. });
  91. expect(!!result.removeMembersFromZone.members.find(m => m.name === countries[0].name)).toBe(false);
  92. expect(!!result.removeMembersFromZone.members.find(m => m.name === countries[2].name)).toBe(false);
  93. expect(!!result.removeMembersFromZone.members.find(m => m.name === countries[3].name)).toBe(true);
  94. });
  95. describe('deletion', () => {
  96. it('deletes Zone not used in any TaxRate', async () => {
  97. const result1 = await adminClient.query<DeleteZone.Mutation, DeleteZone.Variables>(DELETE_ZONE, {
  98. id: pangaea.id,
  99. });
  100. expect(result1.deleteZone).toEqual({
  101. result: DeletionResult.DELETED,
  102. message: '',
  103. });
  104. const result2 = await adminClient.query<GetZones.Query>(GET_ZONE_LIST);
  105. expect(result2.zones.find(c => c.id === pangaea.id)).toBeUndefined();
  106. });
  107. it('does not delete Zone that is used in one or more TaxRates', async () => {
  108. const result1 = await adminClient.query<DeleteZone.Mutation, DeleteZone.Variables>(DELETE_ZONE, {
  109. id: oceania.id,
  110. });
  111. expect(result1.deleteZone).toEqual({
  112. result: DeletionResult.NOT_DELETED,
  113. message:
  114. 'The selected Zone cannot be deleted as it is used in the following ' +
  115. 'TaxRates: Standard Tax Oceania, Reduced Tax Oceania, Zero Tax Oceania',
  116. });
  117. const result2 = await adminClient.query<GetZones.Query>(GET_ZONE_LIST);
  118. expect(result2.zones.find(c => c.id === oceania.id)).not.toBeUndefined();
  119. });
  120. it('does not delete Zone that is used as a Channel defaultTaxZone', async () => {
  121. await adminClient.query<UpdateChannel.Mutation, UpdateChannel.Variables>(UPDATE_CHANNEL, {
  122. input: {
  123. id: 'T_1',
  124. defaultTaxZoneId: oceania.id,
  125. },
  126. });
  127. const result1 = await adminClient.query<DeleteZone.Mutation, DeleteZone.Variables>(DELETE_ZONE, {
  128. id: oceania.id,
  129. });
  130. expect(result1.deleteZone).toEqual({
  131. result: DeletionResult.NOT_DELETED,
  132. message:
  133. 'The selected Zone cannot be deleted as it used as a default in the following Channels: ' +
  134. '__default_channel__',
  135. });
  136. const result2 = await adminClient.query<GetZones.Query>(GET_ZONE_LIST);
  137. expect(result2.zones.find(c => c.id === oceania.id)).not.toBeUndefined();
  138. });
  139. it('does not delete Zone that is used as a Channel defaultShippingZone', async () => {
  140. await adminClient.query<UpdateChannel.Mutation, UpdateChannel.Variables>(UPDATE_CHANNEL, {
  141. input: {
  142. id: 'T_1',
  143. defaultTaxZoneId: 'T_1',
  144. defaultShippingZoneId: oceania.id,
  145. },
  146. });
  147. const result1 = await adminClient.query<DeleteZone.Mutation, DeleteZone.Variables>(DELETE_ZONE, {
  148. id: oceania.id,
  149. });
  150. expect(result1.deleteZone).toEqual({
  151. result: DeletionResult.NOT_DELETED,
  152. message:
  153. 'The selected Zone cannot be deleted as it used as a default in the following Channels: ' +
  154. '__default_channel__',
  155. });
  156. const result2 = await adminClient.query<GetZones.Query>(GET_ZONE_LIST);
  157. expect(result2.zones.find(c => c.id === oceania.id)).not.toBeUndefined();
  158. });
  159. });
  160. });
  161. const DELETE_ZONE = gql`
  162. mutation DeleteZone($id: ID!) {
  163. deleteZone(id: $id) {
  164. result
  165. message
  166. }
  167. }
  168. `;
  169. const GET_ZONE_LIST = gql`
  170. query GetZones {
  171. zones {
  172. id
  173. name
  174. }
  175. }
  176. `;
  177. export const GET_ZONE = gql`
  178. query GetZone($id: ID!) {
  179. zone(id: $id) {
  180. ...Zone
  181. }
  182. }
  183. ${ZONE_FRAGMENT}
  184. `;
  185. export const CREATE_ZONE = gql`
  186. mutation CreateZone($input: CreateZoneInput!) {
  187. createZone(input: $input) {
  188. ...Zone
  189. }
  190. }
  191. ${ZONE_FRAGMENT}
  192. `;
  193. export const UPDATE_ZONE = gql`
  194. mutation UpdateZone($input: UpdateZoneInput!) {
  195. updateZone(input: $input) {
  196. ...Zone
  197. }
  198. }
  199. ${ZONE_FRAGMENT}
  200. `;
  201. export const ADD_MEMBERS_TO_ZONE = gql`
  202. mutation AddMembersToZone($zoneId: ID!, $memberIds: [ID!]!) {
  203. addMembersToZone(zoneId: $zoneId, memberIds: $memberIds) {
  204. ...Zone
  205. }
  206. }
  207. ${ZONE_FRAGMENT}
  208. `;
  209. export const REMOVE_MEMBERS_FROM_ZONE = gql`
  210. mutation RemoveMembersFromZone($zoneId: ID!, $memberIds: [ID!]!) {
  211. removeMembersFromZone(zoneId: $zoneId, memberIds: $memberIds) {
  212. ...Zone
  213. }
  214. }
  215. ${ZONE_FRAGMENT}
  216. `;