-
Notifications
You must be signed in to change notification settings - Fork 45
Jest Test: Example Jest Test
Dan Morse edited this page Sep 16, 2021
·
5 revisions
- Allow for an easily repeatable test structure
- Improves the stability of the test being run on Travis
- Consistent formatting
import { render, stopServer, renderWC } from "../../../testing/testing-helpers";
import schema from "../[COMPONENT NAME].schema";
const componentSelector = "bolt-[COMPONENT NAME]";
const { [PROP KEY], [PROP KEY]... } = schema.properties;
let page, fixtures;
afterAll(async () => {
await stopServer();
await page.close();
}, 100);
beforeEach(async () => {
await page.evaluate(() => {
document.body.innerHTML = "";
});
});
beforeAll(async () => {
page = await global.__BROWSER__.newPage();
await page.goto("http://127.0.0.1:4444/", {
timeout: 0,
});
const defaultData = {
[DATA THAT ALL THE TEST WILL USE]
};
fixtures = {
defaultData,
};
});
describe("Bolt [COMPONENT/ELEMENT NAME] [TYPE]", () => {
// With a Web Component
test(`default`, async () => {
const results = await render(
"@bolt-components-[COMPONENT NAME]/[COMPONENT NAME].twig",
{
...fixtures.defaultData,
}
);
const { innerHTML, outerHTML } = await renderWC(
componentSelector,
results.html,
page
);
await expect(results.ok).toBe(true);
await expect(results.html).toMatchSnapshot();
await expect(innerHTML).toMatchSnapshot();
await expect(outerHTML).toMatchSnapshot();
});
// Without a Web Component
test(`default`, async () => {
const results = await render(
"@bolt-components-[COMPONENT NAME]/[COMPONENT NAME].twig",
{
...fixtures.defaultData,
}
);
await expect(results.ok).toBe(true);
await expect(results.html).toMatchSnapshot();
});
});
describe("Bolt [COMPONENT/ELEMENT NAME] Prop -", () => {
// Target each of the schema keys with the following pattern
[PROP KEY].enum.forEach(async (option) => {
test(`[PROP KEY] items: ${option}`, async () => {
const results = await render(
"@bolt-components-[COMPONENT NAME]/[COMPONENT NAME].twig",
{
...fixtures.defaultData,
[PROP KEY]: option,
}
);
await expect(results.ok).toBe(true);
await expect(results.html).toMatchSnapshot();
});
});
});
- Basic A11y Checklist
- Get started with Bolt locally
- Bolt Specific Standards and Conventions
- How to save SVG graphics and SVG icons
- Upgrade to minor release
- Upgrade to 4.x
- Upgrade to 5.x
- Release Workflow
- VS Code Configuration
- Bolt Doc Writing Guide
- Prefixing Custom Attributes
- Standard Props for Passing Content in Twig
- Building Websites with Bolt in Drupal
- From Design Mockup to Code
- Override with Utility Classes