Skip to content

Commit 4af8696

Browse files
committed
refactor: More test bed changes
1 parent 49ad6f4 commit 4af8696

File tree

2 files changed

+23
-21
lines changed

2 files changed

+23
-21
lines changed

src/components/button/button.spec.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { elementUpdated, expect, fixture, html } from '@open-wc/testing';
22

33
import { defineComponents } from '../common/definitions/defineComponents.js';
4-
import { FormAssociatedTestBed, isFocused } from '../common/utils.spec.js';
4+
import {
5+
createFormAssociatedTestBed,
6+
isFocused,
7+
} from '../common/utils.spec.js';
8+
import IgcInputComponent from '../input/input.js';
59
import IgcButtonComponent from './button.js';
610

711
const Variants: Array<IgcButtonComponent['variant']> = [
@@ -14,7 +18,7 @@ const Types: Array<IgcButtonComponent['type']> = ['button', 'reset', 'submit'];
1418

1519
describe('Button tests', () => {
1620
let button: IgcButtonComponent;
17-
before(() => defineComponents(IgcButtonComponent));
21+
before(() => defineComponents(IgcButtonComponent, IgcInputComponent));
1822

1923
describe('Button component', () => {
2024
const ignored_DOM_parts = {
@@ -214,49 +218,47 @@ describe('Button tests', () => {
214218
});
215219

216220
describe('Form integration', () => {
217-
const spec = new FormAssociatedTestBed(
218-
html`<input type="text" name="username" value="John Doe" />
219-
<igc-button type="submit">Submit</igc-button>`
220-
);
221+
let button: IgcButtonComponent;
222+
const spec = createFormAssociatedTestBed<IgcInputComponent>(html`
223+
<igc-input type="text" name="username" value="John Doe"></igc-input>
224+
<igc-button type="submit">Submit</igc-button>
225+
`);
221226

222-
beforeEach(async () => await spec.setup(IgcButtonComponent.tagName));
227+
beforeEach(async () => {
228+
await spec.setup(IgcInputComponent.tagName);
229+
button = spec.form.querySelector(IgcButtonComponent.tagName)!;
230+
});
223231

224232
it('is form associated', async () => {
225-
expect(spec.element.form).to.equal(spec.form);
233+
expect(button.form).to.equal(spec.form);
226234
});
227235

228236
it('submits the associated form', async () => {
229-
const button = spec.element as unknown as IgcButtonComponent;
230-
231237
const handler = (event: SubmitEvent) => {
232238
event.preventDefault();
233-
expect(
234-
new FormData(event.target as HTMLFormElement).get('username')
235-
).to.equal('John Doe');
239+
expect(new FormData(spec.form).get('username')).to.equal('John Doe');
236240
};
237241

238242
spec.form.addEventListener('submit', handler, { once: true });
239243
button.click();
240244
});
241245

242246
it('resets the associated form', async () => {
243-
const button = spec.element as unknown as IgcButtonComponent;
244-
const input = spec.form.querySelector('input') as HTMLInputElement;
245-
246-
input.value = 'Jane Doe';
247+
spec.setProperties({ value: 'Jane Doe' });
247248
button.type = 'reset';
248-
await elementUpdated(button);
249249

250250
button.click();
251-
expect(input.value).to.equal('John Doe');
251+
expect(spec.element.value).to.equal('John Doe');
252252
});
253253

254-
it('reflects disabled ancestor state', async () => {
254+
it('reflects disabled ancestor state', () => {
255255
spec.setAncestorDisabledState(true);
256256
expect(spec.element.disabled).to.be.true;
257+
expect(button.disabled).to.be.true;
257258

258259
spec.setAncestorDisabledState(false);
259260
expect(spec.element.disabled).to.be.false;
261+
expect(button.disabled).to.be.false;
260262
});
261263
});
262264
});

src/components/common/utils.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export function createFormAssociatedTestBed<T extends IgcFormControl>(
2222

2323
const initialFormData = Object.freeze(new FormData());
2424

25-
export class FormAssociatedTestBed<T extends IgcFormControl> {
25+
class FormAssociatedTestBed<T extends IgcFormControl> {
2626
private _element!: T;
2727
private _form!: HTMLFormElement;
2828

0 commit comments

Comments
 (0)