CoffeeScript tests migration: Decaffeinate files

This is running decaffeinate, with no additional cleanup.
This commit is contained in:
David Ormsbee
2017-08-17 20:09:37 -04:00
committed by Calen Pennington
parent 5c64da2f63
commit 0880502f26
30 changed files with 5897 additions and 4850 deletions

View File

@@ -1,353 +1,435 @@
describe 'Calculator', ->
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
describe('Calculator', function() {
KEY =
TAB : 9
ENTER : 13
ALT : 18
ESC : 27
SPACE : 32
LEFT : 37
UP : 38
RIGHT : 39
const KEY = {
TAB : 9,
ENTER : 13,
ALT : 18,
ESC : 27,
SPACE : 32,
LEFT : 37,
UP : 38,
RIGHT : 39,
DOWN : 40
};
beforeEach ->
loadFixtures 'coffee/fixtures/calculator.html'
@calculator = new Calculator
beforeEach(function() {
loadFixtures('coffee/fixtures/calculator.html');
return this.calculator = new Calculator;
});
describe 'bind', ->
it 'bind the calculator button', ->
expect($('.calc')).toHandleWith 'click', @calculator.toggle
describe('bind', function() {
it('bind the calculator button', function() {
return expect($('.calc')).toHandleWith('click', this.calculator.toggle);
});
it 'bind key up on calculator', ->
expect($('#calculator_wrapper')).toHandle 'keyup', @calculator.handleKeyUpOnHint
it('bind key up on calculator', function() {
return expect($('#calculator_wrapper')).toHandle('keyup', this.calculator.handleKeyUpOnHint);
});
it 'bind the help button', ->
# This events is bind by $.click()
expect($('#calculator_hint')).toHandle 'click'
it('bind the help button', () =>
// This events is bind by $.click()
expect($('#calculator_hint')).toHandle('click')
);
it 'bind the calculator submit', ->
expect($('form#calculator')).toHandleWith 'submit', @calculator.calculate
it('bind the calculator submit', function() {
return expect($('form#calculator')).toHandleWith('submit', this.calculator.calculate);
});
xit 'prevent default behavior on form submit', ->
jasmine.stubRequests()
$('form#calculator').submit (e) ->
expect(e.isDefaultPrevented()).toBeTruthy()
e.preventDefault()
$('form#calculator').submit()
return xit('prevent default behavior on form submit', function() {
jasmine.stubRequests();
$('form#calculator').submit(function(e) {
expect(e.isDefaultPrevented()).toBeTruthy();
return e.preventDefault();
});
return $('form#calculator').submit();
});
});
describe 'toggle', ->
it 'focuses the input when toggled', (done)->
describe('toggle', function() {
it('focuses the input when toggled', function(done){
self = this
focus = ()->
deferred = $.Deferred()
const self = this;
const focus = function(){
const deferred = $.Deferred();
# Since the focus is called asynchronously, we need to
# wait until focus() is called.
spyOn($.fn, 'focus').and.callFake (elementName) ->
deferred.resolve()
// Since the focus is called asynchronously, we need to
// wait until focus() is called.
spyOn($.fn, 'focus').and.callFake(elementName => deferred.resolve());
self.calculator.toggle(jQuery.Event("click"))
self.calculator.toggle(jQuery.Event("click"));
deferred.promise()
return deferred.promise();
};
focus().then(
->
expect($('#calculator_wrapper #calculator_input').focus).toHaveBeenCalled()
).always(done)
return focus().then(
() => expect($('#calculator_wrapper #calculator_input').focus).toHaveBeenCalled()).always(done);
});
it 'toggle the close button on the calculator button', ->
@calculator.toggle(jQuery.Event("click"))
expect($('.calc')).toHaveClass('closed')
return it('toggle the close button on the calculator button', function() {
this.calculator.toggle(jQuery.Event("click"));
expect($('.calc')).toHaveClass('closed');
@calculator.toggle(jQuery.Event("click"))
expect($('.calc')).not.toHaveClass('closed')
this.calculator.toggle(jQuery.Event("click"));
return expect($('.calc')).not.toHaveClass('closed');
});
});
describe 'showHint', ->
it 'show the help overlay', ->
@calculator.showHint()
expect($('.help')).toHaveClass('shown')
expect($('.help')).toHaveAttr('aria-hidden', 'false')
describe('showHint', () =>
it('show the help overlay', function() {
this.calculator.showHint();
expect($('.help')).toHaveClass('shown');
return expect($('.help')).toHaveAttr('aria-hidden', 'false');
})
);
describe 'hideHint', ->
it 'show the help overlay', ->
@calculator.hideHint()
expect($('.help')).not.toHaveClass('shown')
expect($('.help')).toHaveAttr('aria-hidden', 'true')
describe('hideHint', () =>
it('show the help overlay', function() {
this.calculator.hideHint();
expect($('.help')).not.toHaveClass('shown');
return expect($('.help')).toHaveAttr('aria-hidden', 'true');
})
);
describe 'handleClickOnHintButton', ->
it 'on click hint button hint popup becomes visible ', ->
e = jQuery.Event('click');
describe('handleClickOnHintButton', () =>
it('on click hint button hint popup becomes visible ', function() {
const e = jQuery.Event('click');
$('#calculator_hint').trigger(e);
expect($('.help')).toHaveClass 'shown'
return expect($('.help')).toHaveClass('shown');
})
);
describe 'handleClickOnDocument', ->
it 'on click out of the hint popup it becomes hidden', ->
@calculator.showHint()
e = jQuery.Event('click');
describe('handleClickOnDocument', () =>
it('on click out of the hint popup it becomes hidden', function() {
this.calculator.showHint();
const e = jQuery.Event('click');
$(document).trigger(e);
expect($('.help')).not.toHaveClass 'shown'
return expect($('.help')).not.toHaveClass('shown');
})
);
describe 'handleClickOnHintPopup', ->
it 'on click of hint popup it remains visible', ->
@calculator.showHint()
e = jQuery.Event('click');
describe('handleClickOnHintPopup', () =>
it('on click of hint popup it remains visible', function() {
this.calculator.showHint();
const e = jQuery.Event('click');
$('#calculator_input_help').trigger(e);
expect($('.help')).toHaveClass 'shown'
return expect($('.help')).toHaveClass('shown');
})
);
describe 'selectHint', ->
it 'select correct hint item', ->
spyOn($.fn, 'focus')
element = $('.hint-item').eq(1)
@calculator.selectHint(element)
describe('selectHint', function() {
it('select correct hint item', function() {
spyOn($.fn, 'focus');
const element = $('.hint-item').eq(1);
this.calculator.selectHint(element);
expect(element.focus).toHaveBeenCalled()
expect(@calculator.activeHint).toEqual(element)
expect(@calculator.hintPopup).toHaveAttr('data-calculator-hint', element.attr('id'))
expect(element.focus).toHaveBeenCalled();
expect(this.calculator.activeHint).toEqual(element);
return expect(this.calculator.hintPopup).toHaveAttr('data-calculator-hint', element.attr('id'));
});
it 'select the first hint if argument element is not passed', ->
@calculator.selectHint()
expect(@calculator.activeHint.attr('id')).toEqual($('.hint-item').first().attr('id'))
it('select the first hint if argument element is not passed', function() {
this.calculator.selectHint();
return expect(this.calculator.activeHint.attr('id')).toEqual($('.hint-item').first().attr('id'));
});
it 'select the first hint if argument element is empty', ->
@calculator.selectHint([])
expect(@calculator.activeHint.attr('id')).toBe($('.hint-item').first().attr('id'))
return it('select the first hint if argument element is empty', function() {
this.calculator.selectHint([]);
return expect(this.calculator.activeHint.attr('id')).toBe($('.hint-item').first().attr('id'));
});
});
describe 'prevHint', ->
describe('prevHint', function() {
it 'Prev hint item is selected', ->
@calculator.activeHint = $('.hint-item').eq(1)
@calculator.prevHint()
it('Prev hint item is selected', function() {
this.calculator.activeHint = $('.hint-item').eq(1);
this.calculator.prevHint();
expect(@calculator.activeHint.attr('id')).toBe($('.hint-item').eq(0).attr('id'))
return expect(this.calculator.activeHint.attr('id')).toBe($('.hint-item').eq(0).attr('id'));
});
it 'if this was the second item, select the first one', ->
@calculator.activeHint = $('.hint-item').eq(1)
@calculator.prevHint()
it('if this was the second item, select the first one', function() {
this.calculator.activeHint = $('.hint-item').eq(1);
this.calculator.prevHint();
expect(@calculator.activeHint.attr('id')).toBe($('.hint-item').eq(0).attr('id'))
return expect(this.calculator.activeHint.attr('id')).toBe($('.hint-item').eq(0).attr('id'));
});
it 'if this was the first item, select the last one', ->
@calculator.activeHint = $('.hint-item').eq(0)
@calculator.prevHint()
it('if this was the first item, select the last one', function() {
this.calculator.activeHint = $('.hint-item').eq(0);
this.calculator.prevHint();
expect(@calculator.activeHint.attr('id')).toBe($('.hint-item').eq(2).attr('id'))
return expect(this.calculator.activeHint.attr('id')).toBe($('.hint-item').eq(2).attr('id'));
});
it 'if this was the last item, select the second last', ->
@calculator.activeHint = $('.hint-item').eq(2)
@calculator.prevHint()
return it('if this was the last item, select the second last', function() {
this.calculator.activeHint = $('.hint-item').eq(2);
this.calculator.prevHint();
expect(@calculator.activeHint.attr('id')).toBe($('.hint-item').eq(1).attr('id'))
return expect(this.calculator.activeHint.attr('id')).toBe($('.hint-item').eq(1).attr('id'));
});
});
describe 'nextHint', ->
describe('nextHint', function() {
it 'if this was the first item, select the second one', ->
@calculator.activeHint = $('.hint-item').eq(0)
@calculator.nextHint()
it('if this was the first item, select the second one', function() {
this.calculator.activeHint = $('.hint-item').eq(0);
this.calculator.nextHint();
expect(@calculator.activeHint.attr('id')).toBe($('.hint-item').eq(1).attr('id'))
return expect(this.calculator.activeHint.attr('id')).toBe($('.hint-item').eq(1).attr('id'));
});
it 'If this was the second item, select the last one', ->
@calculator.activeHint = $('.hint-item').eq(1)
@calculator.nextHint()
it('If this was the second item, select the last one', function() {
this.calculator.activeHint = $('.hint-item').eq(1);
this.calculator.nextHint();
expect(@calculator.activeHint.attr('id')).toBe($('.hint-item').eq(2).attr('id'))
return expect(this.calculator.activeHint.attr('id')).toBe($('.hint-item').eq(2).attr('id'));
});
it 'If this was the last item, select the first one', ->
@calculator.activeHint = $('.hint-item').eq(2)
@calculator.nextHint()
return it('If this was the last item, select the first one', function() {
this.calculator.activeHint = $('.hint-item').eq(2);
this.calculator.nextHint();
expect(@calculator.activeHint.attr('id')).toBe($('.hint-item').eq(0).attr('id'))
return expect(this.calculator.activeHint.attr('id')).toBe($('.hint-item').eq(0).attr('id'));
});
});
describe 'handleKeyDown', ->
assertHintIsHidden = (calc, key) ->
spyOn(calc, 'hideHint')
calc.showHint()
e = jQuery.Event('keydown', { keyCode: key });
value = calc.handleKeyDown(e)
describe('handleKeyDown', function() {
const assertHintIsHidden = function(calc, key) {
spyOn(calc, 'hideHint');
calc.showHint();
const e = jQuery.Event('keydown', { keyCode: key });
const value = calc.handleKeyDown(e);
expect(calc.hideHint).toHaveBeenCalled
expect(value).toBeFalsy()
expect(e.isDefaultPrevented()).toBeTruthy()
expect(calc.hideHint).toHaveBeenCalled;
expect(value).toBeFalsy();
return expect(e.isDefaultPrevented()).toBeTruthy();
};
assertHintIsVisible = (calc, key) ->
spyOn(calc, 'showHint')
spyOn($.fn, 'focus')
e = jQuery.Event('keydown', { keyCode: key });
value = calc.handleKeyDown(e)
const assertHintIsVisible = function(calc, key) {
spyOn(calc, 'showHint');
spyOn($.fn, 'focus');
const e = jQuery.Event('keydown', { keyCode: key });
const value = calc.handleKeyDown(e);
expect(calc.showHint).toHaveBeenCalled
expect(value).toBeFalsy()
expect(e.isDefaultPrevented()).toBeTruthy()
expect(calc.activeHint.focus).toHaveBeenCalled()
expect(calc.showHint).toHaveBeenCalled;
expect(value).toBeFalsy();
expect(e.isDefaultPrevented()).toBeTruthy();
return expect(calc.activeHint.focus).toHaveBeenCalled();
};
assertNothingHappens = (calc, key) ->
spyOn(calc, 'showHint')
e = jQuery.Event('keydown', { keyCode: key });
value = calc.handleKeyDown(e)
const assertNothingHappens = function(calc, key) {
spyOn(calc, 'showHint');
const e = jQuery.Event('keydown', { keyCode: key });
const value = calc.handleKeyDown(e);
expect(calc.showHint).not.toHaveBeenCalled
expect(value).toBeTruthy()
expect(e.isDefaultPrevented()).toBeFalsy()
expect(calc.showHint).not.toHaveBeenCalled;
expect(value).toBeTruthy();
return expect(e.isDefaultPrevented()).toBeFalsy();
};
it 'hint popup becomes hidden on press ENTER', ->
assertHintIsHidden(@calculator, KEY.ENTER)
it('hint popup becomes hidden on press ENTER', function() {
return assertHintIsHidden(this.calculator, KEY.ENTER);
});
it 'hint popup becomes visible on press ENTER', ->
assertHintIsVisible(@calculator, KEY.ENTER)
it('hint popup becomes visible on press ENTER', function() {
return assertHintIsVisible(this.calculator, KEY.ENTER);
});
it 'hint popup becomes hidden on press SPACE', ->
assertHintIsHidden(@calculator, KEY.SPACE)
it('hint popup becomes hidden on press SPACE', function() {
return assertHintIsHidden(this.calculator, KEY.SPACE);
});
it 'hint popup becomes visible on press SPACE', ->
assertHintIsVisible(@calculator, KEY.SPACE)
it('hint popup becomes visible on press SPACE', function() {
return assertHintIsVisible(this.calculator, KEY.SPACE);
});
it 'Nothing happens on press ALT', ->
assertNothingHappens(@calculator, KEY.ALT)
it('Nothing happens on press ALT', function() {
return assertNothingHappens(this.calculator, KEY.ALT);
});
it 'Nothing happens on press any other button', ->
assertNothingHappens(@calculator, KEY.DOWN)
return it('Nothing happens on press any other button', function() {
return assertNothingHappens(this.calculator, KEY.DOWN);
});
});
describe 'handleKeyDownOnHint', ->
it 'Navigation works in proper way', ->
calc = @calculator
describe('handleKeyDownOnHint', () =>
it('Navigation works in proper way', function() {
const calc = this.calculator;
eventToShowHint = jQuery.Event('keydown', { keyCode: KEY.ENTER } );
const eventToShowHint = jQuery.Event('keydown', { keyCode: KEY.ENTER } );
$('#calculator_hint').trigger(eventToShowHint);
spyOn(calc, 'hideHint')
spyOn(calc, 'prevHint')
spyOn(calc, 'nextHint')
spyOn($.fn, 'focus')
spyOn(calc, 'hideHint');
spyOn(calc, 'prevHint');
spyOn(calc, 'nextHint');
spyOn($.fn, 'focus');
cases =
left:
event:
keyCode: KEY.LEFT
const cases = {
left: {
event: {
keyCode: KEY.LEFT,
shiftKey: false
returnedValue: false
called:
},
returnedValue: false,
called: {
'prevHint': calc
},
isPropagationStopped: true
},
leftWithShift:
returnedValue: true
event:
keyCode: KEY.LEFT
leftWithShift: {
returnedValue: true,
event: {
keyCode: KEY.LEFT,
shiftKey: true
not_called:
},
not_called: {
'prevHint': calc
}
},
up:
event:
keyCode: KEY.UP
up: {
event: {
keyCode: KEY.UP,
shiftKey: false
returnedValue: false
called:
},
returnedValue: false,
called: {
'prevHint': calc
},
isPropagationStopped: true
},
upWithShift:
returnedValue: true
event:
keyCode: KEY.UP
upWithShift: {
returnedValue: true,
event: {
keyCode: KEY.UP,
shiftKey: true
not_called:
},
not_called: {
'prevHint': calc
}
},
right:
event:
keyCode: KEY.RIGHT
right: {
event: {
keyCode: KEY.RIGHT,
shiftKey: false
returnedValue: false
called:
},
returnedValue: false,
called: {
'nextHint': calc
},
isPropagationStopped: true
},
rightWithShift:
returnedValue: true
event:
keyCode: KEY.RIGHT
rightWithShift: {
returnedValue: true,
event: {
keyCode: KEY.RIGHT,
shiftKey: true
not_called:
},
not_called: {
'nextHint': calc
}
},
down:
event:
keyCode: KEY.DOWN
down: {
event: {
keyCode: KEY.DOWN,
shiftKey: false
returnedValue: false
called:
},
returnedValue: false,
called: {
'nextHint': calc
},
isPropagationStopped: true
},
downWithShift:
returnedValue: true
event:
keyCode: KEY.DOWN
downWithShift: {
returnedValue: true,
event: {
keyCode: KEY.DOWN,
shiftKey: true
not_called:
},
not_called: {
'nextHint': calc
}
},
esc:
returnedValue: false
event:
keyCode: KEY.ESC
esc: {
returnedValue: false,
event: {
keyCode: KEY.ESC,
shiftKey: false
called:
'hideHint': calc
},
called: {
'hideHint': calc,
'focus': $.fn
},
isPropagationStopped: true
},
alt:
returnedValue: true
event:
alt: {
returnedValue: true,
event: {
which: KEY.ALT
not_called:
'hideHint': calc
'nextHint': calc
},
not_called: {
'hideHint': calc,
'nextHint': calc,
'prevHint': calc
}
}
};
$.each(cases, (key, data) ->
calc.hideHint.calls.reset()
calc.prevHint.calls.reset()
calc.nextHint.calls.reset()
$.fn.focus.calls.reset()
return $.each(cases, function(key, data) {
calc.hideHint.calls.reset();
calc.prevHint.calls.reset();
calc.nextHint.calls.reset();
$.fn.focus.calls.reset();
e = jQuery.Event('keydown', data.event or {});
value = calc.handleKeyDownOnHint(e)
const e = jQuery.Event('keydown', data.event || {});
const value = calc.handleKeyDownOnHint(e);
if data.called
$.each(data.called, (method, obj) ->
expect(obj[method]).toHaveBeenCalled()
)
if (data.called) {
$.each(data.called, (method, obj) => expect(obj[method]).toHaveBeenCalled());
}
if data.not_called
$.each(data.not_called, (method, obj) ->
expect(obj[method]).not.toHaveBeenCalled()
)
if (data.not_called) {
$.each(data.not_called, (method, obj) => expect(obj[method]).not.toHaveBeenCalled());
}
if data.isPropagationStopped
expect(e.isPropagationStopped()).toBeTruthy()
else
expect(e.isPropagationStopped()).toBeFalsy()
if (data.isPropagationStopped) {
expect(e.isPropagationStopped()).toBeTruthy();
} else {
expect(e.isPropagationStopped()).toBeFalsy();
}
expect(value).toBe(data.returnedValue)
)
return expect(value).toBe(data.returnedValue);
});
})
);
describe 'calculate', ->
beforeEach ->
$('#calculator_input').val '1+2'
spyOn($, 'getWithPrefix').and.callFake (url, data, callback) ->
callback({ result: 3 })
@calculator.calculate()
return describe('calculate', function() {
beforeEach(function() {
$('#calculator_input').val('1+2');
spyOn($, 'getWithPrefix').and.callFake((url, data, callback) => callback({ result: 3 }));
return this.calculator.calculate();
});
it 'send data to /calculate', ->
expect($.getWithPrefix).toHaveBeenCalledWith '/calculate',
equation: '1+2'
, jasmine.any(Function)
it('send data to /calculate', () =>
expect($.getWithPrefix).toHaveBeenCalledWith('/calculate',
{equation: '1+2'}
, jasmine.any(Function))
);
it 'update the calculator output', ->
expect($('#calculator_output').val()).toEqual('3')
return it('update the calculator output', () => expect($('#calculator_output').val()).toEqual('3'));
});
});

