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
11 changes: 8 additions & 3 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ import { SearchProjectComponent } from './modules/shared/components/search-proje
import { HomeComponent } from './modules/home/home.component';
import { LoginComponent } from './modules/login/login.component';
import { ActivityEffects } from './modules/activities-management/store/activity-management.effects';
import { activityManagementReducer } from './modules/activities-management/store';
import { ProjectEffects } from './modules/project-management/store/project.effects';
import { reducers, metaReducers } from './reducers';
import { environment } from '../environments/environment';

@NgModule({
declarations: [
Expand Down Expand Up @@ -68,8 +70,11 @@ import { activityManagementReducer } from './modules/activities-management/store
FormsModule,
ReactiveFormsModule,
HttpClientModule,
StoreModule.forRoot({ activities: activityManagementReducer }),
EffectsModule.forRoot([ActivityEffects]),
StoreModule.forRoot(reducers, {
metaReducers,
}),
!environment.production ? StoreDevtoolsModule.instrument() : [],
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this line replace lines 78-79-80?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated this line

EffectsModule.forRoot([ProjectEffects, ActivityEffects]),
StoreDevtoolsModule.instrument({
maxAge: 15, // Retains last 15 states
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,23 @@ <h1 class="card-title">Project</h1>
</div>

<div class="form-group">
<label for="details">Details:</label>
<label for="description">Description:</label>
<textarea
class="form-control"
rows="3"
id="details"
id="description"
type="text"
formControlName="details"
[class.is-invalid]="details.invalid && details.touched"
formControlName="description"
[class.is-invalid]="description.invalid && description.touched"
required
></textarea>
<p class="text-danger" *ngIf="(details.dirty || details.touched) && details.invalid && details.errors.required">
Project details are required.
<p
class="text-danger"
*ngIf="(description.dirty || description.touched) && description.invalid && description.errors.required"
>
Project description is required.
</p>
</div>

<div class="form-group">
<label for="status">Status:</label>
<select class="form-control" formControlName="status">
<option
*ngFor="let status of projectStatus"
[class.is-invalid]="status.invalid && status.touched"
[value]="status"
>{{ status }}</option
>
</select>
<p class="text-danger" *ngIf="(status.dirty || status.touched) && status.invalid && status.errors.required">
Project status is required.
</p>
</div>

<div class="form-group form-check" [hidden]="!projectToEdit">
<input type="checkbox" class="form-check-input" id="completedProject" formControlName="completed" />
<label class="form-check-label" for="completedProject">Completed project</label>
</div>
<div class="btn-toolbar" role="toolbar">
<div class="btn-group mr-2" role="group">
<button class="btn save-button-style mb-2" type="submit" [disabled]="!projectForm.valid">Save</button>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,62 +1,101 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { provideMockStore, MockStore } from '@ngrx/store/testing';
import { FormsModule, ReactiveFormsModule, FormBuilder } from '@angular/forms';

import { AppState } 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>;

const state = {
projectList: [{ id: 'id', name: 'name', description: 'description' }],
isLoading: false,
};

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ CreateProjectComponent ],
imports: [
FormsModule,
ReactiveFormsModule
]
})
.compileComponents();
declarations: [CreateProjectComponent],
imports: [FormsModule, ReactiveFormsModule],
providers: [FormBuilder, provideMockStore({ initialState: state })],
}).compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(CreateProjectComponent);
component = fixture.componentInstance;
fixture.detectChanges();
store = TestBed.inject(MockStore);
store.setState(state);
});

it('should create', () => {
expect(component).toBeTruthy();
});

it('should send the form data on onSubmit buton #onSubmit', () => {
it('should emit ngOnChange with projectToEdit', () => {
const newData = {
id: 'acf6a6c7-a24e-423c-8d1d-08505a82feae',
name: 'Project Test 13',
description: 'description',
};

component.projectToEdit = newData;
component.ngOnChanges();
expect(component.projectForm.value.name).toEqual(newData.name);
expect(component.projectForm.value.description).toEqual(newData.description);
expect(component.isEdit).toEqual(true);
});

it('should emit ngOnChange and reset ProjectForm', () => {
component.projectToEdit = null;
component.ngOnChanges();

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

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

component.isEdit = false;
spyOn(store, 'dispatch');
component.onSubmit(project);
expect(store.dispatch).toHaveBeenCalledWith(new actions.PostProject(project));
Copy link
Contributor

Choose a reason for hiding this comment

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

I think a better action name would be CreateProject(project)

});

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

spyOn(component.savedProject, 'emit');
component.isEdit = true;
spyOn(store, 'dispatch');
component.onSubmit(project);
expect(component.savedProject.emit).toHaveBeenCalled();
expect(store.dispatch).toHaveBeenCalledWith(new actions.PutProject(project));
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's change from PutProject to UpdateProject

});

