This repository was archived by the owner on Apr 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathHighlightText.test.tsx
74 lines (56 loc) · 2.23 KB
/
HighlightText.test.tsx
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
import { mount, shallow } from "enzyme"
import { shallowToJson } from "enzyme-to-json"
import * as React from "react"
import * as os from "os"
import { HighlightTextByIndex } from "./../browser/src/UI/components/HighlightText"
const highlightComponent = "em"
const initialState = {
highlightComponent,
highlightIndices: [0, 1, 3, 4],
text: "highlight text",
className: "test-class",
}
describe("<HighlightTextByIndex />", () => {
it("renders a shallow instance of the component", () => {
const component = shallow(<HighlightTextByIndex {...initialState} />)
expect(component.length).toEqual(1)
})
it("renders the correct text with highlights", () => {
const component = mount(<HighlightTextByIndex {...initialState} />)
// Check the correct text is there
expect(component.text()).toContain("highlight text")
// Check the structure is correct
expect(component.text()).toHaveLength(14)
// Check only 4 chars were highlighed
expect(component.find("em")).toHaveLength(4)
})
it("renders the correct text with no highlights", () => {
const testState = {
highlightComponent,
highlightIndices: [],
text: "no highlight text",
className: "test-class",
}
const component = mount(<HighlightTextByIndex {...testState} />)
// Check the correct text is there
expect(component.text()).toContain("no highlight text")
// Check the structure is correct
expect(component.text()).toHaveLength(17)
// Check no chars were highlighed
expect(component.find("em")).toHaveLength(0)
})
it("doesn't crash when passed a non-string", () => {
const testState = {
highlightComponent,
highlightIndices: [0, 1, 3, 4],
text: 10101,
className: "test-class",
} as any
const component = mount(<HighlightTextByIndex {...testState} />)
// Should be length one, as only the out span is returned
// due to no inner text.
expect(component.find("span")).toHaveLength(1)
expect(component.text() === "")
expect(component.hasClass("highlight-test") === false)
})
})