View File

@@ -1,31 +1,40 @@
describe 'Courseware', ->
describe 'start', ->
it 'binds the Logger', ->
spyOn(Logger, 'bind')
Courseware.start()
expect(Logger.bind).toHaveBeenCalled()
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
describe('Courseware', function() {
describe('start', () =>
it('binds the Logger', function() {
spyOn(Logger, 'bind');
Courseware.start();
return expect(Logger.bind).toHaveBeenCalled();
})
);
describe 'render', ->
beforeEach ->
jasmine.stubRequests()
@courseware = new Courseware
spyOn(window, 'Histogram')
spyOn(window, 'Problem')
spyOn(window, 'Video')
spyOn(XBlock, 'initializeBlocks')
setFixtures """
<div class="course-content">
<div id="video_1" class="video" data-streams="1.0:abc1234"></div>
<div id="video_2" class="video" data-streams="1.0:def5678"></div>
<div id="problem_3" class="problems-wrapper" data-problem-id="3" data-url="/example/url/">
<div id="histogram_3" class="histogram" data-histogram="[[0, 1]]" style="height: 20px; display: block;">
</div>
</div>
"""
@courseware.render()
return describe('render', function() {
beforeEach(function() {
jasmine.stubRequests();
this.courseware = new Courseware;
spyOn(window, 'Histogram');
spyOn(window, 'Problem');
spyOn(window, 'Video');
spyOn(XBlock, 'initializeBlocks');
setFixtures(`\
<div class="course-content">
<div id="video_1" class="video" data-streams="1.0:abc1234"></div>
<div id="video_2" class="video" data-streams="1.0:def5678"></div>
<div id="problem_3" class="problems-wrapper" data-problem-id="3" data-url="/example/url/">
<div id="histogram_3" class="histogram" data-histogram="[[0, 1]]" style="height: 20px; display: block;">
</div>
</div>\
`
);
return this.courseware.render();
});
it 'ensure that the XModules have been loaded', ->
expect(XBlock.initializeBlocks).toHaveBeenCalled()
it('ensure that the XModules have been loaded', () => expect(XBlock.initializeBlocks).toHaveBeenCalled());
it 'detect the histrogram element and convert it', ->
expect(window.Histogram).toHaveBeenCalledWith('3', [[0, 1]])
return it('detect the histrogram element and convert it', () => expect(window.Histogram).toHaveBeenCalledWith('3', [[0, 1]]));
});
});

