/
codegen.test.ts
65 lines (58 loc) · 1.57 KB
/
codegen.test.ts
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
import { generateMessageFunction } from '../../src/codegen'
describe('generateMessageFunction', () => {
test('development', () => {
expect(
generateMessageFunction('hello', {
sourceMap: true
})
).toMatchSnapshot()
})
test('production', () => {
expect(
generateMessageFunction('hello', { env: 'production' })
).toMatchSnapshot()
})
test('syntax error', () => {
const errors: string[] = []
const { code } = generateMessageFunction(`|`, {
onError(msg) {
errors.push(msg)
}
})
expect(errors.length).toBe(1)
expect(code).toBe(`|`)
})
describe('strictMessage', () => {
test('default: should be checked', () => {
const errors: string[] = []
generateMessageFunction(`<p>hello</p>`, {
onError(msg) {
errors.push(msg)
}
})
expect(errors[0]).toBe(`Detected HTML in '<p>hello</p>' message.`)
})
test('false: should not be checked', () => {
const errors: string[] = []
generateMessageFunction(`<p>hello</p>`, {
strictMessage: false,
onError(msg) {
errors.push(msg)
}
})
expect(errors.length).toBe(0)
})
})
describe('escapeHtml', () => {
test('default: should not be escaped', () => {
const { code } = generateMessageFunction(`<p>hello</p>`, {})
expect(code).toMatchSnapshot()
})
test('true: should be escaped', () => {
const { code } = generateMessageFunction(`<p>hello</p>`, {
escapeHtml: true
})
expect(code).toMatchSnapshot()
})
})
})