-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* yarn add vitest + @testing-library/svelte, create vite.config.ts * write some first basic tests * run vitest in CI on push + PR to main * add test status readme badge
- Loading branch information
Showing
6 changed files
with
873 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: Tests | ||
|
||
on: | ||
workflow_dispatch: | ||
push: | ||
branches: [main] | ||
pull_request: | ||
branches: [main] | ||
|
||
jobs: | ||
tests: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Check out repo | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up node v17 | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: 17 | ||
cache: yarn | ||
|
||
- name: Install dependencies | ||
run: yarn | ||
|
||
- name: Run tests | ||
run: yarn test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { fireEvent, render } from '@testing-library/svelte' | ||
import { expect, test } from 'vitest' | ||
import MultiSelect from '../src/lib' | ||
|
||
const options = [`Banana`, `Watermelon`, `Apple`, `Dates`, `Mango`] | ||
const placeholder = `Select fruits` | ||
|
||
test(`can focus input, enter text, toggle hidden options and select an option`, async () => { | ||
const { getByPlaceholderText, getByText, container } = render(MultiSelect, { | ||
options, | ||
placeholder, | ||
}) | ||
|
||
let ul_ops = container.querySelector(`ul.options`) | ||
|
||
expect(ul_ops?.classList.contains(`hidden`)).to.equal(true) | ||
|
||
const input = getByPlaceholderText(`Select fruits`) | ||
await fireEvent.focus(input) | ||
await fireEvent.input(input, { target: { value: `Apple` } }) | ||
|
||
// use :last-child here cause for some reason there are two ul.options after toggling showOptions | ||
// even though {#key showOptions} is supposed to destroy old DOM nodes | ||
ul_ops = container.querySelector(`ul.options:last-child`) | ||
expect(ul_ops?.classList.contains(`hidden`)).to.equal(false) | ||
|
||
const apple_op = getByText(`Apple`, { | ||
selector: `ul:last-child.options > li`, | ||
}) | ||
|
||
await fireEvent.mouseDown(apple_op) | ||
|
||
const apple_sel = getByText(`Apple`, { selector: `ul.selected > li` }) | ||
expect(apple_sel.textContent?.trim()).toBe(`Apple`) | ||
}) | ||
|
||
test(`default export from index.ts is same as component file`, async () => { | ||
const { default: comp } = await import(`../src/lib/MultiSelect.svelte`) | ||
expect(comp).toBe(MultiSelect) | ||
}) | ||
|
||
// TODO: requires component interaction before classes appear in DOM | ||
// [`liSelectedClass`, `div.multiselect > ul.selected > li`], | ||
// [`liActiveOptionClass`, `div.multiselect > ul.options > li.active`], | ||
for (const [className, selector] of [ | ||
[`outerDivClass`, `div.multiselect`], | ||
[`ulSelectedClass`, `div.multiselect > ul.selected`], | ||
[`ulOptionsClass`, `div.multiselect > ul.options`], | ||
[`liOptionClass`, `div.multiselect > ul.options > li`], | ||
]) { | ||
test(`${className} attaches to correct DOM node`, async () => { | ||
const { container } = render(MultiSelect, { | ||
options, | ||
[className]: `test-${className}`, | ||
}) | ||
|
||
const dom_node = container.querySelector(`${selector}.test-${className}`) | ||
expect(dom_node).toBeTruthy() | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { svelte } from '@sveltejs/vite-plugin-svelte' | ||
|
||
export default { | ||
plugins: [svelte({ hot: !process.env.VITEST })], | ||
test: { | ||
environment: `jsdom`, | ||
}, | ||
} |
Oops, something went wrong.