Skip to content

Instantly share code, notes, and snippets.

@rosenfeld
Created October 6, 2011 01:44
Show Gist options
  • Save rosenfeld/1266283 to your computer and use it in GitHub Desktop.
Save rosenfeld/1266283 to your computer and use it in GitHub Desktop.
show Terms and Conditions
# spec/js/show-terms-and-conditions.spec.coffee:
require './jasmine-sinon' # wouldn't you love if vanilla JavaScript also supported 'require'?
dom = require 'jsdom'
#f = (fn) -> __dirname + '/../../web-app/js/' + fn # if you prefer to be more explicit
f = (fn) -> '../../web-app/js/' + fn
window = $ = null
dom.env
html: '<body></body>' # or require('fs').readFileSync("#{__dirname}/spec/fixures/any.html").toString()
scripts: ['sinon.js', f('jquery/jquery.min.js'), f('jquery/jquery-ui.min.js'), f('wmd/showdown.js'), 'ajax-faker.js',
f('showTermsAndConditions.js')]
# src: ["console.log('all scripts were loaded')", "var loaded=true"]
done: (errors, _window) ->
console.log("errors:", errors) if errors
window = _window
$ = window.$
# jasmine.asyncSpecDone() if window.loaded
# We must tell Jasmine to wait until the DOM is loaded and the script is run
# Jasmine doesn't support a beforeAll, like RSpec
beforeEach(-> waitsFor -> $) unless $
# another approach: (you should uncomment the line above for it to work)
# already_run = false
# beforeEach -> already_run ||= jasmine.asyncSpecWait() or true
describe 'showing Terms and Conditions', ->
it 'should get last Terms and Conditions', ->
@after -> $.ajax.restore() # undo the stubbed ajax call introduced by fake-ajax.js after this example.
expect($.ajax).toHaveBeenCalledOnce()
firstAjaxCallArgs = $.ajax.getCall(0).args[0]
expect(firstAjaxCallArgs.url).toEqual 'termsAndConditions/lastTermsAndConditions'
firstAjaxCallArgs.success id: 1, termsAndConditions: '# title'
describe 'after set-up', ->
beforeEach -> window.sinon.stub $, 'ajax'
afterEach -> $.ajax.restore()
afterEach -> $('.ui-dialog').dialog 'open' # it is usually closed at the end of each example
it 'should convert markdown to HTML', -> expect($('h1').text()).toEqual 'title'
it 'should close the dialog, send a request to server and redirect to ../ when the terms are accepted', ->
$('button:contains(I agree)').click()
ajaxRequestArgs = $.ajax.args[0][0]
expect(ajaxRequestArgs.url).toEqual 'termsAndConditions/agree'
expect(ajaxRequestArgs.data).toEqual id: 1
ajaxRequestArgs.success()
expect(window.location).toEqual '../'
expect($('.ui-dialog:visible').length).toEqual 0
it 'should close the dialog and redirect to ../logout when the terms are not accepted', ->
# the page wasn't really redirected in this simulation by the prior example
$('button:contains(Log out)').click()
expect(window.location).toEqual '../logout'
expect($('.ui-dialog:visible').length).toEqual 0
$ ->
converter = new Attacklab.showdown.converter()
lastTermsAndConditions = {}
$.get 'termsAndConditions/lastTermsAndConditions', (data) ->
lastTermsAndConditions = data
$('<div/>').html(converter.makeHtml(lastTermsAndConditions.termsAndConditions))
.dialog
width: 800, height: 600, modal: true, buttons:
'I agree': onAgreement, 'Log out': onLogout
onAgreement = ->
$.post 'termsAndConditions/agree', id: lastTermsAndConditions.id, =>
$(this).dialog('close')
window.location = '../' # redirect to home
onLogout = ->
$(this).dialog('close')
window.location = '../logout' # sign out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment