| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- import { Facet, LanguageCode, mergeConfig } from '@vendure/core';
- import { createTestEnvironment } from '@vendure/testing';
- import gql from 'graphql-tag';
- import path from 'path';
- import { afterAll, beforeAll, describe, expect, it } from 'vitest';
- import { initialData } from '../../../e2e-common/e2e-initial-data';
- import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
- import { ZONE_FRAGMENT } from './graphql/fragments';
- import * as Codegen from './graphql/generated-e2e-admin-types';
- import { DeletionResult } from './graphql/generated-e2e-admin-types';
- import { GET_COUNTRY_LIST, UPDATE_CHANNEL } from './graphql/shared-definitions';
- /* eslint-disable @typescript-eslint/no-non-null-assertion */
- describe('Zone resolver', () => {
- const { server, adminClient } = createTestEnvironment(
- mergeConfig(testConfig(), {
- customFields: {
- Zone: [
- {
- name: 'relatedFacet',
- type: 'relation',
- entity: Facet,
- },
- ],
- },
- }),
- );
- let countries: Codegen.GetCountryListQuery['countries']['items'];
- let zones: Array<{ id: string; name: string }>;
- let oceania: { id: string; name: string };
- let pangaea: { id: string; name: string; members: any[] };
- beforeAll(async () => {
- await server.init({
- initialData,
- productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
- customerCount: 1,
- });
- await adminClient.asSuperAdmin();
- const result = await adminClient.query<Codegen.GetCountryListQuery>(GET_COUNTRY_LIST, {});
- countries = result.countries.items;
- }, TEST_SETUP_TIMEOUT_MS);
- afterAll(async () => {
- await server.destroy();
- });
- it('zones', async () => {
- const result = await adminClient.query<Codegen.GetZonesQuery>(GET_ZONE_LIST);
- expect(result.zones.items.length).toBe(5);
- zones = result.zones.items;
- oceania = zones[0];
- });
- it('zone', async () => {
- const result = await adminClient.query<Codegen.GetZoneQuery, Codegen.GetZoneQueryVariables>(
- GET_ZONE,
- {
- id: oceania.id,
- },
- );
- expect(result.zone!.name).toBe('Oceania');
- });
- it('zone.members field resolver', async () => {
- const { activeChannel } = await adminClient.query<Codegen.GetActiveChannelWithZoneMembersQuery>(
- GET_ACTIVE_CHANNEL_WITH_ZONE_MEMBERS,
- );
- expect(activeChannel.defaultShippingZone?.members.length).toBe(2);
- });
- it('updateZone', async () => {
- const result = await adminClient.query<
- Codegen.UpdateZoneMutation,
- Codegen.UpdateZoneMutationVariables
- >(UPDATE_ZONE, {
- input: {
- id: oceania.id,
- name: 'oceania2',
- },
- });
- expect(result.updateZone.name).toBe('oceania2');
- });
- it('createZone', async () => {
- const result = await adminClient.query<
- Codegen.CreateZoneMutation,
- Codegen.CreateZoneMutationVariables
- >(CREATE_ZONE, {
- input: {
- name: 'Pangaea',
- memberIds: [countries[0].id, countries[1].id],
- },
- });
- pangaea = result.createZone;
- expect(pangaea.name).toBe('Pangaea');
- expect(pangaea.members.map(m => m.name)).toEqual([countries[0].name, countries[1].name]);
- });
- it('addMembersToZone', async () => {
- const result = await adminClient.query<
- Codegen.AddMembersToZoneMutation,
- Codegen.AddMembersToZoneMutationVariables
- >(ADD_MEMBERS_TO_ZONE, {
- zoneId: oceania.id,
- memberIds: [countries[2].id, countries[3].id],
- });
- expect(!!result.addMembersToZone.members.find(m => m.name === countries[2].name)).toBe(true);
- expect(!!result.addMembersToZone.members.find(m => m.name === countries[3].name)).toBe(true);
- });
- it('removeMembersFromZone', async () => {
- const result = await adminClient.query<
- Codegen.RemoveMembersFromZoneMutation,
- Codegen.RemoveMembersFromZoneMutationVariables
- >(REMOVE_MEMBERS_FROM_ZONE, {
- zoneId: oceania.id,
- memberIds: [countries[0].id, countries[2].id],
- });
- expect(!!result.removeMembersFromZone.members.find(m => m.name === countries[0].name)).toBe(false);
- expect(!!result.removeMembersFromZone.members.find(m => m.name === countries[2].name)).toBe(false);
- expect(!!result.removeMembersFromZone.members.find(m => m.name === countries[3].name)).toBe(true);
- });
- describe('deletion', () => {
- it('deletes Zone not used in any TaxRate', async () => {
- const result1 = await adminClient.query<
- Codegen.DeleteZoneMutation,
- Codegen.DeleteZoneMutationVariables
- >(DELETE_ZONE, {
- id: pangaea.id,
- });
- expect(result1.deleteZone).toEqual({
- result: DeletionResult.DELETED,
- message: '',
- });
- const result2 = await adminClient.query<Codegen.GetZonesQuery>(GET_ZONE_LIST);
- expect(result2.zones.items.find(c => c.id === pangaea.id)).toBeUndefined();
- });
- it('does not delete Zone that is used in one or more TaxRates', async () => {
- const result1 = await adminClient.query<
- Codegen.DeleteZoneMutation,
- Codegen.DeleteZoneMutationVariables
- >(DELETE_ZONE, {
- id: oceania.id,
- });
- expect(result1.deleteZone).toEqual({
- result: DeletionResult.NOT_DELETED,
- message:
- 'The selected Zone cannot be deleted as it is used in the following ' +
- 'TaxRates: Standard Tax Oceania, Reduced Tax Oceania, Zero Tax Oceania',
- });
- const result2 = await adminClient.query<Codegen.GetZonesQuery>(GET_ZONE_LIST);
- expect(result2.zones.items.find(c => c.id === oceania.id)).not.toBeUndefined();
- });
- it('does not delete Zone that is used as a Channel defaultTaxZone', async () => {
- await adminClient.query<Codegen.UpdateChannelMutation, Codegen.UpdateChannelMutationVariables>(
- UPDATE_CHANNEL,
- {
- input: {
- id: 'T_1',
- defaultTaxZoneId: oceania.id,
- },
- },
- );
- const result1 = await adminClient.query<
- Codegen.DeleteZoneMutation,
- Codegen.DeleteZoneMutationVariables
- >(DELETE_ZONE, {
- id: oceania.id,
- });
- expect(result1.deleteZone).toEqual({
- result: DeletionResult.NOT_DELETED,
- message:
- 'The selected Zone cannot be deleted as it used as a default in the following Channels: ' +
- '__default_channel__',
- });
- const result2 = await adminClient.query<Codegen.GetZonesQuery>(GET_ZONE_LIST);
- expect(result2.zones.items.find(c => c.id === oceania.id)).not.toBeUndefined();
- });
- it('does not delete Zone that is used as a Channel defaultShippingZone', async () => {
- await adminClient.query<Codegen.UpdateChannelMutation, Codegen.UpdateChannelMutationVariables>(
- UPDATE_CHANNEL,
- {
- input: {
- id: 'T_1',
- defaultTaxZoneId: 'T_1',
- defaultShippingZoneId: oceania.id,
- },
- },
- );
- const result1 = await adminClient.query<
- Codegen.DeleteZoneMutation,
- Codegen.DeleteZoneMutationVariables
- >(DELETE_ZONE, {
- id: oceania.id,
- });
- expect(result1.deleteZone).toEqual({
- result: DeletionResult.NOT_DELETED,
- message:
- 'The selected Zone cannot be deleted as it used as a default in the following Channels: ' +
- '__default_channel__',
- });
- const result2 = await adminClient.query<Codegen.GetZonesQuery>(GET_ZONE_LIST);
- expect(result2.zones.items.find(c => c.id === oceania.id)).not.toBeUndefined();
- });
- });
- describe('Zone custom fields', () => {
- let testFacet: Codegen.CreateFacetMutation['createFacet'];
- // Create a target entity (Facet) to link the Zone to
- it('create a target Facet', async () => {
- const result = await adminClient.query<
- Codegen.CreateFacetMutation,
- Codegen.CreateFacetMutationVariables
- >(CREATE_FACET_WITH_VALUE, {
- input: {
- code: 'test-relation-facet',
- isPrivate: false,
- translations: [{ languageCode: LanguageCode.en, name: 'Test Relation Facet' }],
- },
- });
- testFacet = result.createFacet;
- expect(testFacet.name).toBe('Test Relation Facet');
- });
- // Test createZone with a custom relation field
- it('createZone persists custom relation field', async () => {
- const input: Codegen.CreateZoneInput = {
- name: 'Zone with Custom Relation',
- memberIds: [],
- customFields: {
- relatedFacetId: testFacet.id,
- },
- };
- const result = await adminClient.query<
- CreateZoneMutationWithCF,
- Codegen.CreateZoneMutationVariables
- >(gql(CREATE_ZONE_WITH_CF), { input });
- // Verify the return value
- expect(result.createZone.customFields.relatedFacet.id).toBe(testFacet.id);
- // Verify by querying it again from the database
- const result2 = await adminClient.query<GetZoneQueryWithCF, Codegen.GetZoneQueryVariables>(
- gql(GET_ZONE_WITH_CUSTOM_FIELDS),
- { id: result.createZone.id },
- );
- expect(result2.zone.customFields.relatedFacet.id).toBe(testFacet.id);
- });
- // Test updateZone with a custom relation field
- it('updateZone persists custom relation field', async () => {
- const result = await adminClient.query<
- UpdateZoneMutationWithCF,
- Codegen.UpdateZoneMutationVariables
- >(gql(UPDATE_ZONE_WITH_CF), {
- input: {
- id: zones[1].id,
- customFields: {
- relatedFacetId: testFacet.id,
- },
- },
- });
- // Verify the return value
- expect(result.updateZone.customFields.relatedFacet.id).toBe(testFacet.id);
- // Verify by querying it again from the database
- const result2 = await adminClient.query<GetZoneQueryWithCF, Codegen.GetZoneQueryVariables>(
- gql(GET_ZONE_WITH_CUSTOM_FIELDS),
- { id: zones[1].id },
- );
- expect(result2.zone.customFields.relatedFacet.id).toBe(testFacet.id);
- });
- });
- });
- type ZoneWithCustomFields = Omit<Codegen.Zone, 'customFields'> & {
- customFields: {
- relatedFacet: {
- id: string;
- };
- };
- };
- type CreateZoneMutationWithCF = Omit<Codegen.CreateZoneMutation, 'createZone'> & {
- createZone: ZoneWithCustomFields;
- };
- type UpdateZoneMutationWithCF = Omit<Codegen.UpdateZoneMutation, 'updateZone'> & {
- updateZone: ZoneWithCustomFields;
- };
- type GetZoneQueryWithCF = Omit<Codegen.GetZoneQuery, 'zone'> & {
- zone: ZoneWithCustomFields;
- };
- const CREATE_FACET_WITH_VALUE = gql`
- mutation CreateFacetWithValue($input: CreateFacetInput!) {
- createFacet(input: $input) {
- id
- name
- }
- }
- `;
- // A new fragment to include the custom fields
- const ZONE_CUSTOM_FIELDS_FRAGMENT = `
- fragment ZoneCustomFields on Zone {
- customFields {
- relatedFacet {
- id
- }
- }
- }
- `;
- // A new mutation to create a Zone with custom fields
- const CREATE_ZONE_WITH_CF = `
- mutation CreateZoneWithCF($input: CreateZoneInput!) {
- createZone(input: $input) {
- id
- name
- ...ZoneCustomFields
- }
- }
- ${ZONE_CUSTOM_FIELDS_FRAGMENT}
- `;
- // A new mutation to update a Zone with custom fields
- const UPDATE_ZONE_WITH_CF = `
- mutation UpdateZoneWithCF($input: UpdateZoneInput!) {
- updateZone(input: $input) {
- id
- name
- ...ZoneCustomFields
- }
- }
- ${ZONE_CUSTOM_FIELDS_FRAGMENT}
- `;
- // A new query to fetch the Zone with its custom fields
- const GET_ZONE_WITH_CUSTOM_FIELDS = `
- query GetZoneWithCustomFields($id: ID!) {
- zone(id: $id) {
- id
- name
- ...ZoneCustomFields
- }
- }
- ${ZONE_CUSTOM_FIELDS_FRAGMENT}
- `;
- const DELETE_ZONE = gql`
- mutation DeleteZone($id: ID!) {
- deleteZone(id: $id) {
- result
- message
- }
- }
- `;
- const GET_ZONE_LIST = gql`
- query GetZones($options: ZoneListOptions) {
- zones(options: $options) {
- items {
- id
- name
- }
- totalItems
- }
- }
- `;
- export const GET_ZONE = gql`
- query GetZone($id: ID!) {
- zone(id: $id) {
- ...Zone
- }
- }
- ${ZONE_FRAGMENT}
- `;
- export const GET_ACTIVE_CHANNEL_WITH_ZONE_MEMBERS = gql`
- query GetActiveChannelWithZoneMembers {
- activeChannel {
- id
- defaultShippingZone {
- id
- members {
- name
- }
- }
- }
- }
- `;
- export const CREATE_ZONE = gql`
- mutation CreateZone($input: CreateZoneInput!) {
- createZone(input: $input) {
- ...Zone
- }
- }
- ${ZONE_FRAGMENT}
- `;
- export const UPDATE_ZONE = gql`
- mutation UpdateZone($input: UpdateZoneInput!) {
- updateZone(input: $input) {
- ...Zone
- }
- }
- ${ZONE_FRAGMENT}
- `;
- export const ADD_MEMBERS_TO_ZONE = gql`
- mutation AddMembersToZone($zoneId: ID!, $memberIds: [ID!]!) {
- addMembersToZone(zoneId: $zoneId, memberIds: $memberIds) {
- ...Zone
- }
- }
- ${ZONE_FRAGMENT}
- `;
- export const REMOVE_MEMBERS_FROM_ZONE = gql`
- mutation RemoveMembersFromZone($zoneId: ID!, $memberIds: [ID!]!) {
- removeMembersFromZone(zoneId: $zoneId, memberIds: $memberIds) {
- ...Zone
- }
- }
- ${ZONE_FRAGMENT}
- `;
|