Skip to content

Instantly share code, notes, and snippets.

@joelmccracken
Created December 28, 2010 07:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joelmccracken/757029 to your computer and use it in GitHub Desktop.
Save joelmccracken/757029 to your computer and use it in GitHub Desktop.
describe "Example", ->
beforeEach ->
window._fake = ->
arguments[2](output: "3") if arguments[2]
post_fake_spy = spyOn(window.$, 'post').andCallFake ->
window.fake.apply(this, post_fake_spy.mostRecentCall.args)
afterEach ->
window.fake = window._fake
describe ".attachToPage", ->
it "should look for examples with jQuery", ->
spyOn(window, '$').andCallThrough()
Example.attachToPage()
expect(window.examples).toNotEqual null
it "should create a new example object for every example", ->
loadFixtures 'example.html'
spyOn(Example.prototype, 'construct').andCallThrough()
Example.attachToPage()
expect(Example.prototype.construct).toHaveBeenCalledWith $(".example")[0]
it "should assign to window.examples", ->
loadFixtures 'example.html'
Example.attachToPage()
expect(window.examples).toNotEqual(null)
describe "#construct", ->
it "should save the passed header", ->
loadFixtures('example.html')
ex_head = $('.example:first')
ex = new Example()
ex.construct(ex_head)
expect(ex.heading).toEqual($(ex_head))
it "should add a div below the header", ->
loadFixtures('example.html')
ex_head = $('.example:first')
console.log "hi there"
ex = new Example()
ex.construct(ex_head)
expect($(".example + div")).toBe("div.example_body")
it "should add a form, output div, textarea, and submit link", ->
loadFixtures('example.html')
ex_head = $('.example:first')
ex = new Example()
ex.construct(ex_head)
sample_div = $(".example + div")
expect(sample_div).toBe("div.example_body")
expect(sample_div.find("form > textarea")).toExist()
expect(sample_div.find("form > a.run_example")).toExist()
expect(sample_div.find("form > div.output")).toExist()
it "should add a click method to a.run_example", ->
loadFixtures 'example.html'
spyOn(window, '$').andCallThrough()
jQuery.extend(window.$, window.jQuery) #get back what the spy stole
ex_head = $ '.example:first'
ex = new Example
ex.construct ex_head
spyOn(ex, 'runClick').andCallThrough()
ex.example_body.find("a.run_example").click()
expect(ex.runClick).toHaveBeenCalled()
describe "#runClick", ->
beforeEach ->
null
it "should submit the code", ->
code = "(+ 1 2)"
loadFixtures('example.html')
ex_head = $('.example:first')
ex = new Example()
ex.construct(ex_head)
ex.code_element.val(code)
ex.run_button.click()
expect($.post).toHaveBeenCalledWith("/eval", {code: code}, ex.afterSubmit)
it "should call #afterSave when done", ->
code = "(+ 1 2)"
loadFixtures 'example.html'
ex_head = $ '.example:first'
ex = new Example()
ex.construct ex_head
ex.code_element.val code
spyOn(ex, 'afterSubmit')
ex.run_button.click()
expect($.post).toHaveBeenCalledWith("/eval", {code: code}, ex.afterSubmit)
expect(ex.afterSubmit).toHaveBeenCalled()
describe "#afterSubmit", ->
@ex = 0;
beforeEach =>
code = "1 + 2"
loadFixtures 'example.html'
ex_head = $ '.example:first'
@ex = new Example
@ex.construct ex_head
it "should display the returned results in the output div", ->
ex.run_button.click()
expect(ex.example_body.find('div.output')).toHaveText("3")
@joelmccracken
Copy link
Author

my current error:
In spec/coffeescripts/Example_spec.coffee, Parse error on line 91: Unexpected 'INDENT'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment