Add fakeRequests array to models and collections during client tests

So you don't have to find the request through jasmine-ajax's functions.
This commit is contained in:
Scott Erickson 2016-04-14 15:26:29 -07:00
parent 12f58cda82
commit d5662fa2c9
3 changed files with 15 additions and 12 deletions

View file

@ -15,6 +15,9 @@ module.exports = class CocoCollection extends Backbone.Collection
@once 'sync', =>
@loaded = true
model.loaded = true for model in @models
if window.application?.testing
@fakeRequests = []
@on 'request', -> @fakeRequests.push jasmine.Ajax.requests.mostRecent()
getURL: ->
return if _.isString @url then @url else @url()

View file

@ -21,6 +21,9 @@ class CocoModel extends Backbone.Model
@on 'add', @onLoaded, @
@saveBackup = _.debounce(@saveBackup, 500)
@usesVersions = @schema()?.properties?.version?
if window.application?.testing
@fakeRequests = []
@on 'request', -> @fakeRequests.push jasmine.Ajax.requests.mostRecent()
created: -> new Date(parseInt(@id.substring(0, 8), 16) * 1000)

View file

@ -63,8 +63,7 @@ describe 'ConvertToTeacherAccountView (/teachers/update-account)', ->
describe 'when the user already has a TrialRequest and is a teacher', ->
beforeEach (done) ->
spyOn(me, 'isTeacher').and.returnValue(true)
request = jasmine.Ajax.requests.mostRecent()
request.respondWith({
_.last(view.trialRequests.fakeRequests).respondWith({
status: 200
responseText: JSON.stringify([{
_id: '1'
@ -86,7 +85,7 @@ describe 'ConvertToTeacherAccountView (/teachers/update-account)', ->
describe 'when the user has role "student"', ->
beforeEach ->
me.set('role', 'student')
jasmine.Ajax.requests.mostRecent().respondWith({ status: 200, responseText: JSON.stringify('[]') })
_.last(view.trialRequests.fakeRequests).respondWith({ status: 200, responseText: JSON.stringify('[]') })
view.render()
it 'shows a warning that they will convert to a teacher account', ->
@ -104,18 +103,16 @@ describe 'ConvertToTeacherAccountView (/teachers/update-account)', ->
form.submit()
it 'requires confirmation', ->
request = jasmine.Ajax.requests.mostRecent()
expect(request.url).not.toBe('/db/trial.request')
expect(request.method).not.toBe('POST')
expect(view.trialRequest.fakeRequests.length).toBe(0)
confirmModal = view.openModalView.calls.argsFor(0)[0]
confirmModal.trigger 'confirm'
request = jasmine.Ajax.requests.mostRecent()
request = _.last(view.trialRequest.fakeRequests)
expect(request.url).toBe('/db/trial.request')
expect(request.method).toBe('POST')
describe '"Log out" link', ->
beforeEach ->
jasmine.Ajax.requests.mostRecent().respondWith({ status: 200, responseText: JSON.stringify('[]') })
_.last(view.trialRequests.fakeRequests).respondWith({ status: 200, responseText: JSON.stringify('[]') })
it 'logs out the user and redirects them to /teachers/signup', ->
spyOn(me, 'logout')
@ -124,13 +121,13 @@ describe 'ConvertToTeacherAccountView (/teachers/update-account)', ->
describe 'submitting the form', ->
beforeEach ->
jasmine.Ajax.requests.mostRecent().respondWith({ status: 200, responseText: JSON.stringify('[]') })
_.last(view.trialRequests.fakeRequests).respondWith({ status: 200, responseText: JSON.stringify('[]') })
form = view.$('form')
forms.objectToForm(form, successForm, {overwriteExisting: true})
form.submit()
it 'creates a new TrialRequest with the information', ->
request = _.last(jasmine.Ajax.requests.filter((r) -> _.string.startsWith(r.url, '/db/trial.request')))
request = _.last(view.trialRequest.fakeRequests)
expect(request).toBeTruthy()
expect(request.method).toBe('POST')
attrs = JSON.parse(request.params)
@ -139,7 +136,7 @@ describe 'ConvertToTeacherAccountView (/teachers/update-account)', ->
expect(attrs.properties?.email).toBe('some@email.com')
it 'redirects to /teachers/classes', ->
request = jasmine.Ajax.requests.mostRecent()
request = _.last(view.trialRequest.fakeRequests)
request.respondWith({
status: 201
responseText: JSON.stringify(_.extend({_id:'fraghlarghl'}, JSON.parse(request.params)))
@ -149,7 +146,7 @@ describe 'ConvertToTeacherAccountView (/teachers/update-account)', ->
expect(args[0]).toBe('/teachers/classes')
it 'sets a teacher role', ->
request = jasmine.Ajax.requests.mostRecent()
request = _.last(view.trialRequest.fakeRequests)
request.respondWith({
status: 201
responseText: JSON.stringify(_.extend({_id:'fraghlarghl'}, JSON.parse(request.params)))