Skip to content

Commit

Permalink
Add coverage to dom utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnturner committed Dec 13, 2023
1 parent 6f7dd95 commit 6bac34d
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion bunfig.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
[test]
coverage = true
coverageThreshold = 1.0
coverageThreshold = 1.0
preload = "./happydom.ts"
3 changes: 3 additions & 0 deletions happydom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { GlobalRegistrator } from "@happy-dom/global-registrator";

GlobalRegistrator.register();
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@
},
"devDependencies": {
"@biomejs/biome": "^1.4.1",
"@happy-dom/global-registrator": "^12.10.3",
"bun-types": "^1.0.17",
"tsup": "^8.0.1",
"type-fest": "^4.8.3",
"typescript": "^5.3.3"
},
"packageManager": "[email protected].14"
"packageManager": "[email protected].17"
}
54 changes: 54 additions & 0 deletions src/dom/popup.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { beforeEach, describe, expect, it, jest } from "bun:test";
import { openPopup } from "./popup.js";

const window = globalThis.window as Window | undefined;
if (!window) {
throw new Error("window is not defined");
}

describe("openPopup", () => {
beforeEach(() => {
jest.restoreAllMocks();
});
it("should return a new window object with the correct properties", () => {
const url = "https://example.com";
const name = "popup";
const updatedWindow = {
...window,
location: {
...window.location,
href: url,
},
name,
};
window.open = jest.fn().mockReturnValue(updatedWindow);

const windowFeatures = "width=500,height=500";
const newWindow = openPopup(url, name, windowFeatures);

expect(window.open).toHaveBeenCalledWith(
url,
name,
`noopener,noreferrer,${windowFeatures}`,
);
expect(newWindow).not.toBeNull();
expect(newWindow?.location.href).toBe(url);
expect(newWindow?.name).toBe(name);
expect(newWindow?.opener).toBeNull();
});
it("should not return a new window object if the browser fails to open the new browsing context", () => {
window.open = jest.fn().mockReturnValue(null);

const url = "https://example.com";
const name = "popup";
const windowFeatures = "width=500,height=500";
const newWindow = openPopup(url, name, windowFeatures);

expect(window.open).toHaveBeenCalledWith(
url,
name,
`noopener,noreferrer,${windowFeatures}`,
);
expect(newWindow).toBeNull();
});
});

0 comments on commit 6bac34d

Please sign in to comment.