Skip to content

Commit b12ab2b

Browse files
author
Tom Adam
committed
[#177] Faking visualViewPort in jest tests + code cleanup.
1 parent 0c8cbc1 commit b12ab2b

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

src/tests/async.test.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,21 @@ import SimpleAutocomplete from "../SimpleAutocomplete.svelte"
33
import '@testing-library/jest-dom/extend-expect'
44
import '@testing-library/jest-dom'
55

6-
async function colors(keyword, nb_items_max) {
6+
async function colors() {
77
return ["White", "Red", "Yellow", "Green", "Blue", "Black", "Mät bläck", "<i>Jét Black</i>"]
88
}
99

10+
window = Object.assign(window, { visualViewport: { height: 1500 } })
11+
1012
test("at first, menu is empty", async () => {
11-
const { component, container } = render(SimpleAutocomplete, {searchFunction: colors})
13+
render(SimpleAutocomplete, {searchFunction: colors})
1214

1315
expect(await screen.queryByText('White')).not.toBeInTheDocument()
1416
expect(await screen.queryByText('Red')).not.toBeInTheDocument()
1517
})
1618

1719
test("even with the input is focused, the menu is empty", async () => {
18-
const { component, container } = render(SimpleAutocomplete, {searchFunction: colors})
20+
const { container } = render(SimpleAutocomplete, {searchFunction: colors})
1921
const queryInput = container.querySelector("input[type='text']");
2022

2123
await fireEvent.focus(queryInput)
@@ -25,7 +27,7 @@ test("even with the input is focused, the menu is empty", async () => {
2527
})
2628

2729
test("when something is queried, only the matching items are shown", async () => {
28-
const { component, container } = render(SimpleAutocomplete, {searchFunction: colors})
30+
const { container } = render(SimpleAutocomplete, {searchFunction: colors})
2931
const queryInput = container.querySelector("input[type='text']");
3032

3133
await fireEvent.input(queryInput, { target: { value: "white" } })
@@ -36,7 +38,8 @@ test("when something is queried, only the matching items are shown", async () =>
3638
})
3739

3840
test("when nothing matches the query, no item is show", async () => {
39-
const { component, container } = render(SimpleAutocomplete, {searchFunction: colors})
41+
42+
const { container } = render(SimpleAutocomplete, {searchFunction: colors})
4043
const queryInput = container.querySelector("input[type='text']");
4144

4245
await fireEvent.input(queryInput, { target: { value: "not-a-color" } })
@@ -46,9 +49,8 @@ test("when nothing matches the query, no item is show", async () => {
4649
})
4750

4851
test("when something is queried, the query is highlighted", async () => {
49-
const { component, container } = render(SimpleAutocomplete, {searchFunction: colors})
52+
const { container } = render(SimpleAutocomplete, {searchFunction: colors})
5053
const queryInput = container.querySelector("input[type='text']");
51-
const list = container.querySelector("autocomplete-list");
5254

5355
await fireEvent.input(queryInput, { target: { value: "whi" } })
5456

src/tests/highlight.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { render } from "@testing-library/svelte"
22
import SimpleAutocomplete from "../SimpleAutocomplete.svelte"
33

4+
window = Object.assign(window, { visualViewport: { height: 1500 } })
5+
46
test("test simple hightlights", async () => {
57
const { component } = render(SimpleAutocomplete)
68
var item = {

src/tests/multiple.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import '@testing-library/jest-dom'
55

66
const colors = ["White", "Red", "Yellow", "Green", "Blue", "Black"]
77

8+
window = Object.assign(window, { visualViewport: { height: 1500 } })
9+
810
test("when selection is multiple, selectedItem is a list", async () => {
911
const { component, container } = render(SimpleAutocomplete, {items: colors, multiple: true})
1012
const queryInput = container.querySelector("input[type='text']");

src/tests/sync.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import '@testing-library/jest-dom'
55

66
const colors = ["White", "Red", "Yellow", "Green", "Blue", "Black", "Mät bläck", "<i>Jét Black</i>"]
77

8+
window = Object.assign(window, { visualViewport: { height: 1500 } })
9+
810
test("items are generated but hidden", async () => {
911
const { component, container } = render(SimpleAutocomplete, {items: colors})
1012

0 commit comments

Comments
 (0)