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-05-23 15:28:48 +01:00
|
|
|
import { vi } from 'vitest';
|
2023-04-13 12:41:03 +01:00
|
|
|
import { createPinia, setActivePinia } from 'pinia';
|
2019-09-09 11:33:39 +01:00
|
|
|
|
2023-08-21 11:55:38 +01:00
|
|
|
import { ProjectsHttpApi } from '@/api/projects';
|
2020-09-15 13:44:23 +01:00
|
|
|
import { Project, ProjectFields, ProjectLimits } from '@/types/projects';
|
2023-04-13 12:41:03 +01:00
|
|
|
import { useProjectsStore } from '@/store/modules/projectsStore';
|
2019-01-28 14:39:51 +00:00
|
|
|
|
2023-04-13 12:41:03 +01:00
|
|
|
const limits = new ProjectLimits(15, 12, 14, 13);
|
|
|
|
const project = new Project('11', 'name', 'descr', '23', 'testOwnerId');
|
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
|
|
|
|
2023-04-13 12:41:03 +01:00
|
|
|
describe('actions', () => {
|
2019-08-22 17:03:13 +01:00
|
|
|
beforeEach(() => {
|
2023-04-13 12:41:03 +01:00
|
|
|
setActivePinia(createPinia());
|
2023-05-23 15:28:48 +01:00
|
|
|
vi.resetAllMocks();
|
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', () => {
|
2023-04-13 12:41:03 +01:00
|
|
|
const store = useProjectsStore();
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2023-04-13 12:41:03 +01:00
|
|
|
store.state.projects = projects;
|
|
|
|
store.selectProject('11');
|
2019-01-08 15:14:00 +00:00
|
|
|
|
2023-04-13 12:41:03 +01:00
|
|
|
expect(store.state.selectedProject.id).toBe('11');
|
|
|
|
expect(store.state.currentLimits.bandwidthLimit).toBe(0);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
it('success fetch projects', async () => {
|
2023-04-13 12:41:03 +01:00
|
|
|
const store = useProjectsStore();
|
|
|
|
|
2023-08-21 11:55:38 +01:00
|
|
|
vi.spyOn(ProjectsHttpApi.prototype, '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
|
|
|
|
2023-04-13 12:41:03 +01:00
|
|
|
await store.getProjects();
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2023-05-16 13:09:46 +01:00
|
|
|
expect(store.state.projects).toStrictEqual(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 () => {
|
2023-04-13 12:41:03 +01:00
|
|
|
const store = useProjectsStore();
|
|
|
|
|
2023-08-21 11:55:38 +01:00
|
|
|
vi.spyOn(ProjectsHttpApi.prototype, 'get').mockImplementation(() => { throw new Error(); });
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2019-08-22 17:03:13 +01:00
|
|
|
try {
|
2023-04-13 12:41:03 +01:00
|
|
|
await store.getProjects();
|
2019-08-22 17:03:13 +01:00
|
|
|
} catch (error) {
|
2023-04-13 12:41:03 +01:00
|
|
|
expect(store.state.projects.length).toBe(0);
|
|
|
|
expect(store.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 () => {
|
2023-04-13 12:41:03 +01:00
|
|
|
const store = useProjectsStore();
|
2019-08-22 17:03:13 +01:00
|
|
|
|
2023-08-21 11:55:38 +01:00
|
|
|
vi.spyOn(ProjectsHttpApi.prototype, 'create').mockReturnValue(
|
2023-04-13 12:41:03 +01:00
|
|
|
Promise.resolve(project),
|
2019-02-20 14:04:55 +00:00
|
|
|
);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2023-04-13 12:41:03 +01:00
|
|
|
await store.createProject(new ProjectFields());
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2023-04-13 12:41:03 +01:00
|
|
|
expect(store.state.projects.length).toBe(1);
|
|
|
|
expect(store.state.currentLimits.bandwidthLimit).toBe(0);
|
2019-08-22 17:03:13 +01:00
|
|
|
});
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2023-04-13 12:41:03 +01:00
|
|
|
it('create throws an error when create api call fails', async () => {
|
|
|
|
const store = useProjectsStore();
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2023-08-21 11:55:38 +01:00
|
|
|
vi.spyOn(ProjectsHttpApi.prototype, 'create').mockImplementation(() => { throw new Error(); });
|
2019-08-22 17:03:13 +01:00
|
|
|
|
|
|
|
try {
|
2023-04-13 12:41:03 +01:00
|
|
|
await store.createProject(new ProjectFields());
|
2019-08-22 17:03:13 +01:00
|
|
|
expect(true).toBe(false);
|
|
|
|
} catch (error) {
|
2023-04-13 12:41:03 +01:00
|
|
|
expect(store.state.projects.length).toBe(0);
|
|
|
|
expect(store.state.currentLimits.bandwidthLimit).toBe(0);
|
2019-08-22 17:03:13 +01:00
|
|
|
}
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2020-09-10 10:32:35 +01:00
|
|
|
it('success update project name', async () => {
|
2023-04-13 12:41:03 +01:00
|
|
|
const store = useProjectsStore();
|
|
|
|
|
2023-08-21 11:55:38 +01:00
|
|
|
vi.spyOn(ProjectsHttpApi.prototype, 'update').mockReturnValue(
|
2020-09-10 10:32:35 +01:00
|
|
|
Promise.resolve(),
|
|
|
|
);
|
|
|
|
|
2023-04-13 12:41:03 +01:00
|
|
|
store.state.selectedProject = projects[0];
|
2020-09-10 10:32:35 +01:00
|
|
|
const newName = 'newName';
|
2023-04-13 12:41:03 +01:00
|
|
|
const fieldsToUpdate = new ProjectFields(newName, projects[0].description);
|
2020-09-10 10:32:35 +01:00
|
|
|
|
2023-04-13 12:41:03 +01:00
|
|
|
await store.updateProjectName(fieldsToUpdate);
|
2020-09-10 10:32:35 +01:00
|
|
|
|
2023-04-13 12:41:03 +01:00
|
|
|
expect(store.state.selectedProject.name).toBe(newName);
|
2020-09-10 10:32:35 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
it('success update project description', async () => {
|
2023-04-13 12:41:03 +01:00
|
|
|
const store = useProjectsStore();
|
|
|
|
|
2023-08-21 11:55:38 +01:00
|
|
|
vi.spyOn(ProjectsHttpApi.prototype, 'update').mockReturnValue(
|
2019-09-13 15:58:18 +01:00
|
|
|
Promise.resolve(),
|
2019-08-22 17:03:13 +01:00
|
|
|
);
|
|
|
|
|
2023-04-13 12:41:03 +01:00
|
|
|
store.state.selectedProject = projects[0];
|
2019-08-22 17:03:13 +01:00
|
|
|
const newDescription = 'newDescription1';
|
2023-04-13 12:41:03 +01:00
|
|
|
const fieldsToUpdate = new ProjectFields(projects[0].name, newDescription);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2023-04-13 12:41:03 +01:00
|
|
|
await store.updateProjectDescription(fieldsToUpdate);
|
2019-08-22 17:03:13 +01:00
|
|
|
|
2023-04-13 12:41:03 +01:00
|
|
|
expect(store.state.selectedProject.description).toBe(newDescription);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2019-12-12 16:25:38 +00:00
|
|
|
it('success get project limits', async () => {
|
2023-04-13 12:41:03 +01:00
|
|
|
const store = useProjectsStore();
|
|
|
|
|
2023-08-21 11:55:38 +01:00
|
|
|
vi.spyOn(ProjectsHttpApi.prototype, 'getLimits').mockReturnValue(
|
2019-12-12 16:25:38 +00:00
|
|
|
Promise.resolve(limits),
|
|
|
|
);
|
|
|
|
|
2023-04-13 12:41:03 +01:00
|
|
|
store.state.projects = projects;
|
2019-12-12 16:25:38 +00:00
|
|
|
|
2023-04-13 12:41:03 +01:00
|
|
|
await store.getProjectLimits(store.state.selectedProject.id);
|
2019-02-20 13:33:56 +00:00
|
|
|
|
2023-04-13 12:41:03 +01:00
|
|
|
expect(store.state.currentLimits.bandwidthUsed).toBe(12);
|
|
|
|
expect(store.state.currentLimits.bandwidthLimit).toBe(15);
|
|
|
|
expect(store.state.currentLimits.storageUsed).toBe(13);
|
|
|
|
expect(store.state.currentLimits.storageLimit).toBe(14);
|
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(() => {
|
2023-04-13 12:41:03 +01:00
|
|
|
setActivePinia(createPinia());
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
|
|
|
|
2019-10-22 12:12:49 +01:00
|
|
|
it('projects array', () => {
|
2023-04-13 12:41:03 +01:00
|
|
|
const store = useProjectsStore();
|
|
|
|
|
|
|
|
store.state.projects = projects;
|
2019-08-22 17:03:13 +01:00
|
|
|
|
2023-04-13 12:41:03 +01:00
|
|
|
const allProjects = store.projects;
|
2019-08-22 17:03:13 +01:00
|
|
|
|
|
|
|
expect(allProjects.length).toEqual(2);
|
2019-02-20 13:33:56 +00:00
|
|
|
});
|
2019-01-08 15:14:00 +00:00
|
|
|
});
|