@@ -4,6 +4,7 @@ import { ActivatedRoute } from '@angular/router';
4
4
import { of , Observable , BehaviorSubject } from 'rxjs' ;
5
5
import { FormControl , FormGroup } from '@angular/forms' ;
6
6
import { By } from '@angular/platform-browser' ;
7
+ import createSpy = jasmine . createSpy ;
7
8
import createSpyObj = jasmine . createSpyObj ;
8
9
import { FieldsUtils } from '../../../services/fields/fields.utils' ;
9
10
import { CaseReferencePipe } from '../../../pipes/case-reference/case-reference.pipe' ;
@@ -22,9 +23,11 @@ import { CaseEditSubmitComponent } from './case-edit-submit.component';
22
23
import { CaseEditComponent } from '../case-edit/case-edit.component' ;
23
24
import { CaseEditPageComponent } from '../case-edit-page/case-edit-page.component' ;
24
25
import { ProfileService , ProfileNotifier } from '../../../services/profile' ;
25
- import { Profile } from '../../../domain' ;
26
+ import { CaseEventData , Profile } from '../../../domain' ;
26
27
import { createAProfile } from '../../../domain/profile/profile.test.fixture' ;
27
28
import { text } from '../../../test/helpers' ;
29
+ import { FieldsPurger } from '../../../services' ;
30
+ import { WizardPageField } from '../domain' ;
28
31
29
32
describe ( 'CaseEditSubmitComponent' , ( ) => {
30
33
@@ -40,6 +43,13 @@ describe('CaseEditSubmitComponent', () => {
40
43
const FORM_GROUP = new FormGroup ( {
41
44
'data' : new FormGroup ( { 'PersonLastName' : new FormControl ( 'Khaleesi' ) } )
42
45
} ) ;
46
+ const FORM_GROUP_WITH_HIDDEN_FIELDS = new FormGroup ( {
47
+ 'data' : new FormGroup ( {
48
+ 'field1' : new FormControl ( { value : 'Hidden value to be retained' , disabled : true } ) ,
49
+ 'field2' : new FormControl ( { value : 'Hidden value not to be retained' , disabled : true } ) ,
50
+ 'field3' : new FormControl ( 'Hide both' )
51
+ } )
52
+ } ) ;
43
53
let caseEditComponent : any ;
44
54
let pages : WizardPage [ ] ;
45
55
let wizard : Wizard ;
@@ -51,6 +61,7 @@ describe('CaseEditSubmitComponent', () => {
51
61
let caseField1 : CaseField = aCaseField ( 'field1' , 'field1' , 'Text' , 'OPTIONAL' , 3 ) ;
52
62
let caseField2 : CaseField = aCaseField ( 'field2' , 'field2' , 'Text' , 'OPTIONAL' , 2 ) ;
53
63
let caseField3 : CaseField = aCaseField ( 'field3' , 'field3' , 'Text' , 'OPTIONAL' , 1 ) ;
64
+ let caseFieldRetainHiddenValue : CaseField = aCaseField ( 'field1' , 'field1' , 'Text' , 'OPTIONAL' , 3 , null , true ) ;
54
65
const $EVENT_NOTES = By . css ( '#fieldset-event' ) ;
55
66
let cancelled : any ;
56
67
let snapshot : any ;
@@ -692,4 +703,129 @@ describe('CaseEditSubmitComponent', () => {
692
703
expect ( error ) . toBeFalsy ( ) ;
693
704
} ) ;
694
705
} ) ;
706
+
707
+ describe ( 'Form submit test' , ( ) => {
708
+ pages = [
709
+ aWizardPage ( 'page1' , 'Page 1' , 1 ) ,
710
+ ] ;
711
+ const firstPage = pages [ 0 ] ;
712
+ caseFieldRetainHiddenValue . show_condition = 'field3!="Hide both"' ;
713
+ caseField2 . show_condition = 'field3!="Hide both"' ;
714
+ const WP_FIELD_1 : WizardPageField = { case_field_id : caseFieldRetainHiddenValue . id } ;
715
+ const WP_FIELD_2 : WizardPageField = { case_field_id : caseField2 . id } ;
716
+ const WP_FIELD_3 : WizardPageField = { case_field_id : caseField3 . id } ;
717
+ firstPage . wizard_page_fields = [ WP_FIELD_1 , WP_FIELD_2 , WP_FIELD_3 ] ;
718
+ wizard = new Wizard ( pages ) ;
719
+ const queryParamMapNoProfile = createSpyObj ( 'queryParamMap' , [ 'get' ] ) ;
720
+ const snapshotNoProfile = {
721
+ pathFromRoot : [
722
+ { } ,
723
+ {
724
+ data : {
725
+ nonProfileData : {
726
+ user : {
727
+ idam : {
728
+ id : 'userId' ,
729
+ email : 'string' ,
730
+ forename : 'string' ,
731
+ surname : 'string' ,
732
+ roles : [ 'caseworker' , 'caseworker-test' , 'caseworker-probate-solicitor' ]
733
+ }
734
+ } ,
735
+ 'isSolicitor' : ( ) => false ,
736
+ }
737
+ }
738
+ }
739
+ ] ,
740
+ queryParamMap : queryParamMapNoProfile ,
741
+ } ;
742
+ let PROFILE_OBS : Observable < Profile > = Observable . of ( PROFILE ) ;
743
+ const mockRouteNoProfile = {
744
+ params : of ( { id : 123 } ) ,
745
+ snapshot : snapshotNoProfile
746
+ } ;
747
+ let caseEditComponentSubmitSpy : any ;
748
+
749
+ beforeEach ( async ( ( ) => {
750
+ // Need to set the page case_fields here because, for some reason, if set initially then these get overridden
751
+ // by some unknown default!
752
+ firstPage . case_fields = [ caseFieldRetainHiddenValue , caseField2 , caseField3 ] ;
753
+ orderService = new OrderService ( ) ;
754
+ casesReferencePipe = createSpyObj < CaseReferencePipe > ( 'caseReference' , [ 'transform' ] ) ;
755
+ cancelled = createSpyObj ( 'cancelled' , [ 'emit' ] )
756
+ caseEditComponent = {
757
+ 'form' : FORM_GROUP_WITH_HIDDEN_FIELDS ,
758
+ 'fieldsPurger' : new FieldsPurger ( fieldsUtils ) ,
759
+ 'data' : '' ,
760
+ 'eventTrigger' : { 'case_fields' : [ caseFieldRetainHiddenValue , caseField2 , caseField3 ] , 'can_save_draft' : true } ,
761
+ 'wizard' : wizard ,
762
+ 'hasPrevious' : ( ) => true ,
763
+ 'getPage' : ( ) => firstPage ,
764
+ 'navigateToPage' : ( ) => undefined ,
765
+ 'next' : ( ) => new FieldsPurger ( fieldsUtils ) . clearHiddenFields (
766
+ caseEditComponent . form , caseEditComponent . wizard , caseEditComponent . eventTrigger , firstPage . id ) ,
767
+ 'cancel' : ( ) => undefined ,
768
+ 'cancelled' : cancelled ,
769
+ 'submit' : createSpy ( 'submit' ) . and . returnValue ( {
770
+ // Provide a dummy subscribe function to be called in place of the real one
771
+ subscribe : ( ) => { }
772
+ } )
773
+ } ;
774
+ formErrorService = createSpyObj < FormErrorService > ( 'formErrorService' , [ 'mapFieldErrors' ] ) ;
775
+ formValueService = createSpyObj < FormValueService > ( 'formValueService' , [ 'sanitise' ] ) ;
776
+ formValueService . sanitise . and . callFake ( ( rawValue : object ) => {
777
+ return rawValue ;
778
+ } ) ;
779
+
780
+ profileService = createSpyObj < ProfileService > ( 'profileService' , [ 'get' ] ) ;
781
+ profileService . get . and . returnValue ( PROFILE_OBS ) ;
782
+ profileNotifier = new ProfileNotifier ( ) ;
783
+ profileNotifier . profile = new BehaviorSubject ( createAProfile ( ) ) . asObservable ( ) ;
784
+ profileNotifierSpy = spyOn ( profileNotifier , 'announceProfile' ) . and . callThrough ( ) ;
785
+
786
+ TestBed . configureTestingModule ( {
787
+ declarations : [
788
+ CaseEditSubmitComponent ,
789
+ IsCompoundPipe ,
790
+ CaseReferencePipe
791
+ ] ,
792
+ schemas : [ NO_ERRORS_SCHEMA ] ,
793
+ providers : [
794
+ { provide : CaseEditComponent , useValue : caseEditComponent } ,
795
+ { provide : FormValueService , useValue : formValueService } ,
796
+ { provide : FormErrorService , useValue : formErrorService } ,
797
+ { provide : CaseFieldService , useValue : caseFieldService } ,
798
+ { provide : FieldsUtils , useValue : fieldsUtils } ,
799
+ { provide : CaseReferencePipe , useValue : casesReferencePipe } ,
800
+ { provide : ActivatedRoute , useValue : mockRouteNoProfile } ,
801
+ { provide : OrderService , useValue : orderService } ,
802
+ { provide : ProfileService , useValue : profileService } ,
803
+ { provide : ProfileNotifier , useValue : profileNotifier }
804
+ ]
805
+ } ) . compileComponents ( ) ;
806
+ } ) ) ;
807
+
808
+ beforeEach ( ( ) => {
809
+ fixture = TestBed . createComponent ( CaseEditSubmitComponent ) ;
810
+ comp = fixture . componentInstance ;
811
+ de = fixture . debugElement ;
812
+ fixture . detectChanges ( ) ;
813
+ } ) ;
814
+
815
+ it ( 'should submit CaseEventData with null for any hidden fields whose values are not to be retained' , ( ) => {
816
+ // Trigger the clearing of hidden fields by invoking next()
817
+ caseEditComponent . next ( ) ;
818
+
819
+ // Submit the form and check the expected CaseEventData is being passed to the CaseEditComponent for submission
820
+ comp . submit ( ) ;
821
+ expect ( caseEditComponent . submit ) . toHaveBeenCalledWith ( {
822
+ data : {
823
+ field2 : null ,
824
+ field3 : 'Hide both'
825
+ } ,
826
+ event_token : undefined ,
827
+ ignore_warning : false
828
+ } ) ;
829
+ } ) ;
830
+ } ) ;
695
831
} ) ;
0 commit comments