move login back to using finally handler and update tests

This commit is contained in:
John Mitchell
2018-10-31 10:28:51 -04:00
parent 7c97989e84
commit 986d299961
2 changed files with 12 additions and 28 deletions

View File

@@ -79,17 +79,12 @@ describe('<LoginPage />', () => {
api.login = jest.fn().mockImplementation(() => { api.login = jest.fn().mockImplementation(() => {
const loginPromise = Promise.resolve({}); const loginPromise = Promise.resolve({});
// wrapped in timeout so this .then happens after state.loading is set to
// false in the actual .then handler
//
// would be improved by using .finally but that's not available for
// some reason
setTimeout(() => { setTimeout(() => {
loginPromise.then(() => { loginPromise.finally(() => {
expect(loginPage.state().loading).toBe(false); expect(loginPage.state().loading).toBe(false);
done(); done();
}); });
}, 50); }, 1);
return loginPromise; return loginPromise;
}); });
@@ -109,21 +104,16 @@ describe('<LoginPage />', () => {
const err = { const err = {
response: { status: 401, message: 'problem' }, response: { status: 401, message: 'problem' },
}; };
const loginPromise = Promise.reject(err); const loginPromise = Promise.reject(err);
// wrapped in timeout so this .then happens after state.loading is set to
// false in the actual .then handler
//
// would be improved by using .finally but that's not available for
// some reason
setTimeout(() => { setTimeout(() => {
loginPromise.catch(() => { loginPromise.catch(() => {
expect(loginPage.state().loading).toBe(false);
expect(loginPage.state().error).toBe(LOGIN_ERROR_MESSAGE); expect(loginPage.state().error).toBe(LOGIN_ERROR_MESSAGE);
}).finally(() => {
expect(loginPage.state().loading).toBe(false);
done(); done();
}); });
}, 50); }, 1);
return loginPromise; return loginPromise;
}); });
@@ -139,26 +129,21 @@ describe('<LoginPage />', () => {
expect(loginPage.state().loading).toBe(true); expect(loginPage.state().loading).toBe(true);
}); });
test('submit calls api.login handles 401 error', (done) => { test('submit calls api.login handles non-401 error', (done) => {
api.login = jest.fn().mockImplementation(() => { api.login = jest.fn().mockImplementation(() => {
const err = { const err = {
response: { status: 500, message: 'problem' }, response: { status: 500, message: 'problem' },
}; };
const loginPromise = Promise.reject(err); const loginPromise = Promise.reject(err);
// wrapped in timeout so this .then happens after state.loading is set to
// false in the actual .then handler
//
// would be improved by using .finally but that's not available for
// some reason
setTimeout(() => { setTimeout(() => {
loginPromise.catch(() => { loginPromise.catch(() => {
expect(loginPage.state().loading).toBe(false);
expect(loginPage.state().error).toBe(''); expect(loginPage.state().error).toBe('');
}).finally(() => {
expect(loginPage.state().loading).toBe(false);
done(); done();
}); });
}, 50); }, 1);
return loginPromise; return loginPromise;
}); });

View File

@@ -50,14 +50,13 @@ class LoginPage extends Component {
this.safeSetState({ loading: true }); this.safeSetState({ loading: true });
api.login(username, password) api.login(username, password)
.then(() => {
this.safeSetState({ loading: false });
})
.catch(error => { .catch(error => {
this.safeSetState({ loading: false });
if (error.response.status === 401) { if (error.response.status === 401) {
this.safeSetState({ error: LOGIN_ERROR_MESSAGE }); this.safeSetState({ error: LOGIN_ERROR_MESSAGE });
} }
})
.finally(() => {
this.safeSetState({ loading: false });
}); });
} }
} }