tea-time/presentation/10_misc_hints.slides

142 lines
2.4 KiB
Plaintext
Raw Normal View History

2012-03-13 17:29:42 +00:00
!SLIDE
# Some miscellaneous hints and tips
!SLIDE
# Testing jQuery
!SLIDE
# Mocking and stubbing `$.fn` calls
2012-03-24 14:20:56 +00:00
!SLIDE even-larger
2012-03-13 17:29:42 +00:00
``` coffeescript
this.containerWaiter = ->
2012-03-24 14:20:56 +00:00
$('#container').
addClass('wait').
append('<div class="waiting" />')
2012-03-13 17:29:42 +00:00
```
2012-03-24 14:20:56 +00:00
!SLIDE even-larger
2012-03-13 17:29:42 +00:00
``` coffeescript
$.fn.makeWait = ->
2012-03-24 14:20:56 +00:00
$(this).
addClass('wait').
append('<div class="waiting" />')
2012-03-13 17:29:42 +00:00
this
```
!SLIDE even-larger
``` coffeescript
this.containerWaiter = ->
$('#container').makeWait()
```
!SLIDE
# `jquery-jasmine`
2012-03-24 14:20:56 +00:00
!SLIDE even-larger
2012-03-13 17:29:42 +00:00
``` coffeescript
describe 'container', ->
beforeEach ->
setFixtures('<div id="container" />')
it 'should make it wait', ->
containerWaiter()
2012-03-24 14:20:56 +00:00
expect($('#container')).
toHaveClass('wait')
expect($('#container')).
toContain('div.waiting')
2012-03-13 17:29:42 +00:00
```
!SLIDE image-80-percent
<img src="assets/wet-cat.jpg" />
!SLIDE image-80-percent
<img src="assets/spaghetti.jpg" />
!SLIDE even-larger
``` coffeescript
describe '$.fn.makeWait', ->
it 'should make wait', ->
$div = $('<div />')
$div.makeWait()
expect($div).toHaveClass('wait')
expect($div).toContain('div.waiting')
```
!SLIDE even-larger
``` coffeescript
describe 'container', ->
beforeEach ->
setFixtures('<div id="container" />')
spyOn($.fn, 'makeWait')
it 'should make it wait', ->
containerWaiter()
2012-03-24 14:20:56 +00:00
expect($.fn.makeWait).
toHaveBeenCalled()
2012-03-13 17:29:42 +00:00
```
!SLIDE
# No longer testing jQuery, just testing for our code
!SLIDE
# Animations and other time-dependent things
!SLIDE even-larger
``` coffeescript
2012-03-24 14:20:56 +00:00
class @Cat
2012-03-13 17:29:42 +00:00
constructor: ->
@mood = "happy"
pet: ->
setTimeout(
-> @mood = "angry"
, 500
)
```
!SLIDE
# Do you really need to test the `setTimeout`?
!SLIDE even-larger
``` coffeescript
class Cat
constructor: ->
@mood = "happy"
pet: -> setTimeout(@makeAngry, 500)
makeAngry: => @mood = "angry"
```
!SLIDE
# Use Jasmine's `waitsFor` and `runs`
!SLIDE larger
``` coffeescript
describe 'cat moods', ->
it 'should change moods', ->
cat = new Cat()
# we want to know the cat's current mood
currentMood = cat.mood
# start petting the cat
runs -> cat.pet()
# wait one second for the cat's mood to change
waitsFor(
->
cat.mood != currentMood
, "Cat changed its mood",
1000
)
# expect the inevitable
runs ->
expect(cat.mood).toEqual('angry')
```