We are currently delving into the world of Ember.js. Our development process is completely test-driven, and we aim to apply the same methodology to Ember.js. Having prior experience in test-driven development with Backbone.js apps using Jasmine or Mocha/Chai, we are familiar with testing front-end code.
However, when it comes to testing views that contain a #linkTo
statement in the template, we encountered some challenges. We have been searching for good examples and best practices for testing such scenarios. This gist represents our journey to uncover effective ways to unit-test Ember applications.
Upon examining the linkTo test in the Ember.js source code, we observed a complete setup of an Ember app to support #linkTo
. This raises the question of whether we can simulate this behavior when testing a template.
How can tests be set up for ember views that include template renders?
Check out this gist containing our test cases, along with a template that will pass the test and another that will fail it.
view_spec.js.coffee
# Written with Mocha / Chai,
# Utilizing chai-jquery and chai-changes extensions
describe 'TodoItemsView', ->
beforeEach ->
testSerializer = DS.JSONSerializer.create
primaryKey: -> 'id'
TestAdapter = DS.Adapter.extend
serializer: testSerializer
TestStore = DS.Store.extend
revision: 11
adapter: TestAdapter.create()
TodoItem = DS.Model.extend
title: DS.attr('string')
store = TestStore.create()
@todoItem = store.createRecord TodoItem
title: 'Do something'
@controller = Em.ArrayController.create
content: []
@view = Em.View.create
templateName: 'working_template'
controller: @controller
@controller.pushObject @todoItem
afterEach ->
@view.destroy()
@controller.destroy()
@todoItem.destroy()
describe 'amount of todos', ->
beforeEach ->
# $('#konacha') serves as a div that is reset between each test
Em.run => @view.appendTo '#konacha'
it 'is displayed', ->
$('#konacha .todos-count').should.have.text '1 things to do'
it 'is dynamically updated', ->
expect(=> $('#konacha .todos-count').text()).to.change.from('1 things to do').to('2 things to do').when =>
Em.run =>
extraTodoItem = store.createRecord TodoItem,
title: 'More tasks'
@controller.pushObject extraTodoItem
broken_template.handlebars
<div class="todos-count"><span class="todos">{{length}}</span> things to do</div>
{{#linkTo "index"}}Home{{/linkTo}}
working_template.handlebars
<div class="todos-count"><span class="todos">{{length}}</span> things to do</div>