Skip to content

Commit 7251561

Browse files
authored
Test: add some extra tests for code not covered (#535)
I've used `web-test-runner --coverage` to find some code that was not tested.
1 parent 543e9c9 commit 7251561

22 files changed

+1134
-20
lines changed

public/_/readthedocs-addons.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@
100100
},
101101
"addons": {
102102
"options": {
103-
"root_selector": null,
103+
"root_selector": "[role=main]",
104104
"load_when_embedded": false
105105
},
106106
"ethicalads": {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"url": "http://localhost:8000/docdiff.html",
3+
"fragment": null,
4+
"content": "<div role=\"main\"><h1>DocDiff original Addons</h1><p>This is a great feature for all builds on Read the Docs.</p></div>",
5+
"external": false
6+
}

public/docdiff.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<meta name="readthedocs-resolver-filename" content="/index.html" />
77
</head>
88
<body>
9-
<div id="main">
9+
<div role="main">
1010
<h1>DocDiff Addons</h1>
1111
<p>This is a great feature for pull request previews.</p>
1212
</div>

src/docdiff.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { default as fetch } from "unfetch";
12
import styleSheet from "./docdiff.css";
23
import docdiffGeneralStyleSheet from "./docdiff.document.css";
34

@@ -16,9 +17,15 @@ import {
1617
EVENT_READTHEDOCS_DOCDIFF_HIDE,
1718
EVENT_READTHEDOCS_ROOT_DOM_CHANGED,
1819
} from "./events";
19-
import { nothing, LitElement } from "lit";
20+
import { CSSResult, nothing, LitElement } from "lit";
2021
import { default as objectPath } from "object-path";
21-
import { AddonBase, getQueryParam, docTool } from "./utils";
22+
import {
23+
AddonBase,
24+
getQueryParam,
25+
docTool,
26+
IS_LOCALHOST_DEVELOPMENT,
27+
IS_TESTING,
28+
} from "./utils";
2229
import { EMBED_API_ENDPOINT } from "./constants";
2330

2431
export const DOCDIFF_URL_PARAM = "readthedocs-diff";
@@ -100,7 +107,11 @@ export class DocDiffElement extends LitElement {
100107
// NOTE: maybe there is a better way to inject this styles?
101108
// Conditionally inject our base styles
102109
if (this.injectStyles) {
103-
document.adoptedStyleSheets.push(docdiffGeneralStyleSheet);
110+
let styleSheet = docdiffGeneralStyleSheet;
111+
if (styleSheet instanceof CSSResult) {
112+
styleSheet = styleSheet.styleSheet;
113+
}
114+
document.adoptedStyleSheets.push(styleSheet);
104115
}
105116

106117
// Enable DocDiff if the URL parameter is present
@@ -125,6 +136,10 @@ export class DocDiffElement extends LitElement {
125136
params["maincontent"] = this.rootSelector;
126137
}
127138

139+
if (IS_LOCALHOST_DEVELOPMENT) {
140+
return "/_/readthedocs-docdiff-embed.json";
141+
}
142+
128143
// NOTE: we don't send ``doctool`` and ``docversion`` on purpose here
129144
// because we don't want the backed to pre-process the response. We need the
130145
// HTML as-is without any pre-processing.
@@ -175,7 +190,13 @@ export class DocDiffElement extends LitElement {
175190
throw new Error("Element not found in both documents.");
176191
}
177192

178-
const diffNode = visualDomDiff.visualDomDiff(
193+
// Depending on the context, visualDomDiff function is found under a different path.
194+
// When running tests we use a different path for it.
195+
let visualDomDiffFunction = visualDomDiff.visualDomDiff;
196+
if (!visualDomDiffFunction && IS_TESTING) {
197+
visualDomDiffFunction = visualDomDiff.default.visualDomDiff;
198+
}
199+
const diffNode = visualDomDiffFunction(
179200
oldBody,
180201
newBody,
181202
VISUAL_DIFF_OPTIONS,

src/filetreediff.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,8 @@ export class FileTreeDiffAddon extends AddonBase {
346346
return (
347347
// The order is important since we don't even want to run the data
348348
// validation if the version is not external.
349-
config.versions.current.type === "external" &&
349+
// We have to use `objectPath` here becase we haven't validated the data yet.
350+
objectPath.get(config, "versions.current.type") === "external" &&
350351
super.isEnabled(config, httpStatus)
351352
);
352353
}

src/linkpreviews.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { default as fetch } from "unfetch";
12
import { html, nothing, render, LitElement } from "lit";
23
import styleSheet from "./linkpreviews.css";
34

@@ -19,8 +20,10 @@ import {
1920
} from "@floating-ui/dom";
2021
import { default as objectPath } from "object-path";
2122

22-
const SHOW_TOOLTIP_DELAY = 300;
23-
const HIDE_TOOLTIP_DELAY = 300;
23+
// Remove delay when running tests
24+
const SHOW_TOOLTIP_DELAY = IS_TESTING ? 0 : 300;
25+
const HIDE_TOOLTIP_DELAY = IS_TESTING ? 0 : 300;
26+
2427
const TOOLTIP_DATA_HREF = "data-linkpreview-href";
2528

2629
function setupTooltip(el, doctoolname, doctoolversion, selector) {

src/readthedocs-config.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import {
88
CLIENT_VERSION,
99
IS_TESTING,
10+
IS_LOCALHOST_DEVELOPMENT,
1011
ADDONS_API_VERSION,
1112
ADDONS_API_ENDPOINT,
1213
getMetadataValue,
@@ -18,7 +19,7 @@ import {
1819
* It uses META HTML tags to get project/version slugs and `sendUrlParam` to
1920
* decide whether or not sending `url=`.
2021
*/
21-
function _getApiUrl(sendUrlParam, apiVersion) {
22+
export function _getApiUrl(sendUrlParam, apiVersion) {
2223
const metaProject = getMetadataValue("readthedocs-project-slug");
2324
const metaVersion = getMetadataValue("readthedocs-version-slug");
2425

@@ -41,14 +42,14 @@ function _getApiUrl(sendUrlParam, apiVersion) {
4142
let url = ADDONS_API_ENDPOINT + "?" + new URLSearchParams(params);
4243

4344
// Retrieve a static JSON file when working in development mode
44-
if (window.location.href.startsWith("http://localhost") && !IS_TESTING) {
45+
if (IS_LOCALHOST_DEVELOPMENT) {
4546
url = "/_/readthedocs-addons.json";
4647
}
4748

4849
return url;
4950
}
5051

51-
function getReadTheDocsUserConfig(sendUrlParam) {
52+
export function getReadTheDocsUserConfig(sendUrlParam) {
5253
// Create a Promise here to handle the user request in a different async task.
5354
// This allows us to start executing our integration independently from the user one.
5455
return new Promise((resolve, reject) => {
@@ -68,9 +69,7 @@ function getReadTheDocsUserConfig(sendUrlParam) {
6869
// this data is ready to be consumed under `event.detail.data()`.
6970
const userApiUrl = _getApiUrl(sendUrlParam, metadataAddonsAPIVersion);
7071

71-
// TODO: revert this change and use the correct URL here
72-
const url = "/_/readthedocs-addons.json";
73-
fetch(url, {
72+
fetch(userApiUrl, {
7473
method: "GET",
7574
}).then((response) => {
7675
if (!response.ok) {
@@ -140,7 +139,7 @@ export function getReadTheDocsConfig(sendUrlParam) {
140139
});
141140
}
142141

143-
function dispatchEvent(eventName, element, data) {
142+
export function dispatchEvent(eventName, element, data) {
144143
const event = new CustomEvent(eventName, { detail: data });
145144
element.dispatchEvent(event);
146145
}

src/search.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { default as fetch } from "unfetch";
12
import { ajv } from "./data-validation";
23
import { library, icon } from "@fortawesome/fontawesome-svg-core";
34
import {
@@ -14,6 +15,7 @@ import styleSheet from "./search.css";
1415
import {
1516
domReady,
1617
CLIENT_VERSION,
18+
IS_TESTING,
1719
AddonBase,
1820
debounce,
1921
addUtmParameters,
@@ -29,7 +31,7 @@ import { classMap } from "lit/directives/class-map.js";
2931
// TODO: play more with the substring limit.
3032
// The idea is to try to fit most of the results in one line.
3133
const MAX_SUBSTRING_LIMIT = 80;
32-
const FETCH_RESULTS_DELAY = 250;
34+
const FETCH_RESULTS_DELAY = IS_TESTING ? 0 : 250;
3335
const CLEAR_RESULTS_DELAY = 300;
3436
const MIN_CHARACTERS_QUERY = 3;
3537
const API_ENDPOINT = "/_/api/v3/search/";

src/utils.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export const IS_TESTING =
3030
typeof WEBPACK_IS_TESTING === "undefined" ? false : WEBPACK_IS_TESTING;
3131
export const IS_PRODUCTION =
3232
typeof WEBPACK_IS_PRODUCTION === "undefined" ? false : WEBPACK_IS_PRODUCTION;
33+
export const IS_LOCALHOST_DEVELOPMENT =
34+
globalThis.location.href.startsWith("http://localhost") && !IS_TESTING;
3335

3436
export const domReady = new Promise((resolve) => {
3537
if (
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* @web/test-runner snapshot v1 */
2+
export const snapshots = {};
3+
4+
snapshots["DocDiff tests initial trigger by event and DOM check"] =
5+
`<main>
6+
<section class="doc-diff-removed">
7+
<h1>
8+
Old title
9+
</h1>
10+
</section>
11+
<ins class="doc-diff-added">
12+
</ins>
13+
<section class="doc-diff-added">
14+
<h1>
15+
New title (changed)
16+
</h1>
17+
<p>
18+
This paragraph was added.
19+
</p>
20+
</section>
21+
<ins class="doc-diff-added">
22+
</ins>
23+
</main>
24+
`;
25+
/* end snapshot DocDiff tests initial trigger by event and DOM check */
26+

0 commit comments

Comments
 (0)