From 1ec85b7d57eacc3410e20775ca6b59d82850f347 Mon Sep 17 00:00:00 2001 From: Csaba Tuncsik Date: Thu, 27 Apr 2023 17:26:20 +0200 Subject: [PATCH] test(editor): Test personal settings view (#6119) --- .../__tests__/SettingsPersonalView.test.ts | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 packages/editor-ui/src/views/__tests__/SettingsPersonalView.test.ts diff --git a/packages/editor-ui/src/views/__tests__/SettingsPersonalView.test.ts b/packages/editor-ui/src/views/__tests__/SettingsPersonalView.test.ts new file mode 100644 index 0000000000..c86816a8bd --- /dev/null +++ b/packages/editor-ui/src/views/__tests__/SettingsPersonalView.test.ts @@ -0,0 +1,79 @@ +import { PiniaVuePlugin } from 'pinia'; +import { render } from '@testing-library/vue'; +import { createTestingPinia } from '@pinia/testing'; +import { merge } from 'lodash-es'; +import type { IN8nUISettings } from 'n8n-workflow'; +import { STORES } from '@/constants'; +import { SETTINGS_STORE_DEFAULT_STATE, waitAllPromises } from '@/__tests__/utils'; +import { i18n } from '@/plugins/i18n'; +import SettingsPersonalView from '@/views/SettingsPersonalView.vue'; +import { useSettingsStore } from '@/stores'; +import { useUsersStore } from '@/stores/users'; + +let pinia: ReturnType; +let settingsStore: ReturnType; +let usersStore: ReturnType; + +const DEFAULT_SETTINGS: IN8nUISettings = SETTINGS_STORE_DEFAULT_STATE.settings; + +const renderComponent = (renderOptions: Parameters[1] = {}) => + render( + SettingsPersonalView, + merge( + { + pinia, + i18n, + }, + renderOptions, + ), + (vue) => { + vue.use(PiniaVuePlugin); + }, + ); + +describe('SettingsPersonalView', () => { + beforeEach(() => { + pinia = createTestingPinia({ + initialState: { + [STORES.SETTINGS]: { + settings: DEFAULT_SETTINGS, + }, + }, + }); + settingsStore = useSettingsStore(pinia); + usersStore = useUsersStore(pinia); + + vi.spyOn(usersStore, 'currentUser', 'get').mockReturnValue({ + id: '1', + firstName: 'John', + lastName: 'Doe', + email: 'joh.doe@example.com', + createdAt: Date().toString(), + isOwner: true, + isDefaultUser: false, + isPendingUser: false, + isPending: false, + }); + }); + + it('should enable email and pw change', async () => { + const { getByTestId, getAllByRole } = renderComponent(); + await waitAllPromises(); + + expect(getAllByRole('textbox').find((el) => el.getAttribute('type') === 'email')).toBeEnabled(); + expect(getByTestId('change-password-link')).toBeInTheDocument(); + }); + + it('should disable email and pw change when SAML login is enabled', async () => { + vi.spyOn(settingsStore, 'isSamlLoginEnabled', 'get').mockReturnValue(true); + vi.spyOn(settingsStore, 'isDefaultAuthenticationSaml', 'get').mockReturnValue(true); + + const { queryByTestId, getAllByRole } = renderComponent(); + await waitAllPromises(); + + expect( + getAllByRole('textbox').find((el) => el.getAttribute('type') === 'email'), + ).toBeDisabled(); + expect(queryByTestId('change-password-link')).not.toBeInTheDocument(); + }); +});