This is a part of an ongoing series on BDD in Sproutcore. You can find the index for the series here
The next thing that I would like to be able to do with the todos application is to be able to add a task. In order to accomplish this, I write the following integration test:
The only thing new here is the simulating of a mouse click on the button within the run loop inside the test. Within sproutcore, there is no such thing as a native "click" event. Instead, Sproutcore registers a click on a mousedown event, followed by a mouseup. Mike Cohen(FrozenCanuck) has a great blog post explaining the idea, which you can view here. For now, know this is how I simulate a clicking on the button.
This generates the following failure:
This means that the page does not have the button currently on it. I add it to the main_page.js:
This then changes the error to read:
This tells me that the button is not doing what it is supposed to be doing (adding a task), so I need to implement that functionality. In order to do that, I need to setup the button to target the tasksController, with an action called addTask, which does not yet exist. I modify main_page.js to be the following:
Again, at this point Sproutcore does not give an error saying that the specified bind function does not exist, but I know that it doesn't, so I add a unit test to implement this functionality:
This then fails with the following:
I add the function to the controller:
Which changes the failure to the following:
To get that test to pass, I add implement the function:
This causes both my unit tests, and my integration tests to pass, leaving me in the green. As such, it's time for me to clean up the view a bit.