Skip to content

Commit

Permalink
test: 单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
D-Sketon committed Jul 20, 2024
1 parent b183c1e commit 1ff3a4f
Show file tree
Hide file tree
Showing 23 changed files with 943 additions and 17 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
68 changes: 68 additions & 0 deletions .github/workflows/tester.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Tester

on:
push:
branches:
- "main"
paths:
- "src/**"
- "test/**"
- "package.json"
- "tsconfig.json"
- ".github/workflows/tester.yml"
pull_request:
paths:
- "src/**"
- "test/**"
- "package.json"
- "tsconfig.json"
- ".github/workflows/tester.yml"

permissions:
contents: read

jobs:
tester:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
node-version: ["18.x"]
fail-fast: false
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Install Dependencies
run: npm install
- name: Test
run: npm test
env:
CI: true
coverage:
permissions:
checks: write # for coverallsapp/github-action to create new checks
contents: read # for actions/checkout to fetch code
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
node-version: ["18.x"]
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Install Dependencies
run: npm install
- name: Coverage
run: npm run test-cov
env:
CI: true
- name: Coveralls
uses: coverallsapp/github-action@master
with:
github-token: ${{ github.token }}
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"browser": "dist/index.umd.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "rollup -c rollup.config.mjs"
"build": "rollup -c rollup.config.mjs",
"test": "mocha -r ts-node/register 'test/**/*.ts'",
"test-cov": "c8 --reporter=html --reporter=text-summary npm test"
},
"repository": {
"type": "git",
Expand All @@ -19,7 +21,16 @@
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^11.1.5",
"@types/chai": "^4.3.16",
"@types/jsdom": "^21.1.7",
"@types/mocha": "^10.0.7",
"c8": "^10.1.2",
"chai": "4",
"jsdom": "^24.1.0",
"mocha": "^10.6.0",
"rollup": "^4.9.4",
"ts-node": "^10.9.2",
"tslib": "^2.6.3",
"typescript": "^5.3.3"
},
"keywords": [
Expand Down
25 changes: 23 additions & 2 deletions src/events/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,25 @@ export function on(
});
}

export function off(
els:
| Window
| Document
| HTMLElement
| NodeList
| Array<HTMLElement>
| HTMLCollection,
events: string | string[],
listener: EventListener,
useCapture?: boolean
): void {
eventForEach(events, (e) => {
forEachEls(els, (el) => {
el.removeEventListener(e, listener, useCapture);
});
});
}

