11import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
2- import { FormsModule , ReactiveFormsModule } from '@angular/forms' ;
2+ import { provideMockStore , MockStore } from '@ngrx/store/testing' ;
3+ import { FormsModule , ReactiveFormsModule , FormBuilder } from '@angular/forms' ;
34
5+ import { ProjectState } from '../../store/project.reducer' ;
46import { CreateProjectComponent } from './create-project.component' ;
7+ import * as actions from '../../store/project.actions' ;
58
69describe ( 'CreateProjectComponent' , ( ) => {
710 let component : CreateProjectComponent ;
811 let fixture : ComponentFixture < CreateProjectComponent > ;
12+ let store : MockStore < ProjectState > ;
13+
14+ const state = {
15+ projectList : [ { id : 'id' , name : 'name' , description : 'description' } ] ,
16+ isLoading : false ,
17+ } ;
918
1019 beforeEach ( async ( ( ) => {
1120 TestBed . configureTestingModule ( {
12- declarations : [ CreateProjectComponent ] ,
13- imports : [
14- FormsModule ,
15- ReactiveFormsModule
16- ]
17- } )
18- . compileComponents ( ) ;
21+ declarations : [ CreateProjectComponent ] ,
22+ imports : [ FormsModule , ReactiveFormsModule ] ,
23+ providers : [ FormBuilder , provideMockStore ( { initialState : state } ) ] ,
24+ } ) . compileComponents ( ) ;
1925 } ) ) ;
2026
2127 beforeEach ( ( ) => {
2228 fixture = TestBed . createComponent ( CreateProjectComponent ) ;
2329 component = fixture . componentInstance ;
2430 fixture . detectChanges ( ) ;
31+ store = TestBed . inject ( MockStore ) ;
32+ store . setState ( state ) ;
2533 } ) ;
2634
2735 it ( 'should create' , ( ) => {
2836 expect ( component ) . toBeTruthy ( ) ;
2937 } ) ;
3038
31- it ( 'should send the form data on onSubmit buton #onSubmit' , ( ) => {
39+ it ( 'should emit ngOnChange with projectToEdit' , ( ) => {
40+ const newData = {
41+ id : 'acf6a6c7-a24e-423c-8d1d-08505a82feae' ,
42+ name : 'Project Test 13' ,
43+ description : 'description' ,
44+ } ;
45+ component . projectToEdit = newData ;
46+
47+ component . ngOnChanges ( ) ;
48+
49+ expect ( component . projectForm . value . name ) . toEqual ( newData . name ) ;
50+ expect ( component . projectForm . value . description ) . toEqual ( newData . description ) ;
51+ expect ( component . isUpdating ) . toEqual ( true ) ;
52+ } ) ;
53+
54+ it ( 'on ngOnChange with projectToEdit=null should reset projectForm' , ( ) => {
55+ component . projectToEdit = null ;
56+
57+ component . ngOnChanges ( ) ;
58+
59+ expect ( component . projectForm . value . name ) . toEqual ( null ) ;
60+ expect ( component . projectForm . value . description ) . toEqual ( null ) ;
61+ expect ( component . isUpdating ) . toEqual ( false ) ;
62+ } ) ;
63+
64+ it ( 'should dispatch CreateProject action #onSubmit if isUpdating=false' , ( ) => {
3265 const project = {
66+ id : '1' ,
3367 name : 'app 4' ,
34- details : 'It is a good app' ,
35- status : 'inactive' ,
36- completed : true
68+ description : 'It is a good app' ,
3769 } ;
70+ component . isUpdating = false ;
71+ spyOn ( store , 'dispatch' ) ;
3872
39- spyOn ( component . savedProject , 'emit' ) ;
4073 component . onSubmit ( project ) ;
41- expect ( component . savedProject . emit ) . toHaveBeenCalled ( ) ;
74+
75+ expect ( store . dispatch ) . toHaveBeenCalledWith ( new actions . CreateProject ( project ) ) ;
4276 } ) ;
4377
44- it ( 'should clean the form and send a cancelForm event #reset ' , ( ) => {
78+ it ( 'should dispatch UpdateProject action #onSubmit if isUpdating=true ' , ( ) => {
4579 const project = {
80+ id : '1' ,
4681 name : 'app 4' ,
47- details : 'It is a good app' ,
48- status : 'inactive' ,
49- completed : true
82+ description : 'It is a good app' ,
5083 } ;
84+ component . isUpdating = true ;
85+ spyOn ( store , 'dispatch' ) ;
86+
87+ component . onSubmit ( project ) ;
5188
89+ expect ( store . dispatch ) . toHaveBeenCalledWith ( new actions . UpdateProject ( project ) ) ;
90+ } ) ;
91+
92+ it ( 'should clean the form and emmit a cancelForm event on #reset' , ( ) => {
93+ const project = {
94+ name : 'app 4' ,
95+ description : 'It is a good app' ,
96+ } ;
5297 component . projectForm . setValue ( project ) ;
5398 spyOn ( component . cancelForm , 'emit' ) ;
99+
54100 component . reset ( ) ;
55- expect ( component . projectForm . value . name ) . toEqual ( null ) ;
56- expect ( component . projectForm . value . details ) . toEqual ( null ) ;
57- expect ( component . projectForm . value . status ) . toEqual ( null ) ;
58- expect ( component . projectForm . value . completed ) . toEqual ( null ) ;
59101
102+ expect ( component . projectForm . value . name ) . toEqual ( null ) ;
103+ expect ( component . projectForm . value . description ) . toEqual ( null ) ;
60104 expect ( component . cancelForm . emit ) . toHaveBeenCalled ( ) ;
61105 } ) ;
62106
63107 it ( 'form invalid when empty' , ( ) => {
64108 expect ( component . projectForm . valid ) . toBeFalsy ( ) ;
65109 } ) ;
66110
67- it ( 'name field validity ' , ( ) => {
111+ it ( 'checks if name field is valid ' , ( ) => {
68112 const name = component . projectForm . controls . name ;
69113 expect ( name . valid ) . toBeFalsy ( ) ;
70114
@@ -75,8 +119,8 @@ describe('CreateProjectComponent', () => {
75119 expect ( name . hasError ( 'required' ) ) . toBeFalsy ( ) ;
76120 } ) ;
77121
78- it ( 'details field validity ' , ( ) => {
79- const details = component . projectForm . controls . details ;
122+ it ( 'checks if description field is valid ' , ( ) => {
123+ const details = component . projectForm . controls . description ;
80124 expect ( details . valid ) . toBeFalsy ( ) ;
81125
82126 details . setValue ( '' ) ;
@@ -85,16 +129,4 @@ describe('CreateProjectComponent', () => {
85129 details . setValue ( 'A' ) ;
86130 expect ( details . hasError ( 'required' ) ) . toBeFalsy ( ) ;
87131 } ) ;
88-
89- it ( 'status field validity' , ( ) => {
90- const status = component . projectForm . controls . status ;
91- expect ( status . valid ) . toBeFalsy ( ) ;
92-
93- status . setValue ( '' ) ;
94- expect ( status . hasError ( 'required' ) ) . toBeTruthy ( ) ;
95-
96- status . setValue ( 'A' ) ;
97- expect ( status . hasError ( 'required' ) ) . toBeFalsy ( ) ;
98- } ) ;
99-
100132} ) ;
0 commit comments