View File

@@ -1,25 +1,33 @@
describe 'FeedbackForm', ->
beforeEach ->
loadFixtures 'coffee/fixtures/feedback_form.html'
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
describe('FeedbackForm', function() {
beforeEach(() => loadFixtures('coffee/fixtures/feedback_form.html'));
describe 'constructor', ->
beforeEach ->
new FeedbackForm
spyOn($, 'postWithPrefix').and.callFake (url, data, callback, format) ->
callback()
return describe('constructor', function() {
beforeEach(function() {
new FeedbackForm;
return spyOn($, 'postWithPrefix').and.callFake((url, data, callback, format) => callback());
});
it 'post data to /send_feedback on click', ->
$('#feedback_subject').val 'Awesome!'
$('#feedback_message').val 'This site is really good.'
$('#feedback_button').click()
it('post data to /send_feedback on click', function() {
$('#feedback_subject').val('Awesome!');
$('#feedback_message').val('This site is really good.');
$('#feedback_button').click();
expect($.postWithPrefix).toHaveBeenCalledWith '/send_feedback', {
subject: 'Awesome!'
message: 'This site is really good.'
return expect($.postWithPrefix).toHaveBeenCalledWith('/send_feedback', {
subject: 'Awesome!',
message: 'This site is really good.',
url: window.location.href
}, jasmine.any(Function), 'json'
}, jasmine.any(Function), 'json');
});
it 'replace the form with a thank you message', ->
$('#feedback_button').click()
return it('replace the form with a thank you message', function() {
$('#feedback_button').click();
expect($('#feedback_div').html()).toEqual 'Feedback submitted. Thank you'
return expect($('#feedback_div').html()).toEqual('Feedback submitted. Thank you');
});
});
});

