-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.spec.js
More file actions
148 lines (129 loc) · 4.82 KB
/
Copy pathindex.spec.js
File metadata and controls
148 lines (129 loc) · 4.82 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/// <reference path="../types/index.d.ts" />
import assert from 'node:assert/strict';
import { afterEach, beforeEach, describe, test } from 'node:test';
import { loadPixelperfect } from '../index.js';
import { createHappyDomWindow, installBrowserGlobals, uninstallBrowserGlobals } from './helpers/happy-dom.js';
/** @param {string} [pathname='/'] */
function setupWindow(pathname = '/') {
const happyDomWindow = createHappyDomWindow(pathname);
installBrowserGlobals(happyDomWindow);
document.body.tabIndex = 0;
document.body.focus();
return happyDomWindow;
}
function dispatchKey(/** @type {string} */ code, options = {}) {
document.dispatchEvent(
new KeyboardEvent('keydown', {
bubbles: true,
cancelable: true,
code,
...options,
}),
);
}
describe('index/loadPixelperfect', () => {
afterEach(() => {
uninstallBrowserGlobals();
});
beforeEach(() => {
setupWindow();
});
test('повторный вызов не инициализирует модуль снова', () => {
loadPixelperfect();
assert.equal(window.isPPLoaded, true);
const styleCount = document.head.querySelectorAll('style').length;
loadPixelperfect();
assert.equal(document.head.querySelectorAll('style').length, styleCount);
});
test('добавляет стиль pixelperfect в head', () => {
loadPixelperfect();
const styleElement = document.head.querySelector('style');
assert.ok(styleElement);
assert.match(styleElement?.textContent ?? '', /\.pixelperfect/);
});
test('определяет страницу из pathname', () => {
setupWindow('/about.html');
loadPixelperfect({ breakpoints: [320] });
dispatchKey('KeyP');
Object.defineProperty(document.body, 'clientWidth', { configurable: true, value: 400 });
window.dispatchEvent(new window.Event('resize'));
const bg = document.body.style.getPropertyValue('--pp-img');
assert.match(bg, /\/about-320\./);
});
test('принимает опции page, folder и breakpoints', () => {
loadPixelperfect({
breakpoints: [768, 320, 768],
ext: 'png',
folder: 'img/pp',
page: 'home',
});
assert.equal(window.isPPLoaded, true);
});
test('не падает при отсутствующих ppOffsets', () => {
assert.doesNotThrow(() => loadPixelperfect());
});
test('не падает при ppOffsets = {}', () => {
localStorage.setItem('ppOffsets', '{}');
assert.doesNotThrow(() => loadPixelperfect());
});
test('не падает при битом JSON в ppOffsets', () => {
localStorage.setItem('ppOffsets', '{invalid');
assert.doesNotThrow(() => loadPixelperfect());
});
test('KeyP переключает класс pixelperfect', () => {
loadPixelperfect();
assert.equal(document.body.classList.contains('pixelperfect'), false);
dispatchKey('KeyP');
assert.equal(document.body.classList.contains('pixelperfect'), true);
assert.equal(localStorage.getItem('pp'), '1');
dispatchKey('KeyP');
assert.equal(document.body.classList.contains('pixelperfect'), false);
});
test('KeyI меняет инверсию только при включённом PP', () => {
loadPixelperfect();
dispatchKey('KeyP');
const filterBefore = document.body.style.getPropertyValue('--pp-filter');
dispatchKey('KeyI');
const filterAfter = document.body.style.getPropertyValue('--pp-filter');
assert.notEqual(filterBefore, filterAfter);
});
test('стрелки смещают offset при включённом PP', () => {
Object.defineProperty(document.body, 'clientWidth', { configurable: true, value: 400 });
loadPixelperfect({ breakpoints: [320], page: 'index' });
dispatchKey('KeyP');
dispatchKey('ArrowRight');
assert.equal(document.body.style.getPropertyValue('--pp-offset-x'), '1px');
const stored = JSON.parse(localStorage.getItem('ppOffsets') || '{}');
assert.equal(stored.index[320][0], 1);
});
test('KeyR очищает ppOffsets и вызывает reload', () => {
loadPixelperfect();
localStorage.setItem('ppOffsets', '{"index":{"320":[1,2]}}');
let reloadCalled = false;
window.location.reload = () => {
reloadCalled = true;
};
dispatchKey('KeyP');
dispatchKey('KeyR');
assert.equal(localStorage.getItem('ppOffsets'), null);
assert.equal(reloadCalled, true);
});
test('игнорирует hotkeys с зажатым Ctrl', () => {
loadPixelperfect();
dispatchKey('KeyP', { ctrlKey: true });
assert.equal(document.body.classList.contains('pixelperfect'), false);
});
test('resize меняет background-image для breakpoint', () => {
Object.defineProperty(document.body, 'clientWidth', { configurable: true, value: 800 });
loadPixelperfect({
breakpoints: [320, 768],
ext: 'png',
folder: 'pp',
page: 'index',
});
dispatchKey('KeyP');
window.dispatchEvent(new window.Event('resize'));
const bg = document.body.style.getPropertyValue('--pp-img');
assert.match(bg, /pp\/index-768\.png/);
});
});