it('should clean the form and send a cancelForm event #reset', () => {
const project = {
name: 'app 4',
details: 'It is a good app',
status: 'inactive',
completed: true
description: 'It is a good app',
};

component.projectForm.setValue(project);
spyOn(component.cancelForm, 'emit');
component.reset();
expect(component.projectForm.value.name).toEqual(null);
expect(component.projectForm.value.details).toEqual(null);
expect(component.projectForm.value.status).toEqual(null);
expect(component.projectForm.value.completed).toEqual(null);

expect(component.projectForm.value.description).toEqual(null);
expect(component.cancelForm.emit).toHaveBeenCalled();
});

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

it('details field validity', () => {
const details = component.projectForm.controls.details;
it('description field validity', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

let's change the test name to:
it('checks if description field is valid',
I know it's not your code but let's fix this.

const details = component.projectForm.controls.description;
expect(details.valid).toBeFalsy();

details.setValue('');
Expand All @@ -85,16 +124,4 @@ describe('CreateProjectComponent', () => {
details.setValue('A');
expect(details.hasError('required')).toBeFalsy();
});

it('status field validity', () => {
const status = component.projectForm.controls.status;
expect(status.valid).toBeFalsy();

status.setValue('');
expect(status.hasError('required')).toBeTruthy();

status.setValue('A');
expect(status.hasError('required')).toBeFalsy();
});

});
Original file line number Diff line number Diff line change
@@ -1,66 +1,61 @@
import {
Component,
Input,
OnChanges,
Output,
EventEmitter
} from '@angular/core';
import { Component, Input, OnChanges, OnInit, Output, EventEmitter } from '@angular/core';
import { FormBuilder, Validators } from '@angular/forms';
import { Store } from '@ngrx/store';
import { Project } from '../../../shared/models';
import * as actions from '../../store/project.actions';

@Component({
selector: 'app-create-project',
templateUrl: './create-project.component.html',
styleUrls: ['./create-project.component.scss']
styleUrls: ['./create-project.component.scss'],
})
export class CreateProjectComponent implements OnChanges {

export class CreateProjectComponent implements OnChanges, OnInit {
projectForm;
editProjectId;
projectStatus = ['', 'Active', 'Inactive'];
isEdit = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

a better name for this variable would be: isUpdating


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

constructor(private formBuilder: FormBuilder) {
constructor(private formBuilder: FormBuilder, private store: Store<any>) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Store<any> ?
I think we have to use Store<ProjectsStore> or so

this.projectForm = this.formBuilder.group({
name: ['', Validators.required],
details: ['', Validators.required],
status: ['', Validators.required],
completed: [false]
description: ['', Validators.required],
});
}

ngOnInit(): void {}

ngOnChanges(): void {
if (this.projectToEdit) {
this.editProjectId = this.projectToEdit.id;
this.projectForm.setValue({
name: this.projectToEdit.name, details: this.projectToEdit.details,
status: this.projectToEdit.status, completed: this.projectToEdit.completed
});
this.projectForm.patchValue(this.projectToEdit);
this.isEdit = true;
} else {
this.projectForm.reset();
this.isEdit = false;
}
}

get name() {
return this.projectForm.get('name');
}

get details() {
return this.projectForm.get('details');
}

get status() {
return this.projectForm.get('status');
get description() {
return this.projectForm.get('description');
}

onSubmit(projectData) {
onSubmit(formData) {
const projectData = { ...this.projectToEdit, ...formData };
if (!this.isEdit) {
this.store.dispatch(new actions.PostProject(projectData));
} else {
this.store.dispatch(new actions.PutProject(projectData));
}
Copy link
Contributor

Choose a reason for hiding this comment

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

In general terms, it's always a good idea to value if(true). In this case, this code should look like:

 if (this.isUpdating) {
      this.store.dispatch(new actions.UpdateProject(projectData));
    } else {
      this.store.dispatch(new actions.CreateProject(projectData));
    }

I am assuming here the actions have been renamed.

this.reset();
this.savedProject.emit(projectData);
}

reset() {
this.projectForm.reset();
this.cancelForm.emit();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ <h2 class="mb-0">
{{ project.name }}
</a>
<div class="btn-group float-right" role="group">
<i (click)="editProject.emit(project.id)" class="far fa-edit btn btn-link text-white"></i>
<i (click)="editProject.emit(project)" class="far fa-edit btn btn-link text-white"></i>
<i
(click)="openModal(project)"
data-toggle="modal"
Expand All @@ -29,12 +29,8 @@ <h2 class="mb-0">

<div [id]="'row' + rowIndex" class="collapse" data-parent="#accordionProject">
<div class="card-body">
<h5>Details:</h5>
<p>{{ project.details }}</p>
<h5>Status:</h5>
<p>{{ project.status }}</p>
<h5>Completed project:</h5>
<p>{{ project.completed ? 'Yes' : 'No' }}</p>
<h5>Description:</h5>
<p>{{ project.description }}</p>
</div>
</div>
</div>
Expand Down
Loading