View File

@@ -1,72 +1,96 @@
jasmine.stubbedMetadata =
slowerSpeedYoutubeId:
id: 'slowerSpeedYoutubeId'
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
jasmine.stubbedMetadata = {
slowerSpeedYoutubeId: {
id: 'slowerSpeedYoutubeId',
duration: 300
normalSpeedYoutubeId:
id: 'normalSpeedYoutubeId'
},
normalSpeedYoutubeId: {
id: 'normalSpeedYoutubeId',
duration: 200
bogus:
},
bogus: {
duration: 100
}
};
jasmine.stubbedCaption =
start: [0, 10000, 20000, 30000]
jasmine.stubbedCaption = {
start: [0, 10000, 20000, 30000],
text: ['Caption at 0', 'Caption at 10000', 'Caption at 20000', 'Caption at 30000']
};
jasmine.stubRequests = ->
spyOn($, 'ajax').and.callFake (settings) ->
if match = settings.url.match /youtube\.com\/.+\/videos\/(.+)\?v=2&alt=jsonc/
settings.success data: jasmine.stubbedMetadata[match[1]]
else if match = settings.url.match /static\/subs\/(.+)\.srt\.sjson/
settings.success jasmine.stubbedCaption
else if settings.url.match /modx\/.+\/problem_get$/
settings.success html: readFixtures('problem_content.html')
else if settings.url == '/calculate' ||
jasmine.stubRequests = () =>
spyOn($, 'ajax').and.callFake(function(settings) {
let match;
if (match = settings.url.match(/youtube\.com\/.+\/videos\/(.+)\?v=2&alt=jsonc/)) {
return settings.success({data: jasmine.stubbedMetadata[match[1]]});
} else if (match = settings.url.match(/static\/subs\/(.+)\.srt\.sjson/)) {
return settings.success(jasmine.stubbedCaption);
} else if (settings.url.match(/modx\/.+\/problem_get$/)) {
return settings.success({html: readFixtures('problem_content.html')});
} else if ((settings.url === '/calculate') ||
settings.url.match(/modx\/.+\/goto_position$/) ||
settings.url.match(/event$/) ||
settings.url.match(/modx\/.+\/problem_(check|reset|show|save)$/)
# do nothing
else
throw "External request attempted for #{settings.url}, which is not defined."
settings.url.match(/modx\/.+\/problem_(check|reset|show|save)$/)) {
// do nothing
} else {
throw `External request attempted for ${settings.url}, which is not defined.`;
}
})
;
jasmine.stubYoutubePlayer = ->
YT.Player = -> jasmine.createSpyObj 'YT.Player', ['cueVideoById', 'getVideoEmbedCode',
jasmine.stubYoutubePlayer = () =>
YT.Player = () => jasmine.createSpyObj('YT.Player', ['cueVideoById', 'getVideoEmbedCode',
'getCurrentTime', 'getPlayerState', 'getVolume', 'setVolume', 'loadVideoById',
'playVideo', 'pauseVideo', 'seekTo']
'playVideo', 'pauseVideo', 'seekTo'])
;
jasmine.stubVideoPlayer = (context, enableParts, createPlayer=true) ->
enableParts = [enableParts] unless $.isArray(enableParts)
jasmine.stubVideoPlayer = function(context, enableParts, createPlayer) {
let currentPartName;
if (createPlayer == null) { createPlayer = true; }
if (!$.isArray(enableParts)) { enableParts = [enableParts]; }
suite = context.suite
currentPartName = suite.description while suite = suite.parentSuite
enableParts.push currentPartName
let { suite } = context;
while ((suite = suite.parentSuite)) { currentPartName = suite.description; }
enableParts.push(currentPartName);
for part in ['VideoCaption', 'VideoSpeedControl', 'VideoVolumeControl', 'VideoProgressSlider']
unless $.inArray(part, enableParts) >= 0
spyOn window, part
for (let part of ['VideoCaption', 'VideoSpeedControl', 'VideoVolumeControl', 'VideoProgressSlider']) {
if (!($.inArray(part, enableParts) >= 0)) {
spyOn(window, part);
}
}
loadFixtures 'video.html'
jasmine.stubRequests()
YT.Player = undefined
context.video = new Video 'example', '.75:slowerSpeedYoutubeId,1.0:normalSpeedYoutubeId'
jasmine.stubYoutubePlayer()
if createPlayer
return new VideoPlayer(video: context.video)
loadFixtures('video.html');
jasmine.stubRequests();
YT.Player = undefined;
context.video = new Video('example', '.75:slowerSpeedYoutubeId,1.0:normalSpeedYoutubeId');
jasmine.stubYoutubePlayer();
if (createPlayer) {
return new VideoPlayer({video: context.video});
}
};
# Stub Youtube API
window.YT =
PlayerState:
UNSTARTED: -1
ENDED: 0
PLAYING: 1
PAUSED: 2
BUFFERING: 3
// Stub Youtube API
window.YT = {
PlayerState: {
UNSTARTED: -1,
ENDED: 0,
PLAYING: 1,
PAUSED: 2,
BUFFERING: 3,
CUED: 5
}
};
# Stub jQuery.cookie
$.cookie = jasmine.createSpy('jQuery.cookie').and.returnValue '1.0'
// Stub jQuery.cookie
$.cookie = jasmine.createSpy('jQuery.cookie').and.returnValue('1.0');
# Stub jQuery.qtip
$.fn.qtip = jasmine.createSpy 'jQuery.qtip'
// Stub jQuery.qtip
$.fn.qtip = jasmine.createSpy('jQuery.qtip');
# Stub jQuery.scrollTo
$.fn.scrollTo = jasmine.createSpy 'jQuery.scrollTo'
// Stub jQuery.scrollTo
$.fn.scrollTo = jasmine.createSpy('jQuery.scrollTo');

