Skip to content

Commit

Permalink
Fix coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardHelm committed Nov 6, 2024
1 parent 6aa9a05 commit a87ebbe
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 33 deletions.
86 changes: 70 additions & 16 deletions libs/components/src/shared/foundation/button/button.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,25 +538,42 @@ describe('Foundation Button', () => {

await connect();

const wasSumbitted = await new Promise((resolve) => {
// Resolve as true when the event listener is handled
form.addEventListener(
'submit',
// @ts-expect-error overload
(event: Event & { submitter: HTMLElement }) => {
event.preventDefault();
expect(event.submitter).toEqual(element.proxy);
resolve(true);
}
);
form.requestSubmit = jest.fn();

element.click();
element.click();

// Resolve false on the next update in case reset hasn't happened
DOM.queueUpdate(() => resolve(false));
});
expect(form.requestSubmit).toHaveBeenCalledWith(element.proxy);

await disconnect();
});

it('should no longer submit the parent form when clicked after type is changed', async () => {
const { connect, disconnect, element, parent } = await setup();
const form = document.createElement('form');
element.setAttribute('type', 'submit');
element.setAttribute('type', '');
form.appendChild(element);
parent.appendChild(form);

await connect();

expect(wasSumbitted).toEqual(true);
form.requestSubmit = jest.fn();

element.click();

expect(form.requestSubmit).not.toHaveBeenCalled();

await disconnect();
});

it('should should not throw when clicked without a form', async () => {
const { connect, disconnect, element, parent } = await setup();
element.setAttribute('type', 'submit');
parent.appendChild(element);

await connect();

element.click();

await disconnect();
});
Expand Down Expand Up @@ -586,6 +603,43 @@ describe('Foundation Button', () => {

await disconnect();
});

it('should no longer reset the parent form when clicked after type is changed', async () => {
const { connect, disconnect, element, parent } = await setup();
const form = document.createElement('form');
element.setAttribute('type', 'reset');
element.setAttribute('type', '');
form.appendChild(element);
parent.appendChild(form);

await connect();

const wasReset = await new Promise((resolve) => {
// Resolve true when the event listener is handled
form.addEventListener('reset', () => resolve(true));

element.click();

// Resolve false on the next update in case reset hasn't happened
DOM.queueUpdate(() => resolve(false));
});

expect(wasReset).toEqual(false);

await disconnect();
});

it('should should not throw when clicked without a form', async () => {
const { connect, disconnect, element, parent } = await setup();
element.setAttribute('type', 'reset');
parent.appendChild(element);

await connect();

element.click();

await disconnect();
});
});

describe("of 'disabled'", () => {
Expand Down
23 changes: 6 additions & 17 deletions libs/components/src/shared/foundation/button/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ export class FoundationButton extends FormAssociatedButton {

this.proxy.setAttribute('type', this.type);

const elements = Array.from(this.control?.children) as HTMLSpanElement[];
const elements = Array.from(this.control.children) as HTMLSpanElement[];
if (elements) {
elements.forEach((span: HTMLSpanElement) => {
span.addEventListener('click', this.handleClick);
Expand All @@ -182,7 +182,7 @@ export class FoundationButton extends FormAssociatedButton {
public override disconnectedCallback(): void {
super.disconnectedCallback();

const elements = Array.from(this.control?.children) as HTMLSpanElement[];
const elements = Array.from(this.control.children) as HTMLSpanElement[];
if (elements) {
elements.forEach((span: HTMLSpanElement) => {
span.removeEventListener('click', this.handleClick);
Expand All @@ -208,21 +208,11 @@ export class FoundationButton extends FormAssociatedButton {
return;
}

const attached = this.proxy.isConnected;
this.attachProxy();

if (!attached) {
this.attachProxy();
}

// Browser support for requestSubmit is not comprehensive
// so click the proxy if it isn't supported
typeof this.form.requestSubmit === 'function'
? this.form.requestSubmit(this.proxy)
: this.proxy.click();
this.form.requestSubmit(this.proxy);

if (!attached) {
this.detachProxy();
}
this.detachProxy();
};

/**
Expand All @@ -232,8 +222,7 @@ export class FoundationButton extends FormAssociatedButton {
this.form?.reset();
};

// @ts-expect-error Type is incorrectly non-optional
public control: HTMLButtonElement;
public control!: HTMLButtonElement;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ describe('The fixture helper', () => {
document.body.removeChild(element.parentElement!);
});

it('should not throw when connect is called multiple times', async () => {
const { connect } = await fixture(name);

await connect();
await connect();
});

it('can disconnect an element', async () => {
const { element, connect, disconnect } = await fixture(name);

Expand Down

0 comments on commit a87ebbe

Please sign in to comment.