Skip to content

Commit

Permalink
add type declarations and export some DOM impl
Browse files Browse the repository at this point in the history
  • Loading branch information
ifpaledman committed Sep 18, 2023
1 parent 91aeb63 commit 01dbfb6
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 3 deletions.
57 changes: 57 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { CookieJar } from 'cookiejar';
import { Response as FetchResponse } from 'node-fetch-commonjs';

declare global {
interface Window {
prototype: Window;
}
interface Document {
prototype: Document;
}
interface MutationObserver {
prototype: Document;
new(callback: MutationCallback): MutationObserver;
}
}

export class BrowserTab {
/** Cheerio context */
get $(): cheerio.Cheerio;
get jar(): CookieJar;
get response(): FetchResponse;
window: Window;
document: Document;
navigateTo(uri: string, headers?: Record<string, any>, statusCode?: number): Promise<BrowserTab>;
load(markup: string): BrowserTab;
focusIframe(element: HTMLElement, src: string): Promise<BrowserTab>;
runScript(script: HTMLElement): void;
runScripts(context?: Node): void;
mutated(targetNode: Node, config?: { childList?: boolean, attributes?: boolean }): Promise<any[]>;
}

export class WebPage {
navigateTo(uri: string, headers?: Record<string, any>, statusCode?: number): Promise<BrowserTab>;
load(markup: string): BrowserTab;
submit(uri: string, options?: Record<string, any>): Promise<BrowserTab>;
fetch(uri: string, requestOptions?: Record<string, any>): Promise<FetchResponse>;
}

export class Browser {
constructor(origin: any, options?: Record<string, any>);
constructor(options?: Record<string, any>);
navigateTo(uri: string, headers?: Record<string, any>, statusCode?: number): Promise<BrowserTab>;
load(markup: string): WebPage;
}

interface DOMInterface {
Document: Document;
HTMLCollection: HTMLCollection;
IntersectionObserver: IntersectionObserver;
MutationObserver: MutationObserver;
Window: Window;
Storage: Storage;
}

export const DOM: DOMInterface;

export default Browser;
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const { CookieJar, Cookie } = require("cookiejar");

const { normalizeHeaders } = require("./lib/getHeaders.js");
const WebPage = require("./lib/WebPage.js");
const BrowserTab = require("./lib/BrowserTab.js");
const DOM = require("./lib/index.js");

const kOrigin = Symbol.for("origin");

Expand Down Expand Up @@ -54,3 +56,7 @@ module.exports = class Tallahassee {
return new WebPage(this[kOrigin], this.jar, requestHeaders, this.options);
}
};

module.exports.WebPage = WebPage;
module.exports.BrowserTab = BrowserTab;
module.exports.DOM = { ...DOM };
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ const Window = require("./Window.js");
const HTMLCollection = require("./HTMLCollection.js");

module.exports = {
Window,
Document,
Storage,
HTMLCollection,
IntersectionObserver,
MutationObserver,
Window,
Storage,
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Lightweight client testing framework",
"main": "index.js",
"license": "BSD-3-Clause",
"types": "index.d.ts",
"scripts": {
"test": "mocha",
"posttest": "eslint . --cache && npm run test-md && npm run toc",
Expand Down Expand Up @@ -45,7 +46,8 @@
},
"files": [
"lib/",
"index.js"
"index.js",
"index.d.ts"
],
"bugs": {
"url": "https://github.com/BonnierNews/tallahassee/issues"
Expand Down
22 changes: 22 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ const Script = require("@bonniernews/wichita");

const { app } = require("../app/app.js");
const Browser = require("../index.js");
const BrowserTab = require("../lib/BrowserTab.js");
const WebPage = require("../lib/WebPage.js");
const Window = require("../lib/Window.js");
const Document = require("../lib/Document.js");
const Storage = require("../lib/Storage");
const MutationObserver = require("../lib/MutationObserver.js");
const IntersectionObserver = require("../lib/IntersectionObserver.js");
const HTMLCollection = require("../lib/HTMLCollection");

describe("Tallahassee", () => {
describe("navigateTo()", () => {
Expand Down Expand Up @@ -392,4 +400,18 @@ describe("Tallahassee", () => {
expect(browser.document.getElementsByTagName("h1")[0].textContent).to.equal("Apocalyptic");
});
});

describe("exports", () => {
it("expected interface", () => {
expect(Browser.BrowserTab).to.equal(BrowserTab);
expect(Browser.WebPage).to.equal(WebPage);
expect(Browser.DOM, "DOM").to.be.an("object");
expect(Browser.DOM.Window).to.equal(Window);
expect(Browser.DOM.Document).to.equal(Document);
expect(Browser.DOM.Storage).to.equal(Storage);
expect(Browser.DOM.HTMLCollection).to.equal(HTMLCollection);
expect(Browser.DOM.IntersectionObserver).to.equal(IntersectionObserver);
expect(Browser.DOM.MutationObserver).to.equal(MutationObserver);
});
});
});

0 comments on commit 01dbfb6

Please sign in to comment.