View File

@@ -1,54 +1,72 @@
describe 'Histogram', ->
beforeEach ->
spyOn $, 'plot'
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
describe('Histogram', function() {
beforeEach(() => spyOn($, 'plot'));
describe 'constructor', ->
it 'instantiate the data arrays', ->
histogram = new Histogram 1, []
expect(histogram.xTicks).toEqual []
expect(histogram.yTicks).toEqual []
expect(histogram.data).toEqual []
describe('constructor', () =>
it('instantiate the data arrays', function() {
const histogram = new Histogram(1, []);
expect(histogram.xTicks).toEqual([]);
expect(histogram.yTicks).toEqual([]);
return expect(histogram.data).toEqual([]);
})
);
describe 'calculate', ->
beforeEach ->
@histogram = new Histogram(1, [[null, 1], [1, 1], [2, 2], [3, 3]])
describe('calculate', function() {
beforeEach(function() {
return this.histogram = new Histogram(1, [[null, 1], [1, 1], [2, 2], [3, 3]]);
});
it 'store the correct value for data', ->
expect(@histogram.data).toEqual [[1, Math.log(2)], [2, Math.log(3)], [3, Math.log(4)]]
it('store the correct value for data', function() {
return expect(this.histogram.data).toEqual([[1, Math.log(2)], [2, Math.log(3)], [3, Math.log(4)]]);
});
it 'store the correct value for x ticks', ->
expect(@histogram.xTicks).toEqual [[1, '1'], [2, '2'], [3, '3']]
it('store the correct value for x ticks', function() {
return expect(this.histogram.xTicks).toEqual([[1, '1'], [2, '2'], [3, '3']]);
});
it 'store the correct value for y ticks', ->
expect(@histogram.yTicks).toEqual
return it('store the correct value for y ticks', function() {
return expect(this.histogram.yTicks).toEqual;
});
});
describe 'render', ->
it 'call flot with correct option', ->
new Histogram(1, [[1, 1], [2, 2], [3, 3]])
return describe('render', () =>
it('call flot with correct option', function() {
new Histogram(1, [[1, 1], [2, 2], [3, 3]]);
firstArg = $.plot.calls.mostRecent().args[0]
secondArg = $.plot.calls.mostRecent().args[1]
thirdArg = $.plot.calls.mostRecent().args[2]
const firstArg = $.plot.calls.mostRecent().args[0];
const secondArg = $.plot.calls.mostRecent().args[1];
const thirdArg = $.plot.calls.mostRecent().args[2];
expect(firstArg.selector).toEqual($("#histogram_1").selector)
expect(secondArg).toEqual([
data: [[1, Math.log(2)], [2, Math.log(3)], [3, Math.log(4)]]
bars:
show: true
align: 'center'
lineWidth: 0
expect(firstArg.selector).toEqual($("#histogram_1").selector);
expect(secondArg).toEqual([{
data: [[1, Math.log(2)], [2, Math.log(3)], [3, Math.log(4)]],
bars: {
show: true,
align: 'center',
lineWidth: 0,
fill: 1.0
},
color: "#b72121"
])
expect(thirdArg).toEqual(
xaxis:
min: -1
max: 4
ticks: [[1, '1'], [2, '2'], [3, '3']]
}
]);
return expect(thirdArg).toEqual({
xaxis: {
min: -1,
max: 4,
ticks: [[1, '1'], [2, '2'], [3, '3']],
tickLength: 0
yaxis:
min: 0.0
max: Math.log(4) * 1.1
ticks: [[Math.log(2), '1'], [Math.log(3), '2'], [Math.log(4), '3']]
},
yaxis: {
min: 0.0,
max: Math.log(4) * 1.1,
ticks: [[Math.log(2), '1'], [Math.log(3), '2'], [Math.log(4), '3']],
labelWidth: 50
)
}
});
})
);
});

