zone.e2e-spec.ts 9.7 KB

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