1+ /* eslint-disable dot-notation */
12import { ComponentFixture , TestBed } from '@angular/core/testing' ;
2- import { FormArray , FormBuilder , ReactiveFormsModule } from '@angular/forms' ;
3+ import { FormArray , FormBuilder , FormGroup , ReactiveFormsModule } from '@angular/forms' ;
34import { provideRouter , Router , RouterModule } from '@angular/router' ;
45import { of , throwError } from 'rxjs' ;
56import { IntacctC1ImportSettingsComponent } from './intacct-c1-import-settings.component' ;
@@ -21,8 +22,7 @@ import {
2122 sageIntacctFieldsSortedByPriorityForC1 ,
2223 importSettingsWithProjectMapping ,
2324 expenseFieldsExpectedForC1 ,
24- costCodeFieldValue ,
25- costTypeFieldValue
25+ blankMapping
2626} from '../../intacct.fixture' ;
2727import { IntacctConfiguration } from 'src/app/core/models/db/configuration.model' ;
2828import { ImportSettingGet , ImportSettingPost , ImportSettings } from 'src/app/core/models/intacct/intacct-configuration/import-settings.model' ;
@@ -224,4 +224,106 @@ describe('IntacctC1ImportSettingsComponent', () => {
224224 expect ( component . saveInProgress ) . toBeFalse ( ) ;
225225 } ) ;
226226 } ) ;
227+
228+ describe ( 'Watchers' , ( ) => {
229+ let isDependentImportEnabledValueChangeSpy : jasmine . Spy ;
230+ let blankExpenseField : FormGroup ;
231+ let costCodesValueChangeSubscription : jasmine . Spy ;
232+ let costTypesValueChangeSubscription : jasmine . Spy ;
233+
234+ beforeEach ( ( ) => {
235+ component . ngOnInit ( ) ;
236+
237+ isDependentImportEnabledValueChangeSpy = spyOn ( component . importSettingsForm . controls . isDependentImportEnabled . valueChanges , 'subscribe' ) . and . callThrough ( ) ;
238+ blankExpenseField = component [ 'createFormGroup' ] ( blankMapping ) ;
239+ costCodesValueChangeSubscription = spyOn ( component . importSettingsForm . controls . costCodes . valueChanges , 'subscribe' ) ;
240+ costTypesValueChangeSubscription = spyOn ( component . importSettingsForm . controls . costTypes . valueChanges , 'subscribe' ) ;
241+
242+ spyOn ( blankExpenseField . valueChanges , 'subscribe' ) . and . callThrough ( ) ;
243+ } ) ;
244+
245+ describe ( 'dependentFieldWatchers' , ( ) => {
246+ beforeEach ( ( ) => {
247+ component [ 'dependentFieldWatchers' ] ( ) ;
248+ } ) ;
249+
250+ it ( 'should setup watchers for dependent fields' , ( ) => {
251+ expect ( component . importSettingsForm . controls . isDependentImportEnabled . valueChanges . subscribe ) . toHaveBeenCalled ( ) ;
252+ expect ( component . importSettingsForm . controls . costCodes . valueChanges . subscribe ) . toHaveBeenCalled ( ) ;
253+ expect ( component . importSettingsForm . controls . costTypes . valueChanges . subscribe ) . toHaveBeenCalled ( ) ;
254+ } ) ;
255+
256+ it ( 'should handle isDependentImportEnabled being set' , ( ) => {
257+ component . importSettingsForm . get ( 'isDependentImportEnabled' ) ?. setValue ( true ) ;
258+
259+ expect ( helperService . enableFormField ) . toHaveBeenCalledWith ( component . importSettingsForm , 'costCodes' ) ;
260+ expect ( helperService . enableFormField ) . toHaveBeenCalledWith ( component . importSettingsForm , 'costTypes' ) ;
261+ expect ( helperService . markControllerAsRequired ) . toHaveBeenCalledWith ( component . importSettingsForm , 'costCodes' ) ;
262+ expect ( helperService . markControllerAsRequired ) . toHaveBeenCalledWith ( component . importSettingsForm , 'costTypes' ) ;
263+ expect ( component . dependentImportFields [ 0 ] . isDisabled ) . toBeFalse ( ) ;
264+ expect ( component . dependentImportFields [ 1 ] . isDisabled ) . toBeFalse ( ) ;
265+ } ) ;
266+
267+ it ( 'should handle isDependentImportEnabled being unset' , ( ) => {
268+ component . importSettingsForm . get ( 'isDependentImportEnabled' ) ?. setValue ( false ) ;
269+
270+ expect ( helperService . disableFormField ) . toHaveBeenCalledWith ( component . importSettingsForm , 'costCodes' ) ;
271+ expect ( helperService . disableFormField ) . toHaveBeenCalledWith ( component . importSettingsForm , 'costTypes' ) ;
272+ expect ( helperService . clearValidatorAndResetValue ) . toHaveBeenCalledWith ( component . importSettingsForm , 'costCodes' ) ;
273+ expect ( helperService . clearValidatorAndResetValue ) . toHaveBeenCalledWith ( component . importSettingsForm , 'costTypes' ) ;
274+ expect ( component . dependentImportFields [ 0 ] . isDisabled ) . toBeTrue ( ) ;
275+ expect ( component . dependentImportFields [ 1 ] . isDisabled ) . toBeTrue ( ) ;
276+ } ) ;
277+ } ) ;
278+
279+ describe ( 'dependentCostFieldsWatchers' , ( ) => {
280+ beforeEach ( ( ) => {
281+ component [ 'dependentCostFieldsWatchers' ] ( 'costCodes' ) ;
282+ component [ 'dependentCostFieldsWatchers' ] ( 'costTypes' ) ;
283+ } ) ;
284+
285+ it ( 'should setup watchers for cost fields' , ( ) => {
286+ expect ( component . importSettingsForm . controls . costCodes . valueChanges . subscribe ) . toHaveBeenCalled ( ) ;
287+ expect ( component . importSettingsForm . controls . costTypes . valueChanges . subscribe ) . toHaveBeenCalled ( ) ;
288+ } ) ;
289+
290+ it ( 'should handle custom field selection for costCodes' , ( ) => {
291+ component . importSettingsForm . controls . costCodes . setValue ( { attribute_type : 'custom_field' } ) ;
292+
293+ expect ( component . customFieldType ) . toBe ( 'costCodes' ) ;
294+ expect ( component . customFieldControl ) . toBe ( component . importSettingsForm . controls . costCodes ) ;
295+ } ) ;
296+
297+ it ( 'should handle custom field selection for costTypes' , ( ) => {
298+ component . importSettingsForm . controls . costTypes . setValue ( { attribute_type : 'custom_field' } ) ;
299+
300+ expect ( component . customFieldType ) . toBe ( 'costTypes' ) ;
301+ expect ( component . customFieldControl ) . toBe ( component . importSettingsForm . controls . costTypes ) ;
302+ } ) ;
303+
304+ it ( 'should handle non-custom field selection' , ( ) => {
305+ component . importSettingsForm . controls . costCodes . setValue ( { attribute_type : 'some_field' } ) ;
306+ expect ( component . dependentImportFields [ 0 ] . isDisabled ) . toBeTrue ( ) ;
307+ } ) ;
308+ } ) ;
309+
310+ describe ( 'importSettingWatcher' , ( ) => {
311+ beforeEach ( ( ) => {
312+ ( component . importSettingsForm . get ( 'expenseFields' ) as FormArray ) . controls = [ blankExpenseField ] ;
313+ component [ 'importSettingWatcher' ] ( ) ;
314+ } ) ;
315+
316+ it ( 'should setup watchers for expense fields' , ( ) => {
317+ expect ( blankExpenseField . valueChanges . subscribe ) . toHaveBeenCalled ( ) ;
318+ } ) ;
319+
320+ it ( 'should handle custom field selection in expense fields' , ( ) => {
321+ spyOn ( blankExpenseField , 'patchValue' ) . and . callThrough ( ) ;
322+ blankExpenseField . patchValue ( { source_field : 'custom_field' } ) ;
323+
324+ expect ( component . customFieldControl ) . toBe ( blankExpenseField ) ;
325+ expect ( blankExpenseField . patchValue ) . toHaveBeenCalledWith ( blankMapping ) ;
326+ } ) ;
327+ } ) ;
328+ } ) ;
227329} ) ;
0 commit comments