Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Fix Email Sender name validation and add tests related to Email Desti…
Browse files Browse the repository at this point in the history
…nation (#189)

* Remove dashes from valid sender name requirements

* Add additional tests for email changes
  • Loading branch information
qreshi authored Sep 29, 2020
1 parent 109b2d0 commit 329a967
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ const Sender = ({ sender, arrayHelpers, context, index, onDelete }) => {
label: 'Sender name',
helpText:
'A unique and descriptive name that is easy to search. ' +
'Valid characters are upper and lowercase a-z, 0-9, _ (underscore) and - (hyphen).',
'Valid characters are upper and lowercase a-z, 0-9, and _ (underscore).',
style: { padding: '10px 0px' },
isInvalid,
error: hasError,
}}
inputProps={{
placeholder: 'my-sender',
placeholder: 'my_sender',
onChange: (e, field, form) => {
field.onChange(e);
onSenderChange(index, sender, arrayHelpers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import _ from 'lodash';
export const validateSenderName = (senders) => (value) => {
if (!value) return 'Required';

if (!/^[A-Z0-9_-]+$/i.test(value)) return 'Invalid sender name';
if (!/^[A-Z0-9_]+$/i.test(value)) return 'Invalid sender name';

const matches = senders.filter((sender) => sender.name === value);
if (matches.length > 1) return 'Sender name is already being used';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { mount } from 'enzyme';
import ManageEmailGroups from './ManageEmailGroups';
import { httpClientMock } from '../../../../../../test/mocks';

const runAllPromises = () => new Promise(setImmediate);

const onClickCancel = jest.fn();
const onClickSave = jest.fn();

Expand Down Expand Up @@ -50,4 +52,72 @@ describe('ManageEmailGroups', () => {
);
expect(wrapper).toMatchSnapshot();
});

test('loadInitialValues', async () => {
const mockEmailGroup = {
id: 'id',
name: 'test_group',
emails: [{ email: '[email protected]' }],
};

// Mock return in getEmailGroups function
httpClientMock.get.mockResolvedValue({
data: { ok: true, emailGroups: [mockEmailGroup] },
});

const wrapper = mount(
<ManageEmailGroups
httpClient={httpClientMock}
isVisible={true}
onClickCancel={onClickCancel}
onClickSave={onClickSave}
/>
);

await runAllPromises();
expect(wrapper.instance().state.initialValues.emailGroups.length).toBe(1);
expect(wrapper.instance().state.initialValues.emailGroups[0].emails).toEqual([
{ label: '[email protected]' },
]);
});

test('getEmailGroups logs resp.data.err when ok:false', async () => {
const log = jest.spyOn(global.console, 'log');
// Mock return in getEmailGroups function
httpClientMock.get.mockResolvedValue({
data: { ok: false, err: 'test' },
});

const wrapper = mount(
<ManageEmailGroups
httpClient={httpClientMock}
isVisible={true}
onClickCancel={onClickCancel}
onClickSave={onClickSave}
/>
);

await runAllPromises();
expect(log).toHaveBeenCalled();
expect(log).toHaveBeenCalledWith('Unable to get email groups', 'test');
});

test('loads empty list of email groups when ok:false', async () => {
// Mock return in getEmailGroups function
httpClientMock.get.mockResolvedValue({
data: { ok: false, err: 'test' },
});

const wrapper = mount(
<ManageEmailGroups
httpClient={httpClientMock}
isVisible={true}
onClickCancel={onClickCancel}
onClickSave={onClickSave}
/>
);

await runAllPromises();
expect(wrapper.instance().state.initialValues.emailGroups).toEqual([]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import React from 'react';
import { mount } from 'enzyme';

import ManageSenders from './ManageSenders';
import * as helpers from './utils/helpers';
import { httpClientMock } from '../../../../../../test/mocks';

const runAllPromises = () => new Promise(setImmediate);

const onClickCancel = jest.fn();
const onClickSave = jest.fn();

Expand Down Expand Up @@ -51,4 +52,73 @@ describe('ManageSenders', () => {
);
expect(wrapper).toMatchSnapshot();
});

test('loadInitialValues', async () => {
const mockEmailAccount = {
id: 'id',
name: 'test_account',
email: '[email protected]',
host: 'smtp.test.com',
port: 25,
method: 'none',
};

// Mock return in getSenders function
httpClientMock.get.mockResolvedValue({
data: { ok: true, emailAccounts: [mockEmailAccount] },
});

const wrapper = mount(
<ManageSenders
httpClient={httpClientMock}
isVisible={true}
onClickCancel={onClickCancel}
onClickSave={onClickSave}
/>
);

await runAllPromises();
expect(wrapper.instance().state.initialValues.senders.length).toBe(1);
expect(wrapper.instance().state.initialValues.senders[0].name).toBe('test_account');
});

test('getSenders logs resp.data.err when ok:false', async () => {
const log = jest.spyOn(global.console, 'log');
// Mock return in getSenders function
httpClientMock.get.mockResolvedValue({
data: { ok: false, err: 'test' },
});

const wrapper = mount(
<ManageSenders
httpClient={httpClientMock}
isVisible={true}
onClickCancel={onClickCancel}
onClickSave={onClickSave}
/>
);

await runAllPromises();
expect(log).toHaveBeenCalled();
expect(log).toHaveBeenCalledWith('Unable to get email accounts', 'test');
});

test('loads empty list of senders when ok:false', async () => {
// Mock return in getSenders function
httpClientMock.get.mockResolvedValue({
data: { ok: false, err: 'test' },
});

const wrapper = mount(
<ManageSenders
httpClient={httpClientMock}
isVisible={true}
onClickCancel={onClickCancel}
onClickSave={onClickSave}
/>
);

await runAllPromises();
expect(wrapper.instance().state.initialValues.senders).toEqual([]);
});
});

0 comments on commit 329a967

Please sign in to comment.