Skip to content

Commit

Permalink
address a number of issues with the js test suite
Browse files Browse the repository at this point in the history
This patch handles a couple of things:

- `require` calls should not be used at the start of test files,
but they were. This switches them to `define`.
- There were a handful of tests that were not fulfilling initialized
promises, which led to the test suite hanging.
- There was a failure that popped up sometimes because `helpDialog`
was not cleaning up after itself. This adds a protection to
make sure that the instance is initialized before it is used.

Change-Id: Ie57f5ee6d45216f0d4071d34cc476201c3b7c9f6
Reviewed-on: https://gerrit.instructure.com/48447
Tested-by: Jenkins
Reviewed-by: Simon Williams <[email protected]>
QA-Review: Adam Stone <[email protected]>
Product-Review: Simon Williams <[email protected]>
  • Loading branch information
John Corrigan committed Feb 20, 2015
1 parent 3308ba8 commit ccb3db7
Show file tree
Hide file tree
Showing 33 changed files with 97 additions and 76 deletions.
1 change: 1 addition & 0 deletions app/coffeescripts/helpDialog.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ define [
}, {
step: =>
#reposition vertically to reflect current height
@initDialog() unless @dialogInited
@$dialog?.dialog('option', 'position', 'center')
duration: 100
complete: ->
Expand Down
1 change: 1 addition & 0 deletions app/coffeescripts/models/WikiPageRevision.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ define [
'compiled/str/splitAssetString'
'compiled/util/PandaPubPoller'
'compiled/jquery.rails_flash_notifications'
'jquery.disableWhileLoading'
], ($, _, Backbone, I18n, DefaultUrlMixin, splitAssetString, PandaPubPoller) ->

pageRevisionOptions = ['contextAssetString', 'page', 'pageUrl', 'latest', 'summary']
Expand Down
4 changes: 3 additions & 1 deletion app/coffeescripts/react_files/components/FilesUsage.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ define [

FilesUsage = React.createClass
displayName: 'FilesUsage'
url: ->
"/api/v1/#{@props.contextType}/#{@props.contextId}/files/quota"

propTypes:
contextType: customPropTypes.contextType.isRequired
contextId: customPropTypes.contextId.isRequired

update: ->
$.get "/api/v1/#{@props.contextType}/#{@props.contextId}/files/quota", (data) =>
$.get @url(), (data) =>
@setState(data)

componentDidMount: ->
Expand Down
7 changes: 5 additions & 2 deletions app/coffeescripts/views/wiki/WikiPageRevisionView.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ define [
json.edited_by = json.edited_by?.display_name
json

windowLocation: ->
return window.location;

restore: (ev) ->
ev?.preventDefault()
@model.restore().done (attrs) =>
if @pages_path
window.location.href = "#{@pages_path}/#{attrs.url}/revisions"
@windowLocation().href = "#{@pages_path}/#{attrs.url}/revisions"
else
window.location.reload()
@windowLocation().reload()
2 changes: 1 addition & 1 deletion app/jsx/external_apps/lib/ExternalAppsStore.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,4 @@ define([
};

return store;
});
});
6 changes: 3 additions & 3 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ module.exports = function(config) {

exclude: [],

// 'dots', 'progress', 'junit', 'growl', 'coverage'
reporters: ['progress'],
// 'dots', 'progress', 'junit', 'growl', 'coverage', 'spec'
reporters: ['progress'],

port: 9876,

colors: true,

// config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
logLevel: config.LOG_INFO,

autoWatch: true,

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require ['compiled/backbone-ext/Model'], (Model) ->
define ['compiled/backbone-ext/Model'], (Model) ->

module 'dateAttributes'

Expand Down
2 changes: 1 addition & 1 deletion spec/coffeescripts/behaviors/autocompleteSpec.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require [
define [
'jquery'
'underscore'
'compiled/behaviors/autocomplete'
Expand Down
2 changes: 1 addition & 1 deletion spec/coffeescripts/behaviors/elementTogglerSpec.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require [
define [
'jquery'
'compiled/behaviors/elementToggler'
], ($, elementToggler)->
Expand Down
2 changes: 1 addition & 1 deletion spec/coffeescripts/calendar/AgendaViewSpec.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require [
define [
'jquery'
'underscore'
'timezone'
Expand Down
2 changes: 1 addition & 1 deletion spec/coffeescripts/calendar/TimeBlockListSpec.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# requires jquery and date.js
require [
define [
'jquery'
'compiled/calendar/TimeBlockList'
], ($, TimeBlockList) ->
Expand Down
2 changes: 1 addition & 1 deletion spec/coffeescripts/calendar/TimeBlockRowSpec.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require [
define [
'jquery'
'compiled/calendar/TimeBlockList'
'compiled/calendar/TimeBlockRow'
Expand Down
2 changes: 2 additions & 0 deletions spec/coffeescripts/collections/CollectionSpec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ define [

module 'Backbone.Collection',
setup: ->
@xhr = sinon.useFakeXMLHttpRequest()
@ajaxSpy = sinon.spy $, 'ajax'
teardown: ->
@xhr.restore
$.ajax.restore()

test 'default options', ->
Expand Down
4 changes: 2 additions & 2 deletions spec/coffeescripts/flashNotificationSpec.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require [
define [
'jquery'
'compiled/jquery.rails_flash_notifications'
], ($) ->
Expand All @@ -23,4 +23,4 @@ require [

test 'screenreader message', ->
$.screenReaderFlashMessage('<script>evil()</script>')
ok $('#flash_screenreader_holder span').html().match(/&lt;script&gt;/)
ok $('#flash_screenreader_holder span').html().match(/&lt;script&gt;/)
2 changes: 1 addition & 1 deletion spec/coffeescripts/helpDialogSpec.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require [
define [
'jquery'
'compiled/helpDialog'
'helpers/fakeENV'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require ['support/jquery.mockjax'], ($) ->
define ['support/jquery.mockjax'], ($) ->
$.mockjax
url: /\/api\/v1\/courses\/\d+(\?.+)?$/
responseText: [
Expand Down
4 changes: 2 additions & 2 deletions spec/coffeescripts/helpers/ajax_mocks/help_links.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require [ 'support/jquery.mockjax'], ($) ->
define [ 'support/jquery.mockjax'], ($) ->
$.mockjax
url: '/help_links*'
responseText: [{
Expand All @@ -12,4 +12,4 @@ require [ 'support/jquery.mockjax'], ($) ->
url: "http://help.instructure.com/forums/337215-feature-requests"
text: "Request a Feature"
available_to: [ "user", "student", "teacher", "admin" ]
}]
}]
12 changes: 6 additions & 6 deletions spec/coffeescripts/jquery/mediaCommentSpec.coffee
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
require [
define [
'jquery'
'compiled/jquery/mediaComment'
], ($)->

module 'mediaComment',
setup: ->
setup: ->
@server = sinon.fakeServer.create()
window.INST.kalturaSettings = "settings set" # pretend kalturaSettings are set.
@$holder = $('<div id="media-holder">').appendTo('#fixtures')

teardown: ->
window.INST.kalturaSettings = null
teardown: ->
window.INST.kalturaSettings = null
@server.restore()
@$holder.remove()

Expand All @@ -28,7 +28,7 @@ require [
JSON.stringify(resp)
]

test "video player is displayed inline", ->
test "video player is displayed inline", ->
id = 10 #ID doesn't matter since we mock out the server
@$holder.mediaComment('show_inline', id)
mockServerResponse(@server, id)
Expand All @@ -45,7 +45,7 @@ require [
equal @$holder.find('video').length, 0, 'There should not be a video tag'


test "video player includes url sources provided by the server", ->
test "video player includes url sources provided by the server", ->
id = 10 #ID doesn't matter since we mock out the server
@$holder.mediaComment('show_inline', id)
mockServerResponse(@server, id)
Expand Down
2 changes: 1 addition & 1 deletion spec/coffeescripts/jquery/mediaCommentThumbnailSpec.coffee
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require [
define [
'jquery'
'underscore'
'compiled/jquery/mediaCommentThumbnail'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
define ['jsx/external_apps/lib/ExternalAppsStore'], (store) ->
define [
'jsx/external_apps/lib/ExternalAppsStore',
'helpers/fakeENV'
], (store, fakeENV) ->
module 'ExternalApps.ExternalAppsStore',
setup: ->
fakeENV.setup({
CONTEXT_BASE_URL: "/courses/1"
})
@server = sinon.fakeServer.create()
store.reset()
@tools = [
Expand Down
10 changes: 5 additions & 5 deletions spec/coffeescripts/models/FileMigrationSpec.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require [
define [
'Backbone'
], (Backbone) ->
module 'FileMigrationSpec',
setup: ->
teardown: ->
], (Backbone) ->
module 'FileMigrationSpec',
setup: ->
teardown: ->
4 changes: 2 additions & 2 deletions spec/coffeescripts/models/WikiPageRevisionSpec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ define [
'underscore'
'compiled/models/WikiPage'
'compiled/models/WikiPageRevision'
'jquery.ajaxJSON'
], ($, _, WikiPage, WikiPageRevision) ->

module 'WikiPageRevision::urls'
Expand Down Expand Up @@ -43,10 +44,9 @@ define [
test 'restore POSTs to the revision', ->
revision = new WikiPageRevision {revision_id: 42}, contextAssetString: 'course_73', pageUrl: 'page-url'
mock = @mock($)
mock.expects('ajaxJSON').atLeast(1).withArgs('/api/v1/courses/73/pages/page-url/revisions/42', 'POST').returns($.Deferred())
mock.expects('ajaxJSON').atLeast(1).withArgs('/api/v1/courses/73/pages/page-url/revisions/42', 'POST').returns($.Deferred().resolve())
revision.restore()


module 'WikiPageRevision::fetch'
test 'the summary flag is passed to the server', ->
@stub($, 'ajax').returns($.Deferred())
Expand Down
12 changes: 6 additions & 6 deletions spec/coffeescripts/object/unflattenSpec.coffee
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
require ['compiled/object/unflatten'], (unflatten) ->
define ['compiled/object/unflatten'], (unflatten) ->

module 'unflatten'

test 'simple object', ->
input =
input =
foo: 1
bar: 'baz'
deepEqual unflatten(input), input

test 'nested params', ->
input =
input =
'a[0]': 1
'a[1]' : 2
'a[2]' : 3
Expand All @@ -20,7 +20,7 @@ require ['compiled/object/unflatten'], (unflatten) ->
'c[g]' : false
'c[h]' : ''
'i': 7

expected =
a: [ 1, 2, 3 ]
b: 4
Expand All @@ -32,5 +32,5 @@ require ['compiled/object/unflatten'], (unflatten) ->
g: false
h: ''
i: 7

deepEqual unflatten(input), expected
34 changes: 17 additions & 17 deletions spec/coffeescripts/react_files/components/FilesUsageSpec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,49 @@ define [
'jquery'
'compiled/react_files/components/FilesUsage'
], (React, $, FilesUsage) ->
Simulate = React.addons.TestUtils.Simulate
TestUtils = React.addons.TestUtils
Simulate = TestUtils.Simulate

filesUpdateTest = (props, test) ->
@filesUsage = React.renderComponent(FilesUsage(props), $('<div>').appendTo('body')[0])
@server = sinon.fakeServer.create()
@filesUsage = TestUtils.renderIntoDocument(FilesUsage(props))

test()

React.unmountComponentAtNode(@filesUsage.getDOMNode().parentNode)

@server.restore()

module 'FilesUsage#update',
test "makes a get request with contextType and contextId", ->
sinon.stub($, 'get')
filesUpdateTest {contextType: 5, contextId: 4}, ->
filesUpdateTest {contextType: 'users', contextId: 4}, ->
@filesUsage.update()
ok $.get.calledWith("/api/v1/5/4/files/quota"), "makes get request with correct params"
ok $.get.calledWith(@filesUsage.url()), "makes get request with correct params"
$.get.restore()

test "sets state with ajax requests returned data", ->
data = {foo: 'bar'}
server = sinon.fakeServer.create()

server.respondWith "/api/v1/5/4/files/quota", [
200
'Content-Type': 'application/json'
JSON.stringify data
]
filesUpdateTest {contextType: 'users', contextId: 4}, ->
@server.respondWith @filesUsage.url(), [
200
'Content-Type': 'application/json'
JSON.stringify data
]

filesUpdateTest {contextType: 5, contextId: 4}, ->
sinon.spy(@filesUsage, 'setState')

@filesUsage.update()
server.respond()
@server.respond()

ok @filesUsage.setState.calledWith(data), 'called set state with returned get request data'

@filesUsage.setState.restore()

server.restore()

test 'update called after component mounted', ->

filesUpdateTest {contextType: 5, contextId: 4}, ->
sinon.stub(@filesUsage, 'update')
filesUpdateTest {contextType: 'users', contextId: 4}, ->
sinon.stub(@filesUsage, 'update').returns(true)
@filesUsage.componentDidMount()
ok @filesUsage.update.calledOnce, "called update after it mounted"
@filesUsage.update.restore()
Expand Down
4 changes: 2 additions & 2 deletions spec/coffeescripts/userSettingsSpec.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

require [
define [
'compiled/userSettings'
], (userSettings)->

Expand Down Expand Up @@ -46,4 +46,4 @@ require [
equal userSettings.contextGet('foo'), 2

ENV.context_asset_string = 'course_1'
equal userSettings.contextGet('foo'), 1
equal userSettings.contextGet('foo'), 1
8 changes: 4 additions & 4 deletions spec/coffeescripts/util/deparamSpec.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require ['compiled/util/deparam'], (deparam) ->
define ['compiled/util/deparam'], (deparam) ->

params_str = 'a[0]=4&a[1]=5&a[2]=6&b[x][]=7&b[y]=8&b[z][0]=9&b[z][1]=0&b[z][2]=true&b[z][3]=false&b[z][4]=undefined&b[z][5]=&c=1'
params_obj = { a:['4','5','6'], b:{x:['7'], y:'8', z:['9','0','true','false','undefined','']}, c:'1' }
params_obj_coerce = { a:[4,5,6], b:{x:[7], y:8, z:[9,0,true,false,undefined,'']}, c:1 }

module "deparam"

test "deparam", ->
deepEqual deparam(), {}, "deparam()"
deepEqual deparam(params_str), params_obj, "deparam( String )"
Expand Down
Loading

0 comments on commit ccb3db7

Please sign in to comment.