11import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
2+ import { provideMockStore , MockStore } from '@ngrx/store/testing' ;
23import { FormsModule , ReactiveFormsModule } from '@angular/forms' ;
34
5+ import { TechnologyState } from '../../store/technology.reducers' ;
6+ import { allTechnologies } from '../../store/technology.selectors' ;
47import { DetailsFieldsComponent } from './details-fields.component' ;
8+ import { FilterProjectPipe } from '../../../shared/pipes' ;
9+ import * as actions from '../../store/technology.actions' ;
510
611describe ( 'DetailsFieldsComponent' , ( ) => {
712 let component : DetailsFieldsComponent ;
813 let fixture : ComponentFixture < DetailsFieldsComponent > ;
14+ let store : MockStore < TechnologyState > ;
15+ let mockTechnologySelector ;
16+ let length ;
17+
18+ const state = {
19+ technologyList : { items : [ { name : 'java' } ] } ,
20+ isLoading : false ,
21+ } ;
22+
923 const initialData = {
1024 project : '' ,
1125 activity : '' ,
1226 ticket : '' ,
13- technology : '' ,
14- comments : ''
27+ comments : '' ,
1528 } ;
1629
1730 const newData = {
1831 project : 'Ernst&Young' ,
1932 activity : 'development' ,
2033 ticket : 'WA-15' ,
21- technology : 'Angular' ,
22- comments : 'No notes'
34+ comments : 'No notes' ,
2335 } ;
2436
2537 beforeEach ( async ( ( ) => {
2638 TestBed . configureTestingModule ( {
27- declarations : [ DetailsFieldsComponent ] ,
28- imports : [
29- FormsModule ,
30- ReactiveFormsModule
31- ] ,
39+ declarations : [ DetailsFieldsComponent , FilterProjectPipe ] ,
40+ providers : [ provideMockStore ( { initialState : state } ) ] ,
41+ imports : [ FormsModule , ReactiveFormsModule ] ,
3242 } ) . compileComponents ( ) ;
43+ store = TestBed . inject ( MockStore ) ;
44+ mockTechnologySelector = store . overrideSelector ( allTechnologies , state ) ;
3345 } ) ) ;
3446
3547 beforeEach ( ( ) => {
@@ -42,12 +54,6 @@ describe('DetailsFieldsComponent', () => {
4254 expect ( component ) . toBeTruthy ( ) ;
4355 } ) ;
4456
45- it ( 'should emit saveEntry event' , ( ) => {
46- spyOn ( component . saveEntry , 'emit' ) ;
47- component . onSubmit ( ) ;
48- expect ( component . saveEntry . emit ) . toHaveBeenCalledWith ( initialData ) ;
49- } ) ;
50-
5157 it ( 'should emit ngOnChange without data' , ( ) => {
5258 component . entryToEdit = null ;
5359 component . ngOnChanges ( ) ;
@@ -59,4 +65,73 @@ describe('DetailsFieldsComponent', () => {
5965 component . ngOnChanges ( ) ;
6066 expect ( component . entryForm . value ) . toEqual ( newData ) ;
6167 } ) ;
68+
69+ it ( 'should dispatch FindTechnology action #getTechnologies' , ( ) => {
70+ const value = 'java' ;
71+ spyOn ( store , 'dispatch' ) ;
72+ length = value . length ;
73+ component . getTechnologies ( value ) ;
74+
75+ expect ( store . dispatch ) . toHaveBeenCalledWith ( new actions . FindTechnology ( value ) ) ;
76+ } ) ;
77+
78+ it ( 'should NOT dispatch FindTechnology action #getTechnologies' , ( ) => {
79+ const value = 'j' ;
80+ spyOn ( store , 'dispatch' ) ;
81+ length = value . length ;
82+ component . getTechnologies ( value ) ;
83+
84+ expect ( store . dispatch ) . not . toHaveBeenCalledWith ( new actions . FindTechnology ( value ) ) ;
85+ } ) ;
86+
87+ it ( 'should add a new tag #setTechnology' , ( ) => {
88+ const name = 'ngrx' ;
89+ component . selectedTechnology = [ 'java' , 'javascript' ] ;
90+ component . selectedTechnology . indexOf ( name ) ;
91+ length = component . selectedTechnology . length ;
92+ component . setTechnology ( name ) ;
93+ expect ( component . selectedTechnology . length ) . toBe ( 3 ) ;
94+ } ) ;
95+
96+ it ( 'should NOT add a new tag #setTechnology' , ( ) => {
97+ const name = 'ngrx' ;
98+ component . selectedTechnology = [
99+ 'java' ,
100+ 'javascript' ,
101+ 'angular' ,
102+ 'angular-ui' ,
103+ 'typescript' ,
104+ 'scss' ,
105+ 'bootstrap' ,
106+ 'jasmine' ,
107+ 'karme' ,
108+ 'github' ,
109+ ] ;
110+ component . selectedTechnology . indexOf ( name ) ;
111+ length = component . selectedTechnology . length ;
112+ component . setTechnology ( name ) ;
113+ expect ( component . selectedTechnology . length ) . toBe ( 10 ) ;
114+ } ) ;
115+
116+ it ( 'should call the removeTag function #setTechnology' , ( ) => {
117+ const name = 'java' ;
118+ component . selectedTechnology = [ 'java' , 'javascript' ] ;
119+ const index = component . selectedTechnology . indexOf ( name ) ;
120+ spyOn ( component , 'removeTag' ) ;
121+ component . setTechnology ( name ) ;
122+ expect ( component . removeTag ) . toHaveBeenCalledWith ( index ) ;
123+ } ) ;
124+
125+ it ( 'should call the removeTag() function #removeTag' , ( ) => {
126+ const index = 1 ;
127+ component . selectedTechnology = [ 'java' , 'angular' ] ;
128+ component . removeTag ( index ) ;
129+ expect ( component . selectedTechnology . length ) . toBe ( 1 ) ;
130+ } ) ;
131+
132+ it ( 'should emit saveEntry event' , ( ) => {
133+ spyOn ( component . saveEntry , 'emit' ) ;
134+ component . onSubmit ( ) ;
135+ expect ( component . saveEntry . emit ) . toHaveBeenCalledWith ( initialData ) ;
136+ } ) ;
62137} ) ;
0 commit comments