Skip to content

Commit 62cfc50

Browse files
authored
Merge pull request #373 from ioet/340-enable-projects
fix: #340 after customer creation enabling projects and project types tabs
2 parents 41fcc64 + d6a8e2e commit 62cfc50

File tree

11 files changed

+97
-12
lines changed

11 files changed

+97
-12
lines changed

src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.spec.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { LoadCustomerProjects, CleanCustomerProjects } from './../../../projects/components/store/project.actions';
2+
import { LoadProjectTypes, CleanProjectTypes } from './../../../projects-type/store/project-type.actions';
13
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
24
import { FormBuilder } from '@angular/forms';
35
import { MockStore, provideMockStore } from '@ngrx/store/testing';
@@ -108,12 +110,34 @@ describe('CreateCustomerComponent', () => {
108110
expect(component.areTabsActive).toBeTruthy();
109111
});
110112

111-
it('set data to update ', () => {
113+
it('loads projects and projectTypes when customerData is not null', () => {
112114
spyOn(store, 'dispatch');
113115

114116
component.ngOnInit();
115117
component.setDataToUpdate(customerData);
116118

117-
expect(store.dispatch).toHaveBeenCalledTimes(2);
119+
expect(store.dispatch).toHaveBeenCalledWith(new LoadProjectTypes(customerData.id));
120+
expect(store.dispatch).toHaveBeenCalledWith(new LoadCustomerProjects(customerData.id));
118121
});
122+
123+
it('cleans projects and projectTypes when customerData is null', () => {
124+
spyOn(store, 'dispatch');
125+
126+
component.ngOnInit();
127+
component.setDataToUpdate(null);
128+
129+
expect(store.dispatch).toHaveBeenCalledWith(new CleanProjectTypes());
130+
expect(store.dispatch).toHaveBeenCalledWith(new CleanCustomerProjects());
131+
});
132+
133+
it('sets areTabsActive to false and emit its value on markTabsAsInactive', () => {
134+
component.areTabsActive = true;
135+
spyOn(component.changeValueAreTabsActives, 'emit');
136+
137+
component.markTabsAsInactive();
138+
139+
expect(component.areTabsActive).toBe(false);
140+
expect(component.changeValueAreTabsActives.emit).toHaveBeenCalledWith(component.areTabsActive);
141+
});
142+
119143
});

src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { CleanProjectTypes } from './../../../projects-type/store/project-type.actions';
12
import { Component, Input, Output, EventEmitter, OnDestroy, OnInit } from '@angular/core';
23
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
34
import { Store, select } from '@ngrx/store';
@@ -12,7 +13,7 @@ import {
1213
ResetCustomerToEdit,
1314
} from 'src/app/modules/customer-management/store';
1415
import { LoadProjectTypes } from '../../../projects-type/store';
15-
import { LoadCustomerProjects } from '../../../projects/components/store/project.actions';
16+
import { LoadCustomerProjects, CleanCustomerProjects } from '../../../projects/components/store/project.actions';
1617

