1+ import { LoadActiveEntry , EntryActionTypes } from './../../store/entry.actions' ;
2+ import { ActivityManagementActionTypes } from './../../../activities-management/store/activity-management.actions' ;
13import { async , ComponentFixture , TestBed } from '@angular/core/testing' ;
24import { MockStore , provideMockStore } from '@ngrx/store/testing' ;
3- import { FormsModule , ReactiveFormsModule } from '@angular/forms' ;
5+ import { FormsModule , ReactiveFormsModule , FormBuilder } from '@angular/forms' ;
46
57import { TechnologyState } from '../../../shared/store/technology.reducers' ;
68import { allTechnologies } from '../../../shared/store/technology.selectors' ;
79import { EntryFieldsComponent } from './entry-fields.component' ;
810import { ProjectState } from '../../../customer-management/components/projects/components/store/project.reducer' ;
911import { getCustomerProjects } from '../../../customer-management/components/projects/components/store/project.selectors' ;
10- import * as entryActions from '../../ store/entry.actions ' ;
12+ import { ActionsSubject } from '@ngrx/ store' ;
1113
1214describe ( 'EntryFieldsComponent' , ( ) => {
1315 type Merged = TechnologyState & ProjectState ;
@@ -16,6 +18,8 @@ describe('EntryFieldsComponent', () => {
1618 let store : MockStore < Merged > ;
1719 let mockTechnologySelector ;
1820 let mockProjectsSelector ;
21+ let entryForm ;
22+ const actionSub : ActionsSubject = new ActionsSubject ( ) ;
1923
2024 const state = {
2125 projects : {
@@ -60,10 +64,11 @@ describe('EntryFieldsComponent', () => {
6064 beforeEach ( async ( ( ) => {
6165 TestBed . configureTestingModule ( {
6266 declarations : [ EntryFieldsComponent ] ,
63- providers : [ provideMockStore ( { initialState : state } ) ] ,
67+ providers : [ provideMockStore ( { initialState : state } ) , { provide : ActionsSubject , useValue : actionSub } ] ,
6468 imports : [ FormsModule , ReactiveFormsModule ] ,
6569 } ) . compileComponents ( ) ;
6670 store = TestBed . inject ( MockStore ) ;
71+ entryForm = TestBed . inject ( FormBuilder ) ;
6772 mockTechnologySelector = store . overrideSelector ( allTechnologies , state . technologies ) ;
6873 mockProjectsSelector = store . overrideSelector ( getCustomerProjects , state . projects ) ;
6974 } ) ) ;
@@ -96,12 +101,6 @@ describe('EntryFieldsComponent', () => {
96101 expect ( component . selectedTechnologies ) . toEqual ( [ ] ) ;
97102 } ) ;
98103
99- it ( 'should dispatch UpdateActiveEntry action #onSubmit' , ( ) => {
100- spyOn ( store , 'dispatch' ) ;
101- component . onSubmit ( ) ;
102- expect ( store . dispatch ) . toHaveBeenCalledWith ( new entryActions . UpdateEntryRunning ( entry ) ) ;
103- } ) ;
104-
105104 it ( 'when a technology is added, then dispatch UpdateActiveEntry' , ( ) => {
106105 const addedTechnologies = [ 'react' ] ;
107106 spyOn ( store , 'dispatch' ) ;
@@ -120,4 +119,114 @@ describe('EntryFieldsComponent', () => {
120119 expect ( store . dispatch ) . toHaveBeenCalled ( ) ;
121120
122121 } ) ;
122+
123+ it ( 'uses the form to check if is valid or not' , ( ) => {
124+ entryForm . valid = false ;
125+
126+ const result = component . entryFormIsValidate ( ) ;
127+
128+ expect ( result ) . toBe ( entryForm . valid ) ;
129+ } ) ;
130+
131+ it ( 'dispatches an action when onSubmit is called' , ( ) => {
132+ spyOn ( store , 'dispatch' ) ;
133+
134+ component . onSubmit ( ) ;
135+
136+ expect ( store . dispatch ) . toHaveBeenCalled ( ) ;
137+ } ) ;
138+
139+ it ( 'dispatches an action when onTechnologyRemoved is called' , ( ) => {
140+ spyOn ( store , 'dispatch' ) ;
141+
142+ component . onTechnologyRemoved ( [ 'foo' ] ) ;
143+
144+ expect ( store . dispatch ) . toHaveBeenCalled ( ) ;
145+ } ) ;
146+
147+
148+ it ( 'sets the technologies on the class when entry has technologies' , ( ) => {
149+ const entryData = { ...entry , technologies : [ 'foo' ] } ;
150+
151+ component . setDataToUpdate ( entryData ) ;
152+
153+ expect ( component . selectedTechnologies ) . toEqual ( entryData . technologies ) ;
154+ } ) ;
155+
156+
157+ it ( 'activites are populated using the payload of the action' , ( ) => {
158+ const actionSubject = TestBed . inject ( ActionsSubject ) as ActionsSubject ;
159+ const action = {
160+ type : ActivityManagementActionTypes . LOAD_ACTIVITIES_SUCCESS ,
161+ payload : [ ] ,
162+ } ;
163+
164+ actionSubject . next ( action ) ;
165+
166+ expect ( component . activities ) . toEqual ( action . payload ) ;
167+ } ) ;
168+
169+ it ( 'LoadActiveEntry is dispatchen after LOAD_ACTIVITIES_SUCCESS' , ( ) => {
170+ spyOn ( store , 'dispatch' ) ;
171+
172+ const actionSubject = TestBed . inject ( ActionsSubject ) as ActionsSubject ;
173+ const action = {
174+ type : ActivityManagementActionTypes . LOAD_ACTIVITIES_SUCCESS ,
175+ payload : [ ] ,
176+ } ;
177+
178+ actionSubject . next ( action ) ;
179+
180+ expect ( store . dispatch ) . toHaveBeenCalledWith ( new LoadActiveEntry ( ) ) ;
181+ } ) ;
182+
183+ it ( 'when entry has an end_date null then LoadActiveEntry is dispatched' , ( ) => {
184+ spyOn ( store , 'dispatch' ) ;
185+
186+ const actionSubject = TestBed . inject ( ActionsSubject ) as ActionsSubject ;
187+ const action = {
188+ type : EntryActionTypes . CREATE_ENTRY_SUCCESS ,
189+ payload : { end_date : null } ,
190+ } ;
191+
192+ actionSubject . next ( action ) ;
193+
194+ expect ( store . dispatch ) . toHaveBeenCalledWith ( new LoadActiveEntry ( ) ) ;
195+ } ) ;
196+
197+ it ( 'when entry has an end_date then nothing is dispatched' , ( ) => {
198+ spyOn ( store , 'dispatch' ) ;
199+
200+ const actionSubject = TestBed . inject ( ActionsSubject ) as ActionsSubject ;
201+ const action = {
202+ type : EntryActionTypes . CREATE_ENTRY_SUCCESS ,
203+ payload : { end_date : new Date ( ) } ,
204+ } ;
205+
206+ actionSubject . next ( action ) ;
207+
208+ expect ( store . dispatch ) . toHaveBeenCalledTimes ( 0 ) ;
209+ } ) ;
210+
211+ it ( 'activeEntry is populated using the payload of LOAD_ACTIVE_ENTRY_SUCCESS' , ( ) => {
212+ const actionSubject = TestBed . inject ( ActionsSubject ) as ActionsSubject ;
213+ const action = {
214+ type : EntryActionTypes . LOAD_ACTIVE_ENTRY_SUCCESS ,
215+ payload : entry ,
216+ } ;
217+
218+ actionSubject . next ( action ) ;
219+
220+ expect ( component . activeEntry ) . toBe ( action . payload ) ;
221+ } ) ;
222+
223+ it ( 'if entryData is null selectedTechnologies is not modified' , ( ) => {
224+ const initialTechnologies = [ 'foo' , 'bar' ] ;
225+ component . selectedTechnologies = initialTechnologies ;
226+
227+ component . setDataToUpdate ( null ) ;
228+
229+ expect ( component . selectedTechnologies ) . toBe ( initialTechnologies ) ;
230+ } ) ;
231+
123232} ) ;
0 commit comments