-
Notifications
You must be signed in to change notification settings - Fork 1
#64 get, create and edit projects #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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)); | ||
|
||
| }); | ||
|
|
||
| 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)); | ||
|
||
| }); | ||
|
|
||
| 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(); | ||
| }); | ||
|
|
||
|
|
@@ -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', () => { | ||
|
||
| const details = component.projectForm.controls.description; | ||
| expect(details.valid).toBeFalsy(); | ||
|
|
||
| details.setValue(''); | ||
|
|
@@ -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; | ||
|
||
|
|
||
| @Input() projectToEdit: Project; | ||
| @Output() savedProject = new EventEmitter(); | ||
| @Output() cancelForm = new EventEmitter(); | ||
|
|
||
| constructor(private formBuilder: FormBuilder) { | ||
| constructor(private formBuilder: FormBuilder, private store: Store<any>) { | ||
|
||
| 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)); | ||
| } | ||
|
||
| this.reset(); | ||
| this.savedProject.emit(projectData); | ||
| } | ||
|
|
||
| reset() { | ||
| this.projectForm.reset(); | ||
| this.cancelForm.emit(); | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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