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
#64 Fix comments
  • Loading branch information
jorgecod committed Apr 7, 2020
commit e60d8df65e40b972702d94adf6a8e5dae040ab48
9 changes: 5 additions & 4 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,12 @@ import { environment } from '../environments/environment';
StoreModule.forRoot(reducers, {
metaReducers,
}),
!environment.production ? StoreDevtoolsModule.instrument() : [],
!environment.production
? StoreDevtoolsModule.instrument({
maxAge: 15, // Retains last 15 states
})
: [],
EffectsModule.forRoot([ProjectEffects, ActivityEffects]),
StoreDevtoolsModule.instrument({
maxAge: 15, // Retains last 15 states
}),
],
providers: [],
bootstrap: [AppComponent],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { provideMockStore, MockStore } from '@ngrx/store/testing';
import { FormsModule, ReactiveFormsModule, FormBuilder } from '@angular/forms';

import { AppState } from '../../store/project.reducer';
import { ProjectState } from '../../store/project.reducer';
import { CreateProjectComponent } from './create-project.component';
import * as actions from '../../store/project.actions';

describe('CreateProjectComponent', () => {
let component: CreateProjectComponent;
let fixture: ComponentFixture<CreateProjectComponent>;
let store: MockStore<AppState>;
let store: MockStore<ProjectState>;

const state = {
projectList: [{ id: 'id', name: 'name', description: 'description' }],
Expand Down Expand Up @@ -47,7 +47,7 @@ describe('CreateProjectComponent', () => {
component.ngOnChanges();
expect(component.projectForm.value.name).toEqual(newData.name);
expect(component.projectForm.value.description).toEqual(newData.description);
expect(component.isEdit).toEqual(true);
expect(component.isUpdating).toEqual(true);
});

it('should emit ngOnChange and reset ProjectForm', () => {
Expand All @@ -56,33 +56,33 @@ describe('CreateProjectComponent', () => {

expect(component.projectForm.value.name).toEqual(null);
expect(component.projectForm.value.description).toEqual(null);
expect(component.isEdit).toEqual(false);
expect(component.isUpdating).toEqual(false);
});

it('should dispatch PostProject action #onSubmit', () => {
it('should dispatch CreateProject action #onSubmit', () => {
const project = {
id: '1',
name: 'app 4',
description: 'It is a good app',
};

component.isEdit = false;
component.isUpdating = false;
spyOn(store, 'dispatch');
component.onSubmit(project);
expect(store.dispatch).toHaveBeenCalledWith(new actions.PostProject(project));
expect(store.dispatch).toHaveBeenCalledWith(new actions.CreateProject(project));
});

it('should dispatch PutProject action #onSubmit', () => {
it('should dispatch UpdateProject action #onSubmit', () => {
const project = {
id: '1',
name: 'app 4',
description: 'It is a good app',
};

component.isEdit = true;
component.isUpdating = true;
spyOn(store, 'dispatch');
component.onSubmit(project);
expect(store.dispatch).toHaveBeenCalledWith(new actions.PutProject(project));
expect(store.dispatch).toHaveBeenCalledWith(new actions.UpdateProject(project));
});

it('should clean the form and send a cancelForm event #reset', () => {
Expand All @@ -103,7 +103,7 @@ describe('CreateProjectComponent', () => {
expect(component.projectForm.valid).toBeFalsy();
});

it('name field validity', () => {
it('checks if name field is valid', () => {
const name = component.projectForm.controls.name;
expect(name.valid).toBeFalsy();

Expand All @@ -114,7 +114,7 @@ describe('CreateProjectComponent', () => {
expect(name.hasError('required')).toBeFalsy();
});

it('description field validity', () => {
it('checks if description field is valid', () => {
const details = component.projectForm.controls.description;
expect(details.valid).toBeFalsy();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Component, Input, OnChanges, OnInit, Output, EventEmitter } from '@angu
import { FormBuilder, Validators } from '@angular/forms';
import { Store } from '@ngrx/store';
import { Project } from '../../../shared/models';
import { ProjectState } from '../../store/project.reducer';
import * as actions from '../../store/project.actions';

@Component({
Expand All @@ -12,12 +13,12 @@ import * as actions from '../../store/project.actions';
export class CreateProjectComponent implements OnChanges, OnInit {
projectForm;
editProjectId;
isEdit = false;
isUpdating = false;

@Input() projectToEdit: Project;
@Output() cancelForm = new EventEmitter();

constructor(private formBuilder: FormBuilder, private store: Store<any>) {
constructor(private formBuilder: FormBuilder, private store: Store<ProjectState>) {
this.projectForm = this.formBuilder.group({
name: ['', Validators.required],
description: ['', Validators.required],
Expand All @@ -29,10 +30,10 @@ export class CreateProjectComponent implements OnChanges, OnInit {
ngOnChanges(): void {
if (this.projectToEdit) {
this.projectForm.patchValue(this.projectToEdit);
this.isEdit = true;
this.isUpdating = true;
} else {
this.projectForm.reset();
this.isEdit = false;
this.isUpdating = false;
}
}

Expand All @@ -46,10 +47,10 @@ export class CreateProjectComponent implements OnChanges, OnInit {

onSubmit(formData) {
const projectData = { ...this.projectToEdit, ...formData };
if (!this.isEdit) {
this.store.dispatch(new actions.PostProject(projectData));
if (this.isUpdating) {
this.store.dispatch(new actions.UpdateProject(projectData));
} else {
this.store.dispatch(new actions.PutProject(projectData));
this.store.dispatch(new actions.CreateProject(projectData));
}
this.reset();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { provideMockStore, MockStore } from '@ngrx/store/testing';

import { ProjectListComponent } from './project-list.component';
import { AppState } from '../../store/project.reducer';
import { ProjectState } from '../../store/project.reducer';
import { allProjects } from '../../store/project.selectors';
import { FilterProjectPipe } from '../../../shared/pipes';

describe('ProjectListComponent', () => {
let component: ProjectListComponent;
let fixture: ComponentFixture<ProjectListComponent>;
let store: MockStore<AppState>;
let store: MockStore<ProjectState>;
let mockProjectsSelector;

const state = {
Expand Down
24 changes: 12 additions & 12 deletions src/app/modules/project-management/store/project.actions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,31 @@ describe('Actions for Projects', () => {
expect(getProjectsFail.type).toEqual(actions.ProjectActionTypes.GET_PROJECTS_FAIL);
});

it('PostProjectSuccess type is ProjectActionTypes.POST_PROJECT_SUCCESS', () => {
const postProjectSuccess = new actions.PostProjectSuccess({
it('CreateProjectSuccess type is ProjectActionTypes.CREATE_PROJECT_SUCCESS', () => {
const createProjectSuccess = new actions.CreateProjectSuccess({
id: '1',
name: 'Training',
description: 'It is good for learning',
});
expect(postProjectSuccess.type).toEqual(actions.ProjectActionTypes.POST_PROJECT_SUCCESS);
expect(createProjectSuccess.type).toEqual(actions.ProjectActionTypes.CREATE_PROJECT_SUCCESS);
});

it('PostProjectFail type is ProjectActionTypes.POST_PROJECT_FAIL', () => {
const postProjectFail = new actions.PostProjectFail('error');
expect(postProjectFail.type).toEqual(actions.ProjectActionTypes.POST_PROJECT_FAIL);
it('CreateProjectFail type is ProjectActionTypes.CREATE_PROJECT_FAIL', () => {
const createProjectFail = new actions.CreateProjectFail('error');
expect(createProjectFail.type).toEqual(actions.ProjectActionTypes.CREATE_PROJECT_FAIL);
});

it('PutProjectSuccess type is ProjectActionTypes.POST_PROJECT_SUCCESS', () => {
const putProjectSuccess = new actions.PutProjectSuccess({
it('UpdateProjectSuccess type is ProjectActionTypes.UPDATE_PROJECT_SUCCESS', () => {
const updateProjectSuccess = new actions.UpdateProjectSuccess({
id: '1',
name: 'Training',
description: 'It is good for learning',
});
expect(putProjectSuccess.type).toEqual(actions.ProjectActionTypes.PUT_PROJECT_SUCCESS);
expect(updateProjectSuccess.type).toEqual(actions.ProjectActionTypes.UPDATE_PROJECT_SUCCESS);
});

it('PutProjectFail type is ProjectActionTypes.POST_PROJECT_FAIL', () => {
const putProjectFail = new actions.PutProjectFail('error');
expect(putProjectFail.type).toEqual(actions.ProjectActionTypes.PUT_PROJECT_FAIL);
it('UpdateProjectFail type is ProjectActionTypes.UPDATE_PROJECT_FAIL', () => {
const updateProjectFail = new actions.UpdateProjectFail('error');
expect(updateProjectFail.type).toEqual(actions.ProjectActionTypes.UPDATE_PROJECT_FAIL);
});
});
48 changes: 24 additions & 24 deletions src/app/modules/project-management/store/project.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ export enum ProjectActionTypes {
GET_PROJECTS = '[Projects] GET_PROJECTS',
GET_PROJECTS_SUCCESS = '[Projects] GET_PROJECTS_SUCCESS',
GET_PROJECTS_FAIL = '[Projects] GET_PROJECTS_FAIL',
POST_PROJECT = '[Projects] POST_PROJECT',
POST_PROJECT_SUCCESS = '[Projects] POST_PROJECT_SUCCESS',
POST_PROJECT_FAIL = '[Projects] POST_PROJECT_FAIL',
PUT_PROJECT = '[Projects] PUT_PROJECT',
PUT_PROJECT_SUCCESS = '[Projects] PUT_PROJECT_SUCCESS',
PUT_PROJECT_FAIL = '[Projects] PUT_PROJECT_FAIL',
CREATE_PROJECT = '[Projects] CREATE_PROJECT',
CREATE_PROJECT_SUCCESS = '[Projects] CREATE_PROJECT_SUCCESS',
CREATE_PROJECT_FAIL = '[Projects] CREATE_PROJECT_FAIL',
UPDATE_PROJECT = '[Projects] UPDATE_PROJECT',
UPDATE_PROJECT_SUCCESS = '[Projects] UPDATE_PROJECT_SUCCESS',
UPDATE_PROJECT_FAIL = '[Projects] UPDATE_PROJECT_FAIL',
}

export class GetProjectsLoad implements Action {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LoadProjects

Expand All @@ -28,38 +28,38 @@ export class GetProjectsFail implements Action {
constructor(public error: string) {}
}

export class PostProject implements Action {
public readonly type = ProjectActionTypes.POST_PROJECT;
export class CreateProject implements Action {
public readonly type = ProjectActionTypes.CREATE_PROJECT;

constructor(public payload: Project) {}
}

export class PostProjectSuccess implements Action {
public readonly type = ProjectActionTypes.POST_PROJECT_SUCCESS;
export class CreateProjectSuccess implements Action {
public readonly type = ProjectActionTypes.CREATE_PROJECT_SUCCESS;

constructor(public payload: Project) {}
}

export class PostProjectFail implements Action {
public readonly type = ProjectActionTypes.POST_PROJECT_FAIL;
export class CreateProjectFail implements Action {
public readonly type = ProjectActionTypes.CREATE_PROJECT_FAIL;

constructor(public error: string) {}
}

export class PutProject implements Action {
public readonly type = ProjectActionTypes.PUT_PROJECT;
export class UpdateProject implements Action {
public readonly type = ProjectActionTypes.UPDATE_PROJECT;

constructor(public payload: Project) {}
}

export class PutProjectSuccess implements Action {
public readonly type = ProjectActionTypes.PUT_PROJECT_SUCCESS;
export class UpdateProjectSuccess implements Action {
public readonly type = ProjectActionTypes.UPDATE_PROJECT_SUCCESS;

constructor(public payload: Project) {}
}

export class PutProjectFail implements Action {
public readonly type = ProjectActionTypes.PUT_PROJECT_FAIL;
export class UpdateProjectFail implements Action {
public readonly type = ProjectActionTypes.UPDATE_PROJECT_FAIL;

constructor(public error: string) {}
}
Expand All @@ -68,9 +68,9 @@ export type ProjectActions =
| GetProjectsLoad
| GetProjectsSuccess
| GetProjectsFail
| PostProject
| PostProjectSuccess
| PostProjectFail
| PutProject
| PutProjectSuccess
| PutProjectFail;
| CreateProject
| CreateProjectSuccess
| CreateProjectFail
| UpdateProject
| UpdateProjectSuccess
| UpdateProjectFail;
20 changes: 10 additions & 10 deletions src/app/modules/project-management/store/project.effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,29 @@ export class ProjectEffects {
);

@Effect()
postProject$: Observable<Action> = this.actions$.pipe(
ofType(actions.ProjectActionTypes.POST_PROJECT),
map((action: actions.PostProject) => action.payload),
createProject$: Observable<Action> = this.actions$.pipe(
ofType(actions.ProjectActionTypes.CREATE_PROJECT),
map((action: actions.CreateProject) => action.payload),
mergeMap((project) =>
this.projectService.createProject(project).pipe(
map((projectData) => {
return new actions.PostProjectSuccess(projectData);
return new actions.CreateProjectSuccess(projectData);
}),
catchError((error) => of(new actions.PostProjectFail(error)))
catchError((error) => of(new actions.CreateProjectFail(error)))
)
)
);

@Effect()
putProject$: Observable<Action> = this.actions$.pipe(
ofType(actions.ProjectActionTypes.PUT_PROJECT),
map((action: actions.PutProject) => action.payload),
updateProject$: Observable<Action> = this.actions$.pipe(
ofType(actions.ProjectActionTypes.UPDATE_PROJECT),
map((action: actions.UpdateProject) => action.payload),
mergeMap((project) =>
this.projectService.updateProject(project).pipe(
map((projectData) => {
return new actions.PutProjectSuccess(projectData);
return new actions.UpdateProjectSuccess(projectData);
}),
catchError((error) => of(new actions.PutProjectFail(error)))
catchError((error) => of(new actions.UpdateProjectFail(error)))
)
)
);
Expand Down
Loading