1
0
mirror of https://github.com/ansible/awx.git synced 2024-10-31 23:51:09 +03:00

remove setTimeout hack for testing api.login Login.jsx handlers

This commit is contained in:
John Mitchell 2018-10-31 12:04:59 -04:00
parent 55586b9b2a
commit e53a6a91d6
No known key found for this signature in database
GPG Key ID: FE6A9B5BD4EB5C94
2 changed files with 37 additions and 63 deletions

View File

@ -1,6 +1,7 @@
import React from 'react';
import { MemoryRouter } from 'react-router-dom';
import { mount, shallow } from 'enzyme';
import { asyncFlush } from '../../jest.setup';
import LoginPage from '../../src/pages/Login';
import api from '../../src/api';
@ -75,88 +76,58 @@ describe('<LoginPage />', () => {
expect(loginPage.state().error).toBe('');
});
test('submit calls api.login successfully', (done) => {
api.login = jest.fn().mockImplementation(() => {
const loginPromise = Promise.resolve({});
setTimeout(() => {
loginPromise.finally(() => {
expect(loginPage.state().loading).toBe(false);
done();
});
}, 1);
return loginPromise;
});
test('api.login not called when loading', () => {
api.login = jest.fn().mockImplementation(() => Promise.resolve({}));
expect(loginPage.state().loading).toBe(false);
loginPage.setState({ username: 'unamee', password: 'pwordd', loading: true });
loginPage.setState({ loading: true });
submitButton.simulate('submit');
expect(api.login).toHaveBeenCalledTimes(0);
loginPage.setState({ loading: false });
submitButton.simulate('submit');
expect(api.login).toHaveBeenCalledTimes(1);
expect(api.login).toHaveBeenCalledWith('unamee', 'pwordd');
expect(loginPage.state().loading).toBe(true);
});
test('submit calls api.login handles 401 error', (done) => {
api.login = jest.fn().mockImplementation(() => {
const err = {
response: { status: 401, message: 'problem' },
};
const loginPromise = Promise.reject(err);
setTimeout(() => {
loginPromise.catch(() => {
expect(loginPage.state().error).toBe(LOGIN_ERROR_MESSAGE);
}).finally(() => {
expect(loginPage.state().loading).toBe(false);
done();
});
}, 1);
return loginPromise;
});
test('submit calls api.login successfully', async () => {
api.login = jest.fn().mockImplementation(() => Promise.resolve({}));
expect(loginPage.state().loading).toBe(false);
loginPage.setState({ username: 'unamee', password: 'pwordd', loading: true });
submitButton.simulate('submit');
expect(api.login).toHaveBeenCalledTimes(0);
loginPage.setState({ loading: false });
expect(loginPage.state().error).toBe('');
loginPage.setState({ username: 'unamee', password: 'pwordd' });
submitButton.simulate('submit');
expect(api.login).toHaveBeenCalledTimes(1);
expect(api.login).toHaveBeenCalledWith('unamee', 'pwordd');
expect(loginPage.state().loading).toBe(true);
await asyncFlush();
expect(loginPage.state().loading).toBe(false);
});
test('submit calls api.login handles non-401 error', (done) => {
test('submit calls api.login handles 401 error', async () => {
api.login = jest.fn().mockImplementation(() => {
const err = {
response: { status: 500, message: 'problem' },
};
const loginPromise = Promise.reject(err);
setTimeout(() => {
loginPromise.catch(() => {
expect(loginPage.state().error).toBe('');
}).finally(() => {
expect(loginPage.state().loading).toBe(false);
done();
});
}, 1);
return loginPromise;
const err = new Error('401 error');
err.response = { status: 401, message: 'problem' };
return Promise.reject(err);
});
expect(loginPage.state().loading).toBe(false);
loginPage.setState({ username: 'unamee', password: 'pwordd', loading: true });
submitButton.simulate('submit');
expect(api.login).toHaveBeenCalledTimes(0);
loginPage.setState({ loading: false });
expect(loginPage.state().error).toBe('');
loginPage.setState({ username: 'unamee', password: 'pwordd' });
submitButton.simulate('submit');
expect(api.login).toHaveBeenCalledTimes(1);
expect(api.login).toHaveBeenCalledWith('unamee', 'pwordd');
expect(loginPage.state().loading).toBe(true);
await asyncFlush();
expect(loginPage.state().error).toBe(LOGIN_ERROR_MESSAGE);
expect(loginPage.state().loading).toBe(false);
});
test('submit calls api.login handles non-401 error', async () => {
api.login = jest.fn().mockImplementation(() => {
const err = new Error('500 error');
err.response = { status: 500, message: 'problem' };
return Promise.reject(err);
});
expect(loginPage.state().loading).toBe(false);
loginPage.setState({ username: 'unamee', password: 'pwordd' });
submitButton.simulate('submit');
expect(api.login).toHaveBeenCalledTimes(1);
expect(api.login).toHaveBeenCalledWith('unamee', 'pwordd');
expect(loginPage.state().loading).toBe(true);
await asyncFlush();
expect(loginPage.state().error).toBe('');
expect(loginPage.state().loading).toBe(false);
});
test('render Redirect to / when already authenticated', () => {

View File

@ -1,5 +1,8 @@
require('@babel/polyfill');
// eslint-disable-next-line import/prefer-default-export
export const asyncFlush = () => new Promise((resolve) => setImmediate(resolve));
const enzyme = require('enzyme');
const Adapter = require('enzyme-adapter-react-16');