1
1
import { 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' ;
3
4
5
+ import { ProjectState } from '../../store/project.reducer' ;
4
6
import { CreateProjectComponent } from './create-project.component' ;
7
+ import * as actions from '../../store/project.actions' ;
5
8
6
9
describe ( 'CreateProjectComponent' , ( ) => {
7
10
let component : CreateProjectComponent ;
8
11
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
+ } ;
9
18
10
19
beforeEach ( async ( ( ) => {
11
20
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 ( ) ;
19
25
} ) ) ;
20
26
21
27
beforeEach ( ( ) => {
22
28
fixture = TestBed . createComponent ( CreateProjectComponent ) ;
23
29
component = fixture . componentInstance ;
24
30
fixture . detectChanges ( ) ;
31
+ store = TestBed . inject ( MockStore ) ;
32
+ store . setState ( state ) ;
25
33
} ) ;
26
34
27
35
it ( 'should create' , ( ) => {
28
36
expect ( component ) . toBeTruthy ( ) ;
29
37
} ) ;
30
38
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' , ( ) => {
32
65
const project = {
66
+ id : '1' ,
33
67
name : 'app 4' ,
34
- details : 'It is a good app' ,
35
- status : 'inactive' ,
36
- completed : true
68
+ description : 'It is a good app' ,
37
69
} ;
70
+ component . isUpdating = false ;
71
+ spyOn ( store , 'dispatch' ) ;
38
72
39
- spyOn ( component . savedProject , 'emit' ) ;
40
73
component . onSubmit ( project ) ;
41
- expect ( component . savedProject . emit ) . toHaveBeenCalled ( ) ;
74
+
75
+ expect ( store . dispatch ) . toHaveBeenCalledWith ( new actions . CreateProject ( project ) ) ;
42
76
} ) ;
43
77
44
- it ( 'should clean the form and send a cancelForm event #reset ' , ( ) => {
78
+ it ( 'should dispatch UpdateProject action #onSubmit if isUpdating=true ' , ( ) => {
45
79
const project = {
80
+ id : '1' ,
46
81
name : 'app 4' ,
47
- details : 'It is a good app' ,
48
- status : 'inactive' ,
49
- completed : true
82
+ description : 'It is a good app' ,
50
83
} ;
84
+ component . isUpdating = true ;
85
+ spyOn ( store , 'dispatch' ) ;
86
+
87
+ component . onSubmit ( project ) ;
51
88
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
+ } ;
52
97
component . projectForm . setValue ( project ) ;
53
98
spyOn ( component . cancelForm , 'emit' ) ;
99
+
54
100
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 ) ;
59
101
102
+ expect ( component . projectForm . value . name ) . toEqual ( null ) ;
103
+ expect ( component . projectForm . value . description ) . toEqual ( null ) ;
60
104
expect ( component . cancelForm . emit ) . toHaveBeenCalled ( ) ;
61
105
} ) ;
62
106
63
107
it ( 'form invalid when empty' , ( ) => {
64
108
expect ( component . projectForm . valid ) . toBeFalsy ( ) ;
65
109
} ) ;
66
110
67
- it ( 'name field validity ' , ( ) => {
111
+ it ( 'checks if name field is valid ' , ( ) => {
68
112
const name = component . projectForm . controls . name ;
69
113
expect ( name . valid ) . toBeFalsy ( ) ;
70
114
@@ -75,8 +119,8 @@ describe('CreateProjectComponent', () => {
75
119
expect ( name . hasError ( 'required' ) ) . toBeFalsy ( ) ;
76
120
} ) ;
77
121
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 ;
80
124
expect ( details . valid ) . toBeFalsy ( ) ;
81
125
82
126
details . setValue ( '' ) ;
@@ -85,16 +129,4 @@ describe('CreateProjectComponent', () => {
85
129
details . setValue ( 'A' ) ;
86
130
expect ( details . hasError ( 'required' ) ) . toBeFalsy ( ) ;
87
131
} ) ;
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
-
100
132
} ) ;
0 commit comments