_querySelector

说明

模拟实现简单的querySelector

源码

用法

_querySelector.test.ts
1
import { _querySelector } from "../../utility/dom/_querySelector";
2
3
4
describe('_querySelector', () => {
5
test('_querySelector should return `null` when no matched', () => {
6
document.body.innerHTML = `
7
<div id="app">
8
<h3 class="title">
9
<p class="text"></p>
10
<a class="link"></a>
11
</h3>
12
<section class="post">
13
<p class="text"></p>
14
</section>
15
</div>
16
`;
17
18
const received = ['.ddzy', 'ddzy', '#ddzy', ''];
19
20
for (const v of received) {
21
expect(_querySelector(v)).toBeNull();
22
}
23
});
24
25
test('_querySelector should return `a single Element` when matched', () => {
26
document.body.innerHTML = `
27
<div id="app">
28
<h3 class="title">
29
<p class="text"></p>
30
<a class="link"></a>
31
</h3>
32
<section class="post">
33
<p class="text"></p>
34
</section>
35
</div>
36
`;
37
38
const received = ['#app', '.title', '.text', 'a'];
39
const expected = [
40
document.querySelector('#app'),
41
document.querySelector('.title'),
42
document.querySelector('.text'),
43
document.querySelector('a'),
44
];
45
46
for (const [i, v] of received.entries()) {
47
expect(_querySelector(v)).toBe(expected[i]);
48
}
49
});
50
});
Copied!
最近更新 1yr ago
复制链接