View File

@@ -1,44 +1,61 @@
describe 'Tab', ->
beforeEach ->
loadFixtures 'coffee/fixtures/tab.html'
@items = $.parseJSON readFixtures('coffee/fixtures/items.json')
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
describe('Tab', function() {
beforeEach(function() {
loadFixtures('coffee/fixtures/tab.html');
return this.items = $.parseJSON(readFixtures('coffee/fixtures/items.json'));
});
describe 'constructor', ->
beforeEach ->
spyOn($.fn, 'tabs')
@tab = new Tab 1, @items
describe('constructor', function() {
beforeEach(function() {
spyOn($.fn, 'tabs');
return this.tab = new Tab(1, this.items);
});
it 'set the element', ->
expect(@tab.el).toEqual $('#tab_1')
it('set the element', function() {
return expect(this.tab.el).toEqual($('#tab_1'));
});
it 'build the tabs', ->
links = $('.navigation li>a').map(-> $(this).attr('href')).get()
expect(links).toEqual ['#tab-1-0', '#tab-1-1', '#tab-1-2']
it('build the tabs', function() {
const links = $('.navigation li>a').map(function() { return $(this).attr('href'); }).get();
return expect(links).toEqual(['#tab-1-0', '#tab-1-1', '#tab-1-2']);
});
it 'build the container', ->
containers = $('section').map(-> $(this).attr('id')).get()
expect(containers).toEqual ['tab-1-0', 'tab-1-1', 'tab-1-2']
it('build the container', function() {
const containers = $('section').map(function() { return $(this).attr('id'); }).get();
return expect(containers).toEqual(['tab-1-0', 'tab-1-1', 'tab-1-2']);
});
it 'bind the tabs', ->
expect($.fn.tabs).toHaveBeenCalledWith show: @tab.onShow
return it('bind the tabs', function() {
return expect($.fn.tabs).toHaveBeenCalledWith({show: this.tab.onShow});
});
});
# As of jQuery 1.9, the onShow callback is deprecated
# http://jqueryui.com/upgrade-guide/1.9/#deprecated-show-event-renamed-to-activate
# The code below tests that onShow does what is expected,
# but note that onShow will NOT be called when the user
# clicks on the tab if we're using jQuery version >= 1.9
describe 'onShow', ->
beforeEach ->
@tab = new Tab 1, @items
@tab.onShow($('#tab-1-0'), {'index': 1})
// As of jQuery 1.9, the onShow callback is deprecated
// http://jqueryui.com/upgrade-guide/1.9/#deprecated-show-event-renamed-to-activate
// The code below tests that onShow does what is expected,
// but note that onShow will NOT be called when the user
// clicks on the tab if we're using jQuery version >= 1.9
return describe('onShow', function() {
beforeEach(function() {
this.tab = new Tab(1, this.items);
return this.tab.onShow($('#tab-1-0'), {'index': 1});
});
it 'replace content in the container', ->
@tab.onShow($('#tab-1-1'), {'index': 1})
expect($('#tab-1-0').html()).toEqual ''
expect($('#tab-1-1').html()).toEqual 'Video 2'
expect($('#tab-1-2').html()).toEqual ''
it('replace content in the container', function() {
this.tab.onShow($('#tab-1-1'), {'index': 1});
expect($('#tab-1-0').html()).toEqual('');
expect($('#tab-1-1').html()).toEqual('Video 2');
return expect($('#tab-1-2').html()).toEqual('');
});
it 'trigger contentChanged event on the element', ->
spyOnEvent @tab.el, 'contentChanged'
@tab.onShow($('#tab-1-1'), {'index': 1})
expect('contentChanged').toHaveBeenTriggeredOn @tab.el
return it('trigger contentChanged event on the element', function() {
spyOnEvent(this.tab.el, 'contentChanged');
this.tab.onShow($('#tab-1-1'), {'index': 1});
return expect('contentChanged').toHaveBeenTriggeredOn(this.tab.el);
});
});
});

