@@ -41,7 +41,7 @@ import {
4141} from '@ng-dynamic-forms/core' ;
4242import { AppState } from '../../../app.reducer' ;
4343import { getMockFormService } from '../../../shared/mocks/form-service.mock' ;
44- import { mockAccessesFormData } from '../../../shared/mocks/submission.mock' ;
44+ import { mockAccessesFormData , mockAccessesServiceData } from '../../../shared/mocks/submission.mock' ;
4545import { accessConditionChangeEvent , checkboxChangeEvent } from '../../../shared/testing/form-event.stub' ;
4646import { XSRFService } from '../../../core/xsrf/xsrf.service' ;
4747import { createSuccessfulRemoteDataObject$ } from '../../../shared/remote-data.utils' ;
@@ -120,6 +120,7 @@ describe('SubmissionSectionAccessesComponent', () => {
120120 formService . validateAllFormFields . and . callFake ( ( ) => null ) ;
121121 formService . isValid . and . returnValue ( observableOf ( true ) ) ;
122122 formService . getFormData . and . returnValue ( observableOf ( mockAccessesFormData ) ) ;
123+ sectionsServiceStub . getSectionErrors . and . returnValue ( observableOf ( null ) ) ;
123124 submissionAccessesConfigService . findByHref . and . returnValue ( createSuccessfulRemoteDataObject$ ( accessConditionSectionConfigRes ) as any ) ;
124125 fixture . detectChanges ( ) ;
125126 } ) ) ;
@@ -183,6 +184,7 @@ describe('SubmissionSectionAccessesComponent', () => {
183184 formService . validateAllFormFields . and . callFake ( ( ) => null ) ;
184185 formService . isValid . and . returnValue ( observableOf ( true ) ) ;
185186 formService . getFormData . and . returnValue ( observableOf ( mockAccessesFormData ) ) ;
187+ sectionsServiceStub . getSectionErrors . and . returnValue ( observableOf ( null ) ) ;
186188 submissionAccessesConfigService . findByHref . and . returnValue ( createSuccessfulRemoteDataObject$ ( accessConditionSectionSingleAccessConfigRes ) as any ) ;
187189 fixture . detectChanges ( ) ;
188190 } ) ) ;
@@ -264,6 +266,7 @@ describe('SubmissionSectionAccessesComponent', () => {
264266 formService . validateAllFormFields . and . callFake ( ( ) => null ) ;
265267 formService . isValid . and . returnValue ( observableOf ( true ) ) ;
266268 formService . getFormData . and . returnValue ( observableOf ( mockAccessesFormData ) ) ;
269+ sectionsServiceStub . getSectionErrors . and . returnValue ( observableOf ( null ) ) ;
267270 fixture . detectChanges ( ) ;
268271 } ) ) ;
269272
@@ -277,4 +280,82 @@ describe('SubmissionSectionAccessesComponent', () => {
277280 } ) ;
278281
279282 } ) ;
283+
284+ describe ( 'when section is required' , ( ) => {
285+
286+ const service = getSectionAccessesService ( ) ;
287+
288+ beforeEach ( async ( ) => {
289+ await TestBed . configureTestingModule ( {
290+ imports : [
291+ BrowserModule ,
292+ TranslateModule . forRoot ( )
293+ ] ,
294+ declarations : [ SubmissionSectionAccessesComponent , FormComponent ] ,
295+ providers : [
296+ { provide : SectionsService , useValue : sectionsServiceStub } ,
297+ { provide : SubmissionAccessesConfigDataService , useValue : submissionAccessesConfigService } ,
298+ { provide : SectionAccessesService , useValue : service } ,
299+ { provide : SectionFormOperationsService , useValue : sectionFormOperationsService } ,
300+ { provide : JsonPatchOperationsBuilder , useValue : operationsBuilder } ,
301+ { provide : TranslateService , useValue : getMockTranslateService ( ) } ,
302+ { provide : FormService , useValue : getMockFormService ( ) } ,
303+ { provide : Store , useValue : storeStub } ,
304+ { provide : XSRFService , useValue : { } } ,
305+ { provide : SubmissionJsonPatchOperationsService , useValue : SubmissionJsonPatchOperationsServiceStub } ,
306+ { provide : 'sectionDataProvider' , useValue : sectionData } ,
307+ { provide : 'submissionIdProvider' , useValue : '1508' } ,
308+ FormBuilderService
309+ ]
310+ } )
311+ . compileComponents ( ) ;
312+ } ) ;
313+
314+ beforeEach ( inject ( [ Store ] , ( store : Store < AppState > ) => {
315+ fixture = TestBed . createComponent ( SubmissionSectionAccessesComponent ) ;
316+ component = fixture . componentInstance ;
317+ formService = TestBed . inject ( FormService ) ;
318+ formService . validateAllFormFields . and . callFake ( ( ) => null ) ;
319+ formService . isValid . and . returnValue ( observableOf ( true ) ) ;
320+ formService . getFormData . and . returnValue ( observableOf ( mockAccessesFormData ) ) ;
321+ sectionsServiceStub . getSectionErrors . and . returnValue ( observableOf ( null ) ) ;
322+ submissionAccessesConfigService . findByHref . and . returnValue ( createSuccessfulRemoteDataObject$ ( accessConditionSectionConfigRes ) as any ) ;
323+ fixture . detectChanges ( ) ;
324+ } ) ) ;
325+
326+ it ( 'should have section status invalid on init' , ( done ) => {
327+ component . required$ . next ( true ) ;
328+ service . getAccessesData . and . returnValue ( observableOf ( { } ) ) ;
329+ fixture . detectChanges ( ) ;
330+
331+ component . getSectionStatus ( ) . subscribe ( status => {
332+ expect ( status ) . toEqual ( false ) ;
333+ done ( ) ;
334+ } ) ;
335+ } ) ;
336+
337+ it ( 'should have section status valid if there are data and no error' , ( done ) => {
338+ service . getAccessesData . and . returnValue ( observableOf ( mockAccessesServiceData ) ) ;
339+ component . required$ . next ( true ) ;
340+ fixture . detectChanges ( ) ;
341+
342+ component . getSectionStatus ( ) . subscribe ( status => {
343+ expect ( status ) . toEqual ( true ) ;
344+ done ( ) ;
345+ } ) ;
346+ } ) ;
347+
348+ it ( 'should have section status invalid if there are data and errors' , ( done ) => {
349+ service . getAccessesData . and . returnValue ( observableOf ( mockAccessesServiceData ) ) ;
350+ component . required$ . next ( true ) ;
351+ sectionsServiceStub . getSectionErrors . and . returnValue ( observableOf ( { error : 'testError' } ) ) ;
352+
353+ fixture . detectChanges ( ) ;
354+
355+ component . getSectionStatus ( ) . subscribe ( status => {
356+ expect ( status ) . toEqual ( false ) ;
357+ done ( ) ;
358+ } ) ;
359+ } ) ;
360+ } ) ;
280361} ) ;
0 commit comments