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';
|
2019-09-09 11:33:39 +01:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
import { ProjectsApiGql } from '@/api/projects';
|
|
|
|
import { makeProjectsModule, PROJECTS_ACTIONS, PROJECTS_MUTATIONS } from '@/store/modules/projects';
|
2020-09-15 13:44:23 +01:00
|
|
|
import { Project, ProjectFields, ProjectLimits } from '@/types/projects';
|
2019-09-09 11:33:39 +01:00
|
|
|
import { createLocalVue } from '@vue/test-utils';
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
const Vue = createLocalVue();
|
|
|
|
const projectsApi = new ProjectsApiGql();
|
2020-09-10 10:32:35 +01:00
|
|
|
const {
|
|
|
|
FETCH,
|
|
|
|
CREATE,
|
|
|
|
SELECT,
|
|
|
|
DELETE,
|
|
|
|
CLEAR,
|
|
|
|
UPDATE_NAME,
|
|
|
|
UPDATE_DESCRIPTION,
|
|
|
|
GET_LIMITS,
|
|
|
|
} = PROJECTS_ACTIONS;
|
|
|
|
const {
|
|
|
|
ADD,
|
|
|
|
SET_PROJECTS,
|
|
|
|
SELECT_PROJECT,
|
|
|
|
UPDATE_PROJECT_NAME,
|
|
|
|
UPDATE_PROJECT_DESCRIPTION,
|
|
|
|
REMOVE,
|
|
|
|
CLEAR_PROJECTS,
|
|
|
|
SET_LIMITS,
|
|
|
|
} = PROJECTS_MUTATIONS;
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
const projectsModule = makeProjectsModule(projectsApi);
|
|
|
|
const selectedProject = new Project('1', '', '', '');
|
|
|
|
projectsModule.state.selectedProject = selectedProject;
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
Vue.use(Vuex);
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2021-08-05 12:07:45 +01:00
|
|
|
const store = new Vuex.Store<{
|
|
|
|
projectsModule: typeof projectsModule.state,
|
|
|
|
}>({ modules: { projectsModule } });
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2021-08-05 12:07:45 +01:00
|
|
|
const state = store.state.projectsModule;
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
const projects = [
|
2019-12-12 16:25:38 +00:00
|
|
|
new Project(
|
|
|
|
'11',
|
|
|
|
'name',
|
|
|
|
'descr',
|
|
|
|
'23',
|
|
|
|
'testOwnerId',
|
|
|
|
false,
|
|
|
|
),
|
|
|
|
new Project(
|
|
|
|
'1',
|
|
|
|
'name2',
|
|
|
|
'descr2',
|
|
|
|
'24',
|
|
|
|
'testOwnerId1',
|
|
|
|
false,
|
|
|
|
),
|
2019-08-22 17:03:13 +01:00
|
|
|
];
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-12-12 16:25:38 +00:00
|
|
|
const limits = new ProjectLimits(15, 12, 14, 13);
|
|
|
|
|
2019-10-22 12:12:49 +01:00
|
|
|
const project = new Project('11', 'name', 'descr', '23', 'testOwnerId');
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-08-22 17:34:41 +01:00
|
|
|
describe('mutations', () => {
|
2019-08-22 17:03:13 +01:00
|
|
|
beforeEach(() => {
|
|
|
|
createLocalVue().use(Vuex);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-08-22 17:34:41 +01:00
|
|
|
it('add project', () => {
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
store.commit(ADD, project);
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
expect(state.projects[0].id).toBe(project.id);
|
|
|
|
expect(state.projects[0].name).toBe(project.name);
|
|
|
|
expect(state.projects[0].description).toBe(project.description);
|
2019-09-13 10:48:27 +01:00
|
|
|
expect(state.projects[0].createdAt).toBe(project.createdAt);
|
2019-10-22 12:12:49 +01:00
|
|
|
expect(state.projects[0].ownerId).toBe(project.ownerId);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-08-22 17:34:41 +01:00
|
|
|
it('set projects', () => {
|
2019-08-22 17:03:13 +01:00
|
|
|
store.commit(SET_PROJECTS, projects);
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
expect(state.projects).toBe(projects);
|
|
|
|
expect(state.selectedProject.id).toBe('1');
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-08-22 17:34:41 +01:00
|
|
|
it('select project', () => {
|
2019-08-22 17:03:13 +01:00
|
|
|
state.projects = projects;
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
store.commit(SELECT_PROJECT, '11');
|
|
|
|
expect(state.selectedProject.id).toBe('11');
|
2020-01-15 18:09:03 +00:00
|
|
|
expect(state.currentLimits.bandwidthLimit).toBe(0);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2020-09-10 10:32:35 +01:00
|
|
|
it('update project name', () => {
|
|
|
|
state.projects = projects;
|
|
|
|
|
|
|
|
const newName = 'newName';
|
|
|
|
|
|
|
|
store.commit(UPDATE_PROJECT_NAME, { id: '11', name: newName });
|
|
|
|
|
2021-08-05 12:07:45 +01:00
|
|
|
const project = state.projects.find((pr: Project) => pr.id === '11');
|
|
|
|
expect(project).toBeDefined();
|
|
|
|
if(project) expect(project.name).toBe(newName);
|
2020-09-10 10:32:35 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('update project description', () => {
|
2019-08-22 17:03:13 +01:00
|
|
|
state.projects = projects;
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
const newDescription = 'newDescription';
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2020-09-10 10:32:35 +01:00
|
|
|
store.commit(UPDATE_PROJECT_DESCRIPTION, { id: '11', description: newDescription });
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2021-08-05 12:07:45 +01:00
|
|
|
const project = state.projects.find((pr: Project) => pr.id === '11');
|
|
|
|
expect(project).toBeDefined();
|
|
|
|
if(project) expect(project.description).toBe(newDescription);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-08-22 17:34:41 +01:00
|
|
|
it('remove project', () => {
|
2019-08-22 17:03:13 +01:00
|
|
|
state.projects = projects;
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
store.commit(REMOVE, '11');
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
expect(state.projects.length).toBe(1);
|
|
|
|
expect(state.projects[0].id).toBe('1');
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-12-12 16:25:38 +00:00
|
|
|
it('set limits', () => {
|
|
|
|
state.projects = projects;
|
|
|
|
|
|
|
|
store.commit(SET_LIMITS, limits);
|
|
|
|
|
2020-01-15 18:09:03 +00:00
|
|
|
expect(state.currentLimits.bandwidthUsed).toBe(12);
|
|
|
|
expect(state.currentLimits.bandwidthLimit).toBe(15);
|
|
|
|
expect(state.currentLimits.storageUsed).toBe(13);
|
|
|
|
expect(state.currentLimits.storageLimit).toBe(14);
|
2019-12-12 16:25:38 +00:00
|
|
|
});
|
|
|
|
|
2019-08-22 17:34:41 +01:00
|
|
|
it('clear projects', () => {
|
2019-08-22 17:03:13 +01:00
|
|
|
state.projects = projects;
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
store.commit(CLEAR_PROJECTS);
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
expect(state.projects.length).toBe(0);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
2019-08-22 17:34:41 +01:00
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
|
|
|
|
describe('actions', () => {
|
2019-02-20 13:33:56 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetAllMocks();
|
2019-08-22 17:03:13 +01:00
|
|
|
createLocalVue().use(Vuex);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
it('success fetch projects', async () => {
|
|
|
|
jest.spyOn(projectsApi, 'get').mockReturnValue(
|
2019-09-13 15:58:18 +01:00
|
|
|
Promise.resolve(projects),
|
2019-08-22 17:03:13 +01:00
|
|
|
);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
await store.dispatch(FETCH);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
expect(state.projects).toBe(projects);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
it('fetch throws an error when api call fails', async () => {
|
|
|
|
state.projects = [];
|
|
|
|
jest.spyOn(projectsApi, 'get').mockImplementation(() => { throw new Error(); });
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
try {
|
|
|
|
await store.dispatch(FETCH);
|
|
|
|
} catch (error) {
|
|
|
|
expect(state.projects.length).toBe(0);
|
2020-01-15 18:09:03 +00:00
|
|
|
expect(state.currentLimits.bandwidthLimit).toBe(0);
|
2019-08-22 17:03:13 +01:00
|
|
|
}
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('success create project', async () => {
|
2019-08-22 17:03:13 +01:00
|
|
|
state.projects = [];
|
|
|
|
jest.spyOn(projectsApi, 'create').mockReturnValue(
|
2019-09-13 15:58:18 +01:00
|
|
|
Promise.resolve(project),
|
2019-02-20 14:04:55 +00:00
|
|
|
);
|
2019-08-22 17:03:13 +01:00
|
|
|
|
2019-09-13 10:48:27 +01:00
|
|
|
await store.dispatch(CREATE, {name: '', description: ''});
|
2019-08-22 17:03:13 +01:00
|
|
|
expect(state.projects.length).toBe(1);
|
2020-01-15 18:09:03 +00:00
|
|
|
expect(state.currentLimits.bandwidthLimit).toBe(0);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
it('create throws an error when create api call fails', async () => {
|
|
|
|
state.projects = [];
|
|
|
|
jest.spyOn(projectsApi, 'create').mockImplementation(() => { throw new Error(); });
|
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch(CREATE, 'testName');
|
|
|
|
expect(true).toBe(false);
|
|
|
|
} catch (error) {
|
|
|
|
expect(state.projects.length).toBe(0);
|
2020-01-15 18:09:03 +00:00
|
|
|
expect(state.currentLimits.bandwidthLimit).toBe(0);
|
2019-08-22 17:03:13 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-10-22 12:12:49 +01:00
|
|
|
it('success delete project', async () => {
|
2019-08-22 17:03:13 +01:00
|
|
|
jest.spyOn(projectsApi, 'delete').mockReturnValue(
|
2019-09-13 15:58:18 +01:00
|
|
|
Promise.resolve(),
|
2019-02-20 14:04:55 +00:00
|
|
|
);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
state.projects = projects;
|
|
|
|
|
|
|
|
await store.dispatch(DELETE, '11');
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
expect(state.projects.length).toBe(1);
|
|
|
|
expect(state.projects[0].id).toBe('1');
|
|
|
|
});
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
it('delete throws an error when api call fails', async () => {
|
|
|
|
jest.spyOn(projectsApi, 'delete').mockImplementation(() => { throw new Error(); });
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
state.projects = projects;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await store.dispatch(DELETE, '11');
|
|
|
|
expect(true).toBe(false);
|
|
|
|
} catch (error) {
|
|
|
|
expect(state.projects).toEqual(projects);
|
|
|
|
}
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('success select project', () => {
|
2019-08-22 17:03:13 +01:00
|
|
|
state.projects = projects;
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
store.dispatch(SELECT, '1');
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
expect(state.selectedProject.id).toEqual('1');
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2020-09-10 10:32:35 +01:00
|
|
|
it('success update project name', async () => {
|
|
|
|
jest.spyOn(projectsApi, 'update').mockReturnValue(
|
|
|
|
Promise.resolve(),
|
|
|
|
);
|
|
|
|
|
|
|
|
state.projects = projects;
|
|
|
|
const newName = 'newName';
|
2020-09-15 13:44:23 +01:00
|
|
|
const fieldsToUpdate = new ProjectFields(newName, state.projects[0].description);
|
2020-09-10 10:32:35 +01:00
|
|
|
|
|
|
|
await store.dispatch(UPDATE_NAME, fieldsToUpdate);
|
|
|
|
|
2021-08-05 12:07:45 +01:00
|
|
|
const project = state.projects.find((pr: Project) => pr.id === '1');
|
|
|
|
expect(project).toBeDefined();
|
|
|
|
if(project) expect(project.name).toBe(newName);
|
2020-09-10 10:32:35 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('success update project description', async () => {
|
2019-08-22 17:03:13 +01:00
|
|
|
jest.spyOn(projectsApi, 'update').mockReturnValue(
|
2019-09-13 15:58:18 +01:00
|
|
|
Promise.resolve(),
|
2019-08-22 17:03:13 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
state.projects = projects;
|
|
|
|
const newDescription = 'newDescription1';
|
2020-09-15 13:44:23 +01:00
|
|
|
const fieldsToUpdate = new ProjectFields(state.projects[0].name, newDescription);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2020-09-10 10:32:35 +01:00
|
|
|
await store.dispatch(UPDATE_DESCRIPTION, fieldsToUpdate);
|
2019-08-22 17:03:13 +01:00
|
|
|
|
2021-08-05 12:07:45 +01:00
|
|
|
const project = state.projects.find((pr: Project) => pr.id === '1');
|
|
|
|
expect(project).toBeDefined();
|
|
|
|
if(project) expect(project.description).toBe(newDescription);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
it('update throws an error when api call fails', async () => {
|
|
|
|
jest.spyOn(projectsApi, 'update').mockImplementation(() => { throw new Error(); });
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
state.projects = projects;
|
|
|
|
const newDescription = 'newDescription2';
|
2020-09-15 13:44:23 +01:00
|
|
|
const fieldsToUpdate = new ProjectFields(state.projects[0].name, newDescription);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
try {
|
2020-09-10 10:32:35 +01:00
|
|
|
await store.dispatch(UPDATE_DESCRIPTION, fieldsToUpdate);
|
2019-08-22 17:03:13 +01:00
|
|
|
expect(true).toBe(false);
|
|
|
|
} catch (error) {
|
2021-08-05 12:07:45 +01:00
|
|
|
const project = state.projects.find((pr: Project) => pr.id === '1');
|
|
|
|
expect(project).toBeDefined();
|
|
|
|
if(project) expect(project.description).toBe('newDescription1');
|
2019-08-22 17:03:13 +01:00
|
|
|
}
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2019-12-12 16:25:38 +00:00
|
|
|
it('success get project limits', async () => {
|
|
|
|
jest.spyOn(projectsApi, 'getLimits').mockReturnValue(
|
|
|
|
Promise.resolve(limits),
|
|
|
|
);
|
|
|
|
|
|
|
|
state.projects = projects;
|
|
|
|
|
|
|
|
await store.dispatch(GET_LIMITS, state.selectedProject.id);
|
|
|
|
|
2020-01-15 18:09:03 +00:00
|
|
|
expect(state.currentLimits.bandwidthUsed).toBe(12);
|
|
|
|
expect(state.currentLimits.bandwidthLimit).toBe(15);
|
|
|
|
expect(state.currentLimits.storageUsed).toBe(13);
|
|
|
|
expect(state.currentLimits.storageLimit).toBe(14);
|
2019-12-12 16:25:38 +00:00
|
|
|
});
|
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
it('success clearProjects', () => {
|
|
|
|
state.projects = projects;
|
|
|
|
store.dispatch(CLEAR);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
expect(state.projects.length).toEqual(0);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('getters', () => {
|
2019-08-22 17:03:13 +01:00
|
|
|
beforeEach(() => {
|
|
|
|
createLocalVue().use(Vuex);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
it('selectedProject', () => {
|
|
|
|
store.commit(PROJECTS_MUTATIONS.SET_PROJECTS, projects);
|
|
|
|
store.commit(PROJECTS_MUTATIONS.SELECT_PROJECT, '1');
|
|
|
|
|
|
|
|
const selectedProject = store.getters.selectedProject;
|
|
|
|
|
|
|
|
expect(selectedProject.id).toBe('1');
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2019-10-22 12:12:49 +01:00
|
|
|
it('projects array', () => {
|
2019-08-22 17:03:13 +01:00
|
|
|
store.commit(PROJECTS_MUTATIONS.SET_PROJECTS, projects);
|
|
|
|
|
|
|
|
const allProjects = store.getters.projects;
|
|
|
|
|
|
|
|
expect(allProjects.length).toEqual(2);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
});
|