zone.e2e-spec.ts 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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('zone.members field resolver', async () => {
  46. const { activeChannel } = await adminClient.query<Codegen.GetActiveChannelWithZoneMembersQuery>(
  47. GET_ACTIVE_CHANNEL_WITH_ZONE_MEMBERS,
  48. );
  49. expect(activeChannel.defaultShippingZone?.members.length).toBe(2);
  50. });
  51. it('updateZone', async () => {
  52. const result = await adminClient.query<
  53. Codegen.UpdateZoneMutation,
  54. Codegen.UpdateZoneMutationVariables
  55. >(UPDATE_ZONE, {
  56. input: {
  57. id: oceania.id,
  58. name: 'oceania2',
  59. },
  60. });
  61. expect(result.updateZone.name).toBe('oceania2');
  62. });
  63. it('createZone', async () => {
  64. const result = await adminClient.query<
  65. Codegen.CreateZoneMutation,
  66. Codegen.CreateZoneMutationVariables
  67. >(CREATE_ZONE, {
  68. input: {
  69. name: 'Pangaea',
  70. memberIds: [countries[0].id, countries[1].id],
  71. },
  72. });
  73. pangaea = result.createZone;
  74. expect(pangaea.name).toBe('Pangaea');
  75. expect(pangaea.members.map(m => m.name)).toEqual([countries[0].name, countries[1].name]);
  76. });
  77. it('addMembersToZone', async () => {
  78. const result = await adminClient.query<
  79. Codegen.AddMembersToZoneMutation,
  80. Codegen.AddMembersToZoneMutationVariables
  81. >(ADD_MEMBERS_TO_ZONE, {
  82. zoneId: oceania.id,
  83. memberIds: [countries[2].id, countries[3].id],
  84. });
  85. expect(!!result.addMembersToZone.members.find(m => m.name === countries[2].name)).toBe(true);
  86. expect(!!result.addMembersToZone.members.find(m => m.name === countries[3].name)).toBe(true);
  87. });
  88. it('removeMembersFromZone', async () => {
  89. const result = await adminClient.query<
  90. Codegen.RemoveMembersFromZoneMutation,
  91. Codegen.RemoveMembersFromZoneMutationVariables
  92. >(REMOVE_MEMBERS_FROM_ZONE, {
  93. zoneId: oceania.id,
  94. memberIds: [countries[0].id, countries[2].id],
  95. });
  96. expect(!!result.removeMembersFromZone.members.find(m => m.name === countries[0].name)).toBe(false);
  97. expect(!!result.removeMembersFromZone.members.find(m => m.name === countries[2].name)).toBe(false);
  98. expect(!!result.removeMembersFromZone.members.find(m => m.name === countries[3].name)).toBe(true);
  99. });
  100. describe('deletion', () => {
  101. it('deletes Zone not used in any TaxRate', async () => {
  102. const result1 = await adminClient.query<
  103. Codegen.DeleteZoneMutation,
  104. Codegen.DeleteZoneMutationVariables
  105. >(DELETE_ZONE, {
  106. id: pangaea.id,
  107. });
  108. expect(result1.deleteZone).toEqual({
  109. result: DeletionResult.DELETED,
  110. message: '',
  111. });
  112. const result2 = await adminClient.query<Codegen.GetZonesQuery>(GET_ZONE_LIST);
  113. expect(result2.zones.find(c => c.id === pangaea.id)).toBeUndefined();
  114. });
  115. it('does not delete Zone that is used in one or more TaxRates', async () => {
  116. const result1 = await adminClient.query<
  117. Codegen.DeleteZoneMutation,
  118. Codegen.DeleteZoneMutationVariables
  119. >(DELETE_ZONE, {
  120. id: oceania.id,
  121. });
  122. expect(result1.deleteZone).toEqual({
  123. result: DeletionResult.NOT_DELETED,
  124. message:
  125. 'The selected Zone cannot be deleted as it is used in the following ' +
  126. 'TaxRates: Standard Tax Oceania, Reduced Tax Oceania, Zero Tax Oceania',
  127. });
  128. const result2 = await adminClient.query<Codegen.GetZonesQuery>(GET_ZONE_LIST);
  129. expect(result2.zones.find(c => c.id === oceania.id)).not.toBeUndefined();
  130. });
  131. it('does not delete Zone that is used as a Channel defaultTaxZone', async () => {
  132. await adminClient.query<Codegen.UpdateChannelMutation, Codegen.UpdateChannelMutationVariables>(
  133. UPDATE_CHANNEL,
  134. {
  135. input: {
  136. id: 'T_1',
  137. defaultTaxZoneId: oceania.id,
  138. },
  139. },
  140. );
  141. const result1 = await adminClient.query<
  142. Codegen.DeleteZoneMutation,
  143. Codegen.DeleteZoneMutationVariables
  144. >(DELETE_ZONE, {
  145. id: oceania.id,
  146. });
  147. expect(result1.deleteZone).toEqual({
  148. result: DeletionResult.NOT_DELETED,
  149. message:
  150. 'The selected Zone cannot be deleted as it used as a default in the following Channels: ' +
  151. '__default_channel__',
  152. });
  153. const result2 = await adminClient.query<Codegen.GetZonesQuery>(GET_ZONE_LIST);
  154. expect(result2.zones.find(c => c.id === oceania.id)).not.toBeUndefined();
  155. });
  156. it('does not delete Zone that is used as a Channel defaultShippingZone', async () => {
  157. await adminClient.query<Codegen.UpdateChannelMutation, Codegen.UpdateChannelMutationVariables>(
  158. UPDATE_CHANNEL,
  159. {
  160. input: {
  161. id: 'T_1',
  162. defaultTaxZoneId: 'T_1',
  163. defaultShippingZoneId: oceania.id,
  164. },
  165. },
  166. );
  167. const result1 = await adminClient.query<
  168. Codegen.DeleteZoneMutation,
  169. Codegen.DeleteZoneMutationVariables
  170. >(DELETE_ZONE, {
  171. id: oceania.id,
  172. });
  173. expect(result1.deleteZone).toEqual({
  174. result: DeletionResult.NOT_DELETED,
  175. message:
  176. 'The selected Zone cannot be deleted as it used as a default in the following Channels: ' +
  177. '__default_channel__',
  178. });
  179. const result2 = await adminClient.query<Codegen.GetZonesQuery>(GET_ZONE_LIST);
  180. expect(result2.zones.find(c => c.id === oceania.id)).not.toBeUndefined();
  181. });
  182. });
  183. });
  184. const DELETE_ZONE = gql`
  185. mutation DeleteZone($id: ID!) {
  186. deleteZone(id: $id) {
  187. result
  188. message
  189. }
  190. }
  191. `;
  192. const GET_ZONE_LIST = gql`
  193. query GetZones {
  194. zones {
  195. id
  196. name
  197. }
  198. }
  199. `;
  200. export const GET_ZONE = gql`
  201. query GetZone($id: ID!) {
  202. zone(id: $id) {
  203. ...Zone
  204. }
  205. }
  206. ${ZONE_FRAGMENT}
  207. `;
  208. export const GET_ACTIVE_CHANNEL_WITH_ZONE_MEMBERS = gql`
  209. query GetActiveChannelWithZoneMembers {
  210. activeChannel {
  211. id
  212. defaultShippingZone {
  213. id
  214. members {
  215. name
  216. }
  217. }
  218. }
  219. }
  220. `;
  221. export const CREATE_ZONE = gql`
  222. mutation CreateZone($input: CreateZoneInput!) {
  223. createZone(input: $input) {
  224. ...Zone
  225. }
  226. }
  227. ${ZONE_FRAGMENT}
  228. `;
  229. export const UPDATE_ZONE = gql`
  230. mutation UpdateZone($input: UpdateZoneInput!) {
  231. updateZone(input: $input) {
  232. ...Zone
  233. }
  234. }
  235. ${ZONE_FRAGMENT}
  236. `;
  237. export const ADD_MEMBERS_TO_ZONE = gql`
  238. mutation AddMembersToZone($zoneId: ID!, $memberIds: [ID!]!) {
  239. addMembersToZone(zoneId: $zoneId, memberIds: $memberIds) {
  240. ...Zone
  241. }
  242. }
  243. ${ZONE_FRAGMENT}
  244. `;
  245. export const REMOVE_MEMBERS_FROM_ZONE = gql`
  246. mutation RemoveMembersFromZone($zoneId: ID!, $memberIds: [ID!]!) {
  247. removeMembersFromZone(zoneId: $zoneId, memberIds: $memberIds) {
  248. ...Zone
  249. }
  250. }
  251. ${ZONE_FRAGMENT}
  252. `;