blob: fc73b3e89ff34cb97a96f410030c175bab42e46e [file] [log] [blame]
Leo Repp58b9f112021-11-22 11:57:47 +01001'use strict'
2
3const assert = require( 'assert' )
4const data = require( './data' )
5const implementation = require( './implementation' )
6const baseAdapter = require( '../' )
7
8const adapter = baseAdapter( implementation )
9
10const getById = id => adapter.findOne(
11 node => adapter.getAttributeValue( node, 'id' ) === id,
12 data
13)
14
15const getByName = name => adapter.findAll(
16 node => adapter.getName( node ) === name,
17 data
18)
19
20const getByClass = className => adapter.findAll(
21 node => adapter.getAttributeValue( node, 'class' ) === className,
22 data
23)
24
25const existsName = name => adapter.existsOne(
26 node => adapter.getName( node ) === name,
27 data
28)
29
30const container = getById( 'container' )
31const strong = getByName( 'strong' )[ 0 ]
32const hello = strong.children[ 0 ]
33const world = container.children[ 1 ]
34
35describe( 'css-select-base-adapter', () => {
36 it( 'getAttributeValue', () => {
37 assert( container )
38 })
39
40 it( 'getName', () => {
41 assert( strong )
42 })
43
44 it( 'findOne', () => {
45 assert( container )
46 })
47
48 it( 'findAll', () => {
49 const messages = getByClass( 'message' )
50
51 assert.equal( messages.length, 2 )
52 assert.equal( messages[ 0 ], container )
53 assert.equal( messages[ 1 ], strong )
54 })
55
56 it( 'getParent', () => {
57 const parent = adapter.getParent( strong )
58
59 assert.equal( parent, container )
60 })
61
62 it( 'getSiblings', () => {
63 const siblings = adapter.getSiblings( strong )
64
65 assert.equal( siblings[ 0 ], strong )
66 assert.equal( siblings[ 1 ], world )
67 })
68
69 it( 'getChildren', () => {
70 const children = adapter.getChildren( container )
71
72 assert.equal( children[ 0 ], strong )
73 })
74
75 it( 'getText', () => {
76 const text = adapter.getText( container )
77
78 assert.equal( text, 'Hello, World!' )
79 })
80
81 it( 'isTag', () => {
82 assert( adapter.isTag( container ) )
83 assert( adapter.isTag( strong ) )
84 assert( !adapter.isTag( hello ) )
85 })
86
87 it( 'hasAttrib', () => {
88 assert( adapter.hasAttrib( container, 'id' ) )
89 assert( !adapter.hasAttrib( strong, 'id' ) )
90 })
91
92 it( 'existsOne', () => {
93 assert( existsName( 'strong' ) )
94 assert( !existsName( 'blink' ) )
95 })
96
97 it( 'removeSubsets', () => {
98 const removed = adapter.removeSubsets([ container, strong, container ])
99
100 assert.equal( removed.length, 1 )
101 assert.equal( removed[ 0 ], container )
102 })
103})