Skip to content

Commit 57d34df

Browse files
authored
Test: readthedocs-addons-data-ready (#559)
Closes #547
1 parent 6401104 commit 57d34df

File tree

6 files changed

+198
-38
lines changed

6 files changed

+198
-38
lines changed

src/readthedocs-config.js

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,19 @@ export function getReadTheDocsUserConfig(sendUrlParam) {
6969
// this data is ready to be consumed under `event.detail.data()`.
7070
const userApiUrl = _getApiUrl(sendUrlParam, metadataAddonsAPIVersion);
7171

72-
fetch(userApiUrl, {
73-
method: "GET",
74-
}).then((response) => {
75-
if (!response.ok) {
76-
return reject(
77-
"Error hitting addons API endpoint for user api-version",
78-
);
79-
}
80-
// Return the data in the API version requested.
81-
return resolve(response.json());
82-
});
72+
return resolve(
73+
fetch(userApiUrl, {
74+
method: "GET",
75+
}).then((response) => {
76+
if (!response.ok) {
77+
return reject(
78+
"Error hitting addons API endpoint for user api-version",
79+
);
80+
}
81+
// Return the data in the API version requested.
82+
return response.json();
83+
}),
84+
);
8385
}
8486

8587
// If the API versions match, we return `undefined`.
@@ -124,8 +126,8 @@ export function getReadTheDocsConfig(sendUrlParam) {
124126
// event was fired can still get access to the data
125127
globalThis.ReadTheDocsEventData = new ReadTheDocsEventData(dataEvent);
126128

127-
// Trigger the addons data ready CustomEvent to with the data the user is expecting.
128-
return dispatchEvent(
129+
// Trigger the addons data ready CustomEvent with the data the user is expecting.
130+
dispatchEvent(
129131
EVENT_READTHEDOCS_ADDONS_USER_DATA_READY,
130132
document,
131133
new ReadTheDocsEventData(dataEvent),

tests/__snapshots__/search.test.snap.js

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/hotkeys.test.html

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<html>
2+
<head>
3+
<meta name="readthedocs-http-status" content="200" />
4+
</head>
5+
<body>
6+
<script type="module">
7+
import { default as sinon } from "sinon";
8+
import { expect, elementUpdated } from "@open-wc/testing";
9+
import { runTests } from "@web/test-runner-mocha";
10+
import * as events from "../src/events";
11+
import * as hotkeys from "../src/hotkeys";
12+
13+
let config;
14+
15+
runTests(async () => {
16+
beforeEach(() => {
17+
config = {
18+
addons: {
19+
hotkeys: {
20+
enabled: true,
21+
doc_diff: {
22+
enabled: true,
23+
trigger: "KeyD",
24+
},
25+
search: {
26+
enabled: true,
27+
trigger: "Slash",
28+
},
29+
},
30+
},
31+
};
32+
});
33+
34+
afterEach(() => {
35+
sinon.restore();
36+
});
37+
38+
describe("HotKeys tests", () => {
39+
it("trigger docdiff when pressing 'd'", async () => {
40+
const addon = new hotkeys.HotKeysAddon(config);
41+
const element = document.querySelector("readthedocs-hotkeys");
42+
await elementUpdated(element);
43+
44+
// Empty shadowDom
45+
await expect(element).shadowDom.to.equal("");
46+
47+
const fakeFunction = sinon.fake();
48+
document.addEventListener(
49+
events.EVENT_READTHEDOCS_DOCDIFF_ADDED_REMOVED_SHOW,
50+
(event) => {
51+
fakeFunction();
52+
},
53+
);
54+
55+
const event = new Event("keydown");
56+
event.code = "KeyD";
57+
document.dispatchEvent(event);
58+
59+
expect(fakeFunction.calledOnce).to.be.true;
60+
});
61+
62+
it("trigger search modal when pressing '/'", async () => {
63+
const addon = new hotkeys.HotKeysAddon(config);
64+
const element = document.querySelector("readthedocs-hotkeys");
65+
await elementUpdated(element);
66+
67+
// Empty shadowDom
68+
await expect(element).shadowDom.to.equal("");
69+
70+
const fakeFunction = sinon.fake();
71+
document.addEventListener(
72+
events.EVENT_READTHEDOCS_SEARCH_SHOW,
73+
(event) => {
74+
fakeFunction();
75+
},
76+
);
77+
78+
const event = new Event("keydown");
79+
event.code = "Slash";
80+
document.dispatchEvent(event);
81+
82+
expect(fakeFunction.calledOnce).to.be.true;
83+
});
84+
});
85+
});
86+
</script>
87+
</body>
88+
</html>

tests/readthedocs-config.test.html

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import { expect, elementUpdated } from "@open-wc/testing";
1010
import { runTests } from "@web/test-runner-mocha";
1111
import * as readthedocsConfig from "../src/readthedocs-config";
12+
import * as events from "../src/events";
1213
import * as utils from "../src/utils";
1314

1415
let server;
@@ -20,6 +21,8 @@
2021
beforeEach(() => {
2122
// Create a sinon fake server to mock requests
2223
server = sinon.fakeServer.create();
24+
server.respondImmediately = true;
25+
2326
if (metadataAddonsAPIVersion) {
2427
metadataAddonsAPIVersion.remove();
2528
}
@@ -54,17 +57,10 @@
5457

5558
it("fetch config from API endpoint", async () => {
5659
const matchMockedUrl = new RegExp(`^/_/addons/`, "g");
57-
server.respondWith("GET", matchMockedUrl, [
58-
200,
59-
{},
60-
"fake response body",
61-
]);
60+
server.respondWith("GET", matchMockedUrl, [200, {}, "{}"]);
6261

6362
const config = readthedocsConfig.getReadTheDocsConfig(false);
6463

65-
// Respond to all the request waiting for a response
66-
server.respond();
67-
6864
expect(server.requests).to.have.length(1);
6965
expect(server.requests[0].status).to.be.equal(200);
7066
expect(server.requests[0].method).to.be.equal("GET");
@@ -78,18 +74,22 @@
7874
metadataAddonsAPIVersion.content = "2";
7975
document.head.appendChild(metadataAddonsAPIVersion);
8076

81-
// Response 204 on requests made to Read the Docs analytics' API
77+
// Response 200 on requests made to Read the Docs analytics' API
8278
const matchMockedUrl = new RegExp(`^/_/addons/`, "g");
8379
server.respondWith("GET", matchMockedUrl, [
8480
200,
8581
{},
86-
"fake response body",
82+
'{"addons": {}, "builds": {"current": {"id": 12345}}, "projects": {}, "versions": {}}',
8783
]);
8884

89-
const config = readthedocsConfig.getReadTheDocsUserConfig(true);
90-
91-
// Respond to all the request waiting for a response
92-
server.respond();
85+
const config =
86+
await readthedocsConfig.getReadTheDocsUserConfig(true);
87+
expect(config).to.deep.equal({
88+
addons: {},
89+
builds: { current: { id: 12345 } },
90+
projects: {},
91+
versions: {},
92+
});
9393

9494
expect(server.requests).to.have.length(1);
9595
expect(server.requests[0].status).to.be.equal(200);
@@ -98,6 +98,40 @@
9898
/^\/_\/addons\/\?client-version=.+&api-version=2&url=.+$/;
9999
expect(server.requests[0].url).to.match(matchApiUrl);
100100
});
101+
102+
it("check readthedocs-addons-data-ready event", async () => {
103+
// Response 200 on requests made to Read the Docs analytics' API
104+
const matchMockedUrl = new RegExp(`^/_/addons/`, "g");
105+
server.respondWith("GET", matchMockedUrl, [
106+
200,
107+
{},
108+
'{"test": "readthedocs-addons-data-ready", "builds": {"current": {"id": 12345}}}',
109+
]);
110+
111+
let eventData;
112+
const fakeFunction = sinon.fake();
113+
document.addEventListener(
114+
events.EVENT_READTHEDOCS_ADDONS_USER_DATA_READY,
115+
(event) => {
116+
// Store the event data to check later
117+
eventData = event.detail.data(true);
118+
119+
// Call the `fakeFunction` inside the event listener to check later it was called once.
120+
fakeFunction();
121+
},
122+
);
123+
124+
const config = await readthedocsConfig.getReadTheDocsConfig(true);
125+
expect(fakeFunction.calledOnce).to.be.true;
126+
expect(eventData).to.deep.equal({
127+
test: "readthedocs-addons-data-ready",
128+
builds: {
129+
current: {
130+
id: 12345,
131+
},
132+
},
133+
});
134+
});
101135
});
102136
});
103137
</script>

tests/search.test.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@
6060
document.dispatchEvent(searchEvent);
6161

6262
await elementUpdated(element);
63-
await expect(element).shadowDom.to.equalSnapshot();
63+
await expect(element).shadowDom.to.equalSnapshot({
64+
ignoreAttributes: [{ tags: ["form"], attributes: ["class"] }],
65+
});
6466
});
6567

