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 { createLocalVue } from '@vue/test-utils';
|
|
|
|
import Vuex from 'vuex';
|
|
|
|
|
2019-08-20 13:57:43 +01:00
|
|
|
import { ProjectMembersApiGql } from '@/api/projectMembers';
|
|
|
|
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
|
|
|
import { ProjectsApiGql } from '@/api/projects';
|
2019-08-20 13:57:43 +01:00
|
|
|
|
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;
|
|
|
|
const TEST_ERROR = 'testError';
|
|
|
|
const UNREACHABLE_ERROR = 'should be unreachable';
|
|
|
|
|
|
|
|
const Vue = createLocalVue();
|
|
|
|
const pmApi = new ProjectMembersApiGql();
|
|
|
|
const projectMembersModule = makeProjectMembersModule(pmApi);
|
|
|
|
|
|
|
|
Vue.use(Vuex);
|
|
|
|
|
|
|
|
const store = new Vuex.Store({modules: {projectsModule, projectMembersModule}});
|
|
|
|
const state = (store.state as any).projectMembersModule;
|
|
|
|
|
|
|
|
const projectMember1 = new ProjectMember('testFullName1', 'testShortName1', 'test1@example.com', 'now1', '1');
|
2019-08-28 09:29:40 +01:00
|
|
|
const projectMember2 = new ProjectMember('testFullName2', 'testShortName2', 'test2@example.com', 'now2', '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 () {
|
|
|
|
store.commit(PROJECT_MEMBER_MUTATIONS.TOGGLE_SELECTION, projectMember1.user.id);
|
|
|
|
|
|
|
|
expect(state.page.projectMembers[0].isSelected).toBe(true);
|
|
|
|
});
|
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-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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('actions', async () => {
|
|
|
|
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]);
|
|
|
|
throw new Error(TEST_ERROR);
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).toBe(TEST_ERROR);
|
|
|
|
}
|
|
|
|
});
|
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(() => {
|
|
|
|
throw new Error(TEST_ERROR);
|
|
|
|
});
|
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) {
|
|
|
|
expect(err.message).toBe(TEST_ERROR);
|
|
|
|
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]);
|
|
|
|
throw new Error(TEST_ERROR);
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).toBe(TEST_ERROR);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
it('delete project member throws error when api call fails', async function () {
|
|
|
|
jest.spyOn(pmApi, 'delete').mockImplementation(() => {
|
|
|
|
throw new Error(TEST_ERROR);
|
|
|
|
});
|
|
|
|
|
|
|
|
const stateDump = state;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch(PM_ACTIONS.DELETE, [projectMember1.user.email]);
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).toBe(TEST_ERROR);
|
|
|
|
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,
|
|
|
|
1))
|
|
|
|
);
|
|
|
|
|
|
|
|
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.partnerId).toBe(projectMember1.user.partnerId);
|
|
|
|
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(() => {
|
|
|
|
throw new Error(TEST_ERROR);
|
|
|
|
});
|
|
|
|
|
|
|
|
const stateDump = state;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch(PM_ACTIONS.FETCH, FIRST_PAGE);
|
|
|
|
} catch (err) {
|
|
|
|
expect(err.message).toBe(TEST_ERROR);
|
|
|
|
expect(state).toBe(stateDump);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail(UNREACHABLE_ERROR);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('set project members search query', async function () {
|
|
|
|
store.dispatch(PM_ACTIONS.SET_SEARCH_QUERY, 'search');
|
|
|
|
|
|
|
|
expect(state.cursor.search).toBe('search');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('set project members sort by', async function () {
|
|
|
|
store.dispatch(PM_ACTIONS.SET_SORT_BY, ProjectMemberOrderBy.CREATED_AT);
|
|
|
|
|
|
|
|
expect(state.cursor.order).toBe(ProjectMemberOrderBy.CREATED_AT);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('set sort direction', async function () {
|
|
|
|
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,
|
|
|
|
2))
|
|
|
|
);
|
|
|
|
|
|
|
|
await store.dispatch(PM_ACTIONS.FETCH, FIRST_PAGE);
|
2019-08-20 13:57:43 +01:00
|
|
|
store.dispatch(PM_ACTIONS.TOGGLE_SELECTION, projectMember1.user.id);
|
|
|
|
|
|
|
|
expect(state.page.projectMembers[0].isSelected).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
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-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', () => {
|
|
|
|
const selectedProjectMember = new ProjectMember('testFullName2', 'testShortName2', 'test2@example.com', 'now2', '2');
|
|
|
|
selectedProjectMember.isSelected = true;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
const retrievedProjectMembers = store.getters.selectedProjectMembers;
|
|
|
|
|
|
|
|
expect(retrievedProjectMembers[0].user.id).toBe(selectedProjectMember.user.id);
|
|
|
|
});
|
|
|
|
});
|