country.e2e-spec.ts 4.5 KB

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