Skip to content

Commit

Permalink
adding test for user email is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
awaisdar001 committed Nov 5, 2020
1 parent 4f1897e commit 33fc72d
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/components/common/PageLoading.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('<PageLoading />', () => {
expect(srElement).toHaveLength(1);
expect(srElement.text()).toEqual(message);
});
it('should not throw a warning with correct props', () => {
it('does not throw a warning with correct props', () => {
const expectedProps = { srMessage: 'Loading...' };
const propsError = checkProps(PageLoading, expectedProps);

Expand Down
18 changes: 17 additions & 1 deletion src/users/UserPage.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('User Page', () => {
});

describe('Checking PropTypes', () => {
it('Should not throw a warning', () => {
it('does not throw a warning', () => {
const expectedProps = { location };
const propsError = checkProps(UserPage, expectedProps);

Expand Down Expand Up @@ -86,6 +86,22 @@ describe('User Page', () => {
it('when user email is not found', async () => {
const validEmail = 'valid-non-existing@email.com';
location.search = `?email=${validEmail}`;
mockAuthApiToThrowError(404);
const expectedAlert = messages.USER_EMAIL_IDENTIFIER_NOT_FOUND_ERROR.replace(
'{identifier}',
validEmail,
);

const wrapper = mount(<UserPageWrapper location={location} />);
await waitForComponentToPaint(wrapper);

const alert = wrapper.find('.alert');
expect(alert).toHaveLength(1);
expect(alert.text()).toEqual(expectedAlert);
});
it('when user email is not found', async () => {
const validEmail = 'valid@email.com';
location.search = `?email=${validEmail}`;
mockAuthApiToThrowError(500);

const wrapper = mount(<UserPageWrapper location={location} />);
Expand Down
6 changes: 3 additions & 3 deletions src/users/UserSearch.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ describe('User Search Page', () => {
});

describe('Checking PropTypes', () => {
it('should not throw a warning', () => {
it('does not throw a warning', () => {
const propsError = checkProps(UserSearch, props);

expect(propsError).toBeUndefined();
});
it('should not throw error with undefined userIdentifier', () => {
it('does not throw error with undefined userIdentifier', () => {
delete props.userIdentifier;
const propsError = checkProps(UserSearch, props);

expect(propsError).toBeUndefined();
});
it('should throw error with undefined search handler', () => {
it('does throw error with undefined search handler', () => {
const propsError = checkProps(UserSearch, {});

expect(propsError).not.toBeUndefined();
Expand Down
5 changes: 3 additions & 2 deletions src/users/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ export async function getSsoRecords(username) {

export async function getUser(userIdentifier) {
let url = `${getConfig().LMS_BASE_URL}/api/user/v1/accounts`;
// I am avoiding an `else` case here because we have already validated the input
// to fall into one of these cases.
const identifierIsEmail = isEmail(userIdentifier);
const identifierIsUsername = isValidUsername(userIdentifier);

// todo: we have already validated the input to fall into one of these cases.
// The following `if` is not required.
if (!(identifierIsEmail || identifierIsUsername)) {
throw new Error('Invalid Argument!');
}
Expand Down
3 changes: 2 additions & 1 deletion src/utils/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ describe('Test Utils', () => {
const invalidEmails = [
'',
' ',
undefined,
'invalid email@mail.com',
'invalid-email',
'2020email.com',
'www.email.com',
];
const validUsername = ['staff', 'admin123'];
const invalidUsername = ['', ' ', 'invalid username', '%invalid'];
const invalidUsername = ['', ' ', undefined, 'invalid username', '%invalid'];
test.each(validEmails)('isEmail return true for %s', (email) => {
expect(isEmail(email)).toBe(true);
});
Expand Down

0 comments on commit 33fc72d

Please sign in to comment.