aboutsummaryrefslogtreecommitdiff
path: root/backend/internal/ibd/html_helpers_test.go
blob: d251c39f93b04191f4c5343cc4595fa56485fca1 (plain) (blame)
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
package ibd

import (
	"strings"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"golang.org/x/net/html"
)

func Test_findClass(t *testing.T) {
	t.Parallel()
	tests := []struct {
		name      string
		html      string
		className string
		found     bool
		expData   string
	}{
		{
			name:      "class exists",
			html:      `<div class="foo"></div>`,
			className: "foo",
			found:     true,
			expData:   "div",
		},
		{
			name:      "class exists nested",
			html:      `<div class="foo"><a class="abc"></a></div>`,
			className: "abc",
			found:     true,
			expData:   "a",
		},
		{
			name:      "class exists multiple",
			html:      `<div class="foo"><a class="foo"></a></div>`,
			className: "foo",
			found:     true,
			expData:   "div",
		},
		{
			name:      "class missing",
			html:      `<div class="abc"><a class="xyz"></a></div>`,
			className: "foo",
			found:     false,
			expData:   "",
		},
		{
			name:      "class missing",
			html:      `<div id="foo"><a abc="xyz"></a></div>`,
			className: "foo",
			found:     false,
			expData:   "",
		},
		{
			name:      "class exists multiple save div",
			html:      `<div class="foo bar"></div>`,
			className: "bar",
			found:     true,
			expData:   "div",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			node, err := html.Parse(strings.NewReader(tt.html))
			require.NoError(t, err)

			got := findClass(node, tt.className)
			if !tt.found {
				require.Nil(t, got)
				return
			}
			require.NotNil(t, got)
			assert.Equal(t, tt.expData, got.Data)
		})
	}
}