Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NAS-133118 / 25.04 / Add tests for IxFormService #11288

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 67 additions & 55 deletions src/app/modules/forms/ix-forms/services/ix-form.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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<IxFormService>;
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<HTMLElement>(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<HTMLElement>(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: [],
});
});
});
Expand Down
8 changes: 4 additions & 4 deletions src/app/modules/forms/ix-forms/services/ix-form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import { ixControlLabelTag } from 'app/modules/forms/ix-forms/directives/registe

@Injectable({ providedIn: 'root' })
export class IxFormService {
private controls = new Map<string, HTMLElement>();
private sections = new Map<IxFormSectionComponent, (NgControl | null)[]>();
private readonly controls = new Map<string, HTMLElement>();
private readonly sections = new Map<IxFormSectionComponent, (NgControl | null)[]>();
private readonly controlNamesWithlabels = new BehaviorSubject<ControlNameWithLabel[]>([]);
private readonly controlSections = new BehaviorSubject<SectionWithControls[]>([]);

controlNamesWithLabels$: Observable<ControlNameWithLabel[]> = this.controlNamesWithlabels.asObservable();
controlSections$: Observable<SectionWithControls[]> = this.controlSections.asObservable();
readonly controlNamesWithLabels$: Observable<ControlNameWithLabel[]> = this.controlNamesWithlabels.asObservable();
readonly controlSections$: Observable<SectionWithControls[]> = this.controlSections.asObservable();

getControlNames(): (string | number | null)[] {
return [...this.controls.keys()];
Expand Down
Loading