6668
it("initial trigger by event and DOM check with filters", async () => {

tests/utils.test.html

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
</head>
55
<body>
66
<script type="module">
7+
import { default as sinon } from "sinon";
78
import { expect, elementUpdated } from "@open-wc/testing";
89
import { runTests } from "@web/test-runner-mocha";
10+
import * as docdiff from "../src/docdiff";
911
import * as utils from "../src/utils";
1012
import * as events from "../src/events";
1113
import * as readthedocsConfig from "../src/readthedocs-config";
@@ -31,6 +33,8 @@
3133
);
3234
eventListener = null;
3335
}
36+
37+
sinon.restore();
3438
});
3539

3640
describe("DocumentationTool tests", () => {
@@ -42,10 +46,7 @@
4246
document.head.appendChild(element);
4347

4448
eventListener = function func(event) {
45-
// We need to use `JSON.stringify` because objects are not equal in javascript
46-
expect(JSON.stringify(event.detail.data())).to.be.equal(
47-
JSON.stringify({ addons: "data" }),
48-
);
49+
expect(event.detail.data()).to.deep.equal({ addons: "data" });
4950
expect(event.detail.httpStatus()).to.be.equal("200");
5051
};
5152

@@ -87,10 +88,7 @@
8788

8889
it("use readthedocs-addons-data-ready without META tag and internal=true", async () => {
8990
eventListener = function func(event) {
90-
// We need to use `JSON.stringify` because objects are not equal in javascript
91-
expect(JSON.stringify(event.detail.data(true))).to.be.equal(
92-
JSON.stringify({ addons: "data" }),
93-
);
91+
expect(event.detail.data(true)).to.deep.equal({ addons: "data" });
9492
};
9593
document.addEventListener(
9694
events.EVENT_READTHEDOCS_ADDONS_USER_DATA_READY,
@@ -104,6 +102,43 @@
104102
);
105103
});
106104

105+
it("trigger URL_CHANGED on SPA without GET parameters", async () => {
106+
utils.setupHistoryEvents();
107+
108+
const fakeFunction = sinon.fake();
109+
window.addEventListener(
110+
events.EVENT_READTHEDOCS_URL_CHANGED,
111+
(event) => {
112+
fakeFunction();
113+
},
114+
);
115+
116+
expect(fakeFunction.notCalled).to.be.true;
117+
history.pushState({}, "", "#newpage");
118+
expect(fakeFunction.calledOnce).to.be.true;
119+
});
120+
121+
it("trigger URL_CHANGED on SPA with `?readthedocs-diff-chunk` parameter", async () => {
122+
utils.setupHistoryEvents();
123+
124+
const fakeFunction = sinon.fake();
125+
window.addEventListener(
126+
events.EVENT_READTHEDOCS_URL_CHANGED,
127+
(event) => {
128+
fakeFunction();
129+
},
130+
);
131+
132+
expect(fakeFunction.notCalled).to.be.true;
133+
134+
const url = new URL(window.location.href);
135+
url.searchParams.set(docdiff.DOCDIFF_CHUNK_URL_PARAM, 1);
136+
history.pushState({}, "", url);
137+
138+
expect(fakeFunction.notCalled).to.be.true;
139+
expect(fakeFunction.calledOnce).to.be.false;
140+
});
141+
107142
it("Sphinx Alabaster", async () => {
108143
element = document.createElement("link");
109144
element.href = "_static/alabaster.css";

0 commit comments

Comments
 (0)