country.e2e-spec.ts 4.4 KB

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