-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtext-group-el.test.js
More file actions
126 lines (100 loc) · 3.59 KB
/
Copy pathtext-group-el.test.js
File metadata and controls
126 lines (100 loc) · 3.59 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import React from 'react'
import renderer from 'react-test-renderer'
import TextGroupEl from '../../../../src/scripts/common/chunk/text-chunk/text-group-el'
import TextGroup from '../../../../src/scripts/common/text-group/text-group'
import StyleableText from '../../../../src/scripts/common/text/styleable-text'
import Dispatcher from '../../../../src/scripts/common/flux/dispatcher'
jest.mock('../../../../src/scripts/common/flux/dispatcher')
describe('TextGroupEl', () => {
let tg
beforeEach(() => {
tg = TextGroup.create(Infinity, { hangingIndent: false })
tg.clear()
// first item, no formatting:
tg.add(new StyleableText('First line'))
// second item, some formatting and a variable:
const st = new StyleableText('Some BOLD text with a {{variable}} included')
st.styleText('b', 5, 9)
tg.add(st, { hangingIndent: true })
})
test('TextGroupEl unformatted', () => {
const component = renderer.create(<TextGroupEl groupIndex={0} textItem={tg.get(0)} />)
const tree = component.toJSON()
expect(tree).toMatchSnapshot()
})
test('TextGroupEl formatted', () => {
const component = renderer.create(<TextGroupEl groupIndex={1} textItem={tg.get(1)} />)
const tree = component.toJSON()
expect(tree).toMatchSnapshot()
})
test('Renders text', () => {
const component = renderer.create(
<TextGroupEl groupIndex={0} textItem={tg.get(0)} parentModel={jest.fn()} />
)
const tree = component.toJSON()
const findText = node => {
if (typeof node === 'string') {
return node
}
if (Array.isArray(node.children) && node.children.length > 0) {
return findText(node.children[0])
}
return null
}
const textContent = findText(tree)
expect(textContent).toBe('First line')
})
test('Variable replacement', () => {
Dispatcher.trigger.mockImplementationOnce((eventName, event, variable) => {
event.text = 'REPLACE(' + variable + ')'
})
const component = renderer.create(
<TextGroupEl groupIndex={1} textItem={tg.get(1)} parentModel={jest.fn()} />
)
const tree = component.toJSON()
const findText = node => {
if (typeof node === 'string') {
return node
}
if (Array.isArray(node.children) && node.children.length > 0) {
return node.children.map(child => findText(child)).join('')
}
return ''
}
const textContent = findText(tree)
expect(textContent).toBe('Some BOLD text with a REPLACE(variable) included')
})
test('Variable replacement with no match', () => {
Dispatcher.trigger.mockImplementationOnce((eventName, event) => {
event.text = null
})
const component = renderer.create(
<TextGroupEl groupIndex={1} textItem={tg.get(1)} parentModel={jest.fn()} />
)
const tree = component.toJSON()
const findText = node => {
if (typeof node === 'string') {
return node
}
if (Array.isArray(node.children) && node.children.length > 0) {
return node.children.map(child => findText(child)).join('')
}
return ''
}
const textContent = findText(tree)
expect(textContent).toBe('Some BOLD text with a {{variable}} included')
})
test('String values of hangingIndent work as expected', () => {
tg = TextGroup.create(Infinity, { hangingIndent: 'false' })
tg.clear()
tg.add(new StyleableText('First line'))
const st = new StyleableText('Second line')
tg.add(st, { hangingIndent: 'true' })
const component1 = renderer.create(<TextGroupEl groupIndex={0} textItem={tg.get(0)} />)
const tree1 = component1.toJSON()
const component2 = renderer.create(<TextGroupEl groupIndex={1} textItem={tg.get(1)} />)
const tree2 = component2.toJSON()
expect(tree1).toMatchSnapshot()
expect(tree2).toMatchSnapshot()
})
})