-
Notifications
You must be signed in to change notification settings - Fork 1
fix: #90 Manage customer project types #152
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 all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,35 @@ | ||
| <div class="container"> | ||
| <form style="width: 600px;"> | ||
|
|
||
| <form style="width: 600px;" [formGroup]="projectTypeForm" (ngSubmit)="onSubmit(projectTypeForm.value)"> | ||
| <div class="form-group"> | ||
| <input type="text" class="form-control form-control-sm" id="" aria-describedby="" placeholder="Name" /> | ||
| <textarea class="form-control form-control-sm mt-2" id="descriptionTextArea" rows="3" | ||
| placeholder="Description"></textarea> | ||
| <button type="submit" class="btn btn-sm btn-primary">Save</button> | ||
| <button type="submit" class="btn btn-sm btn-secondary mb-2 ml-2 mt-2">Cancel</button> | ||
| <input | ||
| type="text" | ||
| class="form-control form-control-sm" | ||
| id="name" | ||
| formControlName="name" | ||
| aria-describedby="" | ||
| [class.is-invalid]="name.invalid && name.touched" | ||
| required | ||
| placeholder="Name" | ||
| /> | ||
| <div class="text-danger" *ngIf="(name.dirty || name.touched) && name.invalid && name.errors.required"> | ||
| Name is required. | ||
| </div> | ||
| <textarea | ||
| class="form-control form-control-sm mt-2" | ||
| id="descriptionTextArea" | ||
| rows="3" | ||
| placeholder="Description" | ||
| formControlName="description" | ||
| ></textarea> | ||
| <div class="btn-toolbar" role="toolbar"> | ||
| <div class="btn-group mr-2" role="group"> | ||
| <button type="submit" class="btn btn-sm btn-primary mb-2 ml-2 mt-2" [disabled]="!projectTypeForm.valid">Save</button> | ||
| </div> | ||
| <div class="btn-group mr-2" role="group"> | ||
| <button class="btn btn-sm btn-secondary mb-2 ml-2 mt-2" type="reset" [hidden]="!projectTypeToEdit" (click)="cancelButton()">Cancel</button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </form> | ||
| <hr /> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can have a clean create-project-type.component if we move please remove |
||
| <app-project-type-list></app-project-type-list> | ||
| </div> | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,146 @@ | ||
| import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | ||
| import { FormBuilder } from '@angular/forms'; | ||
| import { provideMockStore, MockStore } from '@ngrx/store/testing'; | ||
|
|
||
| import { CreateProjectTypeComponent } from './create-project-type.component'; | ||
| import { | ||
| ProjectTypeState, | ||
| CreateProjectType, | ||
| UpdateProjectType, | ||
| projectTypeIdToEdit, | ||
| allProjectTypes, | ||
| ResetProjectTypeToEdit, | ||
| } from '../../store'; | ||
| import { ProjectType } from '../../../../../shared/models/project-type.model'; | ||
|
|
||
| describe('InputProjectTypeComponent', () => { | ||
| let component: CreateProjectTypeComponent; | ||
| let fixture: ComponentFixture<CreateProjectTypeComponent>; | ||
| let store: MockStore<ProjectTypeState>; | ||
| let projectTypeIdToEditMock; | ||
| let allProjectTypesMock; | ||
| let getProjectTypeByIdMock; | ||
|
|
||
| const state = { | ||
| data: [{ id: '', name: '', description: '' }], | ||
| isLoading: false, | ||
| message: '', | ||
| projectTypeIdToEdit: '', | ||
| }; | ||
|
|
||
| const projectType: ProjectType = { | ||
| id: '1', | ||
| name: 'Training', | ||
| description: 'It is good for learning', | ||
| }; | ||
|
|
||
| beforeEach(async(() => { | ||
| TestBed.configureTestingModule({ | ||
| declarations: [CreateProjectTypeComponent], | ||
| providers: [FormBuilder, provideMockStore({ initialState: state })], | ||
| }).compileComponents(); | ||
| })); | ||
|
|
||
| beforeEach(() => { | ||
| fixture = TestBed.createComponent(CreateProjectTypeComponent); | ||
| component = fixture.componentInstance; | ||
| fixture.detectChanges(); | ||
|
|
||
| store = TestBed.inject(MockStore); | ||
| store.setState(state); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| fixture.destroy(); | ||
| }); | ||
|
|
||
| it('component should be created', () => { | ||
| expect(component).toBeTruthy(); | ||
| }); | ||
|
|
||
| it('should reset form onSubmit and dispatch UpdateProjectType action', () => { | ||
| const currentState = { | ||
| data: [{ id: '1', name: 'xxx', description: 'xxxx' }], | ||
| isLoading: false, | ||
| message: '', | ||
| projectTypeIdToEdit: '1', | ||
| }; | ||
|
|
||
| projectTypeIdToEditMock = store.overrideSelector(projectTypeIdToEdit, currentState.projectTypeIdToEdit); | ||
| allProjectTypesMock = store.overrideSelector(allProjectTypes, currentState.data); | ||
| getProjectTypeByIdMock = store.overrideSelector(allProjectTypesMock, projectTypeIdToEditMock); | ||
|
|
||
| component.projectTypeToEdit = getProjectTypeByIdMock; | ||
|
|
||
| const projectTypeForm = { | ||
| name: 'Develop', | ||
| description: 'xxx', | ||
| }; | ||
|
|
||
| const projectTypeUpdated = { | ||
| id: component.projectTypeToEdit.id, | ||
| name: 'Develop', | ||
| description: 'xxx', | ||
| }; | ||
|
|
||
| spyOn(component.projectTypeForm, 'reset'); | ||
| spyOn(store, 'dispatch'); | ||
|
|
||
| component.onSubmit(projectTypeForm); | ||
|
|
||
| expect(component.projectTypeForm.reset).toHaveBeenCalled(); | ||
| expect(store.dispatch).toHaveBeenCalledTimes(1); | ||
| expect(store.dispatch).toHaveBeenCalledWith(new UpdateProjectType(projectTypeUpdated)); | ||
| }); | ||
|
|
||
| it('should reset form onSubmit and dispatch CreateProjectType action', () => { | ||
| component.projectTypeToEdit = undefined; | ||
|
|
||
| spyOn(component.projectTypeForm, 'reset'); | ||
| spyOn(store, 'dispatch'); | ||
|
|
||
| component.onSubmit(projectType); | ||
|
|
||
| expect(component.projectTypeForm.reset).toHaveBeenCalled(); | ||
| expect(store.dispatch).toHaveBeenCalledTimes(1); | ||
| expect(store.dispatch).toHaveBeenCalledWith(new CreateProjectType(projectType)); | ||
| }); | ||
|
|
||
| it('should get name using projectTypeForm', () => { | ||
| spyOn(component.projectTypeForm, 'get'); | ||
| // tslint:disable-next-line:no-unused-expression | ||
| component.name; | ||
| expect(component.projectTypeForm.get).toHaveBeenCalledWith('name'); | ||
| }); | ||
|
|
||
| it('should get description using projectTypeForm', () => { | ||
| spyOn(component.projectTypeForm, 'get'); | ||
|
|
||
| // tslint:disable-next-line:no-unused-expression | ||
| component.description; | ||
|
|
||
| expect(component.projectTypeForm.get).toHaveBeenCalledWith('description'); | ||
| }); | ||
|
|
||
| it('should set data in projectTypeForm', () => { | ||
| const projectTypeDataForm = { | ||
| name: 'Training', | ||
| description: 'It is good for learning', | ||
| }; | ||
|
|
||
| spyOn(component.projectTypeForm, 'setValue'); | ||
|
|
||
| component.setDataToUpdate(projectType); | ||
| expect(component.projectTypeForm.setValue).toHaveBeenCalledTimes(1); | ||
| expect(component.projectTypeForm.setValue).toHaveBeenCalledWith(projectTypeDataForm); | ||
| }); | ||
|
|
||
| it('should dispatch a ResetProjectTypeToEdit action', () => { | ||
| spyOn(store, 'dispatch'); | ||
|
|
||
| component.cancelButton(); | ||
|
|
||
| expect(store.dispatch).toHaveBeenCalledTimes(1); | ||
| expect(store.dispatch).toHaveBeenCalledWith(new ResetProjectTypeToEdit()); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,68 @@ | ||
| import { Component } from '@angular/core'; | ||
| import { Component, OnInit } from '@angular/core'; | ||
| import { FormBuilder, Validators, FormGroup } from '@angular/forms'; | ||
| import { Store, select } from '@ngrx/store'; | ||
|
|
||
| import { ProjectType } from '../../../../../shared/models'; | ||
| import { ProjectTypeState } from '../../store'; | ||
| import { CreateProjectType, ResetProjectTypeToEdit, UpdateProjectType, getProjectTypeById } from '../../store'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-create-project-type', | ||
| templateUrl: './create-project-type.component.html', | ||
| styleUrls: ['./create-project-type.component.scss'], | ||
| }) | ||
| export class CreateProjectTypeComponent { | ||
| constructor() {} | ||
| export class CreateProjectTypeComponent implements OnInit { | ||
| projectTypeForm: FormGroup; | ||
| projectTypeToEdit: ProjectType; | ||
|
|
||
| constructor(private formBuilder: FormBuilder, private store: Store<ProjectTypeState>) { | ||
| this.projectTypeForm = this.formBuilder.group({ | ||
| name: ['', Validators.required], | ||
| description: [''], | ||
| }); | ||
| } | ||
|
|
||
| ngOnInit(): void { | ||
| const projectType$ = this.store.pipe(select(getProjectTypeById)); | ||
| projectType$.subscribe((projectType) => { | ||
| this.projectTypeToEdit = projectType; | ||
| this.setDataToUpdate(this.projectTypeToEdit); | ||
| }); | ||
| } | ||
|
|
||
| get name() { | ||
| return this.projectTypeForm.get('name'); | ||
| } | ||
|
|
||
| get description() { | ||
| return this.projectTypeForm.get('description'); | ||
| } | ||
|
|
||
| setDataToUpdate(projectTypeData: ProjectType) { | ||
| if (projectTypeData) { | ||
| this.projectTypeForm.setValue({ | ||
| name: projectTypeData.name, | ||
| description: projectTypeData.description, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| onSubmit(projectTypeData) { | ||
| this.projectTypeForm.reset(); | ||
|
|
||
| if (this.projectTypeToEdit) { | ||
| const projectType = { | ||
| ...projectTypeData, | ||
| id: this.projectTypeToEdit.id, | ||
| }; | ||
| this.store.dispatch(new UpdateProjectType(projectType)); | ||
| } else { | ||
| this.store.dispatch(new CreateProjectType(projectTypeData)); | ||
| this.projectTypeForm.get('description').setValue(''); | ||
| } | ||
| } | ||
|
|
||
| cancelButton() { | ||
| this.store.dispatch(new ResetProjectTypeToEdit()); | ||
| } | ||
| } |
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.
we can set a default value for optional fields, that way we can avoid the error when we do two continue creations only with required fields