Skip to content

Commit

Permalink
feat: add runtime validation of event properties
Browse files Browse the repository at this point in the history
Closes #362
  • Loading branch information
Matt Lewis committed Oct 22, 2017
1 parent e887b89 commit 194fe59
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 4 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@
"dependencies": {
"angular-draggable-droppable": "^1.1.1",
"angular-resizable-element": "^1.2.4",
"calendar-utils": "0.0.57",
"calendar-utils": "0.0.58",
"date-fns": "^1.28.5",
"positioning": "^1.3.1"
}
Expand Down
9 changes: 9 additions & 0 deletions src/components/common/util.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {
CalendarEvent,
validateEvents as validateEventsWithoutLog
} from 'calendar-utils';

export const validateEvents = (events: CalendarEvent[]) => {
const warn = (...args) => console.warn('angular-calendar', ...args);
return validateEventsWithoutLog(events, warn);
};
5 changes: 5 additions & 0 deletions src/components/day/calendarDayView.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { CalendarDragHelper } from '../../providers/calendarDragHelper.provider'
import { CalendarResizeHelper } from '../../providers/calendarResizeHelper.provider';
import { CalendarEventTimesChangedEvent } from '../../interfaces/calendarEventTimesChangedEvent.interface';
import { CalendarUtils } from '../../providers/calendarUtils.provider';
import { validateEvents } from '../common/util';

/**
* @hidden
Expand Down Expand Up @@ -327,6 +328,10 @@ export class CalendarDayViewComponent implements OnChanges, OnInit, OnDestroy {
this.refreshHourGrid();
}

if (changes.events) {
validateEvents(this.events);
}

if (
changes.viewDate ||
changes.events ||
Expand Down
5 changes: 5 additions & 0 deletions src/components/month/calendarMonthView.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import differenceInSeconds from 'date-fns/difference_in_seconds';
import addSeconds from 'date-fns/add_seconds';
import { CalendarEventTimesChangedEvent } from '../../interfaces/calendarEventTimesChangedEvent.interface';
import { CalendarUtils } from '../../providers/calendarUtils.provider';
import { validateEvents } from '../common/util';

/**
* Shows all events on a given month. Example usage:
Expand Down Expand Up @@ -254,6 +255,10 @@ export class CalendarMonthViewComponent
this.refreshHeader();
}

if (changes.events) {
validateEvents(this.events);
}

if (
changes.viewDate ||
changes.events ||
Expand Down
5 changes: 5 additions & 0 deletions src/components/week/calendarWeekView.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { CalendarDragHelper } from '../../providers/calendarDragHelper.provider'
import { CalendarResizeHelper } from '../../providers/calendarResizeHelper.provider';
import { CalendarEventTimesChangedEvent } from '../../interfaces/calendarEventTimesChangedEvent.interface';
import { CalendarUtils } from '../../providers/calendarUtils.provider';
import { validateEvents } from '../common/util';

export interface WeekViewEventResize {
originalOffset: number;
Expand Down Expand Up @@ -261,6 +262,10 @@ export class CalendarWeekViewComponent implements OnChanges, OnInit, OnDestroy {
this.refreshHeader();
}

if (changes.events) {
validateEvents(this.events);
}

if (changes.events || changes.viewDate || changes.excludeDays) {
this.refreshBody();
}
Expand Down
14 changes: 14 additions & 0 deletions test/calendarDayView.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -981,4 +981,18 @@ describe('CalendarDayViewComponent component', () => {
.toDate()
});
});

it('should log on invalid events', () => {
const stub = sinon.stub(console, 'warn');
const fixture: ComponentFixture<
CalendarDayViewComponent
> = TestBed.createComponent(CalendarDayViewComponent);
fixture.componentInstance.events = [
{ start: '2017-01-01', title: '', color: { primary: '', secondary: '' } }
] as any;
fixture.componentInstance.ngOnChanges({ events: {} });
fixture.detectChanges();
stub.restore();
expect(stub).to.have.been.calledOnce; // tslint:disable-line
});
});
14 changes: 14 additions & 0 deletions test/calendarMonthView.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -741,4 +741,18 @@ describe('calendarMonthView component', () => {
subscription.unsubscribe();
fixture.destroy();
});

it('should log on invalid events', () => {
const stub = sinon.stub(console, 'warn');
const fixture: ComponentFixture<
CalendarMonthViewComponent
> = TestBed.createComponent(CalendarMonthViewComponent);
fixture.componentInstance.events = [
{ start: '2017-01-01', title: '', color: { primary: '', secondary: '' } }
] as any;
fixture.componentInstance.ngOnChanges({ events: {} });
fixture.detectChanges();
stub.restore();
expect(stub).to.have.been.calledOnce; // tslint:disable-line
});
});
14 changes: 14 additions & 0 deletions test/calendarWeekView.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -823,4 +823,18 @@ describe('calendarWeekView component', () => {
).to.equal(true);
fixture.destroy();
});

it('should log on invalid events', () => {
const stub = sinon.stub(console, 'warn');
const fixture: ComponentFixture<
CalendarWeekViewComponent
> = TestBed.createComponent(CalendarWeekViewComponent);
fixture.componentInstance.events = [
{ start: '2017-01-01', title: '', color: { primary: '', secondary: '' } }
] as any;
fixture.componentInstance.ngOnChanges({ events: {} });
fixture.detectChanges();
stub.restore();
expect(stub).to.have.been.calledOnce; // tslint:disable-line
});
});

0 comments on commit 194fe59

Please sign in to comment.