// do not support IE !!!
export function trigger(
els:
Expand All @@ -41,10 +60,12 @@ export function trigger(
opts: Partial<PjaxOptions> = {}
): void {
eventForEach(events, (e) => {
const event = new CustomEvent(e, {
const event = new Event(e, {
bubbles: true,
cancelable: true,
...opts,
});
Object.keys(opts).forEach((key) => {
event[key] = opts[key];
});
forEachEls(els, (el) => {
el.dispatchEvent(event);
Expand Down
4 changes: 2 additions & 2 deletions src/forEachSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import forEachEls from "./forEachEls";
export default function (
selectors: string[],
cb: (el: HTMLElement, index: number, array: HTMLElement[]) => void,
context: any,
DOMcontext: Document = document
context?: any,
DOMcontext: HTMLElement | Document = document
): void {
selectors.forEach((selector) => {
forEachEls(DOMcontext.querySelectorAll(selector), cb, context);
Expand Down
2 changes: 1 addition & 1 deletion src/sendRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function (
request: XMLHttpRequest,
location: string,
options: Partial<PjaxOptions>
) => XMLHttpRequest
) => any
): XMLHttpRequest {
const requestOptions = options.requestOptions || {};
const requestMethod = (requestOptions.requestMethod || "GET").toUpperCase();
Expand Down
15 changes: 5 additions & 10 deletions src/util/contains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,9 @@ export default function (
selectors: string[],
el: Element
): boolean {
for (const selector of selectors) {
const selectedEls = doc.querySelectorAll(selector);
for (let j = 0; j < selectedEls.length; j++) {
if (selectedEls[j].contains(el)) {
return true;
}
}
}

return false;
return selectors.some(selector =>
Array.from(doc.querySelectorAll(selector)).some(selectedEl =>
selectedEl.contains(el)
)
);
}
40 changes: 40 additions & 0 deletions test/evalScript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import chai from "chai";
import "./jsdom";
import evalScript from "../src/evalScript";

const should = chai.should();

describe("evalScript", () => {
it("test evalScript method", () => {
document.body.className = "";
const script = document.createElement("script");
script.innerHTML = "document.body.className = 'executed'";

document.body.className.should.equal("");
evalScript(script);
document.body.className.should.equal("executed");

script.innerHTML = "document.write('failure')";

const bodyText = "document.write hasn't been executed";
// @ts-expect-error
document.body.text = bodyText;
evalScript(script);
// @ts-expect-error
document.body.text.should.equal(bodyText);
});

it("evalScript should not throw an error if the script removed itself", () => {
const script = document.createElement("script");
script.id = "myScript";
script.innerHTML =
"const script = document.querySelector('#myScript');" +
"script.parentNode.removeChild(script);";

try {
evalScript(script);
} catch (e) {
should.fail("evalScript should not throw an error if the script removed itself");
}
});
});
29 changes: 29 additions & 0 deletions test/executeScripts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import chai from "chai";
import "./jsdom";
import executeScripts from "../src/executeScripts";

const should = chai.should();

describe("executeScripts", () => {
it("test executeScripts method when the script tag is inside a container", () => {
document.body.className = "";

const container = document.createElement("div");
container.innerHTML = `
<script>document.body.className = 'executed';</script>
<script>document.body.className += ' correctly';</script>`;
document.body.className.should.equal("");
executeScripts(container);
document.body.className.should.equal("executed correctly");
});

it("test executeScripts method with just a script tag", () => {
document.body.className = "";

const script = document.createElement("script");
script.innerHTML = "document.body.className = 'executed correctly';";
document.body.className.should.equal("");
executeScripts(script);
document.body.className.should.equal("executed correctly");
});
});
40 changes: 40 additions & 0 deletions test/forEachEls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import chai from "chai";
import "./jsdom";
import forEachEls from "../src/forEachEls";

const should = chai.should();

const div = document.createElement("div");
const span = document.createElement("span");
const cb = (el) => {
el.innerHTML = "boom";
};

describe("forEachEls", () => {
it("test forEachEls on one element", () => {
div.innerHTML = "div tag";
forEachEls(div, cb);
div.innerHTML.should.equal("boom");
});

it("test forEachEls on an array", () => {
div.innerHTML = "div tag";
span.innerHTML = "span tag";

forEachEls([div, span], cb);
div.innerHTML.should.equal("boom");
span.innerHTML.should.equal("boom");
});

it("test forEachEls on a NodeList", () => {
div.innerHTML = "div tag";
span.innerHTML = "span tag";

const frag = document.createDocumentFragment();
frag.appendChild(div);
frag.appendChild(span);
forEachEls(frag.childNodes, cb);
div.innerHTML.should.equal("boom");
span.innerHTML.should.equal("boom");
});
});
25 changes: 25 additions & 0 deletions test/forEachSelectors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import chai from "chai";
import "./jsdom";
import forEachSelectors from "../src/forEachSelectors";

const should = chai.should();

const cb = (el) =>{
el.className = "modified";
};

describe("forEachSelectors", () => {
it("test forEachSelector", () => {
forEachSelectors(["html", "body"], cb);
document.documentElement.className.should.equal("modified");
document.body.className.should.equal("modified");

document.documentElement.className = "";
document.body.className = "";

forEachSelectors(["html", "body"], cb, null, document.documentElement);

document.documentElement.className.should.equal("");
document.body.className.should.equal("modified");
});
});
25 changes: 25 additions & 0 deletions test/jsdom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import jsdom from "jsdom";

const { JSDOM } = jsdom;

const dom = new JSDOM("", {
url: "https://example.org/",
runScripts: "dangerously",
});

// @ts-ignore
globalThis.window = dom.window;
globalThis.document = dom.window.document;
globalThis.navigator = dom.window.navigator;
globalThis.location = dom.window.location;
globalThis.Element = dom.window.Element;
globalThis.HTMLElement = dom.window.HTMLElement;
globalThis.HTMLScriptElement = dom.window.HTMLScriptElement;
globalThis.HTMLHeadElement = dom.window.HTMLHeadElement;
globalThis.HTMLBodyElement = dom.window.HTMLBodyElement;
globalThis.Node = dom.window.Node;
globalThis.HTMLCollection = dom.window.HTMLCollection;
globalThis.NodeList = dom.window.NodeList;
globalThis.XMLHttpRequest = dom.window.XMLHttpRequest;
globalThis.CustomEvent = dom.window.CustomEvent;
globalThis.Event = dom.window.Event;
12 changes: 12 additions & 0 deletions test/newUid.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import chai from "chai";
import newUid from "../src/newUid";

const should = chai.should();

describe("newUid", () => {
it("test uniqueid", () => {
const a = newUid();
const b = newUid();
a.should.not.equal(b);
});
});
Loading

0 comments on commit 1ff3a4f

Please sign in to comment.