From 1a41dccc1a44980c48a8e4616c303201ebae6e20 Mon Sep 17 00:00:00 2001 From: Shireen Missi Date: Thu, 7 Nov 2024 19:02:29 +0000 Subject: [PATCH] Add tests --- .../__tests__/CredentialEdit.test.ts | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 packages/editor-ui/src/components/CredentialEdit/__tests__/CredentialEdit.test.ts diff --git a/packages/editor-ui/src/components/CredentialEdit/__tests__/CredentialEdit.test.ts b/packages/editor-ui/src/components/CredentialEdit/__tests__/CredentialEdit.test.ts new file mode 100644 index 0000000000..cf260c08d2 --- /dev/null +++ b/packages/editor-ui/src/components/CredentialEdit/__tests__/CredentialEdit.test.ts @@ -0,0 +1,70 @@ +import { createComponentRenderer } from '@/__tests__/render'; +import CredentialEdit from '@/components/CredentialEdit/CredentialEdit.vue'; +import { createTestingPinia } from '@pinia/testing'; +import { CREDENTIAL_EDIT_MODAL_KEY, STORES } from '@/constants'; +import { cleanupAppModals, createAppModals, retry } from '@/__tests__/utils'; + +vi.mock('@/permissions', () => ({ + getResourcePermissions: vi.fn(() => ({ + credential: { + create: true, + update: true, + }, + })), +})); + +const renderComponent = createComponentRenderer(CredentialEdit, { + pinia: createTestingPinia({ + initialState: { + [STORES.UI]: { + modalsById: { + [CREDENTIAL_EDIT_MODAL_KEY]: { open: true }, + }, + }, + [STORES.SETTINGS]: { + settings: { + templates: { + host: '', + }, + }, + }, + }, + }), +}); +describe('CredentialEdit', () => { + beforeEach(() => { + createAppModals(); + }); + + afterEach(() => { + cleanupAppModals(); + vi.clearAllMocks(); + }); + + test('shows the save button when credentialId is null', async () => { + const { queryByTestId } = renderComponent({ + props: { + isTesting: false, + isSaving: false, + hasUnsavedChanges: false, + modalName: CREDENTIAL_EDIT_MODAL_KEY, + mode: 'new', + }, + }); + await retry(() => expect(queryByTestId('credential-save-button')).toBeInTheDocument()); + }); + + test('hides the save button when credentialId exists and there are no unsaved changes', async () => { + const { queryByTestId } = renderComponent({ + props: { + activeId: '123', // credentialId will be set to this value in edit mode + isTesting: false, + isSaving: false, + hasUnsavedChanges: false, + modalName: CREDENTIAL_EDIT_MODAL_KEY, + mode: 'edit', + }, + }); + await retry(() => expect(queryByTestId('credential-save-button')).not.toBeInTheDocument()); + }); +});