Skip to content

Commit 66b1c99

Browse files
committed
test: TT-218-Dont-allow-deleting-projects
1 parent e50d0a2 commit 66b1c99

File tree

6 files changed

+158
-22
lines changed

6 files changed

+158
-22
lines changed

src/app/modules/customer-management/components/projects/components/project-list/project-list.component.spec.ts

Lines changed: 65 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { ProjectState } from '../store/project.reducer';
99
import { getCustomerProjects } from '../store/project.selectors';
1010
import { SetProjectToEdit, DeleteProject } from '../store/project.actions';
1111
import { FilterProjectPipe } from '../../../../../shared/pipes';
12+
import { ProjectUI } from 'src/app/modules/shared/models';
1213

1314
describe('ProjectListComponent', () => {
1415
let component: ProjectListComponent;
@@ -17,18 +18,34 @@ describe('ProjectListComponent', () => {
1718
let getCustomerProjectsSelectorMock;
1819
let allCustomerProjectsSelectorMock;
1920

21+
const project = { id: '123', name: 'aaa', description: 'xxx', project_type_id: '1234', status: 'inactive' };
22+
2023
const state: ProjectState = {
21-
projects: [],
22-
customerProjects: [],
24+
projects: [project],
25+
customerProjects: [project],
2326
isLoading: false,
2427
message: '',
2528
projectToEdit: undefined,
2629
};
2730

28-
const project = { id: '123', name: 'aaa', description: 'xxx', project_type_id: '1234' };
29-
30-
beforeEach(
31-
() => {
31+
const btnProps = [
32+
{
33+
key: 'active',
34+
_status: false,
35+
btnColor: 'btn-danger',
36+
btnIcon: 'fa-arrow-circle-down',
37+
btnName: 'Archive',
38+
},
39+
{
40+
key: 'inactive',
41+
_status: true,
42+
btnColor: 'btn-primary',
43+
btnIcon: 'fa-arrow-circle-up',
44+
btnName: 'Active',
45+
},
46+
];
47+
48+
beforeEach(() => {
3249
TestBed.configureTestingModule({
3350
imports: [NgxPaginationModule],
3451
declarations: [ProjectListComponent, FilterProjectPipe],
@@ -58,7 +75,11 @@ describe('ProjectListComponent', () => {
5875
it('loads projects from state onInit', () => {
5976
component.ngOnInit();
6077

61-
expect(component.projects).toBe(state.customerProjects);
78+
const StateWithBtnProperties = state.customerProjects.map((projectfilter: ProjectUI) => {
79+
const addProps = btnProps.find((prop) => prop.key === component.setActive(projectfilter.status));
80+
return { ...projectfilter, ...addProps };
81+
});
82+
expect(component.projects).toEqual(StateWithBtnProperties);
6283
});
6384

6485
it('should destroy the subscriptions', () => {
@@ -96,4 +117,41 @@ describe('ProjectListComponent', () => {
96117
expect(store.dispatch).toHaveBeenCalledTimes(1);
97118
expect(store.dispatch).toHaveBeenCalledWith(new DeleteProject(project.id));
98119
});
120+
121+
it('switchStatus should call openModal() on item.status = activate', () => {
122+
const itemData = {
123+
id: '123',
124+
name: 'aaa',
125+
description: 'xxx',
126+
project_type_id: '1234',
127+
status: 'activate',
128+
key: 'activate',
129+
_status: false,
130+
btnColor: 'btn-danger',
131+
btnIcon: 'fa-arrow-circle-down',
132+
btnName: 'Archive',
133+
};
134+
135+
spyOn(component, 'openModal');
136+
component.switchStatus(itemData);
137+
expect(component.openModal).toHaveBeenCalled();
138+
});
139+
140+
it('switchStatus should set showModal false when item.status = inactive', () => {
141+
const itemData = {
142+
id: '123',
143+
name: 'aaa',
144+
description: 'xxx',
145+
project_type_id: '1234',
146+
status: 'inactive',
147+
key: 'inactive',
148+
_status: true,
149+
btnColor: 'btn-primary',
150+
btnIcon: 'fa-arrow-circle-up',
151+
btnName: 'Active',
152+
};
153+
154+
component.switchStatus(itemData);
155+
expect(component.showModal).toBeFalse();
156+
});
99157
});

src/app/modules/customer-management/components/projects/components/store/project.actions.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,22 @@ describe('Actions for Projects', () => {
8181
const deleteProjectFail = new actions.DeleteProjectFail('error');
8282
expect(deleteProjectFail.type).toEqual(actions.ProjectActionTypes.DELETE_PROJECT_FAIL);
8383
});
84+
85+
it('UnarchiveProject type is ProjectActionTypes.UNARCHIVE_PROJECT', () => {
86+
const unarchiveProject = new actions.UnarchiveProject('id');
87+
expect(unarchiveProject.type).toEqual(actions.ProjectActionTypes.UNARCHIVE_PROJECT);
88+
});
89+
90+
it('UnarchiveProjectSuccess type is ProjectActionTypes.UNARCHIVE_PROJECT_SUCCESS', () => {
91+
const unarchiveProjecttSuccess = new actions.UnarchiveProjectSuccess({
92+
id: 'id_test',
93+
status: 'active',
94+
});
95+
expect(unarchiveProjecttSuccess.type).toEqual(actions.ProjectActionTypes.UNARCHIVE_PROJECT_SUCCESS);
96+
});
97+
98+
it('UnarchiveProjectProjectFail type is ProjectActionTypes.UNARCHIVE_PROJECT_FAIL', () => {
99+
const unarchiveProjecttFail = new actions.UnarchiveProjectFail('error');
100+
expect(unarchiveProjecttFail.type).toEqual(actions.ProjectActionTypes.UNARCHIVE_PROJECT_FAIL);
101+
});
84102
});

src/app/modules/customer-management/components/projects/components/store/project.effects.spec.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ describe('ProjectEffects', () => {
1515
let effects: ProjectEffects;
1616
let service: ProjectService;
1717
let toastrService;
18-
const project: Project = { id: 'id', name: 'name', description: 'descrition' };
18+
const project: Project = { id: 'id', name: 'name', description: 'descrition', status: 'inactive' };
1919
const projects: Project[] = [];
2020

2121
beforeEach(() => {
@@ -146,4 +146,27 @@ describe('ProjectEffects', () => {
146146
expect(action.type).toEqual(ProjectActionTypes.LOAD_CUSTOMER_PROJECTS_FAIL);
147147
});
148148
});
149+
150+
it('action type is UNARCHIVE_PROJECT_SUCCESS when service is executed sucessfully', async () => {
151+
const projectId = 'projectId';
152+
actions$ = of({ type: ProjectActionTypes.UNARCHIVE_PROJECT, projectId });
153+
spyOn(toastrService, 'success');
154+
spyOn(service, 'updateProject').and.returnValue(of(project));
155+
156+
effects.unarchiveProject$.subscribe((action) => {
157+
expect(toastrService.success).toHaveBeenCalledWith(INFO_SAVED_SUCCESSFULLY);
158+
expect(action.type).toEqual(ProjectActionTypes.UNARCHIVE_PROJECT_SUCCESS);
159+
});
160+
});
161+
162+
it('action type is UNARCHIVE_PROJECT_FAIL when service fail in execution', async () => {
163+
actions$ = of({ type: ProjectActionTypes.UNARCHIVE_PROJECT, project });
164+
spyOn(toastrService, 'error');
165+
spyOn(service, 'updateProject').and.returnValue(throwError({ error: { message: 'fail!' } }));
166+
167+
effects.unarchiveProject$.subscribe((action) => {
168+
expect(toastrService.error).toHaveBeenCalled();
169+
expect(action.type).toEqual(ProjectActionTypes.UNARCHIVE_PROJECT_FAIL);
170+
});
171+
});
149172
});

src/app/modules/customer-management/components/projects/components/store/project.effects.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,11 @@ export class ProjectEffects {
114114
this.projectService.updateProject(project).pipe(
115115
map((projectData) => {
116116
this.toastrService.success(INFO_SAVED_SUCCESSFULLY);
117-
return new actions.UpdateProjectSuccess(projectData);
117+
return new actions.UnarchiveProjectSuccess(projectData);
118118
}),
119119
catchError((error) => {
120120
this.toastrService.error(error.error.message);
121-
return of(new actions.UpdateProjectFail(error));
121+
return of(new actions.UnarchiveProjectFail(error));
122122
})
123123
)
124124
)

src/app/modules/customer-management/components/projects/components/store/project.reducer.spec.ts

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
import { Project } from '../../../../../shared/models';
1+
import { Project, Status } from '../../../../../shared/models';
22
import * as actions from './project.actions';
33
import { projectReducer, ProjectState } from './project.reducer';
44

55
describe('projectReducer', () => {
66
const initialState: ProjectState = {
7-
projects: [{ id: 'id', name: 'name', project_type_id: '' }],
7+
projects: [{ id: 'id', name: 'name', project_type_id: '', status: 'inactive' }],
88
customerProjects: [], isLoading: false, message: '', projectToEdit: undefined
99
};
10-
const project: Project = { id: '1', name: 'aaa', description: 'bbb', project_type_id: '123' };
10+
const archivedProject: Project = { id: '1', name: 'aaa', description: 'bbb', project_type_id: '123', status: 'inactive' };
11+
const project: Project = { id: '1', name: 'aaa', description: 'bbb', project_type_id: '123', status: 'active' };
1112

1213
it('on CLEAN_CUSTOMER_PROJECTS, customerProjects is empty', () => {
1314
initialState.customerProjects = [project];
@@ -116,10 +117,10 @@ describe('projectReducer', () => {
116117

117118
const state = projectReducer(initialState, action);
118119
expect(state.isLoading).toEqual(true);
119-
expect(state.message).toEqual('Loading delete project');
120+
expect(state.message).toEqual('Loading archive project');
120121
});
121122

122-
it('on DeleteProjectSuccess, message equal to Project removed successfully!', () => {
123+
it('on DeleteProjectSuccess, message equal to Project archived successfully!', () => {
123124
const currentState: ProjectState = {
124125
projects: [project],
125126
customerProjects: [project],
@@ -131,17 +132,51 @@ describe('projectReducer', () => {
131132
const action = new actions.DeleteProjectSuccess(projectIdToDelete);
132133

133134
const state = projectReducer(currentState, action);
134-
expect(state.customerProjects).toEqual([]);
135-
expect(state.message).toEqual('Project removed successfully!');
135+
expect(state.customerProjects).toEqual([archivedProject]);
136+
expect(state.message).toEqual('Project archived successfully!');
136137
});
137138

138-
it('on DeleteProjectFail, message equal to Something went wrong deleting the project!', () => {
139+
it('on DeleteProjectFail, message equal to Something went wrong archiving the project!', () => {
139140
const projectToEdit = '1';
140141
const action = new actions.DeleteProjectFail(projectToEdit);
141142

142143
const state = projectReducer(initialState, action);
143144
expect(state.customerProjects).toEqual([]);
144145
expect(state.isLoading).toEqual(false);
145-
expect(state.message).toEqual('Something went wrong deleting the project!');
146+
expect(state.message).toEqual('Something went wrong archiving the project!');
147+
});
148+
149+
it('on UnarchiveProject, isLoading is true', () => {
150+
const action = new actions.UnarchiveProject('1');
151+
const state = projectReducer(initialState, action);
152+
153+
expect(state.isLoading).toEqual(true);
154+
expect(state.message).toEqual('Loading unarchive project');
155+
});
156+
157+
it('on UnarchiveProjectSuccess, Project status is change to "active" in the store', () => {
158+
const currentState: ProjectState = {
159+
projects: [project],
160+
customerProjects: [archivedProject],
161+
isLoading: false,
162+
message: '',
163+
projectToEdit: project,
164+
};
165+
const projectEdited: Status = { id: '1', status: 'active' };
166+
167+
const action = new actions.UnarchiveProjectSuccess(projectEdited);
168+
const state = projectReducer(currentState, action);
169+
170+
expect(state.customerProjects).toEqual([project]);
171+
expect(state.isLoading).toEqual(false);
172+
});
173+
174+
it('on UnarchiveProjectFail, message equal to Something went wrong unarchiving projects!', () => {
175+
const action = new actions.UnarchiveProjectFail('error');
176+
const state = projectReducer(initialState, action);
177+
178+
expect(state.customerProjects).toEqual(state.customerProjects);
179+
expect(state.message).toEqual('Something went wrong unarchiving projects!');
180+
expect(state.isLoading).toEqual(false);
146181
});
147182
});

src/app/modules/customer-management/components/projects/components/store/project.selectors.spec.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ describe('ProjectSelectors', () => {
1515

1616
it('should select getProjects', () => {
1717
const project = [
18-
{ id: 'id', name: 'abc', description: 'xxx' },
19-
{ id: 'id', name: 'abc', description: 'xxx' },
18+
{ id: '1', name: 'abc', description: 'xxx', status: 'inactive' },
19+
{ id: '2', name: 'abc', description: 'xxx', status: 'active' },
20+
{ id: '3', name: 'abc', description: 'xxx', status: 'inactive' },
2021
];
2122
const projectState = { projects: project };
23+
const filteredProjects = project.filter((item) => item.status === 'active');
2224

23-
expect(selectors.getProjects.projector(projectState)).toBe(project);
25+
expect(selectors.getProjects.projector(projectState)).toEqual(filteredProjects);
2426
});
2527

2628
it('should select getProjectsToEdit', () => {

0 commit comments

Comments
 (0)