From 474b93c39dab57ab09851e572f6b3284876941fa Mon Sep 17 00:00:00 2001 From: RehanY147 Date: Tue, 7 Jan 2025 12:08:20 +0500 Subject: [PATCH] NAS-133118: Add tests (#11288) --- .../ix-forms/services/ix-form.service.spec.ts | 122 ++++++++++-------- .../ix-forms/services/ix-form.service.ts | 8 +- 2 files changed, 71 insertions(+), 59 deletions(-) diff --git a/src/app/modules/forms/ix-forms/services/ix-form.service.spec.ts b/src/app/modules/forms/ix-forms/services/ix-form.service.spec.ts index f1cf85bdf7e..bd78c0e4e4d 100644 --- a/src/app/modules/forms/ix-forms/services/ix-form.service.spec.ts +++ b/src/app/modules/forms/ix-forms/services/ix-form.service.spec.ts @@ -1,78 +1,90 @@ -import { NgControl } from '@angular/forms'; +import { ElementRef } from '@angular/core'; +import { FormControl, NgControl } from '@angular/forms'; import { SpectatorService, createServiceFactory } from '@ngneat/spectator/jest'; +import { TestScheduler } from 'rxjs/testing'; +import { getTestScheduler } from 'app/core/testing/utils/get-test-scheduler.utils'; +import { IxFormSectionComponent } from 'app/modules/forms/ix-forms/components/ix-form-section/ix-form-section.component'; +import { ixControlLabelTag } from 'app/modules/forms/ix-forms/directives/registered-control.directive'; import { IxFormService } from 'app/modules/forms/ix-forms/services/ix-form.service'; -// TODO: https://ixsystems.atlassian.net/browse/NAS-133118 -describe.skip('IxFormService', () => { +class MockNgControl extends NgControl { + override control = new FormControl('mock-value'); + + override viewToModelUpdate(newValue: string): void { + this.control.setValue(newValue); + } +} + +describe('IxFormService', () => { let spectator: SpectatorService; + let testScheduler: TestScheduler; const createService = createServiceFactory({ service: IxFormService, }); - const fakeComponents = [ - { - control: { - name: 'test_control_1', - }, - element: { - nativeElement: { - id: 'test_element_1', - }, - getAttribute: () => 'Test Element 1', - }, - }, - { - control: { - name: 'test_control_2', - }, - element: { - nativeElement: { - id: 'test_element_2', - }, - getAttribute: () => 'Test Element 2', - }, - }, - ] as { - control: NgControl; - element: { nativeElement: HTMLElement; getAttribute: () => string }; - }[]; - beforeEach(() => { spectator = createService(); - fakeComponents.forEach((component) => { - spectator.service.registerControl(component.control.name!.toString(), component.element); - }); + testScheduler = getTestScheduler(); }); - describe('getControlsNames', () => { - it('returns a list of control names', () => { - expect(spectator.service.getControlNames()).toEqual([ - 'test_control_1', - 'test_control_2', - ]); + describe('handles control register/unregister', () => { + it('registers control', () => { + const elRef = new ElementRef(document.createElement('input')); + elRef.nativeElement.setAttribute('id', 'control1'); + elRef.nativeElement.setAttribute(ixControlLabelTag, 'Control1'); + spectator.service.registerControl( + 'control1', + elRef, + ); + + expect(spectator.service.getControlNames()).toEqual(['control1']); + testScheduler.run(({ expectObservable }) => { + expectObservable(spectator.service.controlNamesWithLabels$).toBe('a', { + a: [{ label: 'Control1', name: 'control1' }], + }); + }); + expect(spectator.service.getElementByControlName('control1')).toEqual(elRef.nativeElement); + expect(spectator.service.getElementByLabel('Control1')).toEqual(elRef.nativeElement); }); - }); - describe('getControls', () => { - it('returns a list of controls', () => { - expect(spectator.service.getControlNames()).toEqual([ - 'test_control_1', - 'test_control_2', - ]); + it('unregisters control', () => { + const elRef = new ElementRef(document.createElement('input')); + elRef.nativeElement.setAttribute('id', 'control1'); + elRef.nativeElement.setAttribute(ixControlLabelTag, 'Control1'); + spectator.service.registerControl( + 'control1', + elRef, + ); + + expect(spectator.service.getControlNames()).toEqual(['control1']); + spectator.service.unregisterControl('control1'); + expect(spectator.service.getControlNames()).toEqual([]); }); }); - describe('getControlByName', () => { - it('returns control by name', () => { - expect(spectator.service.getControlNames()).toEqual(['test_control_2']); + it('registers section control', () => { + const ngControl = new MockNgControl(); + const formSection = { + label(): string { return 'Form Section'; }, + } as IxFormSectionComponent; + spectator.service.registerSectionControl( + ngControl, + formSection, + ); + + testScheduler.run(({ expectObservable }) => { + expectObservable(spectator.service.controlSections$).toBe('a', { + a: [ + { section: formSection, controls: [ngControl] }, + ], + }); }); - }); - describe('getElementByControlName', () => { - it('returns element by control name', () => { - expect(spectator.service.getElementByControlName('test_control_2')).toEqual({ - id: 'test_element_2', + spectator.service.unregisterSectionControl(formSection, ngControl); + testScheduler.run(({ expectObservable }) => { + expectObservable(spectator.service.controlSections$).toBe('a', { + a: [], }); }); }); diff --git a/src/app/modules/forms/ix-forms/services/ix-form.service.ts b/src/app/modules/forms/ix-forms/services/ix-form.service.ts index 7ad6aa8a009..a2978b3270e 100644 --- a/src/app/modules/forms/ix-forms/services/ix-form.service.ts +++ b/src/app/modules/forms/ix-forms/services/ix-form.service.ts @@ -7,13 +7,13 @@ import { ixControlLabelTag } from 'app/modules/forms/ix-forms/directives/registe @Injectable({ providedIn: 'root' }) export class IxFormService { - private controls = new Map(); - private sections = new Map(); + private readonly controls = new Map(); + private readonly sections = new Map(); private readonly controlNamesWithlabels = new BehaviorSubject([]); private readonly controlSections = new BehaviorSubject([]); - controlNamesWithLabels$: Observable = this.controlNamesWithlabels.asObservable(); - controlSections$: Observable = this.controlSections.asObservable(); + readonly controlNamesWithLabels$: Observable = this.controlNamesWithlabels.asObservable(); + readonly controlSections$: Observable = this.controlSections.asObservable(); getControlNames(): (string | number | null)[] { return [...this.controls.keys()];