forked from umputun/remark42
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
120 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,52 @@ | ||
/** @jsx createElement */ | ||
import { createElement } from 'preact'; | ||
import { mount } from 'enzyme'; | ||
import { EmailLoginForm, Props, State } from './auth-panel__email-login-form'; | ||
import { mount, ReactWrapper } from 'enzyme'; | ||
import { EmailLoginFormConnected as EmailLoginForm, Props, State } from './auth-panel__email-login-form'; | ||
import { User } from '@app/common/types'; | ||
import { sleep } from '@app/utils/sleep'; | ||
import { validToken } from '@app/testUtils/mocks/jwt'; | ||
import { createIntl } from 'react-intl'; | ||
import { sendEmailVerificationRequest } from '@app/common/api'; | ||
import { IntlProvider } from 'react-intl'; | ||
import enMessages from '../../../locales/en.json'; | ||
|
||
jest.mock('@app/utils/jwt', () => ({ | ||
isJwtExpired: jest | ||
.fn() | ||
.mockImplementationOnce(() => true) | ||
.mockImplementationOnce(() => false) | ||
.mockImplementationOnce(() => true), | ||
})); | ||
|
||
const intl = createIntl({ | ||
locale: `en`, | ||
messages: enMessages, | ||
}); | ||
jest.mock('@app/common/api'); | ||
|
||
function simulateInput(input: ReactWrapper, value: string) { | ||
input.getDOMNode<HTMLTextAreaElement>().value = value; | ||
input.simulate('input'); | ||
} | ||
|
||
describe('EmailLoginForm', () => { | ||
const testUser = ({} as any) as User; | ||
const onSuccess = jest.fn(async () => {}); | ||
const onSignIn = jest.fn(async () => testUser); | ||
|
||
it('works', async () => { | ||
const sendEmailVerification = jest.fn(async () => {}); | ||
beforeEach(() => { | ||
(sendEmailVerificationRequest as any).mockReset(); | ||
}); | ||
|
||
it('works', async () => { | ||
(sendEmailVerificationRequest as any).mockResolvedValueOnce({}); | ||
const el = mount<Props, State>( | ||
<EmailLoginForm | ||
sendEmailVerification={sendEmailVerification} | ||
onSignIn={onSignIn} | ||
onSuccess={onSuccess} | ||
theme="light" | ||
intl={intl} | ||
/> | ||
<IntlProvider locale="en" messages={enMessages}> | ||
<EmailLoginForm onSignIn={onSignIn} onSuccess={onSuccess} theme="light" /> | ||
</IntlProvider> | ||
); | ||
|
||
await new Promise(resolve => | ||
el.setState({ usernameValue: 'someone', addressValue: '[email protected]' } as State, resolve) | ||
); | ||
|
||
simulateInput(el.find(`input[name="email"]`), '[email protected]'); | ||
simulateInput(el.find(`input[name="username"]`), 'someone'); | ||
el.find('form').simulate('submit'); | ||
await sleep(100); | ||
expect(sendEmailVerification).toBeCalledWith('someone', '[email protected]'); | ||
expect(el.state().verificationSent).toBe(true); | ||
|
||
await new Promise(resolve => el.setState({ tokenValue: 'abcd' } as State, resolve)); | ||
expect(sendEmailVerificationRequest).toBeCalledWith('someone', '[email protected]'); | ||
el.update(); | ||
simulateInput(el.find(`textarea[name="token"]`), 'abcd'); | ||
|
||
el.find('form').simulate('submit'); | ||
await sleep(100); | ||
|
@@ -56,47 +55,36 @@ describe('EmailLoginForm', () => { | |
}); | ||
|
||
it('should send form by pasting token', async () => { | ||
const sendEmailVerification = jest.fn(async () => {}); | ||
(sendEmailVerificationRequest as any).mockResolvedValueOnce({}); | ||
const onSignIn = jest.fn(async () => testUser); | ||
|
||
const wrapper = mount<Props, State>( | ||
<EmailLoginForm | ||
sendEmailVerification={sendEmailVerification} | ||
onSignIn={onSignIn} | ||
onSuccess={onSuccess} | ||
theme="light" | ||
intl={intl} | ||
/> | ||
); | ||
await new Promise(resolve => | ||
wrapper.setState({ usernameValue: 'someone', addressValue: '[email protected]' } as State, resolve) | ||
<IntlProvider locale="en" messages={enMessages}> | ||
<EmailLoginForm onSignIn={onSignIn} onSuccess={onSuccess} theme="light" /> | ||
</IntlProvider> | ||
); | ||
simulateInput(wrapper.find(`input[name="email"]`), '[email protected]'); | ||
simulateInput(wrapper.find(`input[name="username"]`), 'someone'); | ||
wrapper.find('form').simulate('submit'); | ||
await sleep(100); | ||
wrapper.update(); | ||
|
||
wrapper.find('textarea').getDOMNode<HTMLTextAreaElement>().value = validToken; | ||
wrapper.find('textarea').simulate('input'); | ||
|
||
simulateInput(wrapper.find(`textarea[name="token"]`), validToken); | ||
await sleep(100); | ||
wrapper.update(); | ||
expect(onSignIn).toBeCalledWith(validToken); | ||
}); | ||
|
||
it('should show error "Token is expired" on paste', async () => { | ||
const sendEmailVerification = jest.fn(async () => {}); | ||
(sendEmailVerificationRequest as any).mockResolvedValueOnce({}); | ||
const onSignIn = jest.fn(async () => testUser); | ||
|
||
const wrapper = mount<Props, State>( | ||
<EmailLoginForm | ||
sendEmailVerification={sendEmailVerification} | ||
onSignIn={onSignIn} | ||
onSuccess={onSuccess} | ||
theme="light" | ||
intl={intl} | ||
/> | ||
); | ||
await new Promise(resolve => | ||
wrapper.setState({ usernameValue: 'someone', addressValue: '[email protected]' } as State, resolve) | ||
<IntlProvider locale="en" messages={enMessages}> | ||
<EmailLoginForm onSignIn={onSignIn} onSuccess={onSuccess} theme="light" /> | ||
</IntlProvider> | ||
); | ||
simulateInput(wrapper.find(`input[name="email"]`), '[email protected]'); | ||
simulateInput(wrapper.find(`input[name="username"]`), 'someone'); | ||
wrapper.find('form').simulate('submit'); | ||
await sleep(100); | ||
wrapper.update(); | ||
|
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