forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReferenceSpec.coffee
More file actions
53 lines (46 loc) · 2.26 KB
/
Copy pathReferenceSpec.coffee
File metadata and controls
53 lines (46 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
define ['compiled/Reference'], (Reference) ->
# THE REFERENCE SPEC
# This is a collection of best practice recommendations from specs
# that have bitten us in the past either because they were too fragile,
# too brittle, or annoying in some other way. Please continue to add to
# it whenever you go fix a bad practice in a JS spec so others can use
# it as a template when trying to write new JS specs
# if you need to share state between functions like this
# initialize the actual value during setup rather than at definition time,
# just put the reference up here and null it out
fixtures = null
storedStupidAwesomeFunction = null
QUnit.module "Reference",
setup: ->
# DO put any necessary DOM artifacts into #fixtures, not "body"
fixtures = document.getElementById('fixtures')
fixtures.innerHTML = "<div id='reference-dom'></div>"
# DO save the original implementation of functions
# that you inted to overwrite either in the setup or in individual
# specs. You can use it to repair state in teardown
storedStupidAwesomeFunction = window.stupidlyAwesomeGlobalFunction
teardown: ->
# DO clean up the fixtures element if you use it,
# don't just remove the view you know
# about, blank the thing entirely, DOM state shouldn't bleed between
# specs
fixtures.innerHTML = ""
# DO repair global state in a teardown, even if
# you're only monkeying with it once, because errors that halt execution
# in a given spec will stop the rest of that function from running
window.stupidlyAwesomeGlobalFunction = storedStupidAwesomeFunction
# DONT nest your tests within module, qunit won't complain, but it WILL
# run the tests within the wrong module context (that is, whichever module
# ran before this one
test "a simple function", ()->
ref = new Reference()
equal(ref.sum(6, 4), 10)
# DONT monkey patch some global function without fixing it afterwards, but
# remember to fix it in a teardown (see docs in teardown above)
test "stubbing out a global", ()->
sentMessage = ""
window.stupidlyAwesomeGlobalFunction = (message)->
sentMessage = message
ref = new Reference()
ref.sendMessage("Hello, QUnit")
equal(sentMessage, "Hello, QUnit")