Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: TT-218-Dont-allow-deleting-projects
  • Loading branch information
jeffqev committed May 4, 2021
commit 66b1c996c6bec8b14d682c5afed803e89fce7ba0
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ProjectState } from '../store/project.reducer';
import { getCustomerProjects } from '../store/project.selectors';
import { SetProjectToEdit, DeleteProject } from '../store/project.actions';
import { FilterProjectPipe } from '../../../../../shared/pipes';
import { ProjectUI } from 'src/app/modules/shared/models';

describe('ProjectListComponent', () => {
let component: ProjectListComponent;
Expand All @@ -17,18 +18,34 @@ describe('ProjectListComponent', () => {
let getCustomerProjectsSelectorMock;
let allCustomerProjectsSelectorMock;

const project = { id: '123', name: 'aaa', description: 'xxx', project_type_id: '1234', status: 'inactive' };

const state: ProjectState = {
projects: [],
customerProjects: [],
projects: [project],
customerProjects: [project],
isLoading: false,
message: '',
projectToEdit: undefined,
};

const project = { id: '123', name: 'aaa', description: 'xxx', project_type_id: '1234' };

beforeEach(
() => {
const btnProps = [
{
key: 'active',
_status: false,
btnColor: 'btn-danger',
btnIcon: 'fa-arrow-circle-down',
btnName: 'Archive',
},
{
key: 'inactive',
_status: true,
btnColor: 'btn-primary',
btnIcon: 'fa-arrow-circle-up',
btnName: 'Active',
},
];

beforeEach(() => {
TestBed.configureTestingModule({
imports: [NgxPaginationModule],
declarations: [ProjectListComponent, FilterProjectPipe],
Expand Down Expand Up @@ -58,7 +75,11 @@ describe('ProjectListComponent', () => {
it('loads projects from state onInit', () => {
component.ngOnInit();

expect(component.projects).toBe(state.customerProjects);
const StateWithBtnProperties = state.customerProjects.map((projectfilter: ProjectUI) => {
const addProps = btnProps.find((prop) => prop.key === component.setActive(projectfilter.status));
return { ...projectfilter, ...addProps };
});
expect(component.projects).toEqual(StateWithBtnProperties);
});

it('should destroy the subscriptions', () => {
Expand Down Expand Up @@ -96,4 +117,41 @@ describe('ProjectListComponent', () => {
expect(store.dispatch).toHaveBeenCalledTimes(1);
expect(store.dispatch).toHaveBeenCalledWith(new DeleteProject(project.id));
});

it('switchStatus should call openModal() on item.status = activate', () => {
const itemData = {
id: '123',
name: 'aaa',
description: 'xxx',
project_type_id: '1234',
status: 'activate',
key: 'activate',
_status: false,
btnColor: 'btn-danger',
btnIcon: 'fa-arrow-circle-down',
btnName: 'Archive',
};

spyOn(component, 'openModal');
component.switchStatus(itemData);
expect(component.openModal).toHaveBeenCalled();
});

it('switchStatus should set showModal false when item.status = inactive', () => {
const itemData = {
id: '123',
name: 'aaa',
description: 'xxx',
project_type_id: '1234',
status: 'inactive',
key: 'inactive',
_status: true,
btnColor: 'btn-primary',
btnIcon: 'fa-arrow-circle-up',
btnName: 'Active',
};

component.switchStatus(itemData);
expect(component.showModal).toBeFalse();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,22 @@ describe('Actions for Projects', () => {
const deleteProjectFail = new actions.DeleteProjectFail('error');
expect(deleteProjectFail.type).toEqual(actions.ProjectActionTypes.DELETE_PROJECT_FAIL);
});

it('UnarchiveProject type is ProjectActionTypes.UNARCHIVE_PROJECT', () => {
const unarchiveProject = new actions.UnarchiveProject('id');
expect(unarchiveProject.type).toEqual(actions.ProjectActionTypes.UNARCHIVE_PROJECT);
});

it('UnarchiveProjectSuccess type is ProjectActionTypes.UNARCHIVE_PROJECT_SUCCESS', () => {
const unarchiveProjecttSuccess = new actions.UnarchiveProjectSuccess({
id: 'id_test',
status: 'active',
});
expect(unarchiveProjecttSuccess.type).toEqual(actions.ProjectActionTypes.UNARCHIVE_PROJECT_SUCCESS);
});

it('UnarchiveProjectProjectFail type is ProjectActionTypes.UNARCHIVE_PROJECT_FAIL', () => {
const unarchiveProjecttFail = new actions.UnarchiveProjectFail('error');
expect(unarchiveProjecttFail.type).toEqual(actions.ProjectActionTypes.UNARCHIVE_PROJECT_FAIL);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('ProjectEffects', () => {
let effects: ProjectEffects;
let service: ProjectService;
let toastrService;
const project: Project = { id: 'id', name: 'name', description: 'descrition' };
const project: Project = { id: 'id', name: 'name', description: 'descrition', status: 'inactive' };
const projects: Project[] = [];

beforeEach(() => {
Expand Down Expand Up @@ -146,4 +146,27 @@ describe('ProjectEffects', () => {
expect(action.type).toEqual(ProjectActionTypes.LOAD_CUSTOMER_PROJECTS_FAIL);
});
});

it('action type is UNARCHIVE_PROJECT_SUCCESS when service is executed sucessfully', async () => {
const projectId = 'projectId';
actions$ = of({ type: ProjectActionTypes.UNARCHIVE_PROJECT, projectId });
spyOn(toastrService, 'success');
spyOn(service, 'updateProject').and.returnValue(of(project));

effects.unarchiveProject$.subscribe((action) => {
expect(toastrService.success).toHaveBeenCalledWith(INFO_SAVED_SUCCESSFULLY);
expect(action.type).toEqual(ProjectActionTypes.UNARCHIVE_PROJECT_SUCCESS);
});
});

it('action type is UNARCHIVE_PROJECT_FAIL when service fail in execution', async () => {
actions$ = of({ type: ProjectActionTypes.UNARCHIVE_PROJECT, project });
spyOn(toastrService, 'error');
spyOn(service, 'updateProject').and.returnValue(throwError({ error: { message: 'fail!' } }));

effects.unarchiveProject$.subscribe((action) => {
expect(toastrService.error).toHaveBeenCalled();
expect(action.type).toEqual(ProjectActionTypes.UNARCHIVE_PROJECT_FAIL);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ export class ProjectEffects {
this.projectService.updateProject(project).pipe(
map((projectData) => {
this.toastrService.success(INFO_SAVED_SUCCESSFULLY);
return new actions.UpdateProjectSuccess(projectData);
return new actions.UnarchiveProjectSuccess(projectData);
}),
catchError((error) => {
this.toastrService.error(error.error.message);
return of(new actions.UpdateProjectFail(error));
return of(new actions.UnarchiveProjectFail(error));
})
)
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { Project } from '../../../../../shared/models';
import { Project, Status } from '../../../../../shared/models';
import * as actions from './project.actions';
import { projectReducer, ProjectState } from './project.reducer';

describe('projectReducer', () => {
const initialState: ProjectState = {
projects: [{ id: 'id', name: 'name', project_type_id: '' }],
projects: [{ id: 'id', name: 'name', project_type_id: '', status: 'inactive' }],
customerProjects: [], isLoading: false, message: '', projectToEdit: undefined
};
const project: Project = { id: '1', name: 'aaa', description: 'bbb', project_type_id: '123' };
const archivedProject: Project = { id: '1', name: 'aaa', description: 'bbb', project_type_id: '123', status: 'inactive' };
const project: Project = { id: '1', name: 'aaa', description: 'bbb', project_type_id: '123', status: 'active' };

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

const state = projectReducer(initialState, action);
expect(state.isLoading).toEqual(true);
expect(state.message).toEqual('Loading delete project');
expect(state.message).toEqual('Loading archive project');
});

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

const state = projectReducer(currentState, action);
expect(state.customerProjects).toEqual([]);
expect(state.message).toEqual('Project removed successfully!');
expect(state.customerProjects).toEqual([archivedProject]);
expect(state.message).toEqual('Project archived successfully!');
});

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

const state = projectReducer(initialState, action);
expect(state.customerProjects).toEqual([]);
expect(state.isLoading).toEqual(false);
expect(state.message).toEqual('Something went wrong deleting the project!');
expect(state.message).toEqual('Something went wrong archiving the project!');
});

it('on UnarchiveProject, isLoading is true', () => {
const action = new actions.UnarchiveProject('1');
const state = projectReducer(initialState, action);

expect(state.isLoading).toEqual(true);
expect(state.message).toEqual('Loading unarchive project');
});

it('on UnarchiveProjectSuccess, Project status is change to "active" in the store', () => {
const currentState: ProjectState = {
projects: [project],
customerProjects: [archivedProject],
isLoading: false,
message: '',
projectToEdit: project,
};
const projectEdited: Status = { id: '1', status: 'active' };

const action = new actions.UnarchiveProjectSuccess(projectEdited);
const state = projectReducer(currentState, action);

expect(state.customerProjects).toEqual([project]);
expect(state.isLoading).toEqual(false);
});

it('on UnarchiveProjectFail, message equal to Something went wrong unarchiving projects!', () => {
const action = new actions.UnarchiveProjectFail('error');
const state = projectReducer(initialState, action);

expect(state.customerProjects).toEqual(state.customerProjects);
expect(state.message).toEqual('Something went wrong unarchiving projects!');
expect(state.isLoading).toEqual(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ describe('ProjectSelectors', () => {

it('should select getProjects', () => {
const project = [
{ id: 'id', name: 'abc', description: 'xxx' },
{ id: 'id', name: 'abc', description: 'xxx' },
{ id: '1', name: 'abc', description: 'xxx', status: 'inactive' },
{ id: '2', name: 'abc', description: 'xxx', status: 'active' },
{ id: '3', name: 'abc', description: 'xxx', status: 'inactive' },
];
const projectState = { projects: project };
const filteredProjects = project.filter((item) => item.status === 'active');

expect(selectors.getProjects.projector(projectState)).toBe(project);
expect(selectors.getProjects.projector(projectState)).toEqual(filteredProjects);
});

it('should select getProjectsToEdit', () => {
Expand Down