-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
123 lines (99 loc) · 2.77 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { test } from 'uvu';
import { JSDOM } from 'jsdom';
import fs from 'fs';
import fetch from 'node-fetch';
import * as assert from 'uvu/assert';
/** fetch HTML from webtoon episode viewer */
const URL =
'https://www.webtoons.com/en/fantasy/soleil/s3-episode-128/viewer?title_no=1823&episode_no=128';
const html = await fetch(URL).then((r) => r.text());
/** create DOM */
const dom = new JSDOM(html, {
url: URL,
referrer: "https://www.webtoons.com/",
runScripts: "dangerously"
});
/** make window, document and window.scrollTo globals */
const browserAPI = {
runtime: {
getUrl: (url) => `.${url}` // emulate relative path
}
}
installPolyfills({
window: dom.window,
document: dom.window.document,
chrome: browserAPI,
browser: browserAPI
});
const noop = () => {};
Object.defineProperty(window, 'scrollTo', { value: noop, writable: true });
/** inject CSS */
const cssFile = fs.readFileSync("./src/style.css", {
encoding: "utf-8"
});
const styleElement = document.createElement("style");
styleElement.textContent = cssFile;
document.head.appendChild(styleElement);
/** inject JS */
const jsFile = fs.readFileSync("./src/index.js", {
encoding: "utf-8"
})
const scriptElement = document.createElement("script");
scriptElement.textContent = jsFile
// replace chrome object
.replace(/chrome/g, "{ runtime: { getURL: (s) => s } }")
document.head.append(scriptElement);
/** tests */
const button = document.querySelector("#btn-rotate");
const viewer = document.querySelector("#_viewerBox");
test('Rotate button is inserted', () => {
assert.is(button.tagName, "BUTTON")
});
test("Button trigger rotating", () => {
button.click();
const rotated = viewer.classList.contains("viewer-rotate");
assert.is(rotated, true);
button.click();
const rotated2 = viewer.classList.contains("viewer-rotate");
assert.is(rotated2, false);
})
test("Rotating works", () => {
button.click();
const transform = window.getComputedStyle(viewer).getPropertyValue("transform");
assert.is(transform, "rotate(-90deg) translateX(-100%)");
})
test("vertical scrollbar is hidden in landscape", () => {
const overflow = window.getComputedStyle(document.body).getPropertyValue("overflow-y");
assert.is(overflow, "hidden");
})
test("episodde is visible", () => {
// TODO
})
test("scroll to Top is hiddem", () => {
// TODO
})
test("wheel in landscape scroll by x axis", () => {
// TODO
})
test.run();
/**
* @example
* const dom = new JSDOM(html);
* const globals = {
* window: dom.window,
* document: dom.window.document
* };
* installPolyfills(globals)
*
* @param {{[key: string]: *}} globals
*/
function installPolyfills(globals) {
for (const name in globals) {
Object.defineProperty(globalThis, name, {
enumerable: true,
configurable: true,
writable: true,
value: globals[name],
});
}
}