global-settings.e2e-spec.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { createErrorResultGuard, createTestEnvironment, ErrorResultGuard } 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 { GLOBAL_SETTINGS_FRAGMENT } from './graphql/fragments';
  8. import {
  9. GetGlobalSettingsQuery,
  10. GlobalSettingsFragment,
  11. LanguageCode,
  12. UpdateGlobalSettingsMutation,
  13. UpdateGlobalSettingsMutationVariables,
  14. } from './graphql/generated-e2e-admin-types';
  15. import { UPDATE_GLOBAL_SETTINGS } from './graphql/shared-definitions';
  16. describe('GlobalSettings resolver', () => {
  17. const { server, adminClient } = createTestEnvironment({
  18. ...testConfig(),
  19. ...{
  20. customFields: {
  21. Customer: [{ name: 'age', type: 'int' }],
  22. },
  23. },
  24. });
  25. let globalSettings: GlobalSettingsFragment;
  26. const globalSettingsGuard: ErrorResultGuard<GlobalSettingsFragment> = createErrorResultGuard(
  27. input => !!input.availableLanguages,
  28. );
  29. beforeAll(async () => {
  30. await server.init({
  31. initialData,
  32. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  33. customerCount: 1,
  34. });
  35. await adminClient.asSuperAdmin();
  36. await adminClient.query<UpdateGlobalSettingsMutation, UpdateGlobalSettingsMutationVariables>(
  37. UPDATE_GLOBAL_SETTINGS,
  38. {
  39. input: {
  40. trackInventory: false,
  41. },
  42. },
  43. );
  44. const result = await adminClient.query<GetGlobalSettingsQuery>(GET_GLOBAL_SETTINGS);
  45. globalSettings = result.globalSettings;
  46. }, TEST_SETUP_TIMEOUT_MS);
  47. afterAll(async () => {
  48. await server.destroy();
  49. });
  50. describe('globalSettings query', () => {
  51. it('includes basic settings', () => {
  52. expect(globalSettings.availableLanguages).toEqual([LanguageCode.en]);
  53. expect(globalSettings.trackInventory).toBe(false);
  54. });
  55. it('includes orderProcess', () => {
  56. expect(globalSettings.serverConfig.orderProcess[0]).toEqual({
  57. name: 'Created',
  58. to: ['AddingItems', 'Draft'],
  59. });
  60. });
  61. it('includes permittedAssetTypes', () => {
  62. expect(globalSettings.serverConfig.permittedAssetTypes).toEqual([
  63. 'image/*',
  64. 'video/*',
  65. 'audio/*',
  66. '.pdf',
  67. ]);
  68. });
  69. it('includes customFieldConfig', () => {
  70. expect(globalSettings.serverConfig.customFieldConfig.Customer).toEqual([{ name: 'age' }]);
  71. });
  72. it('includes non-internal permission definitions', () => {
  73. const permissionNames = globalSettings.serverConfig.permissions.map(p => p.name);
  74. expect(permissionNames).toContain('CreateAdministrator');
  75. expect(permissionNames).not.toContain('SuperAdmin');
  76. expect(permissionNames).not.toContain('Owner');
  77. expect(permissionNames).not.toContain('Authenticated');
  78. });
  79. });
  80. describe('update', () => {
  81. it('returns error result when removing required language', async () => {
  82. const { updateGlobalSettings } = await adminClient.query<
  83. UpdateGlobalSettingsMutation,
  84. UpdateGlobalSettingsMutationVariables
  85. >(UPDATE_GLOBAL_SETTINGS, {
  86. input: {
  87. availableLanguages: [LanguageCode.zh],
  88. },
  89. });
  90. globalSettingsGuard.assertErrorResult(updateGlobalSettings);
  91. expect(updateGlobalSettings.message).toBe(
  92. 'Cannot make language "en" unavailable as it is used as the defaultLanguage by the channel "__default_channel__"',
  93. );
  94. });
  95. it('successful update', async () => {
  96. const { updateGlobalSettings } = await adminClient.query<
  97. UpdateGlobalSettingsMutation,
  98. UpdateGlobalSettingsMutationVariables
  99. >(UPDATE_GLOBAL_SETTINGS, {
  100. input: {
  101. availableLanguages: [LanguageCode.en, LanguageCode.zh],
  102. trackInventory: true,
  103. },
  104. });
  105. globalSettingsGuard.assertSuccess(updateGlobalSettings);
  106. expect(updateGlobalSettings.availableLanguages).toEqual([LanguageCode.en, LanguageCode.zh]);
  107. expect(updateGlobalSettings.trackInventory).toBe(true);
  108. });
  109. });
  110. });
  111. const GET_GLOBAL_SETTINGS = gql`
  112. query GetGlobalSettings {
  113. globalSettings {
  114. ...GlobalSettings
  115. }
  116. }
  117. ${GLOBAL_SETTINGS_FRAGMENT}
  118. `;