spec: add full-suite fetch mocking
Change-Id: I2fe307a24aab37380790bae4fa8012ba1f504506 Reviewed-on: https://gerrit.instructure.com/207918 Reviewed-by: Clay Diffrient <cdiffrient@instructure.com> Tested-by: Jenkins QA-Review: Jeremy Neander <jneander@instructure.com> Product-Review: Jeremy Neander <jneander@instructure.com>
This commit is contained in:
parent
3df50f1ce2
commit
0a4b8987f8
|
@ -93,6 +93,7 @@ QUnit.module('EditView', {
|
|||
setup() {
|
||||
fakeENV.setup()
|
||||
this.server = sinon.fakeServer.create({respondImmediately: true})
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
},
|
||||
teardown() {
|
||||
this.server.restore()
|
||||
|
@ -370,6 +371,7 @@ QUnit.module('EditView - ConditionalRelease', {
|
|||
}
|
||||
$(document).on('submit', () => false)
|
||||
this.server = sinon.fakeServer.create({respondImmediately: true})
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
},
|
||||
teardown() {
|
||||
this.server.restore()
|
||||
|
@ -565,6 +567,7 @@ QUnit.module('EditView: Assignment External Tools', {
|
|||
setup() {
|
||||
fakeENV.setup({})
|
||||
this.server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
},
|
||||
|
||||
teardown() {
|
||||
|
|
|
@ -124,6 +124,7 @@ QUnit.module('EditView', {
|
|||
// don't clean up properly, we make sure that these run in a clean tiny state each time
|
||||
tinymce.remove()
|
||||
this.server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
},
|
||||
teardown() {
|
||||
this.server.restore()
|
||||
|
@ -614,6 +615,7 @@ QUnit.module('EditView: handleGroupCategoryChange', {
|
|||
COURSE_ID: 1
|
||||
})
|
||||
this.server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
},
|
||||
teardown() {
|
||||
this.server.restore()
|
||||
|
@ -680,6 +682,7 @@ QUnit.module('#handleAnonymousGradingChange', (hooks) => {
|
|||
COURSE_ID: 1
|
||||
})
|
||||
server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
view = editView()
|
||||
})
|
||||
|
||||
|
@ -757,6 +760,7 @@ QUnit.module('#togglePeerReviewsAndGroupCategoryEnabled', (hooks) => {
|
|||
COURSE_ID: 1
|
||||
})
|
||||
server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
view = editView()
|
||||
})
|
||||
|
||||
|
@ -816,6 +820,7 @@ QUnit.module('EditView: group category inClosedGradingPeriod', {
|
|||
COURSE_ID: 1
|
||||
})
|
||||
this.server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
},
|
||||
teardown() {
|
||||
this.server.restore()
|
||||
|
@ -855,6 +860,7 @@ QUnit.module('EditView: enableCheckbox', {
|
|||
COURSE_ID: 1
|
||||
})
|
||||
this.server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
},
|
||||
|
||||
teardown() {
|
||||
|
@ -902,6 +908,7 @@ QUnit.module('EditView: setDefaultsIfNew', {
|
|||
COURSE_ID: 1
|
||||
})
|
||||
this.server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
},
|
||||
teardown() {
|
||||
this.server.restore()
|
||||
|
@ -971,6 +978,7 @@ QUnit.module('EditView: setDefaultsIfNew: no localStorage', {
|
|||
})
|
||||
sandbox.stub(userSettings, 'contextGet').returns(null)
|
||||
this.server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
},
|
||||
teardown() {
|
||||
this.server.restore()
|
||||
|
@ -1002,6 +1010,7 @@ QUnit.module('EditView: cacheAssignmentSettings', {
|
|||
COURSE_ID: 1
|
||||
})
|
||||
this.server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
},
|
||||
teardown() {
|
||||
this.server.restore()
|
||||
|
@ -1046,6 +1055,7 @@ QUnit.module('EditView: Conditional Release', {
|
|||
})
|
||||
$(document).on('submit', () => false)
|
||||
this.server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
},
|
||||
|
||||
teardown() {
|
||||
|
@ -1138,6 +1148,7 @@ QUnit.module('Editview: Intra-Group Peer Review toggle', {
|
|||
COURSE_ID: 1
|
||||
})
|
||||
this.server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
},
|
||||
teardown() {
|
||||
this.server.restore()
|
||||
|
@ -1192,6 +1203,7 @@ QUnit.module('EditView: Assignment Configuration Tools', {
|
|||
COURSE_ID: 1
|
||||
})
|
||||
this.server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
},
|
||||
|
||||
teardown() {
|
||||
|
@ -1256,8 +1268,11 @@ test('it is hidden if the plagiarism_detection_platform flag is disabled', funct
|
|||
|
||||
QUnit.module('EditView: Assignment External Tools', {
|
||||
setup() {
|
||||
fakeENV.setup({})
|
||||
fakeENV.setup({
|
||||
COURSE_ID: 1
|
||||
})
|
||||
this.server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
},
|
||||
|
||||
teardown() {
|
||||
|
@ -1288,6 +1303,7 @@ QUnit.module('EditView: Quizzes 2', {
|
|||
COURSE_ID: 1
|
||||
})
|
||||
this.server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
this.view = editView({
|
||||
submission_types: ['external_tool'],
|
||||
is_quiz_lti_assignment: true
|
||||
|
@ -1327,6 +1343,7 @@ QUnit.module('EditView: anonymous grading', (hooks) => {
|
|||
COURSE_ID: 1
|
||||
})
|
||||
server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
});
|
||||
|
||||
hooks.afterEach(() => {
|
||||
|
@ -1389,6 +1406,7 @@ QUnit.module('EditView: Anonymous Instructor Annotations', (hooks) => {
|
|||
hooks.beforeEach(() => {
|
||||
fixtures.innerHTML = '<span data-component="ModeratedGradingFormFieldGroup"></span>'
|
||||
server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
})
|
||||
|
||||
hooks.afterEach(() => {
|
||||
|
@ -1432,6 +1450,7 @@ QUnit.module('EditView: Anonymous Moderated Marking', (hooks) => {
|
|||
COURSE_ID: 1
|
||||
})
|
||||
server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
})
|
||||
|
||||
hooks.afterEach(() => {
|
||||
|
@ -1467,6 +1486,7 @@ QUnit.module('EditView#validateFinalGrader', (hooks) => {
|
|||
COURSE_ID: 1
|
||||
})
|
||||
server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
view = editView()
|
||||
})
|
||||
|
||||
|
@ -1512,6 +1532,7 @@ QUnit.module('EditView#validateGraderCount', (hooks) => {
|
|||
COURSE_ID: 1
|
||||
})
|
||||
server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
view = editView()
|
||||
})
|
||||
|
||||
|
@ -1570,6 +1591,7 @@ QUnit.module('EditView#renderModeratedGradingFormFieldGroup', (suiteHooks) => {
|
|||
COURSE_ID: 1
|
||||
})
|
||||
server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
view = editView()
|
||||
})
|
||||
|
||||
|
@ -1702,6 +1724,7 @@ QUnit.module('EditView#handleModeratedGradingChanged', (hooks) => {
|
|||
COURSE_ID: 1
|
||||
})
|
||||
server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
view = editView()
|
||||
})
|
||||
|
||||
|
@ -1771,6 +1794,7 @@ QUnit.module('EditView#handleGraderCommentsVisibleToGradersChanged', (hooks) =>
|
|||
COURSE_ID: 1
|
||||
})
|
||||
server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
view = editView()
|
||||
})
|
||||
|
||||
|
@ -1823,6 +1847,7 @@ QUnit.module('EditView#uncheckAndHideGraderAnonymousToGraders', (hooks) => {
|
|||
COURSE_ID: 1
|
||||
})
|
||||
server = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
view = editView()
|
||||
})
|
||||
|
||||
|
|
|
@ -21,7 +21,6 @@ import React from 'react'
|
|||
import {mount} from 'enzyme'
|
||||
import AssignmentExternalTools from 'jsx/assignments/AssignmentExternalTools'
|
||||
|
||||
|
||||
QUnit.module('AssignmentExternalTools', hooks => {
|
||||
let toolDefinitions;
|
||||
let wrapper;
|
||||
|
@ -70,6 +69,8 @@ QUnit.module('AssignmentExternalTools', hooks => {
|
|||
]
|
||||
sandbox.stub($, 'ajax').returns({status: 200, data: toolDefinitions});
|
||||
ENV.LTI_LAUNCH_FRAME_ALLOWANCES = ['midi', 'media']
|
||||
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/lti_apps/launch_definitions', 200)
|
||||
}
|
||||
|
||||
function teardown () {
|
||||
|
|
|
@ -52,6 +52,9 @@ QUnit.module('DashboardCard Reordering', {
|
|||
]
|
||||
|
||||
fakeServer = sinon.fakeServer.create()
|
||||
sandbox.fetch.mock('path:/api/v1/courses/1/activity_stream/summary', 200)
|
||||
sandbox.fetch.mock('path:/api/v1/courses/2/activity_stream/summary', 200)
|
||||
sandbox.fetch.mock('path:/api/v1/courses/3/activity_stream/summary', 200)
|
||||
},
|
||||
teardown() {
|
||||
fakeENV.teardown()
|
||||
|
|
|
@ -16,6 +16,7 @@
|
|||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import FetchSandbox from './sandboxes/FetchSandbox'
|
||||
import SinonSandbox from './sandboxes/SinonSandbox'
|
||||
|
||||
export default class SandboxFactory {
|
||||
|
@ -25,6 +26,7 @@ export default class SandboxFactory {
|
|||
this._options.global.sandbox = {}
|
||||
|
||||
this._sandboxes = {
|
||||
fetch: new FetchSandbox(options),
|
||||
sinon: new SinonSandbox(options)
|
||||
}
|
||||
|
||||
|
@ -42,14 +44,17 @@ export default class SandboxFactory {
|
|||
}
|
||||
|
||||
setup() {
|
||||
this._sandboxes.fetch.setup()
|
||||
this._sandboxes.sinon.setup()
|
||||
}
|
||||
|
||||
teardown() {
|
||||
this._sandboxes.sinon.teardown()
|
||||
this._sandboxes.fetch.teardown()
|
||||
}
|
||||
|
||||
verify() {
|
||||
this._sandboxes.fetch.verify()
|
||||
this._sandboxes.sinon.verify()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Copyright (C) 2019 - present Instructure, Inc.
|
||||
*
|
||||
* This file is part of Canvas.
|
||||
*
|
||||
* Canvas is free software: you can redistribute it and/or modify it under
|
||||
* the terms of the GNU Affero General Public License as published by the Free
|
||||
* Software Foundation, version 3 of the License.
|
||||
*
|
||||
* Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
||||
* A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
||||
* details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License along
|
||||
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import fetchMock from 'fetch-mock'
|
||||
|
||||
/*
|
||||
* You might be seeing something like this in your test:
|
||||
* ERROR LOG: 'Unexpected fetch with path "/some/url". …'
|
||||
*
|
||||
* This means that you have not mocked one or more `fetch()` calls occurring
|
||||
* during the lifecycle of a test. You need to mock this request like so:
|
||||
*
|
||||
* sandbox.fetch.mock('path:/some/url', 200)
|
||||
*
|
||||
* Other mocking forms are available. For more documentation on fetch-mock, the
|
||||
* library we are using for testing `fetch`, visit:
|
||||
* http://www.wheresrhys.co.uk/fetch-mock/
|
||||
*/
|
||||
|
||||
export default class FetchSandbox {
|
||||
constructor(options) {
|
||||
this._options = options
|
||||
|
||||
this._fetchMock = fetchMock.sandbox()
|
||||
this._options.global.sandbox.fetch = this._fetchMock
|
||||
}
|
||||
|
||||
setup() {
|
||||
const {global, qunit} = this._options
|
||||
const {_fetchMock} = this
|
||||
|
||||
global.fetch = function fetch(...args) {
|
||||
return _fetchMock.call(_fetchMock, ...args)
|
||||
}
|
||||
|
||||
// NEVER TOUCH THE NETWORK IN SPECS
|
||||
this._fetchMock.config.fallbackToNetwork = false
|
||||
|
||||
this._fetchMock.catch(requestPath => {
|
||||
const test = qunit.config.current
|
||||
test.pushFailure(
|
||||
`Unexpected fetch with path "${requestPath}". This probably means you made an unmocked \`fetch\` request in your tests. See the fetch-mock docs (http://www.wheresrhys.co.uk/fetch-mock/#api-mockingmock) for how to mock this request.`
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
teardown() {
|
||||
this._fetchMock.resetBehavior()
|
||||
}
|
||||
|
||||
verify() {}
|
||||
}
|
Loading…
Reference in New Issue