Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: gerryster/js_training
base: ffeae27494
...
head fork: gerryster/js_training
compare: 44e65239ad
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 4 files changed
  • 0 commit comments
  • 1 contributor
Commits on Apr 23, 2012
@gerryster TodoList.spec.coffee now exists and is passing. I also put
BackBone.LocalStore in its own module so that tests can mock it out.
4a90eff
@gerryster Merge branch 'master' of github.com:gerryster/js_training 44e6523
View
1  TODO.md
@@ -1,4 +1,3 @@
-- Get tests working
- Test in Chrome stable
- Convert index.html to use the rewritten app
- delete unused files from the koans
View
64 specs/models/TodoList.spec.coffee
@@ -0,0 +1,64 @@
+define -> ({loadModule})->
+
+ beforeEach ->
+ loadModule
+ # stub out the LocalStorage dependency by returning an empty object:
+ 'helpers/LocalStorage': -> {}
+ (@TodoList)=>
+
+ describe 'collection (TodoList)', ->
+ it 'Can add Model instances as objects one at a time, or as arrays of models.', ->
+ todos = new @TodoList()
+
+ expect(todos.length).toBe(0)
+
+ todos.add({ text: 'Clean the kitchen' })
+
+ expect(todos.length).toEqual(1)
+
+ # How would you add multiple models to the collection with a single method call?
+ todos.add([{ text: 'Clean the kitchen' }, { text: 'task 2'}])
+
+ expect(todos.length).toEqual(3)
+
+ it 'Can have a comparator function to keep the collection sorted.', ->
+ todos = new @TodoList()
+
+ # Without changing the sequence of the Todo objects in the array, how would you
+ # get the expectations below to pass?
+ #
+ # How is the collection sorting the models when they are added? (see TodoList.comparator)
+ #
+ # Hint: Could you change attribute values on the todos themselves?
+
+ todos.add([{ text: 'Do the laundry', order: 2},
+ { text: 'Clean the house', order: 1},
+ { text: 'Take a nap', order: 3}])
+
+ expect(todos.at(0).get('text')).toEqual('Clean the house')
+ expect(todos.at(1).get('text')).toEqual('Do the laundry')
+ expect(todos.at(2).get('text')).toEqual('Take a nap')
+
+ # How are you supposed to know what Backbone objects trigger events? To the docs!
+ # http://documentcloud.github.com/backbone/#FAQ-events
+ it 'Fires custom named events when the contents of the collection change.', ->
+ todos = new @TodoList();
+
+ addModelCallback = jasmine.createSpy('-add model callback-')
+ todos.bind('add', addModelCallback)
+
+ # How would you get both expectations to pass with a single method call?
+ todo = new Todo({text: 'pass this test'})
+ todos.add(todo)
+
+ expect(todos.length).toEqual(1)
+ expect(addModelCallback).toHaveBeenCalled()
+
+ removeModelCallback = jasmine.createSpy('-remove model callback-')
+ todos.bind('remove', removeModelCallback)
+
+ # How would you get both expectations to pass with a single method call?
+ todos.remove(todo)
+
+ expect(todos.length).toEqual(0)
+ expect(removeModelCallback).toHaveBeenCalled()
View
4 src/helpers/LocalStorage.coffee
@@ -0,0 +1,4 @@
+# Instead of using BackBone.LocalStorage directly from models or collections,
+# require this module. This allows unit tests to mock out this dependency.
+define [], ()->
+ Backbone.LocalStorage
View
11 src/models/TodoList.coffee
@@ -1,10 +1,13 @@
-define ['models/Todo'], (Todo)->
+define [
+ 'models/Todo'
+ 'helpers/LocalStorage'
+], (Todo,LocalStorage)->
TodoList = Backbone.Collection.extend
model: Todo
-
+
# Use HTML5 local storage instead of saving models to a web server.
- localStorage: new Backbone.LocalStorage("TodoList")
+ localStorage: new LocalStorage("TodoList")
url: '/todos/'
@@ -14,7 +17,7 @@ define ['models/Todo'], (Todo)->
remaining: ->
this.without.apply(this, this.done())
-
+
nextOrder: ->
if (!this.length)
return 1

No commit comments for this range

Something went wrong with that request. Please try again.