-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomAPIUtils_test.go
142 lines (130 loc) · 3.04 KB
/
domAPIUtils_test.go
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
package goDom
import (
"testing"
"github.com/stretchr/testify/assert"
)
type domAPIUtilsTestPair[V any, E any] struct {
target *Element
value V
expect E
expectErr bool
descr string
}
var (
elementMatchesQueryTestPairs = []domAPIUtilsTestPair[string, bool]{
{
target: mockEl_header_0,
value: "header",
expect: true,
descr: "Should matches by tag name.",
},
{
target: mockEl_ul_1,
value: "#nav_list",
expect: true,
descr: "Should matches by Id attribute.",
},
{
target: mockEl_li_1,
value: ".red",
expect: true,
descr: "Should matches by class",
},
{
target: mockEl_button_1,
value: "button[disabled]",
expect: true,
descr: "Should matches by attribute without value",
},
{
target: mockEl_div_9,
value: "div.nested#[data-pic='pow']",
expect: true,
descr: "Should matches by tag name, class, id and attribute",
},
}
findOneByConditionTestPairs = []domAPIUtilsTestPair[func(*Element) bool, any]{
{
target: mockEl_div_6,
value: func(e *Element) bool {
return e.TagName == "div"
},
expect: mockEl_div_7,
descr: "Find one nearest element",
},
{
target: mockEl_ul_1,
value: func(e *Element) bool {
return e.TagName == "span"
},
expect: mockEl_span_0,
descr: "Find one far away element",
},
{
target: mockEl_footer_0,
value: func(e *Element) bool {
return e.TagName == "strong"
},
expectErr: true,
descr: "Not found in nested elements.",
},
}
findAllByConditionTestPairs = []domAPIUtilsTestPair[func(*Element) bool, any]{
{
target: mockEl_div_5,
value: func(e *Element) bool {
return e.TagName == "div"
},
expect: []*Element{mockEl_div_6, mockEl_div_7, mockEl_div_8, mockEl_div_9, mockEl_div_10},
descr: "Find all nearest element",
},
{
target: mockEl_body_0,
value: func(e *Element) bool {
return e.TagName == "button"
},
expect: []*Element{mockEl_button_1, mockEl_button_2, mockEl_button_3, mockEl_button_4},
descr: "Find all far away element",
},
{
target: mockEl_footer_0,
value: func(e *Element) bool {
return e.TagName == "strong"
},
expectErr: true,
descr: "Not found in nested elements.",
},
}
)
func Test_elementMatchesQuery(t *testing.T) {
for _, p := range elementMatchesQueryTestPairs {
q, _ := parseQuery(p.value)
assert.Equal(t, p.expect, domAPIUtils{}.elementMatchesQuery(*q, p.target))
}
}
func Test_findOneByCondition(t *testing.T) {
for _, p := range findOneByConditionTestPairs {
r, err := domAPIUtils{}.findOneByCondition(p.value, p.target)
if p.expectErr {
if err != nil {
continue
}
t.Error("\nfor", p.descr, "\nexpected error")
} else {
assert.Equal(t, p.expect, r)
}
}
}
func Test_findAllByCondition(t *testing.T) {
for _, p := range findAllByConditionTestPairs {
r, err := domAPIUtils{}.findAllByCondition(p.value, p.target)
if p.expectErr {
if err != nil {
continue
}
t.Error("\nfor", p.descr, "\nexpected error")
} else {
assert.Equal(t, p.expect, r)
}
}
}