Skip to content

Commit 390c6f3

Browse files
committed
test: move test helpers out of vitest bindings
1 parent 7897caf commit 390c6f3

5 files changed

Lines changed: 35 additions & 36 deletions

File tree

apps/docs/__tests__/TestUtils.res

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
let getByTextExact = (element, text) => Vitest.getByTextWithOptions(element, text, {"exact": true})
2+
3+
let sleep = ms =>
4+
Promise.make((resolve, _) => {
5+
let _timeoutId = setTimeout(~handler=() => {
6+
resolve()
7+
}, ~timeout=ms)
8+
})
9+
10+
external imageFromNode: WebAPI.DOMAPI.node => WebAPI.DOMAPI.htmlImageElement = "%identity"
11+
12+
let waitForImages = async (selector: string) => {
13+
let root = switch document->WebAPI.Document.querySelector(selector) {
14+
| Value(root) => root
15+
| Null => failwith(`expected to find screenshot target ${selector}`)
16+
}
17+
18+
let images = root->WebAPI.Element.querySelectorAll("img")
19+
20+
if images.length > 0 {
21+
for i in 0 to images.length - 1 {
22+
let image = images->WebAPI.NodeList.item(i)->imageFromNode
23+
await image->WebAPI.HTMLImageElement.decode
24+
}
25+
}
26+
}

apps/docs/__tests__/visual/BlogArticle_.test.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ test("desktop blog article with article image shows image", async () => {
166166
await element(title)->toBeVisible
167167

168168
let wrapper = await screen->getByTestId("blog-article-wrapper")
169-
await waitForImages("[data-testid='blog-article-wrapper']")
169+
await TestUtils.waitForImages("[data-testid='blog-article-wrapper']")
170170
await element(wrapper)->toMatchScreenshot("desktop-blog-article-with-image")
171171
})
172172

apps/docs/__tests__/visual/LandingPage_.test.res

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
open ReactRouter
22
open Vitest
33

4-
@module("vitest")
5-
external testWithTimeout: (string, unit => promise<unit>, int) => unit = "test"
6-
7-
let sleep = ms =>
8-
Promise.make((resolve, _) => {
9-
let _timeoutId = setTimeout(~handler=() => {
10-
resolve()
11-
}, ~timeout=ms)
12-
})
13-
144
let snapshotSection = async (~width, ~height, ~sectionTestId, ~screenshotName) => {
155
await viewport(width, height)
166

@@ -29,11 +19,11 @@ let snapshotSection = async (~width, ~height, ~sectionTestId, ~screenshotName) =
2919

3020
if sectionTestId == "landing-other-selling-points" {
3121
let sourceSelector = `[data-testid="${sectionTestId}"]`
32-
await waitForImages(sourceSelector)
22+
await TestUtils.waitForImages(sourceSelector)
3323
// Headless UI's appear transition mutates classes after first render. Since
3424
// these tests snapshot a cloned outerHTML string, wait for the live section
3525
// to settle so the clone does not preserve a transient opacity-0 state.
36-
await sleep(1100)
26+
await TestUtils.sleep(1100)
3727
}
3828

3929
let sandboxTestId = `${sectionTestId}-snapshot`
@@ -50,7 +40,7 @@ let snapshotSection = async (~width, ~height, ~sectionTestId, ~screenshotName) =
5040

5141
let snapshotTarget = await snapshotScreen->getByTestId(sandboxTestId)
5242
await element(snapshotTarget)->toBeVisible
53-
await waitForImages(`[data-testid="${sandboxTestId}"]`)
43+
await TestUtils.waitForImages(`[data-testid="${sandboxTestId}"]`)
5444
await element(snapshotTarget)->toMatchScreenshot(screenshotName)
5545
await snapshotScreen->unmount
5646
}

apps/docs/__tests__/visual/MarkdownComponents_.test.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ test("renders Image with caption", async () => {
214214
await element(caption)->toBeVisible
215215

216216
let wrapper = await screen->getByTestId("image-wrapper")
217-
await waitForImages("[data-testid='image-wrapper']")
217+
await TestUtils.waitForImages("[data-testid='image-wrapper']")
218218
await element(wrapper)->toMatchScreenshot("markdown-image")
219219
})
220220

@@ -292,7 +292,7 @@ test("renders Image with small size", async () => {
292292
await element(caption)->toBeVisible
293293

294294
let wrapper = await screen->getByTestId("image-small-wrapper")
295-
await waitForImages("[data-testid='image-small-wrapper']")
295+
await TestUtils.waitForImages("[data-testid='image-small-wrapper']")
296296
await element(wrapper)->toMatchScreenshot("markdown-image-small")
297297
})
298298

packages/shared/src/Vitest.res

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ type mock
99
@module("vitest")
1010
external test: (string, unit => promise<unit>) => unit = "test"
1111

12+
@module("vitest")
13+
external testWithTimeout: (string, unit => promise<unit>, int) => unit = "test"
14+
1215
@module("vitest") @scope("vi")
1316
external fn: unit => 'a => 'b = "fn"
1417

@@ -45,8 +48,6 @@ external getByText: (element, string) => promise<element> = "getByText"
4548
@send
4649
external getByTextWithOptions: (element, string, {"exact": bool}) => promise<element> = "getByText"
4750

48-
let getByTextExact = (element, text) => getByTextWithOptions(element, text, {"exact": true})
49-
5051
@send
5152
external getByLabelText: (element, string) => promise<element> = "getByLabelText"
5253

@@ -56,24 +57,6 @@ external getAllByLabelText: (element, string) => promise<array<element>> = "getA
5657
@send
5758
external getByRole: (element, [#button]) => promise<element> = "getByRole"
5859

59-
external imageFromNode: WebAPI.DOMAPI.node => WebAPI.DOMAPI.htmlImageElement = "%identity"
60-
61-
let waitForImages = async (selector: string) => {
62-
let root = switch document->WebAPI.Document.querySelector(selector) {
63-
| Value(root) => root
64-
| Null => failwith(`expected to find screenshot target ${selector}`)
65-
}
66-
67-
let images = root->WebAPI.Element.querySelectorAll("img")
68-
69-
if images.length > 0 {
70-
for i in 0 to images.length - 1 {
71-
let image = images->WebAPI.NodeList.item(i)->imageFromNode
72-
await image->WebAPI.HTMLImageElement.decode
73-
}
74-
}
75-
}
76-
7760
/**
7861
* Actions
7962
*/

0 commit comments

Comments
 (0)