country.e2e-spec.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import gql from 'graphql-tag';
  2. import path from 'path';
  3. import { TEST_SETUP_TIMEOUT_MS } from './config/test-config';
  4. import { COUNTRY_FRAGMENT } from './graphql/fragments';
  5. import {
  6. CreateCountry,
  7. DeleteCountry,
  8. DeletionResult,
  9. GetCountry,
  10. GetCountryList,
  11. LanguageCode,
  12. UpdateCountry,
  13. } from './graphql/generated-e2e-admin-types';
  14. import { GET_COUNTRY_LIST, UPDATE_COUNTRY } from './graphql/shared-definitions';
  15. import { TestAdminClient } from './test-client';
  16. import { TestServer } from './test-server';
  17. // tslint:disable:no-non-null-assertion
  18. describe('Facet resolver', () => {
  19. const client = new TestAdminClient();
  20. const server = new TestServer();
  21. let countries: GetCountryList.Items[];
  22. let GB: GetCountryList.Items;
  23. let AT: GetCountryList.Items;
  24. beforeAll(async () => {
  25. await server.init({
  26. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  27. customerCount: 1,
  28. });
  29. await client.init();
  30. }, TEST_SETUP_TIMEOUT_MS);
  31. afterAll(async () => {
  32. await server.destroy();
  33. });
  34. it('countries', async () => {
  35. const result = await client.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 client.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 client.query<UpdateCountry.Mutation, UpdateCountry.Variables>(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 client.query<CreateCountry.Mutation, CreateCountry.Variables>(CREATE_COUNTRY, {
  58. input: {
  59. code: 'GL',
  60. enabled: true,
  61. translations: [{ languageCode: LanguageCode.en, name: 'Gondwanaland' }],
  62. },
  63. });
  64. expect(result.createCountry.name).toBe('Gondwanaland');
  65. });
  66. describe('deletion', () => {
  67. it('deletes Country not used in any address', async () => {
  68. const result1 = await client.query<DeleteCountry.Mutation, DeleteCountry.Variables>(DELETE_COUNTRY, { id: AT.id });
  69. expect(result1.deleteCountry).toEqual({
  70. result: DeletionResult.DELETED,
  71. message: '',
  72. });
  73. const result2 = await client.query<GetCountryList.Query>(GET_COUNTRY_LIST, {});
  74. expect(result2.countries.items.find(c => c.id === AT.id)).toBeUndefined();
  75. });
  76. it('does not delete Country that is used in one or more addresses', async () => {
  77. const result1 = await client.query<DeleteCountry.Mutation, DeleteCountry.Variables>(DELETE_COUNTRY, { id: GB.id });
  78. expect(result1.deleteCountry).toEqual({
  79. result: DeletionResult.NOT_DELETED,
  80. message: 'The selected Country cannot be deleted as it is used in 1 Address',
  81. });
  82. const result2 = await client.query<GetCountryList.Query>(GET_COUNTRY_LIST, {});
  83. expect(result2.countries.items.find(c => c.id === GB.id)).not.toBeUndefined();
  84. });
  85. });
  86. });
  87. export const DELETE_COUNTRY = gql`
  88. mutation DeleteCountry($id: ID!) {
  89. deleteCountry(id: $id) {
  90. result
  91. message
  92. }
  93. }
  94. `;
  95. export const GET_COUNTRY = gql`
  96. query GetCountry($id: ID!) {
  97. country(id: $id) {
  98. ...Country
  99. }
  100. }
  101. ${COUNTRY_FRAGMENT}
  102. `;
  103. export const CREATE_COUNTRY = gql`
  104. mutation CreateCountry($input: CreateCountryInput!) {
  105. createCountry(input: $input) {
  106. ...Country
  107. }
  108. }
  109. ${COUNTRY_FRAGMENT}
  110. `;