forked from lorengreenfield/halfcab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
223 lines (195 loc) · 6.71 KB
/
test.js
File metadata and controls
223 lines (195 loc) · 6.71 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import chai, {expect} from 'chai'
import dirtyChai from 'dirty-chai'
import { noCallThru } from 'proxyquire'
const proxyquire = noCallThru()
chai.use(dirtyChai)
import jsdomGlobal from 'jsdom-global'
import decache from 'decache'
let jsdom = jsdomGlobal()
let halfcab
let halfcabModule
let { ssr, html, defineRoute, formField, cache, updateState, injectMarkdown, formIsValid, emptyBody, css } = {}
function serverMode(){
jsdom && jsdom()
decache('bel')
halfcabModule = proxyquire('./halfcab', {})
;({ ssr, html, css } = halfcabModule)
}
function browserMode(){
global.requestAnimationFrame = (fcn) => {
fcn()
}
decache('bel')
jsdom = jsdomGlobal()
halfcabModule = proxyquire('./halfcab', {})
;({ html, defineRoute, formField, cache, updateState, injectMarkdown, formIsValid, emptyBody, css } = halfcabModule)
halfcab = halfcabModule.default
}
describe('halfcab', () =>{
describe('Server', () => {
before(done => {
serverMode()
done()
})
it('Produces a string when doing SSR', () =>{
var style = css`
.myStyle {
width: 100px;
}
`
var { componentsString, stylesString } = ssr(html`
<div class="${style.myStyle}" oninput=${() => {}}></div>
`)
expect(typeof componentsString === 'string').to.be.true()
})
})
describe('Client', () => {
before(done => {
browserMode()
done()
})
it('Produces an HTML element when rendering', () =>{
var el = html`
<div oninput=${() => {}}></div>
`
expect(el instanceof HTMLDivElement).to.be.true()
})
it('Produces an HTML element wrapping as a reusable component', () =>{
var el = cache(() => html`
<div oninput=${() => {}}></div>
`, {})
expect(el instanceof HTMLDivElement).to.be.true()
})
it('Runs halfcab function without error', () =>{
return halfcab({
baseName: 'Resorts Interactive',
baseApiPath: '/api/webroutes',
components(){
return html `<div></div>`
}
})
.then(rootEl => {
expect(typeof rootEl === 'object').to.be.true()
})
})
it('updating state causes a rerender with state', () =>{
return halfcab({
baseName: 'Resorts Interactive',
baseApiPath: '/api/webroutes',
components(args){
return html `<div>${args.testing || ''}</div>`
}
})
.then(rootEl => {
updateState({testing: 'works'})
expect(rootEl.innerHTML.indexOf('works') !== -1).to.be.true()
})
})
it('updating state without deepmerge overwrites objects', () =>{
var style = css`
.myStyle {
width: 100px;
}
`
return halfcab({
baseName: 'Resorts Interactive',
baseApiPath: '/api/webroutes',
components(args){
return html `<div class="${style.myStyle}">${args.testing.inner || ''}</div>`
}
})
.then(rootEl => {
updateState({testing: { inner: 'works'}})
updateState({testing: { inner2: 'works'}}, {
deepMerge: false
})
expect(rootEl.innerHTML.indexOf('works')).to.equal(-1)
})
})
it('injects external content without error', () =>{
return halfcab({
baseName: 'Resorts Interactive',
baseApiPath: '/api/webroutes',
components(args){
return html `<div>${injectMarkdown('### Heading')}</div>`
}
})
.then(rootEl => {
emptyBody()
expect(rootEl.innerHTML.indexOf('###')).to.equal(-1)
expect(rootEl.innerHTML.indexOf('<h3')).not.to.equal(-1)
})
})
describe('formField', () => {
it('Returns a function', () =>{
var holdingPen = {};
var output = formField(holdingPen, 'test')
expect(typeof output === 'function').to.be.true()
})
it('Sets a property within the valid object of the same name', () =>{
var holdingPen = {};
var output = formField(holdingPen, 'test')
var e = {
currentTarget: {
type: 'text',
validity: {
valid: false
}
}
}
output(e)
expect(holdingPen.valid.test).to.exist()
})
it('Sets checkboxes without error', () =>{
var holdingPen = {};
var output = formField(holdingPen, 'test')
var e = {
currentTarget: {
type: 'checkbox',
validity: {
valid: false
},
checked: true
}
}
output(e)
expect(holdingPen.valid.test).to.exist()
})
it('Sets radio buttons without error', () =>{
var holdingPen = {};
var output = formField(holdingPen, 'test')
var e = {
currentTarget: {
type: 'radio',
validity: {
valid: false
},
checked: true
}
}
output(e)
expect(holdingPen.valid.test).to.exist()
})
it('Validates a form without error', () =>{
var holdingPen = {
test: '',
[Symbol('valid')]: {
test: false
}
}
var output = formField(holdingPen, 'test')
var e = {
currentTarget: {
type: 'radio',
validity: {
valid: true
},
checked: true
}
}
output(e)
expect(formIsValid(holdingPen)).to.be.true()
})
})
})
})