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

fix(core/select): added test for undefined select items #1568

Merged
merged 4 commits into from
Dec 17, 2024
Merged
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
84 changes: 73 additions & 11 deletions packages/angular-test-app/src/select/select.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,66 @@ import { By } from '@angular/platform-browser';

@Component({
selector: 'ix-example-form-select',
template: ` <form [formGroup]="form">
<button>Test</button>
<ix-button>Test</ix-button>
<ix-select formControlName="select">
<ix-select-item label="Item 1" value="1"></ix-select-item>
<ix-select-item label="Item 2" value="2"></ix-select-item>
<ix-select-item label="Item 3" value="3"></ix-select-item>
<ix-select-item label="Item 4" value="4"></ix-select-item>
template: `
<form [formGroup]="form">
<ix-select formControlName="select">
<ix-select-item label="Item 1" value="1"></ix-select-item>
<ix-select-item label="Item 2" value="2"></ix-select-item>
<ix-select-item label="Item 3" value="3"></ix-select-item>
<ix-select-item label="Item 4" value="4"></ix-select-item>
</ix-select>
</form>
<h1>TypeError-Example</h1>
<ix-select [value]="value">
<ix-select-item
*ngFor="let item of selection"
[label]="item"
[value]="item"
></ix-select-item>
</ix-select>
</form>`,
`,
})
class SelectComponent {
public form = new FormGroup({ select: new FormControl('1') });
value = '3';
selection = ['3', '4', '5'];

public updateSelection() {
this.value = '6';
this.selection = ['6', '7', '8'];
}
}

function waitForHydration(element: HTMLElement, timeout = 5000): Promise<void> {
return new Promise((resolve, reject) => {
const interval = 50;
let elapsedTime = 0;

const checkClass = () => {
if (element.classList.contains('hydrated')) {
resolve();
} else if (elapsedTime >= timeout) {
reject(new Error(`Timeout waiting for hydration`));
} else {
elapsedTime += interval;
setTimeout(checkClass, interval);
}
};

checkClass();
});
}

function checkForError(consoleSpy: jasmine.Spy, errorName: string): boolean {
return consoleSpy.calls
.allArgs()
.some((arg) => arg[0].toString().includes(errorName));
}

describe('SelectFormComponent', () => {
let component: SelectComponent;
let fixture: ComponentFixture<SelectComponent>;
let consoleSpy: jasmine.Spy;

beforeEach(async () => {
await TestBed.configureTestingModule({
Expand All @@ -44,19 +86,39 @@ describe('SelectFormComponent', () => {

fixture = TestBed.createComponent(SelectComponent);
component = fixture.componentInstance;

consoleSpy = spyOn(console, 'error').and.callThrough();

fixture.detectChanges();
});

beforeEach(async () => {
// until https://github.com/angular/angular/issues/24218 is fixed
await TestBed.inject(ApplicationInitStatus).donePromise;
});

it('should change the input value', async () => {
const select = fixture.debugElement.query(By.css('ix-select'));
it('should change the form control value', async () => {
const select = fixture.debugElement.query(By.css('ix-select[formControlName="select"]'));

await waitForHydration(select.nativeElement);

component.form.get('select')!.setValue('2');
fixture.detectChanges();

expect(select.nativeElement.value).toBe('2');
expect(component).toBeDefined();
});

it('should change the input value and check for errors', async () => {
const select = fixture.debugElement.query(By.css('ix-select:not([formControlName="select"])'));

await waitForHydration(select.nativeElement);

component.updateSelection();
fixture.detectChanges();

expect(checkForError(consoleSpy, 'TypeError')).toBe(false);
expect(select.nativeElement.value).toBe('6');
expect(component).toBeDefined();
});
});
Loading