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', => @once 'sync', =>
@loaded = true @loaded = true
model.loaded = true for model in @models model.loaded = true for model in @models
if window.application?.testing
@fakeRequests = []
@on 'request', -> @fakeRequests.push jasmine.Ajax.requests.mostRecent()
getURL: -> getURL: ->
return if _.isString @url then @url else @url() return if _.isString @url then @url else @url()

View file

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