2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2019-01-08 15:14:00 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
import { createPinia, setActivePinia } from 'pinia';
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
import { ProjectMembersApiGql } from '@/api/projectMembers';
|
|
|
|
import { SortDirection } from '@/types/common';
|
|
|
|
import { ProjectMember, ProjectMemberOrderBy, ProjectMembersPage } from '@/types/projectMembers';
|
|
|
|
import { Project } from '@/types/projects';
|
2023-04-07 13:28:16 +01:00
|
|
|
import { useProjectMembersStore } from '@/store/modules/projectMembersStore';
|
2019-08-20 13:57:43 +01:00
|
|
|
|
|
|
|
const selectedProject = new Project();
|
|
|
|
selectedProject.id = '1';
|
|
|
|
|
|
|
|
const FIRST_PAGE = 1;
|
2022-04-07 12:04:24 +01:00
|
|
|
const TEST_ERROR = new Error('testError');
|
2019-08-20 13:57:43 +01:00
|
|
|
const UNREACHABLE_ERROR = 'should be unreachable';
|
|
|
|
|
2020-09-28 14:44:01 +01:00
|
|
|
const date = new Date(0);
|
|
|
|
const projectMember1 = new ProjectMember('testFullName1', 'testShortName1', 'test1@example.com', date, '1');
|
|
|
|
const projectMember2 = new ProjectMember('testFullName2', 'testShortName2', 'test2@example.com', date, '2');
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
describe('actions', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
setActivePinia(createPinia());
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fetch project members', async function () {
|
|
|
|
const store = useProjectMembersStore();
|
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
const testProjectMembersPage = new ProjectMembersPage();
|
|
|
|
testProjectMembersPage.projectMembers = [projectMember1];
|
|
|
|
testProjectMembersPage.totalCount = 1;
|
|
|
|
testProjectMembersPage.pageCount = 1;
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
jest.spyOn(ProjectMembersApiGql.prototype, 'get')
|
|
|
|
.mockImplementation(() => Promise.resolve(testProjectMembersPage));
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
await store.getProjectMembers(FIRST_PAGE, selectedProject.id);
|
|
|
|
|
|
|
|
expect(store.state.page.projectMembers.length).toBe(1);
|
|
|
|
expect(store.state.page.search).toBe('');
|
|
|
|
expect(store.state.page.order).toBe(ProjectMemberOrderBy.NAME);
|
|
|
|
expect(store.state.page.orderDirection).toBe(SortDirection.ASCENDING);
|
|
|
|
expect(store.state.page.limit).toBe(6);
|
|
|
|
expect(store.state.page.pageCount).toBe(1);
|
|
|
|
expect(store.state.page.currentPage).toBe(1);
|
|
|
|
expect(store.state.page.totalCount).toBe(1);
|
2019-08-20 13:57:43 +01:00
|
|
|
});
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
it('set project members page number', function () {
|
|
|
|
const store = useProjectMembersStore();
|
|
|
|
|
|
|
|
store.setPageNumber(2);
|
2019-08-20 13:57:43 +01:00
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
expect(store.state.cursor.page).toBe(2);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
it('set search query', function () {
|
2023-04-07 13:28:16 +01:00
|
|
|
const store = useProjectMembersStore();
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
store.setSearchQuery('testSearchQuery');
|
|
|
|
|
|
|
|
expect(store.state.cursor.search).toBe('testSearchQuery');
|
2019-08-20 13:57:43 +01:00
|
|
|
});
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
it('set sort order', function () {
|
2023-04-07 13:28:16 +01:00
|
|
|
const store = useProjectMembersStore();
|
|
|
|
|
|
|
|
store.setSortingBy(ProjectMemberOrderBy.EMAIL);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
expect(store.state.cursor.order).toBe(ProjectMemberOrderBy.EMAIL);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
it('set sort direction', function () {
|
2023-04-07 13:28:16 +01:00
|
|
|
const store = useProjectMembersStore();
|
|
|
|
|
|
|
|
store.setSortingDirection(SortDirection.DESCENDING);
|
2019-08-20 13:57:43 +01:00
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
expect(store.state.cursor.orderDirection).toBe(SortDirection.DESCENDING);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
it('toggle selection', async function () {
|
|
|
|
const store = useProjectMembersStore();
|
|
|
|
|
2019-10-15 14:22:41 +01:00
|
|
|
const testProjectMembersPage = new ProjectMembersPage();
|
|
|
|
testProjectMembersPage.projectMembers = [projectMember1];
|
|
|
|
testProjectMembersPage.totalCount = 1;
|
|
|
|
testProjectMembersPage.pageCount = 1;
|
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
store.setPage(testProjectMembersPage);
|
|
|
|
store.toggleProjectMemberSelection(projectMember1);
|
|
|
|
|
|
|
|
expect(store.state.page.projectMembers[0].isSelected).toBe(true);
|
|
|
|
expect(store.state.selectedProjectMembersEmails.length).toBe(1);
|
2019-08-20 13:57:43 +01:00
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
jest.spyOn(ProjectMembersApiGql.prototype, 'get')
|
|
|
|
.mockImplementation(() => Promise.resolve(testProjectMembersPage));
|
2019-10-15 14:22:41 +01:00
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
await store.getProjectMembers(FIRST_PAGE, selectedProject.id);
|
2019-10-15 14:22:41 +01:00
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
expect(store.state.selectedProjectMembersEmails.length).toBe(1);
|
2019-10-15 14:22:41 +01:00
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
store.toggleProjectMemberSelection(projectMember1);
|
2019-10-15 14:22:41 +01:00
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
expect(store.state.page.projectMembers[0].isSelected).toBe(false);
|
|
|
|
expect(store.state.selectedProjectMembersEmails.length).toBe(0);
|
2019-08-20 13:57:43 +01:00
|
|
|
});
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
it('clear selection', function () {
|
2023-04-07 13:28:16 +01:00
|
|
|
const store = useProjectMembersStore();
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
const testProjectMembersPage = new ProjectMembersPage();
|
|
|
|
testProjectMembersPage.projectMembers = [projectMember1, projectMember2];
|
|
|
|
testProjectMembersPage.totalCount = 1;
|
|
|
|
testProjectMembersPage.pageCount = 1;
|
2019-08-20 13:57:43 +01:00
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
store.setPage(testProjectMembersPage);
|
|
|
|
store.toggleProjectMemberSelection(projectMember1);
|
|
|
|
store.clearProjectMemberSelection();
|
2019-08-20 13:57:43 +01:00
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
store.state.page.projectMembers.forEach((pm: ProjectMember) => {
|
|
|
|
expect(pm.isSelected).toBe(false);
|
|
|
|
});
|
2019-08-20 13:57:43 +01:00
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
expect(store.state.selectedProjectMembersEmails.length).toBe(0);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
it('add project members', async function () {
|
2023-04-07 13:28:16 +01:00
|
|
|
const store = useProjectMembersStore();
|
|
|
|
|
|
|
|
jest.spyOn(ProjectMembersApiGql.prototype, 'add').mockReturnValue(Promise.resolve());
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
try {
|
2023-04-07 13:28:16 +01:00
|
|
|
await store.addProjectMembers([projectMember1.user.email], selectedProject.id);
|
2022-04-07 12:04:24 +01:00
|
|
|
throw TEST_ERROR;
|
2019-08-20 13:57:43 +01:00
|
|
|
} catch (err) {
|
2022-04-07 12:04:24 +01:00
|
|
|
expect(err).toBe(TEST_ERROR);
|
2019-08-20 13:57:43 +01:00
|
|
|
}
|
|
|
|
});
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
it('add project member throws error when api call fails', async function () {
|
2023-04-07 13:28:16 +01:00
|
|
|
const store = useProjectMembersStore();
|
|
|
|
|
|
|
|
jest.spyOn(ProjectMembersApiGql.prototype, 'add').mockImplementation(() => {
|
2022-04-07 12:04:24 +01:00
|
|
|
throw TEST_ERROR;
|
2019-08-20 13:57:43 +01:00
|
|
|
});
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
const stateDump = store.state;
|
2019-08-20 13:57:43 +01:00
|
|
|
|
|
|
|
try {
|
2023-04-07 13:28:16 +01:00
|
|
|
await store.addProjectMembers([projectMember1.user.email], selectedProject.id);
|
2019-08-20 13:57:43 +01:00
|
|
|
} catch (err) {
|
2022-04-07 12:04:24 +01:00
|
|
|
expect(err).toBe(TEST_ERROR);
|
2023-04-07 13:28:16 +01:00
|
|
|
expect(store.state).toBe(stateDump);
|
2019-08-20 13:57:43 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail(UNREACHABLE_ERROR);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('delete project members', async function () {
|
2023-04-07 13:28:16 +01:00
|
|
|
const store = useProjectMembersStore();
|
|
|
|
|
|
|
|
jest.spyOn(ProjectMembersApiGql.prototype, 'delete').mockReturnValue(Promise.resolve());
|
2019-08-20 13:57:43 +01:00
|
|
|
|
|
|
|
try {
|
2023-04-07 13:28:16 +01:00
|
|
|
await store.deleteProjectMembers(selectedProject.id);
|
2022-04-07 12:04:24 +01:00
|
|
|
throw TEST_ERROR;
|
2019-08-20 13:57:43 +01:00
|
|
|
} catch (err) {
|
2022-04-07 12:04:24 +01:00
|
|
|
expect(err).toBe(TEST_ERROR);
|
2019-08-20 13:57:43 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('delete project member throws error when api call fails', async function () {
|
2023-04-07 13:28:16 +01:00
|
|
|
const store = useProjectMembersStore();
|
|
|
|
|
|
|
|
jest.spyOn(ProjectMembersApiGql.prototype, 'delete').mockImplementation(() => {
|
2022-04-07 12:04:24 +01:00
|
|
|
throw TEST_ERROR;
|
2019-08-20 13:57:43 +01:00
|
|
|
});
|
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
const stateDump = store.state;
|
2019-08-20 13:57:43 +01:00
|
|
|
|
|
|
|
try {
|
2023-04-07 13:28:16 +01:00
|
|
|
await store.deleteProjectMembers(selectedProject.id);
|
2019-08-20 13:57:43 +01:00
|
|
|
} catch (err) {
|
2022-04-07 12:04:24 +01:00
|
|
|
expect(err).toBe(TEST_ERROR);
|
2023-04-07 13:28:16 +01:00
|
|
|
expect(store.state).toBe(stateDump);
|
2019-08-20 13:57:43 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail(UNREACHABLE_ERROR);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fetch project members', async function () {
|
2023-04-07 13:28:16 +01:00
|
|
|
const store = useProjectMembersStore();
|
|
|
|
|
|
|
|
jest.spyOn(ProjectMembersApiGql.prototype, 'get').mockReturnValue(
|
2019-08-20 13:57:43 +01:00
|
|
|
Promise.resolve(new ProjectMembersPage(
|
|
|
|
[projectMember1],
|
|
|
|
'',
|
|
|
|
ProjectMemberOrderBy.NAME,
|
|
|
|
SortDirection.ASCENDING,
|
|
|
|
6,
|
|
|
|
1,
|
|
|
|
1,
|
2019-09-13 15:58:18 +01:00
|
|
|
1)),
|
2019-08-20 13:57:43 +01:00
|
|
|
);
|
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
await store.getProjectMembers(FIRST_PAGE, selectedProject.id);
|
2019-08-20 13:57:43 +01:00
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
expect(store.state.page.projectMembers[0].isSelected).toBe(false);
|
|
|
|
expect(store.state.page.projectMembers[0].joinedAt).toBe(projectMember1.joinedAt);
|
|
|
|
expect(store.state.page.projectMembers[0].user.email).toBe(projectMember1.user.email);
|
|
|
|
expect(store.state.page.projectMembers[0].user.id).toBe(projectMember1.user.id);
|
|
|
|
expect(store.state.page.projectMembers[0].user.fullName).toBe(projectMember1.user.fullName);
|
|
|
|
expect(store.state.page.projectMembers[0].user.shortName).toBe(projectMember1.user.shortName);
|
2019-08-20 13:57:43 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('fetch project members throws error when api call fails', async function () {
|
2023-04-07 13:28:16 +01:00
|
|
|
const store = useProjectMembersStore();
|
|
|
|
|
|
|
|
jest.spyOn(ProjectMembersApiGql.prototype, 'get').mockImplementation(() => {
|
2022-04-07 12:04:24 +01:00
|
|
|
throw TEST_ERROR;
|
2019-08-20 13:57:43 +01:00
|
|
|
});
|
|
|
|
|
2023-04-07 13:28:16 +01:00
|
|
|
const stateDump = store.state;
|
2019-08-20 13:57:43 +01:00
|
|
|
|
|
|
|
try {
|
2023-04-07 13:28:16 +01:00
|
|
|
await store.getProjectMembers(FIRST_PAGE, selectedProject.id);
|
2019-08-20 13:57:43 +01:00
|
|
|
} catch (err) {
|
2022-04-07 12:04:24 +01:00
|
|
|
expect(err).toBe(TEST_ERROR);
|
2023-04-07 13:28:16 +01:00
|
|
|
expect(store.state).toBe(stateDump);
|
2019-08-20 13:57:43 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail(UNREACHABLE_ERROR);
|
|
|
|
});
|
|
|
|
});
|