mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-09-09 08:49:49 -04:00
Restore all mocks after each test
This is easier than re-assigning the mock functions manually, as
we're reusing Jest's in-built behaviour. Because all the mocks
are restored, we need to move the ones we had in the beforeAll
block into the beforeEach block.
Note: "require('./support/teardown.js')" also resets all Jest
mocks, but "require" only runs once, so we can't use it in a
beforeEach block [1]. We could do a "jest.resetModules()" to fix
that, which seems worse on the whole. I think there's a broader
discussion here about whether we could / should have a global
reset of Jest mocks after each test - I quickly tried this and
it causes some existing tests to fail :-|.
[1]: https://stackoverflow.com/questions/48989643/how-to-reset-module-imported-between-tests
This commit is contained in:
@@ -2,40 +2,39 @@ beforeAll(() => {
|
|||||||
window.CBOR = require('../../node_modules/cbor-js/cbor.js')
|
window.CBOR = require('../../node_modules/cbor-js/cbor.js')
|
||||||
require('../../app/assets/javascripts/authenticateSecurityKey.js')
|
require('../../app/assets/javascripts/authenticateSecurityKey.js')
|
||||||
|
|
||||||
// disable console.error() so we don't see it in test output
|
// populate missing values to allow consistent jest.spyOn()
|
||||||
// you might need to comment this out to debug some failures
|
window.fetch = () => { }
|
||||||
jest.spyOn(console, 'error').mockImplementation(() => { })
|
window.navigator.credentials = { get: () => { } }
|
||||||
|
|
||||||
// ensure window.alert() is implemented to simplify errors
|
|
||||||
jest.spyOn(window, 'alert').mockImplementation(() => { })
|
|
||||||
})
|
})
|
||||||
|
|
||||||
afterAll(() => {
|
afterAll(() => {
|
||||||
require('./support/teardown.js')
|
require('./support/teardown.js')
|
||||||
|
|
||||||
|
// restore window attributes to their original undefined state
|
||||||
|
delete window.fetch
|
||||||
|
delete window.navigator.credentials
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Authenticate with security key', () => {
|
describe('Authenticate with security key', () => {
|
||||||
let button
|
let button
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
// disable console.error() so we don't see it in test output
|
||||||
|
// you might need to comment this out to debug some failures
|
||||||
|
jest.spyOn(console, 'error').mockImplementation(() => { })
|
||||||
|
|
||||||
|
// ensure window.alert() is implemented to simplify errors
|
||||||
|
jest.spyOn(window, 'alert').mockImplementation(() => { })
|
||||||
|
|
||||||
document.body.innerHTML = `
|
document.body.innerHTML = `
|
||||||
<button type="submit" data-module="authenticate-security-key" data-csrf-token="abc123"></button>
|
<button type="submit" data-module="authenticate-security-key" data-csrf-token="abc123"></button>`
|
||||||
`
|
|
||||||
button = document.querySelector('[data-module="authenticate-security-key"]')
|
button = document.querySelector('[data-module="authenticate-security-key"]')
|
||||||
|
|
||||||
// populate missing values to allow consistent jest.spyOn()
|
|
||||||
window.fetch = () => { }
|
|
||||||
window.navigator.credentials = { get: () => { } }
|
|
||||||
window.alert = () => { }
|
|
||||||
|
|
||||||
window.GOVUK.modules.start()
|
window.GOVUK.modules.start()
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
// restore window attributes to their original undefined state
|
jest.restoreAllMocks()
|
||||||
delete window.fetch
|
|
||||||
delete window.navigator.credentials
|
|
||||||
delete window.alert
|
|
||||||
})
|
})
|
||||||
|
|
||||||
test('authenticates a credential and redirects based on the admin app response', (done) => {
|
test('authenticates a credential and redirects based on the admin app response', (done) => {
|
||||||
|
|||||||
@@ -2,13 +2,6 @@ beforeAll(() => {
|
|||||||
window.CBOR = require('../../node_modules/cbor-js/cbor.js')
|
window.CBOR = require('../../node_modules/cbor-js/cbor.js')
|
||||||
require('../../app/assets/javascripts/registerSecurityKey.js')
|
require('../../app/assets/javascripts/registerSecurityKey.js')
|
||||||
|
|
||||||
// disable console.error() so we don't see it in test output
|
|
||||||
// you might need to comment this out to debug some failures
|
|
||||||
jest.spyOn(console, 'error').mockImplementation(() => {})
|
|
||||||
|
|
||||||
// ensure window.alert() is implemented to simplify errors
|
|
||||||
jest.spyOn(window, 'alert').mockImplementation(() => {})
|
|
||||||
|
|
||||||
// populate missing values to allow consistent jest.spyOn()
|
// populate missing values to allow consistent jest.spyOn()
|
||||||
window.fetch = () => {}
|
window.fetch = () => {}
|
||||||
window.navigator.credentials = { create: () => {} }
|
window.navigator.credentials = { create: () => {} }
|
||||||
@@ -26,6 +19,13 @@ describe('Register security key', () => {
|
|||||||
let button
|
let button
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
|
// disable console.error() so we don't see it in test output
|
||||||
|
// you might need to comment this out to debug some failures
|
||||||
|
jest.spyOn(console, 'error').mockImplementation(() => {})
|
||||||
|
|
||||||
|
// ensure window.alert() is implemented to simplify errors
|
||||||
|
jest.spyOn(window, 'alert').mockImplementation(() => {})
|
||||||
|
|
||||||
document.body.innerHTML = `
|
document.body.innerHTML = `
|
||||||
<a href="#" role="button" draggable="false" class="govuk-button govuk-button--secondary" data-module="register-security-key">
|
<a href="#" role="button" draggable="false" class="govuk-button govuk-button--secondary" data-module="register-security-key">
|
||||||
Register a key
|
Register a key
|
||||||
@@ -35,6 +35,10 @@ describe('Register security key', () => {
|
|||||||
window.GOVUK.modules.start()
|
window.GOVUK.modules.start()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
jest.restoreAllMocks()
|
||||||
|
})
|
||||||
|
|
||||||
test('creates a new credential and reloads', (done) => {
|
test('creates a new credential and reloads', (done) => {
|
||||||
|
|
||||||
jest.spyOn(window, 'fetch').mockImplementationOnce((_url) => {
|
jest.spyOn(window, 'fetch').mockImplementationOnce((_url) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user