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.
|
|
|
|
|
|
|
|
import { projectsModule } from '@/store/modules/projects';
|
2019-01-28 14:39:51 +00:00
|
|
|
import * as api from '@/api/projects';
|
2019-01-08 15:14:00 +00:00
|
|
|
import { createProjectRequest, deleteProjectRequest, fetchProjectsRequest, updateProjectRequest } from '@/api/projects';
|
|
|
|
import { PROJECTS_MUTATIONS } from '@/store/mutationConstants';
|
2019-01-28 14:39:51 +00:00
|
|
|
import { createLocalVue } from '@vue/test-utils';
|
|
|
|
import Vuex from 'vuex';
|
2019-07-18 14:39:39 +01:00
|
|
|
import { RequestResponse } from '@/types/response';
|
|
|
|
import { Project } from '@/types/projects';
|
2019-01-28 14:39:51 +00:00
|
|
|
|
|
|
|
const mutations = projectsModule.mutations;
|
2019-01-08 15:14:00 +00:00
|
|
|
|
|
|
|
describe('mutations', () => {
|
2019-02-20 13:33:56 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
createLocalVue().use(Vuex);
|
|
|
|
});
|
|
|
|
it('create project', () => {
|
|
|
|
const state = {
|
|
|
|
projects: [],
|
|
|
|
};
|
|
|
|
const project = {
|
|
|
|
name: 'testName',
|
|
|
|
};
|
|
|
|
const store = new Vuex.Store({state, mutations});
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
store.commit(PROJECTS_MUTATIONS.CREATE, project);
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
expect(state.projects.length).toBe(1);
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
const mutatedProject: Project = state.projects[0];
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
expect(mutatedProject.name).toBe('testName');
|
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
it('fetch project', () => {
|
|
|
|
const state = {
|
|
|
|
projects: []
|
|
|
|
};
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
const store = new Vuex.Store({state, mutations});
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
const projectsToPush = [{id: '1'}, {id: '2'}];
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
store.commit(PROJECTS_MUTATIONS.FETCH, projectsToPush);
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
expect(state.projects.length).toBe(2);
|
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
it('success select project', () => {
|
|
|
|
const state = {
|
|
|
|
projects: [{id: '1'}, {id: 'testId'}, {id: '2'}, ],
|
|
|
|
selectedProject: {
|
|
|
|
id: ''
|
|
|
|
}
|
|
|
|
};
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
const store = new Vuex.Store({state, mutations});
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
const projectId = 'testId';
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
store.commit(PROJECTS_MUTATIONS.SELECT, projectId);
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
expect(state.selectedProject.id).toBe('testId');
|
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
it('error select project', () => {
|
|
|
|
const state = {
|
|
|
|
projects: [{id: '1'}, {id: 'testId'}, {id: '2'}, ],
|
|
|
|
selectedProject: {
|
|
|
|
id: 'old'
|
|
|
|
}
|
|
|
|
};
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
const store = new Vuex.Store({state, mutations});
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
const projectId = '3';
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
store.commit(PROJECTS_MUTATIONS.SELECT, projectId);
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
expect(state.selectedProject.id).toBe('old');
|
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
it('error update project not exist', () => {
|
|
|
|
const state = {
|
|
|
|
projects: [{id: '1'}, {id: 'testId'}, {id: '2'}, ],
|
|
|
|
selectedProject: {
|
|
|
|
id: 'old'
|
|
|
|
}
|
|
|
|
};
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
const store = new Vuex.Store({state, mutations});
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
const projectId = {id: '3'};
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
store.commit(PROJECTS_MUTATIONS.UPDATE, projectId);
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
expect(state.selectedProject.id).toBe('old');
|
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
it('error update project not selected', () => {
|
|
|
|
const state = {
|
|
|
|
projects: [{id: '1'}, {id: 'testId'}, {id: '2'}, ],
|
|
|
|
selectedProject: {
|
|
|
|
id: 'old',
|
|
|
|
description: 'oldD'
|
|
|
|
}
|
|
|
|
};
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
const store = new Vuex.Store({state, mutations});
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
const project = {id: '2', description: 'newD'};
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
store.commit(PROJECTS_MUTATIONS.UPDATE, project);
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
expect(state.selectedProject.id).toBe('old');
|
|
|
|
expect(state.selectedProject.description).toBe('oldD');
|
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
it('success update project', () => {
|
|
|
|
const state = {
|
|
|
|
projects: [{id: '1'}, {id: 'testId'}, {id: '2'}],
|
|
|
|
selectedProject: {
|
|
|
|
id: '2',
|
|
|
|
description: 'oldD'
|
|
|
|
}
|
|
|
|
};
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
const store = new Vuex.Store({state, mutations});
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
const project = {id: '2', description: 'newD'};
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
store.commit(PROJECTS_MUTATIONS.UPDATE, project);
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
expect(state.selectedProject.id).toBe('2');
|
|
|
|
expect(state.selectedProject.description).toBe('newD');
|
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
it('error delete project', () => {
|
|
|
|
const state = {
|
|
|
|
selectedProject: {
|
|
|
|
id: '1',
|
|
|
|
}
|
|
|
|
};
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
const store = new Vuex.Store({state, mutations});
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
const projectId = '2';
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
store.commit(PROJECTS_MUTATIONS.DELETE, projectId);
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
expect(state.selectedProject.id).toBe('1');
|
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
it('success delete project', () => {
|
|
|
|
const state = {
|
|
|
|
selectedProject: {
|
|
|
|
id: '2',
|
|
|
|
}
|
|
|
|
};
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
const store = new Vuex.Store({state, mutations});
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
const projectId = '2';
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
store.commit(PROJECTS_MUTATIONS.DELETE, projectId);
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
expect(state.selectedProject.id).toBe('');
|
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('actions', () => {
|
2019-02-20 13:33:56 +00:00
|
|
|
beforeEach(() => {
|
|
|
|
jest.resetAllMocks();
|
|
|
|
});
|
|
|
|
it('success fetch project', async () => {
|
2019-02-20 14:04:55 +00:00
|
|
|
jest.spyOn(api, 'fetchProjectsRequest').mockReturnValue(
|
|
|
|
Promise.resolve(<RequestResponse<Project[]>>{isSuccess: true, data: [{id: '1'}, {id: '2'}]})
|
|
|
|
);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
|
|
|
const commit = jest.fn();
|
|
|
|
|
|
|
|
const dispatchResponse = await projectsModule.actions.fetchProjects({commit});
|
|
|
|
|
|
|
|
expect(dispatchResponse.isSuccess).toBeTruthy();
|
|
|
|
expect(commit).toHaveBeenCalledWith(PROJECTS_MUTATIONS.FETCH, [{id: '1'}, {id: '2'}]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('error fetch project', async () => {
|
2019-02-20 14:04:55 +00:00
|
|
|
jest.spyOn(api, 'fetchProjectsRequest').mockReturnValue(
|
|
|
|
Promise.resolve(<RequestResponse<Project[]>>{isSuccess: false})
|
|
|
|
);
|
2019-02-20 13:33:56 +00:00
|
|
|
const commit = jest.fn();
|
|
|
|
const dispatchResponse = await projectsModule.actions.fetchProjects({commit});
|
|
|
|
|
|
|
|
expect(dispatchResponse.isSuccess).toBeFalsy();
|
|
|
|
expect(commit).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('success create project', async () => {
|
2019-02-20 14:04:55 +00:00
|
|
|
jest.spyOn(api, 'createProjectRequest').mockReturnValue(
|
|
|
|
Promise.resolve(<RequestResponse<Project>>{isSuccess: true, data: {id: '1'}})
|
|
|
|
);
|
2019-02-20 13:33:56 +00:00
|
|
|
const commit = jest.fn();
|
|
|
|
const project: Project = {
|
|
|
|
name: '',
|
|
|
|
id: '',
|
|
|
|
description: '',
|
|
|
|
isSelected: false,
|
|
|
|
createdAt: ''
|
|
|
|
};
|
|
|
|
|
|
|
|
const dispatchResponse = await projectsModule.actions.createProject({commit}, project);
|
|
|
|
|
|
|
|
expect(dispatchResponse.isSuccess).toBeTruthy();
|
|
|
|
expect(commit).toHaveBeenCalledWith(PROJECTS_MUTATIONS.CREATE, {id: '1'});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('error create project', async () => {
|
2019-02-20 14:04:55 +00:00
|
|
|
jest.spyOn(api, 'createProjectRequest').mockReturnValue(
|
|
|
|
Promise.resolve(<RequestResponse<Project>>{isSuccess: false})
|
|
|
|
);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
|
|
|
const commit = jest.fn();
|
|
|
|
|
|
|
|
const project: Project = {
|
|
|
|
name: '',
|
|
|
|
id: '',
|
|
|
|
description: '',
|
|
|
|
isSelected: false,
|
|
|
|
createdAt: ''
|
|
|
|
};
|
|
|
|
|
|
|
|
const dispatchResponse = await projectsModule.actions.createProject({commit}, project);
|
|
|
|
|
|
|
|
expect(dispatchResponse.isSuccess).toBeFalsy();
|
|
|
|
expect(commit).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('success select project', () => {
|
|
|
|
const commit = jest.fn();
|
|
|
|
|
|
|
|
projectsModule.actions.selectProject({commit}, 'id');
|
|
|
|
|
|
|
|
expect(commit).toHaveBeenCalledWith(PROJECTS_MUTATIONS.SELECT, 'id');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('success update project description', async () => {
|
2019-02-20 14:04:55 +00:00
|
|
|
jest.spyOn(api, 'updateProjectRequest').mockReturnValue(Promise.resolve(<RequestResponse<null>>{isSuccess: true}));
|
2019-02-20 13:33:56 +00:00
|
|
|
const commit = jest.fn();
|
|
|
|
const project: Project = {
|
|
|
|
name: '',
|
|
|
|
id: 'id',
|
|
|
|
description: 'desc',
|
|
|
|
isSelected: false,
|
|
|
|
createdAt: ''
|
|
|
|
};
|
|
|
|
|
|
|
|
const dispatchResponse = await projectsModule.actions.updateProject({commit}, project);
|
|
|
|
|
|
|
|
expect(dispatchResponse.isSuccess).toBeTruthy();
|
|
|
|
expect(commit).toBeCalledWith(PROJECTS_MUTATIONS.UPDATE, project);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('error update project description', async () => {
|
2019-02-20 14:04:55 +00:00
|
|
|
jest.spyOn(api, 'updateProjectRequest').mockReturnValue(Promise.resolve(<RequestResponse<null>>{isSuccess: false}));
|
2019-02-20 13:33:56 +00:00
|
|
|
const commit = jest.fn();
|
|
|
|
const project: Project = {
|
|
|
|
name: '',
|
|
|
|
id: '',
|
|
|
|
description: '',
|
|
|
|
isSelected: false,
|
|
|
|
createdAt: ''
|
|
|
|
};
|
|
|
|
|
|
|
|
const dispatchResponse = await projectsModule.actions.updateProject({commit}, project);
|
|
|
|
|
|
|
|
expect(dispatchResponse.isSuccess).toBeFalsy();
|
|
|
|
expect(commit).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('success delete project', async () => {
|
2019-02-20 14:04:55 +00:00
|
|
|
jest.spyOn(api, 'deleteProjectRequest').mockReturnValue(Promise.resolve(<RequestResponse<null>>{isSuccess: true}));
|
2019-02-20 13:33:56 +00:00
|
|
|
const commit = jest.fn();
|
|
|
|
const project = 'id';
|
|
|
|
|
|
|
|
const dispatchResponse = await projectsModule.actions.deleteProject({commit}, project);
|
|
|
|
|
|
|
|
expect(dispatchResponse.isSuccess).toBeTruthy();
|
|
|
|
expect(commit).toHaveBeenCalledWith(PROJECTS_MUTATIONS.DELETE, project);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('error delete project', async () => {
|
2019-02-20 14:04:55 +00:00
|
|
|
jest.spyOn(api, 'deleteProjectRequest').mockReturnValue(Promise.resolve(<RequestResponse<null>>{isSuccess: false}));
|
2019-02-20 13:33:56 +00:00
|
|
|
const commit = jest.fn();
|
|
|
|
|
|
|
|
const dispatchResponse = await projectsModule.actions.deleteProject({commit}, 'id');
|
|
|
|
|
|
|
|
expect(dispatchResponse.isSuccess).toBeFalsy();
|
|
|
|
expect(commit).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('getters', () => {
|
|
|
|
|
2019-02-20 13:33:56 +00:00
|
|
|
it('getter projects', () => {
|
|
|
|
const state = {
|
2019-07-18 14:39:39 +01:00
|
|
|
projects: [
|
|
|
|
{
|
2019-02-20 13:33:56 +00:00
|
|
|
name: '1',
|
|
|
|
id: '1',
|
|
|
|
companyName: '1',
|
|
|
|
description: '1',
|
|
|
|
createdAt: '1',
|
|
|
|
}],
|
|
|
|
selectedProject: {
|
|
|
|
id: '1'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const projectsGetterArray = projectsModule.getters.projects(state);
|
|
|
|
|
|
|
|
expect(projectsGetterArray.length).toBe(1);
|
|
|
|
|
|
|
|
const firstProject = projectsGetterArray[0];
|
|
|
|
|
|
|
|
expect(firstProject.name).toBe('1');
|
|
|
|
expect(firstProject.id).toBe('1');
|
|
|
|
expect(firstProject.description).toBe('1');
|
|
|
|
expect(firstProject.createdAt).toBe('1');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('getter projects', () => {
|
|
|
|
const state = {
|
|
|
|
projects: [{
|
|
|
|
name: '1',
|
|
|
|
id: '1',
|
|
|
|
companyName: '1',
|
|
|
|
description: '1',
|
|
|
|
isTermsAccepted: true,
|
|
|
|
createdAt: '1',
|
|
|
|
}],
|
|
|
|
selectedProject: {
|
|
|
|
id: '2'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const projectsGetterArray = projectsModule.getters.projects(state);
|
|
|
|
|
|
|
|
expect(projectsGetterArray.length).toBe(1);
|
|
|
|
|
|
|
|
const firstProject = projectsGetterArray[0];
|
|
|
|
|
|
|
|
expect(firstProject.name).toBe('1');
|
|
|
|
expect(firstProject.id).toBe('1');
|
|
|
|
expect(firstProject.description).toBe('1');
|
|
|
|
expect(firstProject.createdAt).toBe('1');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('getters selected project', () => {
|
|
|
|
const state = {
|
|
|
|
selectedProject: {
|
|
|
|
name: '1',
|
|
|
|
id: '1',
|
|
|
|
description: '1',
|
|
|
|
isTermsAccepted: true,
|
|
|
|
createdAt: '1',
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const selectedProjectGetterObject = projectsModule.getters.selectedProject(state);
|
|
|
|
|
|
|
|
expect(selectedProjectGetterObject.name).toBe('1');
|
|
|
|
expect(selectedProjectGetterObject.id).toBe('1');
|
|
|
|
expect(selectedProjectGetterObject.description).toBe('1');
|
|
|
|
expect(selectedProjectGetterObject.createdAt).toBe('1');
|
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
});
|