Skip to content

Commit

Permalink
[BUGFIX release] Test {{each}} with duplicate items.
Browse files Browse the repository at this point in the history
  • Loading branch information
rwjblue committed Jul 22, 2015
1 parent 6e3085a commit 3204d4e
Showing 1 changed file with 50 additions and 16 deletions.
66 changes: 50 additions & 16 deletions packages/ember-htmlbars/tests/helpers/each_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -759,22 +759,19 @@ QUnit.test('can specify `@identity` to represent mixed object and primitive item
equal(view.$().text(), 'foobarbaz');
});

QUnit.test('duplicate keys trigger a useful error (temporary until we can deal with this properly in HTMLBars)', function() {
QUnit.test('duplicate keys work properly with primitive items', function() {
runDestroy(view);
view = EmberView.create({
items: ['a', 'a', 'a'],
template: compile('{{#each view.items as |item|}}{{item}}{{/each}}')
});

throws(
function() {
runAppend(view);
},
`Duplicate key found ('a') for '{{each}}' helper, please use a unique key or switch to '{{#each model key="@index"}}{{/each}}'.`
);
runAppend(view);

equal(view.$().text(), 'aaa');
});

QUnit.test('pushing a new duplicate key will trigger a useful error (temporary until we can deal with this properly in HTMLBars)', function() {
QUnit.test('pushing a new duplicate key will render properly with primitive items', function() {
runDestroy(view);
view = EmberView.create({
items: A(['a', 'b', 'c']),
Expand All @@ -783,12 +780,49 @@ QUnit.test('pushing a new duplicate key will trigger a useful error (temporary u

runAppend(view);

throws(
function() {
run(function() {
view.get('items').pushObject('a');
});
},
`Duplicate key found ('a') for '{{each}}' helper, please use a unique key or switch to '{{#each model key="@index"}}{{/each}}'.`
);
run(function() {
view.get('items').pushObject('a');
});

equal(view.$().text(), 'abca');
});

QUnit.test('duplicate keys work properly with objects', function() {
runDestroy(view);
let duplicateItem = { display: 'foo' };
view = EmberView.create({
items: [
duplicateItem,
duplicateItem,
{ display: 'bar' },
{ display: 'qux' }
],
template: compile('{{#each view.items as |item|}}{{item.display}}{{/each}}')
});

runAppend(view);

equal(view.$().text(), 'foofoobarqux');
});

QUnit.test('pushing a new duplicate key will render properly with objects', function() {
runDestroy(view);

let duplicateItem = { display: 'foo' };
view = EmberView.create({
items: A([
duplicateItem,
{ display: 'bar' },
{ display: 'qux' }
]),
template: compile('{{#each view.items as |item|}}{{item.display}}{{/each}}')
});

runAppend(view);

run(function() {
view.get('items').pushObject(duplicateItem);
});

equal(view.$().text(), 'foobarquxfoo');
});

0 comments on commit 3204d4e

Please sign in to comment.