index.js
2.31 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
'use strict'
const assert = require( 'assert' )
const data = require( './data' )
const implementation = require( './implementation' )
const baseAdapter = require( '../' )
const adapter = baseAdapter( implementation )
const getById = id => adapter.findOne(
node => adapter.getAttributeValue( node, 'id' ) === id,
data
)
const getByName = name => adapter.findAll(
node => adapter.getName( node ) === name,
data
)
const getByClass = className => adapter.findAll(
node => adapter.getAttributeValue( node, 'class' ) === className,
data
)
const existsName = name => adapter.existsOne(
node => adapter.getName( node ) === name,
data
)
const container = getById( 'container' )
const strong = getByName( 'strong' )[ 0 ]
const hello = strong.children[ 0 ]
const world = container.children[ 1 ]
describe( 'css-select-base-adapter', () => {
it( 'getAttributeValue', () => {
assert( container )
})
it( 'getName', () => {
assert( strong )
})
it( 'findOne', () => {
assert( container )
})
it( 'findAll', () => {
const messages = getByClass( 'message' )
assert.equal( messages.length, 2 )
assert.equal( messages[ 0 ], container )
assert.equal( messages[ 1 ], strong )
})
it( 'getParent', () => {
const parent = adapter.getParent( strong )
assert.equal( parent, container )
})
it( 'getSiblings', () => {
const siblings = adapter.getSiblings( strong )
assert.equal( siblings[ 0 ], strong )
assert.equal( siblings[ 1 ], world )
})
it( 'getChildren', () => {
const children = adapter.getChildren( container )
assert.equal( children[ 0 ], strong )
})
it( 'getText', () => {
const text = adapter.getText( container )
assert.equal( text, 'Hello, World!' )
})
it( 'isTag', () => {
assert( adapter.isTag( container ) )
assert( adapter.isTag( strong ) )
assert( !adapter.isTag( hello ) )
})
it( 'hasAttrib', () => {
assert( adapter.hasAttrib( container, 'id' ) )
assert( !adapter.hasAttrib( strong, 'id' ) )
})
it( 'existsOne', () => {
assert( existsName( 'strong' ) )
assert( !existsName( 'blink' ) )
})
it( 'removeSubsets', () => {
const removed = adapter.removeSubsets([ container, strong, container ])
assert.equal( removed.length, 1 )
assert.equal( removed[ 0 ], container )
})
})