Skip to content

Commit 39aa2fc

Browse files
committed
Add unit tests
1 parent abb8474 commit 39aa2fc

File tree

3 files changed

+115
-8
lines changed

3 files changed

+115
-8
lines changed

src/LineWriterImpl.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
Copyright 2022 Google LLC
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
https://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
import { LineWriter } from "@puppeteer/replay"
15+
16+
export class LineWriterImpl implements LineWriter {
17+
#indentation: string
18+
#currentIndentation = 0
19+
#lines: string[] = []
20+
21+
constructor(indentation: string) {
22+
this.#indentation = indentation
23+
}
24+
25+
appendLine(line: string): LineWriter {
26+
const indentedLine = line
27+
? this.#indentation.repeat(this.#currentIndentation) + line.trimEnd()
28+
: ""
29+
this.#lines.push(indentedLine)
30+
return this
31+
}
32+
33+
startBlock(): LineWriter {
34+
this.#currentIndentation++
35+
return this
36+
}
37+
38+
endBlock(): LineWriter {
39+
this.#currentIndentation--
40+
return this
41+
}
42+
43+
toString(): string {
44+
// Scripts should end with a final blank line.
45+
return this.#lines.join("\n")
46+
}
47+
}

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ export class Extension implements StringifyExtension {
4646
switch (step.type) {
4747
case "click":
4848
out.appendLine(
49-
`userEvent.click(${JSON.stringify(
50-
stringifySelector(step.selectors[0]),
51-
)})`,
49+
`await userEvent.click(${stringifySelector(step.selectors[0])}${
50+
step.button === "secondary" ? ", { buttons: 2 }" : ""
51+
})`,
5252
)
5353
break
5454
case "navigate":

src/test.ts

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,71 @@
11
import { readFile } from "fs/promises"
22
import { join } from "path"
3-
import { stringify, UserFlow } from "@puppeteer/replay"
3+
import {
4+
stringify,
5+
type Selector,
6+
type Step,
7+
type UserFlow,
8+
} from "@puppeteer/replay"
49
import { Extension } from "."
510
import flow from "./fixtures/Example.json"
11+
import { LineWriterImpl } from "./LineWriterImpl"
612

7-
test("Extension", async () => {
8-
expect(
9-
await stringify(flow as UserFlow, { extension: new Extension() }),
10-
).toBe(await readFile(join(__dirname, "fixtures/example.test.js"), "utf8"))
13+
const extension = new Extension()
14+
const selectors: Selector[] = [["aria/Test"], ["#test"]]
15+
16+
describe("Extension", () => {
17+
test("stringify", async () => {
18+
expect(await stringify(flow as UserFlow, { extension })).toBe(
19+
await readFile(join(__dirname, "fixtures/example.test.js"), "utf8"),
20+
)
21+
})
22+
23+
describe("stringifyStep", () => {
24+
test.each<[Step, string]>([
25+
[
26+
{
27+
type: "click",
28+
selectors,
29+
offsetX: 0,
30+
offsetY: 0,
31+
},
32+
'await userEvent.click(screen.getByText("Test"))',
33+
],
34+
[
35+
{
36+
type: "click",
37+
selectors,
38+
button: "secondary",
39+
offsetX: 0,
40+
offsetY: 0,
41+
},
42+
'await userEvent.click(screen.getByText("Test"), { buttons: 2 })',
43+
],
44+
[
45+
{
46+
type: "navigate",
47+
url: "https://example.com/",
48+
assertedEvents: [
49+
{
50+
type: "navigation",
51+
url: "https://example.com/",
52+
title: "Example Domain",
53+
},
54+
],
55+
},
56+
'expect(location.href).toBe("https://example.com/")\nexpect(document.title).toBe("Example Domain")',
57+
],
58+
[
59+
{
60+
type: "waitForElement",
61+
selectors,
62+
},
63+
'await waitFor(() => screen.getByText("Test"))',
64+
],
65+
])("%p", async (step, expected) => {
66+
const writer = new LineWriterImpl(" ")
67+
await extension.stringifyStep(writer, step, { title: "", steps: [step] })
68+
expect(writer.toString()).toBe(expected)
69+
})
70+
})
1171
})

0 commit comments

Comments
 (0)