1718
@Component({
1819
selector: 'app-create-customer',
@@ -35,8 +36,7 @@ export class CreateCustomerComponent implements OnInit, OnDestroy {
3536
}
3637

3738
ngOnInit() {
38-
this.areTabsActive = true;
39-
this.changeValueAreTabsActives.emit(this.areTabsActive);
39+
this.markTabsAsInactive();
4040
const customers$ = this.store.pipe(select(getCustomerUnderEdition));
4141
this.editSubscription = customers$.subscribe((customer) => {
4242
this.customerToEdit = customer;
@@ -73,10 +73,18 @@ export class CreateCustomerComponent implements OnInit, OnDestroy {
7373
description: customerData.description,
7474
});
7575
} else {
76+
this.store.dispatch(new CleanProjectTypes());
77+
this.store.dispatch(new CleanCustomerProjects());
78+
this.markTabsAsInactive();
7679
this.customerForm.reset();
7780
}
7881
}
7982

83+
markTabsAsInactive() {
84+
this.areTabsActive = false;
85+
this.changeValueAreTabsActives.emit(this.areTabsActive);
86+
}
87+
8088
get name() {
8189
return this.customerForm.get('name');
8290
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import * as actions from './project-type.actions';
22

33
describe('LoadProjectTypesSuccess', () => {
4+
5+
it('CleanProjectTypes type is ProjectTypeActionTypes.CLEAN_PROJECT_TYPES', () => {
6+
const action = new actions.CleanProjectTypes();
7+
expect(action.type).toEqual(actions.ProjectTypeActionTypes.CLEAN_PROJECT_TYPES);
8+
});
9+
410
it('LoadProjectTypesSuccess type is ProjectTypeActionTypes.LOAD_PROJECT_TYPES_SUCCESS', () => {
511
const loadProjectTypesSuccess = new actions.LoadProjectTypesSuccess([]);
612
expect(loadProjectTypesSuccess.type).toEqual(actions.ProjectTypeActionTypes.LOAD_PROJECT_TYPES_SUCCESS);

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ export enum ProjectTypeActionTypes {
1818
SET_PROJECT_TYPE_ID_TO_EDIT = '[ProjectType] SET_PROJECT_TYPE_ID_TO_EDIT',
1919
RESET_PROJECT_TYPE_ID_TO_EDIT = '[ProjectType] RESET_PROJECT_TYPE_ID_TO_EDIT',
2020
DEFAULT_PROJECT_TYPE = '[ProjectType] DEFAULT_PROJECT_TYPE',
21+
CLEAN_PROJECT_TYPES = '[ProjectType] CLEAN_PROJECT_TYPES',
22+
}
23+
24+
export class CleanProjectTypes implements Action {
25+
public readonly type = ProjectTypeActionTypes.CLEAN_PROJECT_TYPES;
26+
constructor() {}
2127
}
2228

2329
export class LoadProjectTypes implements Action {
@@ -105,6 +111,7 @@ export class DefaultProjectTypes implements Action {
105111
}
106112

107113
export type ProjectTypeActions =
114+
| CleanProjectTypes
108115
| LoadProjectTypes
109116
| LoadProjectTypesSuccess
110117
| LoadProjectTypesFail

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ describe('projectTypeReducer', () => {
1212
expect(state.data).toEqual(initialState.data);
1313
});
1414

15+
it('on CLEAN_PROJECT_TYPES, data is cleared', () => {
16+
initialState.data = [projectType];
17+
const action = new actions.CleanProjectTypes();
18+
19+
const state = projectTypeReducer(initialState, action);
20+
21+
expect(state.data).toEqual([]);
22+
});
23+
1524
it('on LoadProjectTypes, isLoading is true', () => {
1625
const action = new actions.LoadProjectTypes();
1726

@@ -46,6 +55,7 @@ describe('projectTypeReducer', () => {
4655
});
4756

4857
it('on CreateProjectTypeSuccess, activitiesFound are saved in the store', () => {
58+
initialState.data = [];
4959
const action = new actions.CreateProjectTypeSuccess(projectType);
5060

5161
const state = projectTypeReducer(initialState, action);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ export const initialState: ProjectTypeState = {
1818
export const projectTypeReducer = (state: ProjectTypeState = initialState, action: ProjectTypeActions) => {
1919
const projectTypeList = [...state.data];
2020
switch (action.type) {
21+
case ProjectTypeActionTypes.CLEAN_PROJECT_TYPES: {
22+
return {
23+
...state,
24+
data: [],
25+
};
26+
}
2127
case ProjectTypeActionTypes.LOAD_PROJECT_TYPES: {
2228
return {
2329
...state,

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import * as actions from './project.actions';
22

33
describe('Actions for Projects', () => {
44

5+
it('CleanCustomerProjects type is ProjectActionTypes.CLEAN_CUSTOMER_PROJECTS', () => {
6+
const action = new actions.CleanCustomerProjects();
7+
expect(action.type).toEqual(actions.ProjectActionTypes.CLEAN_CUSTOMER_PROJECTS);
8+
});
9+
510
it('LoadProjectsSuccess type is ProjectActionTypes.LOAD_PROJECTS_SUCCESS', () => {
611
const action = new actions.LoadProjectsSuccess([]);
712
expect(action.type).toEqual(actions.ProjectActionTypes.LOAD_PROJECTS_SUCCESS);

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ export enum ProjectActionTypes {
1919
DELETE_PROJECT = '[Projects] DELETE_PROJECT',
2020
DELETE_PROJECT_SUCCESS = '[Projects] DELETE_PROJECT_SUCESS',
2121
DELETE_PROJECT_FAIL = '[Projects] DELETE_PROJECT_FAIL',
22+
CLEAN_CUSTOMER_PROJECTS = '[Projects] CLEAN_CUSTOMER_PROJECTS',
23+
}
24+
25+
export class CleanCustomerProjects implements Action {
26+
public readonly type = ProjectActionTypes.CLEAN_CUSTOMER_PROJECTS;
27+
constructor() {}
2228
}
2329

2430
export class LoadProjects implements Action {
@@ -117,6 +123,7 @@ export class DeleteProjectFail implements Action {
117123
}
118124

119125
export type ProjectActions =
126+
| CleanCustomerProjects
120127
| LoadProjects
121128
| LoadProjectsSuccess
122129
| LoadProjectsFail

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ describe('projectReducer', () => {
99
};
1010
const project: Project = { id: '1', name: 'aaa', description: 'bbb', project_type_id: '123' };
1111

12+
it('on CLEAN_CUSTOMER_PROJECTS, customerProjects is empty', () => {
13+
initialState.customerProjects = [project];
14+
const action = new actions.CleanCustomerProjects();
15+
16+
const state = projectReducer(initialState, action);
17+
18+
expect(state.customerProjects).toEqual([]);
19+
});
20+
1221
it('on LoadProjects, isLoading is true', () => {
1322
const action = new actions.LoadCustomerProjects('1');
1423
const state = projectReducer(initialState, action);
@@ -36,18 +45,20 @@ describe('projectReducer', () => {
3645
});
3746

3847
it('on CreateProjectSuccess, project is saved in the store', () => {
48+
initialState.customerProjects = [];
3949
const action = new actions.CreateProjectSuccess(project);
50+
4051
const state = projectReducer(initialState, action);
4152

4253
expect(state.customerProjects).toEqual([project]);
4354
expect(state.isLoading).toEqual(false);
4455
});
4556

46-
it('on CreateProjectFail, customerProjects equal []', () => {
57+
it('on CreateProjectFail, isLoading is false', () => {
4758
const action = new actions.CreateProjectFail('error');
59+
4860
const state = projectReducer(initialState, action);
4961

50-
expect(state.customerProjects).toEqual([]);
5162
expect(state.isLoading).toEqual(false);
5263
});
5364

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ export const projectReducer = (state: ProjectState = initialState, action: Proje
8787
return {
8888
...state,
8989
isLoading: false,
90-
message: 'Something went wrong creating projects!',
91-
projectToEdit: undefined,
9290
};
9391
}
9492

@@ -163,6 +161,12 @@ export const projectReducer = (state: ProjectState = initialState, action: Proje
163161
projectToEdit: undefined,
164162
};
165163
}
164+
case ProjectActionTypes.CLEAN_CUSTOMER_PROJECTS: {
165+
return {
166+
...state,
167+
customerProjects: [],
168+
};
169+
}
166170

167171
default:
168172
return state;

0 commit comments

Comments
 (0)