Skip to content

Commit 3f60051

Browse files
author
Evan You
committed
unit tests for custom element
1 parent 19d15ec commit 3f60051

File tree

4 files changed

+133
-3
lines changed

4 files changed

+133
-3
lines changed

src/compiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ CompilerProto.compile = function (node, root) {
241241
}
242242

243243
// custom elements has 2nd highest priority
244-
} else if (customElementFn) {
244+
} else if (!root && customElementFn) {
245245

246246
addChild(customElementFn)
247247

src/utils.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,18 @@ var utils = module.exports = {
153153
var components = options.components,
154154
partials = options.partials,
155155
template = options.template,
156+
elements = options.elements,
156157
key
157158
if (components) {
158159
for (key in components) {
159160
components[key] = utils.toConstructor(components[key])
160161
}
161162
}
163+
if (elements) {
164+
for (key in elements) {
165+
elements[key] = utils.toConstructor(elements[key])
166+
}
167+
}
162168
if (partials) {
163169
for (key in partials) {
164170
partials[key] = utils.toFragment(partials[key])

test/unit/specs/api.js

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe('UNIT: API', function () {
103103

104104
describe('component()', function () {
105105

106-
var testId = 'api-vm-test',
106+
var testId = 'api-component-test',
107107
testId2 = testId + '2',
108108
opts = {
109109
className: 'hihi',
@@ -143,6 +143,64 @@ describe('UNIT: API', function () {
143143

144144
})
145145

146+
describe('element()', function () {
147+
148+
var testId = 'api-element-test',
149+
testId2 = testId + '2',
150+
testId3 = testId + '3',
151+
opts = {
152+
className: 'hihi',
153+
scope: { hi: 'ok' }
154+
},
155+
Test = Seed.extend(opts),
156+
utils = require('seed/src/utils')
157+
158+
it('should register a Custom Element constructor', function () {
159+
Seed.element(testId, Test)
160+
assert.strictEqual(utils.elements[testId], Test)
161+
})
162+
163+
it('should also work with option objects', function () {
164+
Seed.element(testId2, opts)
165+
assert.ok(utils.elements[testId2].prototype instanceof Seed)
166+
})
167+
168+
it('should accept simple function as-is', function () {
169+
var fn = function (el) {
170+
el.className = 'hihi3'
171+
el.textContent = 'ok3'
172+
}
173+
Seed.element(testId3, fn)
174+
assert.strictEqual(utils.elements[testId3], fn)
175+
})
176+
177+
it('should retrieve the VM if has only one arg', function () {
178+
assert.strictEqual(Seed.element(testId), Test)
179+
})
180+
181+
it('should work with custom tag names', function () {
182+
183+
mock(testId, '<' + testId + '>{{hi}}</' + testId + '>')
184+
var t = new Seed({ el: '#' + testId }),
185+
child = t.$el.querySelector(testId)
186+
assert.strictEqual(child.className, 'hihi')
187+
assert.strictEqual(child.textContent, 'ok')
188+
189+
mock(testId2, '<' + testId2 + '>{{hi}}</' + testId2 + '>')
190+
var t2 = new Seed({ el: '#' + testId2 }),
191+
child2 = t2.$el.querySelector(testId2)
192+
assert.strictEqual(child2.className, 'hihi')
193+
assert.strictEqual(child2.textContent, 'ok')
194+
195+
mock(testId3, '<' + testId3 + '></' + testId3 + '>')
196+
var t3 = new Seed({ el: '#' + testId3 }),
197+
child3 = t3.$el.querySelector(testId3)
198+
assert.strictEqual(child3.className, 'hihi3')
199+
assert.strictEqual(child3.textContent, 'ok3')
200+
})
201+
202+
})
203+
146204
describe('partial()', function () {
147205

148206
var testId = 'api-partial-test',
@@ -588,6 +646,66 @@ describe('UNIT: API', function () {
588646
})
589647

590648
})
649+
650+
describe('elements', function () {
651+
652+
it('should allow the VM to use private custom elements', function () {
653+
var Child = Seed.extend({
654+
scope: {
655+
name: 'child'
656+
}
657+
})
658+
var Parent = Seed.extend({
659+
template: '<p>{{name}}</p><child>{{name}}</child>',
660+
scope: {
661+
name: 'dad'
662+
},
663+
elements: {
664+
child: Child
665+
}
666+
})
667+
var p = new Parent()
668+
assert.strictEqual(p.$el.querySelector('p').textContent, 'dad')
669+
assert.strictEqual(p.$el.querySelector('child').textContent, 'child')
670+
})
671+
672+
it('should work with plain option object', function () {
673+
var Parent = Seed.extend({
674+
template: '<p>{{name}}</p><child>{{name}}</child>',
675+
scope: {
676+
name: 'dad'
677+
},
678+
elements: {
679+
child: {
680+
scope: {
681+
name: 'child'
682+
}
683+
}
684+
}
685+
})
686+
var p = new Parent()
687+
assert.strictEqual(p.$el.querySelector('p').textContent, 'dad')
688+
assert.strictEqual(p.$el.querySelector('child').textContent, 'child')
689+
})
690+
691+
it('should work with a simple function', function () {
692+
var Parent = Seed.extend({
693+
template: '<p>{{name}}</p><child></child>',
694+
scope: {
695+
name: 'dad'
696+
},
697+
elements: {
698+
child: function (el) {
699+
el.textContent = 'child'
700+
}
701+
}
702+
})
703+
var p = new Parent()
704+
assert.strictEqual(p.$el.querySelector('p').textContent, 'dad')
705+
assert.strictEqual(p.$el.querySelector('child').textContent, 'child')
706+
})
707+
708+
})
591709

592710
describe('partials', function () {
593711

test/unit/specs/utils.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,9 @@ describe('UNIT: Utils', function () {
209209
a: { scope: { data: 1 } },
210210
b: { scope: { data: 2 } }
211211
},
212+
elements: {
213+
c: { scope: { data: 3 }}
214+
},
212215
template: '<a>{{hi}}</a>'
213216
}
214217

@@ -231,12 +234,15 @@ describe('UNIT: Utils', function () {
231234
assert.equal(options.template.querySelector('a').textContent, '{{hi}}')
232235
})
233236

234-
it('should convert plain object components to constructors', function () {
237+
it('should convert plain object components & elements to constructors', function () {
235238
var components = options.components
236239
assert.ok(components.a.prototype instanceof Seed)
237240
assert.strictEqual(components.a.options.scope.data, 1)
238241
assert.ok(components.b.prototype instanceof Seed)
239242
assert.strictEqual(components.b.options.scope.data, 2)
243+
var elements = options.elements
244+
assert.ok(elements.c.prototype instanceof Seed)
245+
assert.strictEqual(elements.c.options.scope.data, 3)
240246
})
241247

242248
})

0 commit comments

Comments
 (0)