Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix .add() 无法覆盖同名模板的问题,并增加相应单元测试 #20

Merged
merged 1 commit into from
Nov 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/underscore-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,13 @@ var template = function () {
if (templateCode) {
var templateId = _toTemplateId(id)
/** DEBUG_INFO_START **/
if (_cacheTemplate[templateId]) {
if (_cacheTemplate[templateId] || _cacheCompiledTemplate[templateId]) {
console.warn('[Template] Template id "' + templateId + '" already existed.')
}
/** DEBUG_INFO_END **/
if (_cacheCompiledTemplate[templateId]) {
_cacheCompiledTemplate[templateId] = null
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

所有写在 /** DEBUG_INFO_START **//** DEBUG_INFO_END **/ 之间的代码在发布文件中会被清除。

result = _cacheTemplate[templateId] = templateCode
} else {
//todo: support `_.template.add(id)` to add from dummy script element
Expand Down
23 changes: 20 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ void function () {
'</ul>'
].join('\n')

//compiledTemplate
var fn1 = _.template(templateCode1)
var fn2 = _.template(templateCode2)

//template data
var templateData1 = {text: HELLO}
var templateData2 = [
Expand Down Expand Up @@ -328,18 +332,31 @@ void function () {
expect(_cacheTemplate).to.deep.equal(data)
expect(_cacheCompiledTemplate).to.deep.equal({})
})
it('overwrites while adding existed template id', function () {
it('overwrites template while adding existed template id', function () {
expect(_cacheTemplate).to.deep.equal({})
data = {}

template.add(TEMPLATE_ID_1, templateCode1)
data[TEMPLATE_ID_1] = templateCode1
expect(_cacheTemplate).to.deep.equal(data)

template.add(TEMPLATE_ID_2, templateCode2)
data[TEMPLATE_ID_2] = templateCode2
template.add(TEMPLATE_ID_1, templateCode2)
data[TEMPLATE_ID_1] = templateCode2
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原来的测试代码写错了?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我觉得写错了,我认为应该这么测试:add一次id为TEMPLATE_ID_1的模板templateCode1,然后再add一次同名的模板templateCode2, data是正确的,然后比较_cacheTemplate和data是否一致。

expect(_cacheTemplate).to.deep.equal(data)
})
it('overwrites compiledTemplate while adding existed template id', function () {
expect(_cacheCompiledTemplate).to.deep.equal({})

template.add(TEMPLATE_ID_1, templateCode1)
template.render(TEMPLATE_ID_1, templateData1)
expect(_cacheCompiledTemplate[TEMPLATE_ID_1].source).to.equal(fn1.source)

template.add(TEMPLATE_ID_1, templateCode2)
expect(_cacheCompiledTemplate[TEMPLATE_ID_1]).to.equal(null)
template.render(TEMPLATE_ID_1, templateData2)
expect(_cacheCompiledTemplate[TEMPLATE_ID_1]).to.be.a('function')
expect(_cacheCompiledTemplate[TEMPLATE_ID_1].source).to.equal(fn2.source)
})
it('does nothing if missing template code as second param', function () {
expect(_cacheTemplate).to.deep.equal({})
template.add('foo')
Expand Down