Convert Switch tests to svelte-inline-compile

This commit is contained in:
Ryan Gossiaux
2022-05-18 02:06:04 -07:00
parent bdbc6f4cdd
commit e36ad5a8d2

View File

@@ -1,5 +1,4 @@
import { render } from "@testing-library/svelte"; import { render } from "@testing-library/svelte";
import TestRenderer from "../../test-utils/TestRenderer.svelte";
import { Switch, SwitchDescription, SwitchGroup, SwitchLabel } from "."; import { Switch, SwitchDescription, SwitchGroup, SwitchLabel } from ".";
import { import {
assertActiveElement, assertActiveElement,
@@ -15,9 +14,9 @@ jest.mock("../../hooks/use-id");
describe("Safe guards", () => { describe("Safe guards", () => {
it("should be possible to render a Switch without crashing", () => { it("should be possible to render a Switch without crashing", () => {
render(TestRenderer, { render(svelte`
allProps: [Switch, { checked: false, onChange: console.log }], <Switch checked={false} on:change={console.log} />
}); `);
}); });
}); });
@@ -43,16 +42,16 @@ describe("Rendering", () => {
}) })
it("should be possible to render an (on) Switch using an `as` prop", () => { it("should be possible to render an (on) Switch using an `as` prop", () => {
render(TestRenderer, { render(svelte`
allProps: [Switch, { as: "span", checked: true, onChange: console.log }], <Switch as={"span"} checked={true} on:change={console.log} />
}); `);
assertSwitch({ state: SwitchState.On, tag: "span" }); assertSwitch({ state: SwitchState.On, tag: "span" });
}); });
it("should be possible to render an (off) Switch using an `as` prop", () => { it("should be possible to render an (off) Switch using an `as` prop", () => {
render(TestRenderer, { render(svelte`
allProps: [Switch, { as: "span", checked: false, onChange: console.log }], <Switch as={"span"} checked={false} on:change={console.log} />
}); `);
assertSwitch({ state: SwitchState.Off, tag: "span" }); assertSwitch({ state: SwitchState.Off, tag: "span" });
}); });
@@ -67,37 +66,25 @@ describe("Rendering", () => {
describe("`type` attribute", () => { describe("`type` attribute", () => {
it('should set the `type` to "button" by default', async () => { it('should set the `type` to "button" by default', async () => {
render(TestRenderer, { render(svelte`
allProps: [ <Switch checked={false} on:change={console.log}>Trigger</Switch>
Switch, `);
{ checked: false, onChange: console.log },
"Trigger",
],
});
expect(getSwitch()).toHaveAttribute("type", "button"); expect(getSwitch()).toHaveAttribute("type", "button");
}); });
it('should not set the `type` to "button" if it already contains a `type`', async () => { it('should not set the `type` to "button" if it already contains a `type`', async () => {
render(TestRenderer, { render(svelte`
allProps: [ <Switch checked={false} on:change={console.log} type={"submit"}>Trigger</Switch>
Switch, `);
{ checked: false, onChange: console.log, type: "submit" },
"Trigger",
],
});
expect(getSwitch()).toHaveAttribute("type", "submit"); expect(getSwitch()).toHaveAttribute("type", "submit");
}); });
it('should not set the type if the "as" prop is not a "button"', async () => { it('should not set the type if the "as" prop is not a "button"', async () => {
render(TestRenderer, { render(svelte`
allProps: [ <Switch checked={false} on:change={console.log} as={"div"}>Trigger</Switch>
Switch, `);
{ checked: false, onChange: console.log, as: "div" },
"Trigger",
],
});
expect(getSwitch()).not.toHaveAttribute("type"); expect(getSwitch()).not.toHaveAttribute("type");
}); });
@@ -106,31 +93,23 @@ describe("Rendering", () => {
describe("Render composition", () => { describe("Render composition", () => {
it("should be possible to render a Switch.Group, Switch and Switch.Label", () => { it("should be possible to render a Switch.Group, Switch and Switch.Label", () => {
render(TestRenderer, { render(svelte`
allProps: [ <SwitchGroup>
SwitchGroup, <Switch checked={false} on:change={console.log} />
{}, <SwitchLabel>Enable notifications</SwitchLabel>
[ </SwitchGroup>
[Switch, { checked: false, onChange: console.log }], `);
[SwitchLabel, {}, "Enable notifications"],
],
],
});
assertSwitch({ state: SwitchState.Off, label: "Enable notifications" }); assertSwitch({ state: SwitchState.Off, label: "Enable notifications" });
}); });
it("should be possible to render a Switch.Group, Switch and Switch.Label (before the Switch)", () => { it("should be possible to render a Switch.Group, Switch and Switch.Label (before the Switch)", () => {
render(TestRenderer, { render(svelte`
allProps: [ <SwitchGroup>
SwitchGroup, <SwitchLabel>Label B</SwitchLabel>
{}, <Switch checked={false} on:change={console.log}>Label A</Switch>
[ </SwitchGroup>
[SwitchLabel, {}, "Label B"], `);
[Switch, { checked: false, onChange: console.log }, "Label A"],
],
],
});
// Warning! Using aria-label or aria-labelledby will hide any descendant content from assistive // Warning! Using aria-label or aria-labelledby will hide any descendant content from assistive
// technologies. // technologies.
@@ -140,16 +119,12 @@ describe("Render composition", () => {
}); });
it("should be possible to render a Switch.Group, Switch and Switch.Label (after the Switch)", () => { it("should be possible to render a Switch.Group, Switch and Switch.Label (after the Switch)", () => {
render(TestRenderer, { render(svelte`
allProps: [ <SwitchGroup>
SwitchGroup, <Switch checked={false} on:change={console.log}>Label A</Switch>
{}, <SwitchLabel>Label B</SwitchLabel>
[ </SwitchGroup>
[Switch, { checked: false, onChange: console.log }, "Label A"], `);
[SwitchLabel, {}, "Label B"],
],
],
});
// Warning! Using aria-label or aria-labelledby will hide any descendant content from assistive // Warning! Using aria-label or aria-labelledby will hide any descendant content from assistive
// technologies. // technologies.
@@ -159,16 +134,12 @@ describe("Render composition", () => {
}); });
it("should be possible to render a Switch.Group, Switch and Switch.Description (before the Switch)", async () => { it("should be possible to render a Switch.Group, Switch and Switch.Description (before the Switch)", async () => {
render(TestRenderer, { render(svelte`
allProps: [ <SwitchGroup>
SwitchGroup, <SwitchDescription>This is an important feature</SwitchDescription>
{}, <Switch checked={false} on:change={console.log} />
[ </SwitchGroup>
[SwitchDescription, {}, "This is an important feature"], `);
[Switch, { checked: false, onChange: console.log }],
],
],
});
assertSwitch({ assertSwitch({
state: SwitchState.Off, state: SwitchState.Off,
@@ -177,16 +148,12 @@ describe("Render composition", () => {
}); });
it("should be possible to render a Switch.Group, Switch and Switch.Description (after the Switch)", () => { it("should be possible to render a Switch.Group, Switch and Switch.Description (after the Switch)", () => {
render(TestRenderer, { render(svelte`
allProps: [ <SwitchGroup>
SwitchGroup, <Switch checked={false} on:change={console.log} />
{}, <SwitchDescription>This is an important feature</SwitchDescription>
[ </SwitchGroup>
[Switch, { checked: false, onChange: console.log }], `);
[SwitchDescription, {}, "This is an important feature"],
],
],
});
assertSwitch({ assertSwitch({
state: SwitchState.Off, state: SwitchState.Off,
@@ -195,17 +162,13 @@ describe("Render composition", () => {
}); });
it("should be possible to render a Switch.Group, Switch, Switch.Label and Switch.Description", () => { it("should be possible to render a Switch.Group, Switch, Switch.Label and Switch.Description", () => {
render(TestRenderer, { render(svelte`
allProps: [ <SwitchGroup>
SwitchGroup, <SwitchLabel>Label A</SwitchLabel>
{}, <Switch checked={false} on:change={console.log} />
[ <SwitchDescription>This is an important feature</SwitchDescription>
[SwitchLabel, {}, "Label A"], </SwitchGroup>
[Switch, { checked: false, onChange: console.log }], `);
[SwitchDescription, {}, "This is an important feature"],
],
],
});
assertSwitch({ assertSwitch({
state: SwitchState.Off, state: SwitchState.Off,
@@ -218,9 +181,9 @@ describe("Render composition", () => {
describe("Keyboard interactions", () => { describe("Keyboard interactions", () => {
describe("`Space` key", () => { describe("`Space` key", () => {
it("should be possible to toggle the Switch with Space", async () => { it("should be possible to toggle the Switch with Space", async () => {
render(TestRenderer, { render(svelte`
allProps: [ManagedSwitch, {}], <ManagedSwitch />
}); `);
// Ensure checkbox is off // Ensure checkbox is off
assertSwitch({ state: SwitchState.Off }); assertSwitch({ state: SwitchState.Off });
@@ -245,9 +208,9 @@ describe("Keyboard interactions", () => {
describe("`Enter` key", () => { describe("`Enter` key", () => {
it("should not be possible to use Enter to toggle the Switch", async () => { it("should not be possible to use Enter to toggle the Switch", async () => {
let handleChange = jest.fn(); let handleChange = jest.fn();
render(TestRenderer, { render(svelte`
allProps: [ManagedSwitch, { onChange: handleChange }], <ManagedSwitch onChange={handleChange}/>
}); `);
// Ensure checkbox is off // Ensure checkbox is off
assertSwitch({ state: SwitchState.Off }); assertSwitch({ state: SwitchState.Off });
@@ -291,9 +254,9 @@ describe("Keyboard interactions", () => {
describe("Mouse interactions", () => { describe("Mouse interactions", () => {
it("should be possible to toggle the Switch with a click", async () => { it("should be possible to toggle the Switch with a click", async () => {
render(TestRenderer, { render(svelte`
allProps: [ManagedSwitch, {}], <ManagedSwitch />
}); `);
// Ensure checkbox is off // Ensure checkbox is off
assertSwitch({ state: SwitchState.Off }); assertSwitch({ state: SwitchState.Off });
@@ -312,16 +275,12 @@ describe("Mouse interactions", () => {
}); });
it("should be possible to toggle the Switch with a click on the Label", async () => { it("should be possible to toggle the Switch with a click on the Label", async () => {
render(TestRenderer, { render(svelte`
allProps: [ <SwitchGroup>
SwitchGroup, <ManagedSwitch />
{}, <SwitchLabel>The label</SwitchLabel>
[ </SwitchGroup>
[ManagedSwitch, {}], `);
[SwitchLabel, {}, "The label"],
],
],
});
// Ensure checkbox is off // Ensure checkbox is off
assertSwitch({ state: SwitchState.Off }); assertSwitch({ state: SwitchState.Off });
@@ -346,16 +305,12 @@ describe("Mouse interactions", () => {
}); });
it("should not be possible to toggle the Switch with a click on the Label (passive)", async () => { it("should not be possible to toggle the Switch with a click on the Label (passive)", async () => {
render(TestRenderer, { render(svelte`
allProps: [ <SwitchGroup>
SwitchGroup, <ManagedSwitch />
{}, <SwitchLabel passive={true}>The label</SwitchLabel>
[ </SwitchGroup>
[ManagedSwitch, {}], `);
[SwitchLabel, { passive: true }, "The label"],
],
],
});
// Ensure checkbox is off // Ensure checkbox is off
assertSwitch({ state: SwitchState.Off }); assertSwitch({ state: SwitchState.Off });