-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.test.ts
More file actions
118 lines (104 loc) · 3.11 KB
/
basic.test.ts
File metadata and controls
118 lines (104 loc) · 3.11 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
import test from "tape";
import { scrap, $ } from '../lib';
const STR_TO_SCRAP = `
<h1 class="title">Hello</h1>
<ul>
<li><span>Guten Tag</span></li>
<li><span class="msg">Ciao</span></li>
<li><span>Bonjour</span></li>
</ul>
<a href="/read-more">read more ...</a>
`;
test('Basic', (main) => {
test('should scrap <h1/> text from string', (t) => {
const result = scrap(STR_TO_SCRAP, {
title: $.text('h1.title')
});
t.equal(result, { title: 'Hello'});
t.end();
});
test('should scrap attributes from <h1/>', (t) => {
const result = scrap(STR_TO_SCRAP, {
title: $.attr('h1.title', 'class')
});
t.equal(result, { title: 'title'});
t.end();
});
test('should scrap items from <span/>', (t) => {
const result = scrap(STR_TO_SCRAP, {
items: $.list('li', {
text: $.text('span')
})
});
t.equal(result.items.length, 3);
t.equal(result.items[2].text, 'Bonjour');
t.end();
});
test('should scrap text from <li><span/>', (t) => {
const result = scrap(STR_TO_SCRAP, {
items: $.list('li', {
text: $.text('span')
})
});
t.equal(result.items.length, 3);
t.equal(result.items[2].text, 'Bonjour');
t.end();
});
test('should scrap text from <span/> by omitting <li/>', (t) => {
const result = scrap(STR_TO_SCRAP, {
items: $.list('span', {
text: $.text('')
})
});
t.equal(result.items.length, 3);
t.equal(result.items[2].text, 'Bonjour');
t.end();
});
test('should get list of texts', (t) => {
const result = scrap(STR_TO_SCRAP, {
texts: $.list('li', $.text('span'))
});
t.equals(result.texts, [
'Guten Tag',
'Ciao',
'Bonjour'
]);
t.end();
});
test('should user deep query', (t) => {
const result = scrap(STR_TO_SCRAP, {
title: $.text('.title'),
data: {
msg: $.text('.msg')
}
});
t.equal(result.title, 'Hello');
t.equal(result.data.msg, 'Ciao');
t.end();
});
test('should count <span/> elements', (t) => {
const result = scrap(STR_TO_SCRAP, {
spanCount: $.count('span')
});
t.equal(result.spanCount, 3);
t.end();
});
test('should count not exists element', (t) => {
const result = scrap(STR_TO_SCRAP, {
spanCount: $.count('table')
});
t.equal(result.spanCount, 0);
t.end();
});
test('should use only selector to scrap title', (t) => {
const title = scrap(STR_TO_SCRAP, $.text('.title'));
t.equal(title, 'Hello');
});
test('should use only selector to scrap <span/>', (t) => {
const spans = scrap(STR_TO_SCRAP, $.list('span', $.text('')));
t.equal(spans.length, 3);
t.equal(spans[0], 'Guten Tag');
t.end();
});
main.end();
});