Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Component, provideZonelessChangeDetection } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { FormControl, ReactiveFormsModule } from '@angular/forms';

import { ThriftData } from '../../../../models';
import { ThriftAstMetadata } from '../../../../types';

import { StructFormComponent } from './struct-form.component';

@Component({
imports: [ReactiveFormsModule, StructFormComponent],
template: `<v-struct-form [data]="data" [formControl]="control" />`,
})
class StructFormHost {
control = new FormControl<Record<string, unknown>>({});
metadata: ThriftAstMetadata[] = [
{ name: 'test', path: 'test.thrift', ast: { struct: { Parent: [], Empty: [] } } },
];
data = new ThriftData<string, 'struct'>(
this.metadata,
'test',
'Empty',
{ name: 'empty', type: 'Empty', option: 'optional' },
new ThriftData(this.metadata, 'test', 'Parent'),
);
}

describe('Optional struct presence', () => {
beforeEach(() => {
TestBed.configureTestingModule({ providers: [provideZonelessChangeDetection()] });
});

it('checks an empty struct and preserves its value when toggled', async () => {
const fixture = TestBed.createComponent(StructFormHost);
await fixture.whenStable();
const checkbox: HTMLInputElement = fixture.nativeElement.querySelector('input');

expect(checkbox.checked).toBe(true);
expect(fixture.componentInstance.control.value).toEqual({});

checkbox.click();
await fixture.whenStable();
expect(fixture.componentInstance.control.value).toBeNull();

checkbox.click();
await fixture.whenStable();
expect(fixture.componentInstance.control.value).toEqual({});
});

it.each([null, undefined])('leaves an absent struct unchecked (%s)', async (value) => {
const fixture = TestBed.createComponent(StructFormHost);
fixture.componentInstance.control.setValue(value);
await fixture.whenStable();

const checkbox: HTMLInputElement = fixture.nativeElement.querySelector('input');
expect(checkbox.checked).toBe(false);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export class StructFormComponent<T extends Record<string, unknown>>

handleIncomingValue(value: T) {
this.control.patchValue(value as never, { emitEvent: false });
this.setLabelControl(!!(value && Object.keys(value).length));
this.setLabelControl(!isNil(value));
}

override validate(_control: AbstractControl): ValidationErrors | null {
Expand Down
Loading