From d5662fa2c9f5add9f702aa70c76afa76a9c25ce8 Mon Sep 17 00:00:00 2001 From: Scott Erickson Date: Thu, 14 Apr 2016 15:26:29 -0700 Subject: [PATCH] Add fakeRequests array to models and collections during client tests So you don't have to find the request through jasmine-ajax's functions. --- app/collections/CocoCollection.coffee | 3 +++ app/models/CocoModel.coffee | 3 +++ .../ConvertToTeacherAccountView.spec.coffee | 21 ++++++++----------- 3 files changed, 15 insertions(+), 12 deletions(-) diff --git a/app/collections/CocoCollection.coffee b/app/collections/CocoCollection.coffee index 33b53f111..8d311f378 100644 --- a/app/collections/CocoCollection.coffee +++ b/app/collections/CocoCollection.coffee @@ -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() diff --git a/app/models/CocoModel.coffee b/app/models/CocoModel.coffee index 658dc52d9..009e32554 100644 --- a/app/models/CocoModel.coffee +++ b/app/models/CocoModel.coffee @@ -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) diff --git a/test/app/views/teachers/ConvertToTeacherAccountView.spec.coffee b/test/app/views/teachers/ConvertToTeacherAccountView.spec.coffee index 45797cd18..3a5647d9a 100644 --- a/test/app/views/teachers/ConvertToTeacherAccountView.spec.coffee +++ b/test/app/views/teachers/ConvertToTeacherAccountView.spec.coffee @@ -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)))