* Prevent new calendar save if duplicate id * add test for duplicate id detection * Use danger notif for duplicate id error
This commit is contained in:
parent
b7af462dc0
commit
5bb75a2b9e
|
@ -103,7 +103,24 @@ export class NewCalendar extends Component {
|
|||
}
|
||||
}
|
||||
|
||||
isDuplicateId = () => {
|
||||
const { calendars, formCalendarId } = this.state;
|
||||
|
||||
for (let i = 0; i < calendars.length; i++) {
|
||||
if (calendars[i].calendar_id === formCalendarId) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
onCreate = async () => {
|
||||
const { formCalendarId } = this.state;
|
||||
|
||||
if (this.isDuplicateId()) {
|
||||
toastNotifications.addDanger(`Cannot create calendar with id [${formCalendarId}] as it already exists.`);
|
||||
} else {
|
||||
const calendar = this.setUpCalendarForApi();
|
||||
this.setState({ saving: true });
|
||||
|
||||
|
@ -116,6 +133,7 @@ export class NewCalendar extends Component {
|
|||
toastNotifications.addDanger(`An error occurred creating calendar ${calendar.calendarId}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
onEdit = async () => {
|
||||
const calendar = this.setUpCalendarForApi();
|
||||
|
|
|
@ -50,6 +50,32 @@ import { shallow, mount } from 'enzyme';
|
|||
import React from 'react';
|
||||
import { NewCalendar } from './new_calendar';
|
||||
|
||||
const calendars = [
|
||||
{
|
||||
'calendar_id': 'farequote-calendar',
|
||||
'job_ids': ['farequote'],
|
||||
'description': 'test ',
|
||||
'events': [{
|
||||
'description': 'Downtime feb 9 2017 10:10 to 10:30',
|
||||
'start_time': 1486656600000,
|
||||
'end_time': 1486657800000,
|
||||
'calendar_id': 'farequote-calendar',
|
||||
'event_id': 'Ee-YgGcBxHgQWEhCO_xj'
|
||||
}]
|
||||
},
|
||||
{
|
||||
'calendar_id': 'this-is-a-new-calendar',
|
||||
'job_ids': ['test'],
|
||||
'description': 'new calendar',
|
||||
'events': [{
|
||||
'description': 'New event!',
|
||||
'start_time': 1544076000000,
|
||||
'end_time': 1544162400000,
|
||||
'calendar_id': 'this-is-a-new-calendar',
|
||||
'event_id': 'ehWKhGcBqHkXuWNrIrSV'
|
||||
}]
|
||||
}];
|
||||
|
||||
describe('NewCalendar', () => {
|
||||
|
||||
test('Renders new calendar form', () => {
|
||||
|
@ -84,4 +110,18 @@ describe('NewCalendar', () => {
|
|||
expect(wrapper.state('isNewEventModalVisible')).toBe(true);
|
||||
});
|
||||
|
||||
test('isDuplicateId returns true if form calendar id already exists in calendars', () => {
|
||||
const wrapper = mount(
|
||||
<NewCalendar />
|
||||
);
|
||||
|
||||
const instance = wrapper.instance();
|
||||
instance.setState({
|
||||
calendars,
|
||||
formCalendarId: calendars[0].calendar_id
|
||||
});
|
||||
wrapper.update();
|
||||
expect(instance.isDuplicateId()).toBe(true);
|
||||
});
|
||||
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue