-
Notifications
You must be signed in to change notification settings - Fork 395
/
Template.test.ts
95 lines (83 loc) · 2.6 KB
/
Template.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
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
import T from '../../src/templates'
const icon = () => null
describe('T.template()', () => {
test('builds template through constructor', () => {
expect(
T.template({
id: 'foo',
title: 'some title',
schemaType: 'author',
value: {name: 'Default name!'},
icon
}).serialize()
).toMatchSnapshot()
})
test('throws on missing id', () => {
expect(() => T.template().serialize()).toThrowError(/required "id"/)
})
test('throws on missing title', () => {
expect(() =>
T.template()
.id('id')
.serialize()
).toThrowError(/required "title"/)
})
test('throws on missing schemaType', () => {
expect(() =>
T.template()
.id('id')
.title('Blah')
.serialize()
).toThrowError(/required "schemaType"/)
})
test('throws on missing value', () => {
expect(() =>
T.template()
.id('id')
.title('Blah')
.schemaType('author')
.serialize()
).toThrowError(/required "value"/)
})
test('can construct using builder', () => {
expect(
T.template()
.id('yeah')
.title('Yeah')
.schemaType('author')
.icon(icon)
.value({name: 'bar'})
.serialize()
).toMatchSnapshot()
})
test('builder is immutable', () => {
const original = T.template()
expect(original.id('foo')).not.toEqual(original)
expect(original.title('foo')).not.toEqual(original)
expect(original.schemaType('author')).not.toEqual(original)
expect(original.icon(icon)).not.toEqual(original)
expect(original.value({name: 'foo'})).not.toEqual(original)
})
test('getters work', () => {
const original = T.template()
expect(original.id('foo').getId()).toEqual('foo')
expect(original.title('bar').getTitle()).toEqual('bar')
expect(original.schemaType('author').getSchemaType()).toEqual('author')
expect(original.icon(icon).getIcon()).toEqual(icon)
expect(original.value({name: 'bar'}).getValue()).toEqual({name: 'bar'})
})
})
describe('T.defaultTemplateForType()', () => {
test('generates correct representation of type without initial value', () => {
expect(T.defaultTemplateForType('author').serialize()).toMatchSnapshot()
})
test('generates correct representation of type with initial value', () => {
expect(T.defaultTemplateForType('post').serialize()).toMatchSnapshot()
})
})
describe('T.defaults()', () => {
test('generates array of all schema type templates', () => {
expect(T.defaults()).toMatchSnapshot()
expect(T.defaults().map(tpl => tpl.serialize())).toMatchSnapshot()
})
})