|  | 
|  | 1 | +import { TestBed } from '@angular/core/testing'; | 
|  | 2 | +import { instance, mock, reset, when } from 'ts-mockito'; | 
|  | 3 | +import { ActivatedProjectService } from 'xforge-common/activated-project.service'; | 
|  | 4 | +import { createTestFeatureFlag, FeatureFlagService } from 'xforge-common/feature-flags/feature-flag.service'; | 
|  | 5 | +import { SFProjectProfileDoc } from '../../core/models/sf-project-profile-doc'; | 
|  | 6 | +import { BuildDto } from '../../machine-api/build-dto'; | 
|  | 7 | +import { DraftOptionsService, FORMATTING_OPTIONS_SUPPORTED_DATE } from './draft-options.service'; | 
|  | 8 | + | 
|  | 9 | +const activatedProjectMock = mock(ActivatedProjectService); | 
|  | 10 | +const featureFlagServiceMock = mock(FeatureFlagService); | 
|  | 11 | + | 
|  | 12 | +describe('DraftOptionsService', () => { | 
|  | 13 | +  let service: DraftOptionsService; | 
|  | 14 | + | 
|  | 15 | +  beforeEach(() => { | 
|  | 16 | +    TestBed.configureTestingModule({ | 
|  | 17 | +      providers: [ | 
|  | 18 | +        DraftOptionsService, | 
|  | 19 | +        { provide: ActivatedProjectService, useValue: instance(activatedProjectMock) }, | 
|  | 20 | +        { provide: FeatureFlagService, useValue: instance(featureFlagServiceMock) } | 
|  | 21 | +      ] | 
|  | 22 | +    }); | 
|  | 23 | +    // default: feature flag enabled unless a test overrides | 
|  | 24 | +    when(featureFlagServiceMock.usfmFormat).thenReturn(createTestFeatureFlag(true)); | 
|  | 25 | +    service = TestBed.inject(DraftOptionsService); | 
|  | 26 | +  }); | 
|  | 27 | + | 
|  | 28 | +  function setProjectDoc(data: any | undefined): void { | 
|  | 29 | +    when(activatedProjectMock.projectDoc).thenReturn( | 
|  | 30 | +      data == null ? (undefined as unknown as SFProjectProfileDoc) : ({ data } as SFProjectProfileDoc) | 
|  | 31 | +    ); | 
|  | 32 | +  } | 
|  | 33 | + | 
|  | 34 | +  describe('areFormattingOptionsSelected', () => { | 
|  | 35 | +    it('returns true when flag enabled and both options set', () => { | 
|  | 36 | +      setProjectDoc({ translateConfig: { draftConfig: { usfmConfig: { paragraphFormat: 'p', quoteFormat: 'q1' } } } }); | 
|  | 37 | +      expect(service.areFormattingOptionsSelected()).toBe(true); | 
|  | 38 | +    }); | 
|  | 39 | + | 
|  | 40 | +    it('returns false when flag enabled and one option missing', () => { | 
|  | 41 | +      setProjectDoc({ translateConfig: { draftConfig: { usfmConfig: { paragraphFormat: 'p' } } } }); | 
|  | 42 | +      expect(service.areFormattingOptionsSelected()).toBe(false); | 
|  | 43 | +    }); | 
|  | 44 | + | 
|  | 45 | +    it('returns false when flag enabled and both options missing', () => { | 
|  | 46 | +      setProjectDoc({ translateConfig: { draftConfig: { usfmConfig: {} } } }); | 
|  | 47 | +      expect(service.areFormattingOptionsSelected()).toBe(false); | 
|  | 48 | +    }); | 
|  | 49 | + | 
|  | 50 | +    it('returns false when project doc missing', () => { | 
|  | 51 | +      setProjectDoc(undefined); | 
|  | 52 | +      expect(service.areFormattingOptionsSelected()).toBe(false); | 
|  | 53 | +    }); | 
|  | 54 | + | 
|  | 55 | +    it('returns false when flag disabled even if both options set', () => { | 
|  | 56 | +      reset(featureFlagServiceMock); | 
|  | 57 | +      when(featureFlagServiceMock.usfmFormat).thenReturn(createTestFeatureFlag(false)); | 
|  | 58 | +      setProjectDoc({ translateConfig: { draftConfig: { usfmConfig: { paragraphFormat: 'p', quoteFormat: 'q1' } } } }); | 
|  | 59 | +      expect(service.areFormattingOptionsSelected()).toBe(false); | 
|  | 60 | +    }); | 
|  | 61 | +  }); | 
|  | 62 | + | 
|  | 63 | +  describe('areFormattingOptionsAvailableButUnselected', () => { | 
|  | 64 | +    it('returns true when flag enabled and both options missing', () => { | 
|  | 65 | +      setProjectDoc({ translateConfig: { draftConfig: { usfmConfig: {} } } }); | 
|  | 66 | +      expect(service.areFormattingOptionsAvailableButUnselected()).toBe(true); | 
|  | 67 | +    }); | 
|  | 68 | + | 
|  | 69 | +    it('returns true when flag enabled and one option missing', () => { | 
|  | 70 | +      setProjectDoc({ translateConfig: { draftConfig: { usfmConfig: { quoteFormat: 'q1' } } } }); | 
|  | 71 | +      expect(service.areFormattingOptionsAvailableButUnselected()).toBe(true); | 
|  | 72 | +    }); | 
|  | 73 | + | 
|  | 74 | +    it('returns false when flag enabled and both options set', () => { | 
|  | 75 | +      setProjectDoc({ translateConfig: { draftConfig: { usfmConfig: { paragraphFormat: 'p', quoteFormat: 'q1' } } } }); | 
|  | 76 | +      expect(service.areFormattingOptionsAvailableButUnselected()).toBe(false); | 
|  | 77 | +    }); | 
|  | 78 | + | 
|  | 79 | +    it('returns false when flag disabled', () => { | 
|  | 80 | +      reset(featureFlagServiceMock); | 
|  | 81 | +      when(featureFlagServiceMock.usfmFormat).thenReturn(createTestFeatureFlag(false)); | 
|  | 82 | +      setProjectDoc({ translateConfig: { draftConfig: { usfmConfig: {} } } }); | 
|  | 83 | +      expect(service.areFormattingOptionsAvailableButUnselected()).toBe(false); | 
|  | 84 | +    }); | 
|  | 85 | + | 
|  | 86 | +    it('returns false when project doc missing', () => { | 
|  | 87 | +      setProjectDoc(undefined); | 
|  | 88 | +      // Without a project doc, formatting options are implicitly unselected while flag is enabled | 
|  | 89 | +      expect(service.areFormattingOptionsAvailableButUnselected()).toBe(true); | 
|  | 90 | +    }); | 
|  | 91 | +  }); | 
|  | 92 | + | 
|  | 93 | +  describe('areFormattingOptionsSupportedForBuild', () => { | 
|  | 94 | +    function buildWith(date: Date | undefined, flagEnabled: boolean = true): BuildDto | undefined { | 
|  | 95 | +      reset(featureFlagServiceMock); | 
|  | 96 | +      when(featureFlagServiceMock.usfmFormat).thenReturn(createTestFeatureFlag(flagEnabled)); | 
|  | 97 | +      if (date == null) { | 
|  | 98 | +        return { additionalInfo: {} } as BuildDto; | 
|  | 99 | +      } | 
|  | 100 | +      return { additionalInfo: { dateFinished: date.toJSON() } } as BuildDto; | 
|  | 101 | +    } | 
|  | 102 | + | 
|  | 103 | +    it('returns true when flag enabled and date after supported date', () => { | 
|  | 104 | +      const entry = buildWith(new Date(FORMATTING_OPTIONS_SUPPORTED_DATE.getTime() + 1)); | 
|  | 105 | +      expect(service.areFormattingOptionsSupportedForBuild(entry)).toBe(true); | 
|  | 106 | +    }); | 
|  | 107 | + | 
|  | 108 | +    it('returns false when flag disabled even if date after supported date', () => { | 
|  | 109 | +      const entry = buildWith(new Date(FORMATTING_OPTIONS_SUPPORTED_DATE.getTime() + 1), false); | 
|  | 110 | +      expect(service.areFormattingOptionsSupportedForBuild(entry)).toBe(false); | 
|  | 111 | +    }); | 
|  | 112 | + | 
|  | 113 | +    it('returns false when date equals supported date', () => { | 
|  | 114 | +      const entry = buildWith(new Date(FORMATTING_OPTIONS_SUPPORTED_DATE.getTime())); | 
|  | 115 | +      expect(service.areFormattingOptionsSupportedForBuild(entry)).toBe(false); | 
|  | 116 | +    }); | 
|  | 117 | + | 
|  | 118 | +    it('returns false when date before supported date', () => { | 
|  | 119 | +      const entry = buildWith(new Date(FORMATTING_OPTIONS_SUPPORTED_DATE.getTime() - 1)); | 
|  | 120 | +      expect(service.areFormattingOptionsSupportedForBuild(entry)).toBe(false); | 
|  | 121 | +    }); | 
|  | 122 | + | 
|  | 123 | +    it('returns false when dateFinished missing', () => { | 
|  | 124 | +      const entry = buildWith(undefined); | 
|  | 125 | +      expect(service.areFormattingOptionsSupportedForBuild(entry)).toBe(false); | 
|  | 126 | +    }); | 
|  | 127 | + | 
|  | 128 | +    it('returns false when entry undefined', () => { | 
|  | 129 | +      reset(featureFlagServiceMock); | 
|  | 130 | +      when(featureFlagServiceMock.usfmFormat).thenReturn(createTestFeatureFlag(true)); | 
|  | 131 | +      expect(service.areFormattingOptionsSupportedForBuild(undefined)).toBe(false); | 
|  | 132 | +    }); | 
|  | 133 | +  }); | 
|  | 134 | +}); | 
0 commit comments