mirror of
https://github.com/ansible/awx.git
synced 2026-05-19 14:57:39 -02:30
move login back to using finally handler and update tests
This commit is contained in:
@@ -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;
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -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 });
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user