-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This includes a regression test for #167
- Loading branch information
Showing
13 changed files
with
341 additions
and
27 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,18 @@ | ||
import { basename } from "node:path"; | ||
import { Glob, spawn } from "bun"; | ||
|
||
const glob = new Glob("**/*.test.ts"); | ||
|
||
console.log("Running bun test files individually"); | ||
|
||
// Scans the current working directory and each of its sub-directories recursively | ||
for await (const file of glob.scan(".")) { | ||
if (basename(file) === "bun-test-cannot-run-all-tests.test.ts") { | ||
continue; | ||
} | ||
console.log(`Running: bun test "${file}"`); | ||
|
||
if ((await spawn(["bun", "test", file], {}).exited) !== 0) { | ||
throw new Error(`Bun test failed for file: ${file}`); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/clipboard-polyfill/implementations/text.blank-document.test.ts
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,11 @@ | ||
import { test, expect } from "bun:test"; | ||
|
||
(globalThis as any).document = {}; | ||
|
||
test("writeText(…) failure in an unsupported browser", async () => { | ||
const { writeText } = await import("./text"); | ||
|
||
expect(async () => writeText("hello")).toThrowError( | ||
"document.addEventListener is not a function.", | ||
); | ||
}); |
14 changes: 14 additions & 0 deletions
14
src/clipboard-polyfill/implementations/text.blank-environment.test.ts
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,14 @@ | ||
import { setDebugLog } from "../debug"; | ||
import { test, expect, mock } from "bun:test"; | ||
|
||
const consoleLogMock = mock(console.log); | ||
setDebugLog(consoleLogMock); | ||
|
||
test("writeText(…) failure in a blank environmnent", async () => { | ||
const { writeText } = await import("./text"); | ||
|
||
expect(async () => writeText("hello")).toThrowError( | ||
"Can't find variable: document", | ||
); | ||
expect(consoleLogMock).toHaveBeenCalledTimes(0); | ||
}); |
32 changes: 32 additions & 0 deletions
32
src/clipboard-polyfill/implementations/text.execCommand-fallback.test.ts
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,32 @@ | ||
import { | ||
createDocumentMock, | ||
createDebugLogConsoleMock, | ||
createWriteTextMock, | ||
} from "../../test/mocks"; | ||
import { test, expect, mock } from "bun:test"; | ||
|
||
const debugLogConsoleMock = createDebugLogConsoleMock(); | ||
const { documentMock, eventMock } = createDocumentMock(); | ||
|
||
test("writeText(…) execCommand fallback", async () => { | ||
const { writeText } = await import("./text"); | ||
|
||
expect(() => writeText("hello execCommand fallback")).not.toThrow(); | ||
|
||
expect(debugLogConsoleMock.mock.calls).toEqual([ | ||
["listener called"], | ||
["regular execCopy worked"], | ||
]); | ||
|
||
expect(documentMock.execCommand.mock.calls).toEqual([["copy"]]); | ||
|
||
expect(documentMock.addEventListener).toHaveBeenCalledTimes(1); | ||
expect(documentMock.removeEventListener).toHaveBeenCalledTimes(1); | ||
|
||
expect(eventMock.preventDefault).toHaveBeenCalledTimes(1); | ||
expect(eventMock.clipboardData.setData.mock.calls).toEqual([ | ||
["text/plain", "hello execCommand fallback"], | ||
]); | ||
|
||
expect(eventMock.clipboardData.getData.mock.calls).toEqual([["text/plain"]]); | ||
}); |
43 changes: 43 additions & 0 deletions
43
src/clipboard-polyfill/implementations/text.modern-browser-async-api-disabled.test.ts
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,43 @@ | ||
import { | ||
createDocumentMock, | ||
createDebugLogConsoleMock, | ||
createWriteTextMock, | ||
} from "../../test/mocks"; | ||
import { test, expect, mock } from "bun:test"; | ||
|
||
const debugLogConsoleMock = createDebugLogConsoleMock(); | ||
const writeTextMock = createWriteTextMock(async () => { | ||
throw new Error("writeText(…) is disabled"); | ||
}); | ||
const { documentMock, eventMock } = createDocumentMock(); | ||
|
||
// Regression test for https://github.com/lgarron/clipboard-polyfill/issues/167 | ||
test("writeText(…) success in a modern browser with the async API disabled", async () => { | ||
const { writeText } = await import("./text"); | ||
|
||
expect(() => | ||
writeText("hello modern browser with async API disabled"), | ||
).not.toThrow(); | ||
|
||
expect(debugLogConsoleMock.mock.calls).toEqual([ | ||
["Using `navigator.clipboard.writeText()`."], | ||
["listener called"], | ||
["regular execCopy worked"], | ||
]); | ||
|
||
expect(writeTextMock.mock.calls).toEqual([ | ||
["hello modern browser with async API disabled"], | ||
]); | ||
|
||
expect(documentMock.execCommand.mock.calls).toEqual([["copy"]]); | ||
|
||
expect(documentMock.addEventListener).toHaveBeenCalledTimes(1); | ||
expect(documentMock.removeEventListener).toHaveBeenCalledTimes(1); | ||
|
||
expect(eventMock.preventDefault).toHaveBeenCalledTimes(1); | ||
expect(eventMock.clipboardData.setData.mock.calls).toEqual([ | ||
["text/plain", "hello modern browser with async API disabled"], | ||
]); | ||
|
||
expect(eventMock.clipboardData.getData.mock.calls).toEqual([["text/plain"]]); | ||
}); |
26 changes: 26 additions & 0 deletions
26
src/clipboard-polyfill/implementations/text.modern-browser.test.ts
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,26 @@ | ||
import { | ||
createDocumentMock, | ||
createDebugLogConsoleMock, | ||
createWriteTextMock, | ||
} from "../../test/mocks"; | ||
import { test, expect, mock } from "bun:test"; | ||
|
||
const debugLogConsoleMock = createDebugLogConsoleMock(); | ||
const writeTextMock = createWriteTextMock(); | ||
const { documentMock } = createDocumentMock(); | ||
|
||
test("writeText(…) success", async () => { | ||
const { writeText } = await import("./text"); | ||
|
||
expect(() => writeText("hello modern browser")).not.toThrow(); | ||
|
||
expect(debugLogConsoleMock.mock.calls).toEqual([ | ||
["Using `navigator.clipboard.writeText()`."], | ||
]); | ||
|
||
expect(writeTextMock.mock.calls).toEqual([["hello modern browser"]]); | ||
|
||
expect(documentMock.execCommand).toHaveBeenCalledTimes(0); | ||
expect(documentMock.addEventListener).toHaveBeenCalledTimes(0); | ||
expect(documentMock.removeEventListener).toHaveBeenCalledTimes(0); | ||
}); |
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 { test, expect } from "bun:test"; | ||
|
||
test("`bun test` must be run one file at a time", () => { | ||
console.log( | ||
"\n\n\n\n⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️\n\n\n\n[clipboard-polyfill] Each test file requires a fresh global environment before importing library code. Run `make test-bun` to run test files one at a time instead.\n\n\n\n⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️\n\n\n\n", | ||
); | ||
expect(true).toBe(false); | ||
}); |
Oops, something went wrong.