Skip to content
This repository was archived by the owner on Jan 1, 2025. It is now read-only.

Commit 84ee099

Browse files
committed
feat: add more tests
1 parent fb4ea5b commit 84ee099

File tree

1 file changed

+134
-28
lines changed

1 file changed

+134
-28
lines changed

src/detect.spec.ts

+134-28
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,148 @@
11
import { expect, describe, it } from 'bun:test';
22
import { load } from 'cheerio';
3-
43
import { DetectConfig, detect } from './detect';
54

6-
const BLOCK_HTML = `
7-
<div class="parent">
8-
<div class="first-child">First Child</div>
9-
<div class="second-child">Second Child</div>
10-
<div class="third-child">Third Child</div>
11-
<div class="full-child">Content</div>
12-
<div class="empty-child"></div>
13-
<div class="number-content-child">632</div>
14-
<div class="regex-test-child">Year 2021</div>
15-
<div class="regex-template-test-child">https://example.com/ > example > test</div>
16-
<a class="link" href="https://example.com/">Test Url</a>
17-
</div>
18-
`;
19-
20-
const SAMPLE_HTML = `
21-
<body>
22-
<div class="blocks">
23-
<div class="unblock">Unblock</div>
24-
${BLOCK_HTML.repeat(4)}
25-
</div>
26-
</body>
27-
`;
28-
29-
const $ = load(SAMPLE_HTML);
30-
315
describe('detect', () => {
32-
it('should return true if the element matches the config', () => {
6+
const SAMPLE_HTML = `
7+
<body>
8+
<div class="blocks">
9+
<div class="unblock">Unblock</div>
10+
<div class="parent">
11+
<div class="first-child">First Child</div>
12+
<div class="second-child">Second Child</div>
13+
</div>
14+
</div>
15+
</body>
16+
`;
17+
18+
const $ = load(SAMPLE_HTML);
19+
20+
it('should return true if the element matches any of the configs in oneOf', () => {
21+
const el = $('.parent');
22+
const config: DetectConfig = {
23+
oneOf: [
24+
{ hasClassName: 'parent' },
25+
{ withInnerSelector: '.first-child' },
26+
{ exactMatchClassName: 'non-existent-class' }
27+
]
28+
};
29+
30+
const result = detect(el, config);
31+
32+
expect(result).toBe(true);
33+
});
34+
35+
it('should return false if the element does not match any of the configs in oneOf', () => {
36+
const el = $('.parent');
37+
const config: DetectConfig = {
38+
oneOf: [
39+
{ hasClassName: 'non-existent-class' },
40+
{ withInnerSelector: '.non-existent-child' },
41+
{ exactMatchClassName: 'non-existent-class' }
42+
]
43+
};
44+
45+
const result = detect(el, config);
46+
47+
expect(result).toBe(false);
48+
});
49+
50+
it('should return true if the element matches the config with inner selector', () => {
51+
const el = $('.parent');
52+
const config: DetectConfig = {
53+
withInnerSelector: '.first-child'
54+
};
55+
56+
const result = detect(el, config);
57+
58+
expect(result).toBe(true);
59+
});
60+
61+
it('should return false if the element does not match the config with inner selector', () => {
62+
const el = $('.parent');
63+
const config: DetectConfig = {
64+
withInnerSelector: '.non-existent-child'
65+
};
66+
67+
const result = detect(el, config);
68+
69+
expect(result).toBe(false);
70+
});
71+
72+
it('should return true if the element matches the config with exact match class name', () => {
73+
const el = $('.parent');
74+
const config: DetectConfig = {
75+
exactMatchClassName: 'parent'
76+
};
77+
78+
const result = detect(el, config);
79+
80+
expect(result).toBe(true);
81+
});
82+
83+
it('should return false if the element does not match the config with exact match class name', () => {
84+
const el = $('.parent');
85+
const config: DetectConfig = {
86+
exactMatchClassName: 'non-existent-class'
87+
};
88+
89+
const result = detect(el, config);
90+
91+
expect(result).toBe(false);
92+
});
93+
94+
it('should return true if the element matches the config with custom function', () => {
95+
const el = $('.parent');
96+
const config: DetectConfig = {
97+
custom: (el) => el.find('.first-child').length > 0
98+
};
99+
100+
const result = detect(el, config);
101+
102+
expect(result).toBe(true);
103+
});
104+
105+
it('should return false if the element does not match the config with custom function', () => {
106+
const el = $('.parent');
107+
const config: DetectConfig = {
108+
custom: (el) => el.find('.non-existent-child').length > 0
109+
};
110+
111+
const result = detect(el, config);
112+
113+
expect(result).toBe(false);
114+
});
115+
116+
it('should return true if the element has the specified class name', () => {
33117
const el = $('.parent');
34118
const config: DetectConfig = {
35-
withInnerSelector: '.full-child'
119+
hasClassName: 'parent'
36120
};
37121

38122
const result = detect(el, config);
39123

40124
expect(result).toBe(true);
41125
});
126+
127+
it('should return false if the element does not have the specified class name', () => {
128+
const el = $('.parent');
129+
const config: DetectConfig = {
130+
hasClassName: 'non-existent-class'
131+
};
132+
133+
const result = detect(el, config);
134+
135+
expect(result).toBe(false);
136+
});
137+
138+
it('should return false if the element does not have all the specified class names', () => {
139+
const el = $('.parent');
140+
const config: DetectConfig = {
141+
hasClassNames: ['parent', 'non-existent-class']
142+
};
143+
144+
const result = detect(el, config);
145+
146+
expect(result).toBe(false);
147+
});
42148
});

0 commit comments

Comments
 (0)