zone.e2e-spec.ts 8.9 KB

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