country.e2e-spec.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 { COUNTRY_FRAGMENT } from './graphql/fragments';
  8. import { DeletionResult, GetCountryListQuery, LanguageCode } from './graphql/generated-e2e-admin-types';
  9. import * as Codegen from './graphql/generated-e2e-admin-types';
  10. import { GET_COUNTRY_LIST, UPDATE_COUNTRY } from './graphql/shared-definitions';
  11. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  12. describe('Country resolver', () => {
  13. const { server, adminClient } = createTestEnvironment(testConfig());
  14. let countries: GetCountryList.Items[];
  15. let GB: GetCountryListQuery['countries']['items'][number];
  16. let AT: GetCountryListQuery['countries']['items'][number];
  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. }, TEST_SETUP_TIMEOUT_MS);
  25. afterAll(async () => {
  26. await server.destroy();
  27. });
  28. it('countries', async () => {
  29. const result = await adminClient.query<Codegen.GetCountryListQuery>(GET_COUNTRY_LIST, {});
  30. expect(result.countries.totalItems).toBe(7);
  31. countries = result.countries.items;
  32. GB = countries.find(c => c.code === 'GB')!;
  33. AT = countries.find(c => c.code === 'AT')!;
  34. });
  35. it('country', async () => {
  36. const result = await adminClient.query<Codegen.GetCountryQuery, Codegen.GetCountryQueryVariables>(
  37. GET_COUNTRY,
  38. {
  39. id: GB.id,
  40. },
  41. );
  42. expect(result.country!.name).toBe(GB.name);
  43. });
  44. it('updateCountry', async () => {
  45. const result = await adminClient.query<
  46. Codegen.UpdateCountryMutation,
  47. Codegen.UpdateCountryMutationVariables
  48. >(UPDATE_COUNTRY, {
  49. input: {
  50. id: AT.id,
  51. enabled: false,
  52. },
  53. });
  54. expect(result.updateCountry.enabled).toBe(false);
  55. });
  56. it('createCountry', async () => {
  57. const result = await adminClient.query<
  58. Codegen.CreateCountryMutation,
  59. Codegen.CreateCountryMutationVariables
  60. >(CREATE_COUNTRY, {
  61. input: {
  62. code: 'GL',
  63. enabled: true,
  64. translations: [{ languageCode: LanguageCode.en, name: 'Gondwanaland' }],
  65. },
  66. });
  67. expect(result.createCountry.name).toBe('Gondwanaland');
  68. });
  69. describe('deletion', () => {
  70. it('deletes Country not used in any address', async () => {
  71. const result1 = await adminClient.query<
  72. Codegen.DeleteCountryMutation,
  73. Codegen.DeleteCountryMutationVariables
  74. >(DELETE_COUNTRY, { id: AT.id });
  75. expect(result1.deleteCountry).toEqual({
  76. result: DeletionResult.DELETED,
  77. message: '',
  78. });
  79. const result2 = await adminClient.query<Codegen.GetCountryListQuery>(GET_COUNTRY_LIST, {});
  80. expect(result2.countries.items.find(c => c.id === AT.id)).toBeUndefined();
  81. });
  82. it('does not delete Country that is used in one or more addresses', async () => {
  83. const result1 = await adminClient.query<
  84. Codegen.DeleteCountryMutation,
  85. Codegen.DeleteCountryMutationVariables
  86. >(DELETE_COUNTRY, { id: GB.id });
  87. expect(result1.deleteCountry).toEqual({
  88. result: DeletionResult.NOT_DELETED,
  89. message: 'The selected Country cannot be deleted as it is used in 1 Address',
  90. });
  91. const result2 = await adminClient.query<Codegen.GetCountryListQuery>(GET_COUNTRY_LIST, {});
  92. expect(result2.countries.items.find(c => c.id === GB.id)).not.toBeUndefined();
  93. });
  94. });
  95. });
  96. export const DELETE_COUNTRY = gql`
  97. mutation DeleteCountry($id: ID!) {
  98. deleteCountry(id: $id) {
  99. result
  100. message
  101. }
  102. }
  103. `;
  104. export const GET_COUNTRY = gql`
  105. query GetCountry($id: ID!) {
  106. country(id: $id) {
  107. ...Country
  108. }
  109. }
  110. ${COUNTRY_FRAGMENT}
  111. `;
  112. export const CREATE_COUNTRY = gql`
  113. mutation CreateCountry($input: CreateCountryInput!) {
  114. createCountry(input: $input) {
  115. ...Country
  116. }
  117. }
  118. ${COUNTRY_FRAGMENT}
  119. `;