-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.test.js
59 lines (54 loc) · 1.89 KB
/
index.test.js
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
import React from 'react'
import TestRenderer from 'react-test-renderer'
import renderTemplate, { DOUBLE_CURLY } from './'
// test component
const Link = ({ href, children }) => <a href={href}>{children}</a>
test('tokens are replaced', () => {
const template = 'found {COUNT} instances of {TERM}'
const tokenDict = { COUNT: 42, TERM: 'Presence' }
expect(renderTemplate(template, tokenDict)).toBe(
'found 42 instances of Presence'
)
})
test('quotes are html-ified', () => {
const template = 'found {COUNT} instances of {TERM}'
const tokenDict = { COUNT: 42, TERM: '"Presence"' }
expect(renderTemplate(template, tokenDict)).toBe(
'found 42 instances of "Presence"'
)
})
test('tokens are replaced, jsx value', () => {
const template = 'found {COUNT} instances of {TERM} in the document'
const tokenDict = {
COUNT: 42,
TERM: <Link href="/presence">Presence</Link>,
}
const expected = TestRenderer.create([
'found 42 instances of ',
<Link key={1} href="/presence">
Presence
</Link>,
' in the document',
])
const result = TestRenderer.create(renderTemplate(template, tokenDict))
expect(result.toJSON()).toEqual(expected.toJSON())
})
test('keys are added to jsx elements', () => {
const template = 'found {COUNT} instances of {TERM}'
const tokenDict = {
COUNT: <Link href="/details">42</Link>,
TERM: <Link href="/presence">Presence</Link>,
}
const result = TestRenderer.create(renderTemplate(template, tokenDict))
const linkNodes = result.root.findAllByType(Link)
expect(linkNodes.length).toBe(2)
expect(linkNodes[0]._fiber.key).toEqual('0')
expect(linkNodes[1]._fiber.key).toEqual('1')
})
test('do not replace tokens unless they are in the dict', () => {
const template = 'found {COUNT} instances of {TERM}'
const tokenDict = { COUNT: 42 }
expect(renderTemplate(template, tokenDict)).toBe(
'found 42 instances of {TERM}'
)
})