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.
|
|
|
|
|
2019-01-28 14:39:51 +00:00
|
|
|
import Vuex from 'vuex';
|
2022-09-08 15:11:09 +01:00
|
|
|
import { createLocalVue } from '@vue/test-utils';
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
import { ProjectMembersApiGql } from '@/api/projectMembers';
|
2019-09-09 11:33:39 +01:00
|
|
|
import { ProjectsApiGql } from '@/api/projects';
|
2019-08-20 13:57:43 +01:00
|
|
|
import { makeProjectMembersModule, PROJECT_MEMBER_MUTATIONS } from '@/store/modules/projectMembers';
|
|
|
|
import { makeProjectsModule } from '@/store/modules/projects';
|
|
|
|
import { SortDirection } from '@/types/common';
|
|
|
|
import { ProjectMember, ProjectMemberOrderBy, ProjectMembersPage } from '@/types/projectMembers';
|
|
|
|
import { Project } from '@/types/projects';
|
|
|
|
import { PM_ACTIONS } from '@/utils/constants/actionNames';
|
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
const projectsApi = new ProjectsApiGql();
|
|
|
|
const projectsModule = makeProjectsModule(projectsApi);
|
2019-08-20 13:57:43 +01:00
|
|
|
const selectedProject = new Project();
|
|
|
|
selectedProject.id = '1';
|
|
|
|
projectsModule.state.selectedProject = selectedProject;
|
|
|
|
|
|
|
|
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';
|
|
|
|
|
|
|
|
const Vue = createLocalVue();
|
|
|
|
const pmApi = new ProjectMembersApiGql();
|
|
|
|
const projectMembersModule = makeProjectMembersModule(pmApi);
|
|
|
|
|
|
|
|
Vue.use(Vuex);
|
|
|
|
|
2021-08-05 12:07:45 +01:00
|
|
|
const store = new Vuex.Store<{
|
|
|
|
projectsModule: typeof projectsModule.state,
|
|
|
|
projectMembersModule: typeof projectMembersModule.state,
|
2022-09-08 15:11:09 +01:00
|
|
|
}>({ modules: { projectsModule, projectMembersModule } });
|
2021-08-05 12:07:45 +01:00
|
|
|
const state = store.state.projectMembersModule;
|
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
|
|
|
|
|
|
|
describe('mutations', () => {
|
2019-08-20 13:57:43 +01:00
|
|
|
it('fetch project members', function () {
|
|
|
|
const testProjectMembersPage = new ProjectMembersPage();
|
|
|
|
testProjectMembersPage.projectMembers = [projectMember1];
|
|
|
|
testProjectMembersPage.totalCount = 1;
|
|
|
|
testProjectMembersPage.pageCount = 1;
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
store.commit(PROJECT_MEMBER_MUTATIONS.FETCH, testProjectMembersPage);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
expect(state.page.projectMembers.length).toBe(1);
|
|
|
|
expect(state.page.search).toBe('');
|
|
|
|
expect(state.page.order).toBe(ProjectMemberOrderBy.NAME);
|
|
|
|
expect(state.page.orderDirection).toBe(SortDirection.ASCENDING);
|
|
|
|
expect(state.page.limit).toBe(6);
|
|
|
|
expect(state.page.pageCount).toBe(1);
|
|
|
|
expect(state.page.currentPage).toBe(1);
|
|
|
|
expect(state.page.totalCount).toBe(1);
|
|
|
|
});
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
it('set project members page', function () {
|
|
|
|
store.commit(PROJECT_MEMBER_MUTATIONS.SET_PAGE, 2);
|
|
|
|
|
|
|
|
expect(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 () {
|
|
|
|
store.commit(PROJECT_MEMBER_MUTATIONS.SET_SEARCH_QUERY, 'testSearchQuery');
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
expect(state.cursor.search).toBe('testSearchQuery');
|
|
|
|
});
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
it('set sort order', function () {
|
|
|
|
store.commit(PROJECT_MEMBER_MUTATIONS.CHANGE_SORT_ORDER, ProjectMemberOrderBy.EMAIL);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
expect(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 () {
|
|
|
|
store.commit(PROJECT_MEMBER_MUTATIONS.CHANGE_SORT_ORDER_DIRECTION, SortDirection.DESCENDING);
|
|
|
|
|
|
|
|
expect(state.cursor.orderDirection).toBe(SortDirection.DESCENDING);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
it('toggle selection', function () {
|
2019-10-15 14:22:41 +01:00
|
|
|
const testProjectMembersPage = new ProjectMembersPage();
|
|
|
|
testProjectMembersPage.projectMembers = [projectMember1];
|
|
|
|
testProjectMembersPage.totalCount = 1;
|
|
|
|
testProjectMembersPage.pageCount = 1;
|
|
|
|
|
|
|
|
store.commit(PROJECT_MEMBER_MUTATIONS.TOGGLE_SELECTION, projectMember1);
|
2019-08-20 13:57:43 +01:00
|
|
|
|
|
|
|
expect(state.page.projectMembers[0].isSelected).toBe(true);
|
2019-10-15 14:22:41 +01:00
|
|
|
expect(state.selectedProjectMembersEmails.length).toBe(1);
|
|
|
|
|
|
|
|
store.commit(PROJECT_MEMBER_MUTATIONS.FETCH, testProjectMembersPage);
|
|
|
|
|
|
|
|
expect(state.selectedProjectMembersEmails.length).toBe(1);
|
|
|
|
|
|
|
|
store.commit(PROJECT_MEMBER_MUTATIONS.TOGGLE_SELECTION, projectMember1);
|
|
|
|
|
|
|
|
expect(state.page.projectMembers[0].isSelected).toBe(false);
|
|
|
|
expect(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 () {
|
2019-02-20 13:33:56 +00:00
|
|
|
store.commit(PROJECT_MEMBER_MUTATIONS.CLEAR_SELECTION);
|
|
|
|
|
2019-08-28 09:29:40 +01:00
|
|
|
state.page.projectMembers.forEach((pm: ProjectMember) => {
|
|
|
|
expect(pm.isSelected).toBe(false);
|
|
|
|
});
|
2019-10-15 14:22:41 +01:00
|
|
|
|
|
|
|
expect(state.selectedProjectMembersEmails.length).toBe(0);
|
2019-08-20 13:57:43 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('clear store', function () {
|
|
|
|
store.commit(PROJECT_MEMBER_MUTATIONS.CLEAR);
|
|
|
|
|
|
|
|
expect(state.cursor.page).toBe(1);
|
|
|
|
expect(state.cursor.search).toBe('');
|
|
|
|
expect(state.cursor.order).toBe(ProjectMemberOrderBy.NAME);
|
|
|
|
expect(state.cursor.orderDirection).toBe(SortDirection.ASCENDING);
|
|
|
|
expect(state.page.projectMembers.length).toBe(0);
|
2019-10-15 14:22:41 +01:00
|
|
|
expect(state.selectedProjectMembersEmails.length).toBe(0);
|
2019-08-20 13:57:43 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-01-08 15:58:28 +00:00
|
|
|
describe('actions', () => {
|
2019-08-20 13:57:43 +01:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetAllMocks();
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
it('add project members', async function () {
|
|
|
|
jest.spyOn(pmApi, 'add').mockReturnValue(Promise.resolve());
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
try {
|
|
|
|
await store.dispatch(PM_ACTIONS.ADD, [projectMember1.user.email]);
|
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 () {
|
|
|
|
jest.spyOn(pmApi, '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
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
const stateDump = state;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch(PM_ACTIONS.ADD, [projectMember1.user.email]);
|
|
|
|
} catch (err) {
|
2022-04-07 12:04:24 +01:00
|
|
|
expect(err).toBe(TEST_ERROR);
|
2019-08-20 13:57:43 +01:00
|
|
|
expect(state).toBe(stateDump);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail(UNREACHABLE_ERROR);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('delete project members', async function () {
|
|
|
|
jest.spyOn(pmApi, 'delete').mockReturnValue(Promise.resolve());
|
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch(PM_ACTIONS.DELETE, [projectMember1.user.email]);
|
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 () {
|
|
|
|
jest.spyOn(pmApi, 'delete').mockImplementation(() => {
|
2022-04-07 12:04:24 +01:00
|
|
|
throw TEST_ERROR;
|
2019-08-20 13:57:43 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const stateDump = state;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch(PM_ACTIONS.DELETE, [projectMember1.user.email]);
|
|
|
|
} catch (err) {
|
2022-04-07 12:04:24 +01:00
|
|
|
expect(err).toBe(TEST_ERROR);
|
2019-08-20 13:57:43 +01:00
|
|
|
expect(state).toBe(stateDump);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail(UNREACHABLE_ERROR);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fetch project members', async function () {
|
|
|
|
jest.spyOn(pmApi, 'get').mockReturnValue(
|
|
|
|
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
|
|
|
);
|
|
|
|
|
|
|
|
await store.dispatch(PM_ACTIONS.FETCH, FIRST_PAGE);
|
|
|
|
|
|
|
|
expect(state.page.projectMembers[0].isSelected).toBe(false);
|
|
|
|
expect(state.page.projectMembers[0].joinedAt).toBe(projectMember1.joinedAt);
|
|
|
|
expect(state.page.projectMembers[0].user.email).toBe(projectMember1.user.email);
|
|
|
|
expect(state.page.projectMembers[0].user.id).toBe(projectMember1.user.id);
|
|
|
|
expect(state.page.projectMembers[0].user.fullName).toBe(projectMember1.user.fullName);
|
|
|
|
expect(state.page.projectMembers[0].user.shortName).toBe(projectMember1.user.shortName);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fetch project members throws error when api call fails', async function () {
|
|
|
|
jest.spyOn(pmApi, 'get').mockImplementation(() => {
|
2022-04-07 12:04:24 +01:00
|
|
|
throw TEST_ERROR;
|
2019-08-20 13:57:43 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
const stateDump = state;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch(PM_ACTIONS.FETCH, FIRST_PAGE);
|
|
|
|
} catch (err) {
|
2022-04-07 12:04:24 +01:00
|
|
|
expect(err).toBe(TEST_ERROR);
|
2019-08-20 13:57:43 +01:00
|
|
|
expect(state).toBe(stateDump);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail(UNREACHABLE_ERROR);
|
|
|
|
});
|
|
|
|
|
2019-09-09 11:33:39 +01:00
|
|
|
it('set project members search query', function () {
|
2019-08-20 13:57:43 +01:00
|
|
|
store.dispatch(PM_ACTIONS.SET_SEARCH_QUERY, 'search');
|
|
|
|
|
|
|
|
expect(state.cursor.search).toBe('search');
|
|
|
|
});
|
|
|
|
|
2019-09-09 11:33:39 +01:00
|
|
|
it('set project members sort by', function () {
|
2019-08-20 13:57:43 +01:00
|
|
|
store.dispatch(PM_ACTIONS.SET_SORT_BY, ProjectMemberOrderBy.CREATED_AT);
|
|
|
|
|
|
|
|
expect(state.cursor.order).toBe(ProjectMemberOrderBy.CREATED_AT);
|
|
|
|
});
|
|
|
|
|
2019-09-09 11:33:39 +01:00
|
|
|
it('set sort direction', function () {
|
2019-08-20 13:57:43 +01:00
|
|
|
store.dispatch(PM_ACTIONS.SET_SORT_DIRECTION, SortDirection.DESCENDING);
|
|
|
|
|
|
|
|
expect(state.cursor.orderDirection).toBe(SortDirection.DESCENDING);
|
|
|
|
});
|
|
|
|
|
2019-08-28 09:29:40 +01:00
|
|
|
it('toggle selection', async function () {
|
|
|
|
jest.spyOn(pmApi, 'get').mockReturnValue(
|
|
|
|
Promise.resolve(new ProjectMembersPage(
|
|
|
|
[projectMember1, projectMember2],
|
|
|
|
'',
|
|
|
|
ProjectMemberOrderBy.NAME,
|
|
|
|
SortDirection.ASCENDING,
|
|
|
|
6,
|
|
|
|
1,
|
|
|
|
1,
|
2019-09-13 15:58:18 +01:00
|
|
|
2)),
|
2019-08-28 09:29:40 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
await store.dispatch(PM_ACTIONS.FETCH, FIRST_PAGE);
|
2019-10-15 14:22:41 +01:00
|
|
|
store.dispatch(PM_ACTIONS.TOGGLE_SELECTION, projectMember1);
|
2019-08-20 13:57:43 +01:00
|
|
|
|
|
|
|
expect(state.page.projectMembers[0].isSelected).toBe(true);
|
2019-10-15 14:22:41 +01:00
|
|
|
expect(state.selectedProjectMembersEmails.length).toBe(1);
|
|
|
|
|
|
|
|
store.dispatch(PM_ACTIONS.TOGGLE_SELECTION, projectMember2);
|
|
|
|
|
|
|
|
expect(state.page.projectMembers[1].isSelected).toBe(true);
|
|
|
|
expect(state.selectedProjectMembersEmails.length).toBe(2);
|
|
|
|
|
|
|
|
await store.dispatch(PM_ACTIONS.FETCH, FIRST_PAGE);
|
|
|
|
|
|
|
|
expect(state.page.projectMembers[1].isSelected).toBe(true);
|
|
|
|
expect(state.selectedProjectMembersEmails.length).toBe(2);
|
|
|
|
|
|
|
|
store.dispatch(PM_ACTIONS.TOGGLE_SELECTION, projectMember1);
|
|
|
|
|
|
|
|
expect(state.page.projectMembers[0].isSelected).toBe(false);
|
|
|
|
expect(state.selectedProjectMembersEmails.length).toBe(1);
|
2019-08-20 13:57:43 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('clear selection', function () {
|
|
|
|
store.dispatch(PM_ACTIONS.CLEAR_SELECTION);
|
|
|
|
|
2019-08-28 09:29:40 +01:00
|
|
|
state.page.projectMembers.forEach((pm: ProjectMember) => {
|
|
|
|
expect(pm.isSelected).toBe(false);
|
|
|
|
});
|
2019-08-20 13:57:43 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('clear store', function () {
|
|
|
|
store.commit(PM_ACTIONS.CLEAR);
|
|
|
|
|
|
|
|
expect(state.cursor.page).toBe(1);
|
|
|
|
expect(state.cursor.search).toBe('');
|
|
|
|
expect(state.cursor.order).toBe(ProjectMemberOrderBy.NAME);
|
|
|
|
expect(state.cursor.orderDirection).toBe(SortDirection.ASCENDING);
|
|
|
|
expect(state.page.projectMembers.length).toBe(0);
|
2019-10-17 11:34:45 +01:00
|
|
|
|
|
|
|
state.page.projectMembers.forEach((pm: ProjectMember) => {
|
|
|
|
expect(pm.isSelected).toBe(false);
|
|
|
|
});
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
});
|
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
describe('getters', () => {
|
2020-09-28 14:44:01 +01:00
|
|
|
const selectedProjectMember = new ProjectMember('testFullName2', 'testShortName2', 'test2@example.com', date, '2');
|
2019-08-20 13:57:43 +01:00
|
|
|
|
|
|
|
it('selected project members', function () {
|
|
|
|
const testProjectMembersPage = new ProjectMembersPage();
|
|
|
|
testProjectMembersPage.projectMembers = [selectedProjectMember];
|
|
|
|
testProjectMembersPage.totalCount = 1;
|
|
|
|
testProjectMembersPage.pageCount = 1;
|
|
|
|
|
|
|
|
store.commit(PROJECT_MEMBER_MUTATIONS.FETCH, testProjectMembersPage);
|
2019-10-15 14:22:41 +01:00
|
|
|
store.commit(PROJECT_MEMBER_MUTATIONS.TOGGLE_SELECTION, selectedProjectMember);
|
2019-08-20 13:57:43 +01:00
|
|
|
|
|
|
|
const retrievedProjectMembers = store.getters.selectedProjectMembers;
|
|
|
|
|
|
|
|
expect(retrievedProjectMembers[0].user.id).toBe(selectedProjectMember.user.id);
|
|
|
|
});
|
|
|
|
});
|