country.e2e-spec.ts 4.4 KB

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