Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(editor): Increase test coverage for users settings page and modal #10623

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/editor-ui/src/__tests__/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import type { ISettingsState } from '@/Interface';
import { UserManagementAuthenticationMethod } from '@/Interface';
import { defaultSettings } from './defaults';
import { APP_MODALS_ELEMENT_ID } from '@/constants';
import type { Mock } from 'vitest';
import type { Store, StoreDefinition } from 'pinia';
import type { ComputedRef } from 'vue';

/**
* Retries the given assertion until it passes or the timeout is reached
Expand Down Expand Up @@ -108,3 +111,28 @@ export const createAppModals = () => {
export const cleanupAppModals = () => {
document.body.innerHTML = '';
};

/**
* Typescript helper for mocking pinia store actions return value
*
* @see https://pinia.vuejs.org/cookbook/testing.html#Mocking-the-returned-value-of-an-action
*/
export const mockedStore = <TStoreDef extends () => unknown>(
r00gm marked this conversation as resolved.
Show resolved Hide resolved
useStore: TStoreDef,
): TStoreDef extends StoreDefinition<infer Id, infer State, infer Getters, infer Actions>
? Store<
Id,
State,
Record<string, never>,
{
[K in keyof Actions]: Actions[K] extends (...args: infer Args) => infer ReturnT
? Mock<Args, ReturnT>
: Actions[K];
}
> & {
[K in keyof Getters]: Getters[K] extends ComputedRef<infer T> ? T : never;
}
: ReturnType<TStoreDef> => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return useStore() as any;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can omit type casting here to any, or if you want to do it, it could be TStoreDef

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this method is copy pasted from the documentation, but anyway i tried and it needs to be any.

the important part is that when used it correctly infers the store types

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought just to avoid using eslint disable comment because we're not using any

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it is likely doesn't matter since the return type of the function already defined as ReturnType<TStoreDef>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i agree but it doesn't work :(

};
145 changes: 145 additions & 0 deletions packages/editor-ui/src/components/DeleteUserModal.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import { createComponentRenderer } from '@/__tests__/render';
import DeleteUserModal from './DeleteUserModal.vue';
import { createTestingPinia } from '@pinia/testing';
import { getDropdownItems } from '@/__tests__/utils';
import { createProjectListItem } from '@/__tests__/data/projects';
import { createUser } from '@/__tests__/data/users';

import { DELETE_USER_MODAL_KEY } from '@/constants';
import { ProjectTypes } from '@/types/projects.types';
import userEvent from '@testing-library/user-event';
import { useUsersStore } from '@/stores/users.store';
import { STORES } from '@/constants';

const ModalStub = {
template: `
<div>
<slot name="header" />
<slot name="title" />
<slot name="content" />
<slot name="footer" />
</div>
`,
};

const loggedInUser = createUser();
const invitedUser = createUser({ firstName: undefined });
const user = createUser();

const initialState = {
[STORES.UI]: {
modalsById: {
[DELETE_USER_MODAL_KEY]: {
open: true,
},
},
modalStack: [DELETE_USER_MODAL_KEY],
},
[STORES.PROJECTS]: {
projects: [
ProjectTypes.Personal,
ProjectTypes.Personal,
ProjectTypes.Team,
ProjectTypes.Team,
].map(createProjectListItem),
},
[STORES.USERS]: {
usersById: {
[loggedInUser.id]: loggedInUser,
[user.id]: user,
[invitedUser.id]: invitedUser,
},
},
};

const global = {
stubs: {
Modal: ModalStub,
},
};

const renderModal = createComponentRenderer(DeleteUserModal);
let pinia: ReturnType<typeof createTestingPinia>;

describe('DeleteUserModal', () => {
beforeEach(() => {
pinia = createTestingPinia({ initialState });
});

it('should delete invited users', async () => {
const { getByTestId } = renderModal({
props: {
activeId: invitedUser.id,
},
global,
pinia,
});

const userStore = useUsersStore();

await userEvent.click(getByTestId('confirm-delete-user-button'));

expect(userStore.deleteUser).toHaveBeenCalledWith({ id: invitedUser.id });
});

it('should delete user and transfer workflows and credentials', async () => {
const { getByTestId, getAllByRole } = renderModal({
props: {
activeId: user.id,
},
global,
pinia,
});

const confirmButton = getByTestId('confirm-delete-user-button');
expect(confirmButton).toBeDisabled();

await userEvent.click(getAllByRole('radio')[0]);

const projectSelect = getByTestId('project-sharing-select');
expect(projectSelect).toBeVisible();

const projectSelectDropdownItems = await getDropdownItems(projectSelect);
await userEvent.click(projectSelectDropdownItems[0]);

const userStore = useUsersStore();

expect(confirmButton).toBeEnabled();
await userEvent.click(confirmButton);

expect(userStore.deleteUser).toHaveBeenCalledWith({
id: user.id,
transferId: expect.any(String),
});
});

it('should delete user without transfer', async () => {
const { getByTestId, getAllByRole, getByRole } = renderModal({
props: {
activeId: user.id,
},
global,
pinia,
});

const userStore = useUsersStore();

const confirmButton = getByTestId('confirm-delete-user-button');
expect(confirmButton).toBeDisabled();

await userEvent.click(getAllByRole('radio')[1]);

const input = getByRole('textbox');

await userEvent.type(input, 'delete all ');
expect(confirmButton).toBeDisabled();

await userEvent.type(input, 'data');
expect(confirmButton).toBeEnabled();

await userEvent.click(confirmButton);
expect(userStore.deleteUser).toHaveBeenCalledWith({
id: user.id,
});
});
});
1 change: 1 addition & 0 deletions packages/editor-ui/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ export const enum STORES {
PUSH = 'push',
ASSISTANT = 'assistant',
BECOME_TEMPLATE_CREATOR = 'becomeTemplateCreator',
PROJECTS = 'projects',
}

export const enum SignInType {
Expand Down
3 changes: 2 additions & 1 deletion packages/editor-ui/src/stores/projects.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import { hasPermission } from '@/utils/rbac/permissions';
import type { IWorkflowDb } from '@/Interface';
import { useWorkflowsStore } from '@/stores/workflows.store';
import { useCredentialsStore } from '@/stores/credentials.store';
import { STORES } from '@/constants';

export const useProjectsStore = defineStore('projects', () => {
export const useProjectsStore = defineStore(STORES.PROJECTS, () => {
const route = useRoute();
const rootStore = useRootStore();
const settingsStore = useSettingsStore();
Expand Down
Loading
Loading