View File

@@ -1,95 +1,114 @@
describe "RequireJS namespacing", ->
beforeEach ->
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
describe("RequireJS namespacing", function() {
beforeEach(() =>
# Jasmine does not provide a way to use the typeof operator. We need
# to create our own custom matchers so that a TypeError is not thrown.
jasmine.addMatchers
requirejsTobeUndefined: ->
{
compare: ->
{
pass: typeof requirejs is "undefined"
}
// Jasmine does not provide a way to use the typeof operator. We need
// to create our own custom matchers so that a TypeError is not thrown.
jasmine.addMatchers({
requirejsTobeUndefined() {
return {
compare() {
return {
pass: typeof requirejs === "undefined"
};
}
};
},
requireTobeUndefined: ->
{
compare: ->
{
pass: typeof require is "undefined"
}
requireTobeUndefined() {
return {
compare() {
return {
pass: typeof require === "undefined"
};
}
};
},
defineTobeUndefined: ->
{
compare: ->
{
pass: typeof define is "undefined"
}
defineTobeUndefined() {
return {
compare() {
return {
pass: typeof define === "undefined"
};
}
};
}}));
it "check that the RequireJS object is present in the global namespace", ->
expect(RequireJS).toEqual jasmine.any(Object)
expect(window.RequireJS).toEqual jasmine.any(Object)
it("check that the RequireJS object is present in the global namespace", function() {
expect(RequireJS).toEqual(jasmine.any(Object));
return expect(window.RequireJS).toEqual(jasmine.any(Object));
});
it "check that requirejs(), require(), and define() are not in the global namespace", ->
return it("check that requirejs(), require(), and define() are not in the global namespace", function() {
# The custom matchers that we defined in the beforeEach() function do
# not operate on an object. We pass a dummy empty object {} not to
# confuse Jasmine.
expect({}).requirejsTobeUndefined()
expect({}).requireTobeUndefined()
expect({}).defineTobeUndefined()
expect(window.requirejs).not.toBeDefined()
expect(window.require).not.toBeDefined()
expect(window.define).not.toBeDefined()
// The custom matchers that we defined in the beforeEach() function do
// not operate on an object. We pass a dummy empty object {} not to
// confuse Jasmine.
expect({}).requirejsTobeUndefined();
expect({}).requireTobeUndefined();
expect({}).defineTobeUndefined();
expect(window.requirejs).not.toBeDefined();
expect(window.require).not.toBeDefined();
return expect(window.define).not.toBeDefined();
});
});
describe "RequireJS module creation", ->
inDefineCallback = undefined
inRequireCallback = undefined
it "check that we can use RequireJS to define() and require() a module", (done) ->
d1 = $.Deferred()
d2 = $.Deferred()
# Because Require JS works asynchronously when defining and requiring
# modules, we need to use the special Jasmine functions runs(), and
# waitsFor() to set up this test.
func = () ->
describe("RequireJS module creation", function() {
let inDefineCallback = undefined;
let inRequireCallback = undefined;
return it("check that we can use RequireJS to define() and require() a module", function(done) {
const d1 = $.Deferred();
const d2 = $.Deferred();
// Because Require JS works asynchronously when defining and requiring
// modules, we need to use the special Jasmine functions runs(), and
// waitsFor() to set up this test.
const func = function() {
# Initialize the variable that we will test for. They will be set
# to true in the appropriate callback functions called by Require
# JS. If their values do not change, this will mean that something
# is not working as is intended.
inDefineCallback = false
inRequireCallback = false
// Initialize the variable that we will test for. They will be set
// to true in the appropriate callback functions called by Require
// JS. If their values do not change, this will mean that something
// is not working as is intended.
inDefineCallback = false;
inRequireCallback = false;
# Define our test module.
RequireJS.define "test_module", [], ->
inDefineCallback = true
// Define our test module.
RequireJS.define("test_module", [], function() {
inDefineCallback = true;
d1.resolve()
d1.resolve();
# This module returns an object. It can be accessed via the
# Require JS require() function.
module_status: "OK"
// This module returns an object. It can be accessed via the
// Require JS require() function.
return {module_status: "OK"};
});
# Require our defined test module.
RequireJS.require ["test_module"], (test_module) ->
inRequireCallback = true
// Require our defined test module.
return RequireJS.require(["test_module"], function(test_module) {
inRequireCallback = true;
# If our test module was defined properly, then we should
# be able to get the object it returned, and query some
# property.
expect(test_module.module_status).toBe "OK"
// If our test module was defined properly, then we should
// be able to get the object it returned, and query some
// property.
expect(test_module.module_status).toBe("OK");
d2.resolve()
return d2.resolve();
});
};
func()
# We will wait before checking if our module was defined and that we were able to require() the module.
$.when(d1, d2).done(->
# The final test behavior
expect(inDefineCallback).toBeTruthy()
expect(inRequireCallback).toBeTruthy()
).always(done)
func();
// We will wait before checking if our module was defined and that we were able to require() the module.
return $.when(d1, d2).done(function() {
// The final test behavior
expect(inDefineCallback).toBeTruthy();
return expect(inRequireCallback).toBeTruthy();
}).always(done);
});
});