Skip to content

Commit

Permalink
fix(matchers): 🐛 add toHaveSelectedOptions matcher for jest (#271)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcoops authored Feb 19, 2020
1 parent 66cc962 commit a260377
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions projects/spectator/jest/src/lib/matchers-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ declare namespace jest {
toHaveDescendant(selector: string | Element): boolean;

toHaveDescendantWithText({ selector, text }: { selector: string; text: string }): boolean;

toHaveSelectedOptions(expected: string | string[] | HTMLOptionElement | HTMLOptionElement[]): boolean;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ReactiveFormsModule } from '@angular/forms';
import { Spectator, createComponentFactory } from '@ngneat/spectator/jest';

import { FormSelectComponent } from './form-select.component';

describe('FormSelectComponent', () => {
let spectator: Spectator<FormSelectComponent>;

const createComponent = createComponentFactory<FormSelectComponent>({
component: FormSelectComponent,
imports: [ReactiveFormsModule]
});

beforeEach(() => (spectator = createComponent()));

it('should set the correct option on standard select', () => {
const select = spectator.query('#test-single-select') as HTMLSelectElement;
spectator.selectOption(select, '1');
expect(select).toHaveSelectedOptions('1');
});
});
21 changes: 21 additions & 0 deletions projects/spectator/jest/test/form-select/form-select.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Component, ChangeDetectionStrategy } from '@angular/core';

@Component({
selector: 'app-form-select',
template: `
<select id="test-single-select">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class FormSelectComponent {
/**
* Empty method to spy on
*/
public handleChange(): void {
return;
}
}

0 comments on commit a260377

Please sign in to comment.