This repository has been archived by the owner on Jan 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#924] Updates recovery code on backup account submit
with @anikarni
- Loading branch information
1 parent
d1459b6
commit 9238896
Showing
5 changed files
with
86 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 59 additions & 29 deletions
88
web-ui/src/backup_account/backup_email/backup_email.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,120 @@ | ||
import { shallow } from 'enzyme'; | ||
import expect from 'expect'; | ||
import React from 'react'; | ||
import fetchMock from 'fetch-mock'; | ||
import { BackupEmail } from 'src/backup_account/backup_email/backup_email'; | ||
import browser from 'helpers/browser'; | ||
|
||
describe('BackupEmail', () => { | ||
let page; | ||
let mockTranslations; | ||
let backupEmail; | ||
let mockOnSubmit; | ||
|
||
beforeEach(() => { | ||
mockTranslations = key => key; | ||
page = shallow(<BackupEmail t={mockTranslations} />); | ||
mockOnSubmit = expect.createSpy(); | ||
const mockTranslations = key => key; | ||
backupEmail = shallow(<BackupEmail t={mockTranslations} onSubmit={mockOnSubmit} />); | ||
}); | ||
|
||
it('renders backup email title', () => { | ||
expect(page.find('h1').text()).toEqual('backup-account.backup-email.title'); | ||
expect(backupEmail.find('h1').text()).toEqual('backup-account.backup-email.title'); | ||
}); | ||
|
||
it('renders backup account email input field', () => { | ||
expect(page.find('InputField').props().name).toEqual('email'); | ||
expect(backupEmail.find('InputField').props().name).toEqual('email'); | ||
}); | ||
|
||
it('renders backup account submit button', () => { | ||
expect(page.find('SubmitButton').props().buttonText).toEqual('backup-account.backup-email.button'); | ||
}); | ||
|
||
it('form submit should call parameter custom submit', () => { | ||
const mockOnSubmit = expect.createSpy(); | ||
const event = { preventDefault() {} }; | ||
page = shallow(<BackupEmail t={mockTranslations} onSubmit={mockOnSubmit} />); | ||
|
||
page.instance().submitHandler(event); | ||
expect(mockOnSubmit).toHaveBeenCalled(); | ||
expect(backupEmail.find('SubmitButton').props().buttonText).toEqual('backup-account.backup-email.button'); | ||
}); | ||
|
||
describe('Email validation', () => { | ||
let pageInstance; | ||
let backupEmailInstance; | ||
|
||
beforeEach(() => { | ||
pageInstance = page.instance(); | ||
backupEmailInstance = backupEmail.instance(); | ||
}); | ||
|
||
it('verify initial state', () => { | ||
expect(pageInstance.state.error).toEqual(''); | ||
expect(page.find('SubmitButton').props().disabled).toEqual(true); | ||
expect(backupEmailInstance.state.error).toEqual(''); | ||
expect(backupEmail.find('SubmitButton').props().disabled).toBe(true); | ||
}); | ||
|
||
context('with invalid email', () => { | ||
beforeEach(() => { | ||
pageInstance.validateEmail({ target: { value: 'test' } }); | ||
backupEmailInstance.validateEmail({ target: { value: 'test' } }); | ||
}); | ||
|
||
it('sets error in state', () => { | ||
expect(pageInstance.state.error).toEqual('backup-account.backup-email.error.invalid-email'); | ||
expect(backupEmailInstance.state.error).toEqual('backup-account.backup-email.error.invalid-email'); | ||
}); | ||
|
||
it('disables submit button', () => { | ||
expect(page.find('SubmitButton').props().disabled).toEqual(true); | ||
expect(backupEmail.find('SubmitButton').props().disabled).toBe(true); | ||
}); | ||
}); | ||
|
||
context('with valid email', () => { | ||
beforeEach(() => { | ||
pageInstance.validateEmail({ target: { value: 'test@test.com' } }); | ||
backupEmailInstance.validateEmail({ target: { value: 'test@test.com' } }); | ||
}); | ||
|
||
it('does not set error in state', () => { | ||
expect(pageInstance.state.error).toEqual(''); | ||
expect(backupEmailInstance.state.error).toEqual(''); | ||
}); | ||
|
||
it('submit button is enabled', () => { | ||
expect(page.find('SubmitButton').props().disabled).toEqual(false); | ||
expect(backupEmail.find('SubmitButton').props().disabled).toBe(false); | ||
}); | ||
}); | ||
|
||
context('with empty email', () => { | ||
beforeEach(() => { | ||
pageInstance.validateEmail({ target: { value: '' } }); | ||
backupEmailInstance.validateEmail({ target: { value: '' } }); | ||
}); | ||
|
||
it('not set error in state', () => { | ||
expect(pageInstance.state.error).toEqual(''); | ||
expect(backupEmailInstance.state.error).toEqual(''); | ||
}); | ||
|
||
it('disables submit button', () => { | ||
expect(page.find('SubmitButton').props().disabled).toEqual(true); | ||
expect(backupEmail.find('SubmitButton').props().disabled).toBe(true); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Submit', () => { | ||
let preventDefaultSpy; | ||
|
||
beforeEach(() => { | ||
preventDefaultSpy = expect.createSpy(); | ||
expect.spyOn(browser, 'getCookie').andReturn('abc123'); | ||
|
||
fetchMock.post('/backup-account', 204); | ||
backupEmail.find('form').simulate('submit', { preventDefault: preventDefaultSpy }); | ||
}); | ||
|
||
it('posts backup email', () => { | ||
expect(fetchMock.called('/backup-account')).toBe(true, 'Backup account POST was not called'); | ||
}); | ||
|
||
it('sends csrftoken as content', () => { | ||
expect(fetchMock.lastOptions('/backup-account').body).toContain('"csrftoken":["abc123"]'); | ||
}); | ||
|
||
it('sends content-type header', () => { | ||
expect(fetchMock.lastOptions('/backup-account').headers['Content-Type']).toEqual('application/json'); | ||
}); | ||
|
||
it('sends same origin headers', () => { | ||
expect(fetchMock.lastOptions('/backup-account').credentials).toEqual('same-origin'); | ||
}); | ||
|
||
it('prevents default call to refresh page', () => { | ||
expect(preventDefaultSpy).toHaveBeenCalled(); | ||
}); | ||
|
||
it('calls onSubmit from props', () => { | ||
expect(mockOnSubmit).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters