Ver Fonte

tests for eventAllow/selectAllow

Adam Shaw há 9 anos atrás
pai
commit
abbeae643d
3 ficheiros alterados com 150 adições e 0 exclusões
  1. 56 0
      tests/automated/eventAllow.js
  2. 50 0
      tests/automated/selectAllow.js
  3. 44 0
      tests/lib/time-grid.js

+ 56 - 0
tests/automated/eventAllow.js

@@ -0,0 +1,56 @@
+describe('eventAllow', function() {
+	var options;
+
+	beforeEach(function() {
+		options = {
+			now: '2016-09-04',
+			defaultView: 'agendaWeek',
+			scrollTime: '00:00',
+			editable: true,
+			events: [
+				{
+					title: 'event 1',
+					start: '2016-09-04T01:00'
+				}
+			]
+		};
+		affix('#cal');
+	});
+
+	it('disallows dragging when returning false', function(done) { // and given correct params
+		options.eventAllow = function(dropInfo, event) {
+			expect(typeof dropInfo).toBe('object');
+			expect(moment.isMoment(dropInfo.start)).toBe(true);
+			expect(moment.isMoment(dropInfo.end)).toBe(true);
+			expect(typeof event).toBe('object');
+			expect(event.title).toBe('event 1');
+			return false;
+		};
+		spyOn(options, 'eventAllow').and.callThrough();
+
+		$('#cal').fullCalendar(options);
+
+		dragTimeGridEvent($('.fc-event'), '2016-09-04T03:00:00')
+			.then(function(modifiedEvent) {
+				expect(modifiedEvent).toBeFalsy(); // drop failure?
+				expect(options.eventAllow).toHaveBeenCalled();
+				done();
+			});
+	});
+
+	it('allows dragging when returning true', function(done) {
+		options.eventAllow = function(dropInfo, event) {
+			return true;
+		};
+		spyOn(options, 'eventAllow').and.callThrough();
+
+		$('#cal').fullCalendar(options);
+
+		dragTimeGridEvent($('.fc-event'), '2016-09-04T03:00:00')
+			.then(function(modifiedEvent) {
+				expect(modifiedEvent.start.format()).toBe('2016-09-04T03:00:00');
+				expect(options.eventAllow).toHaveBeenCalled();
+				done();
+			});
+	});
+});

+ 50 - 0
tests/automated/selectAllow.js

@@ -0,0 +1,50 @@
+describe('selectAllow', function() {
+	var options;
+
+	beforeEach(function() {
+		options = {
+			now: '2016-09-04',
+			defaultView: 'agendaWeek',
+			scrollTime: '00:00',
+			selectable: true
+		};
+		affix('#cal');
+	});
+
+	it('disallows selecting when returning false', function(done) { // and given correct params
+		options.selectAllow = function(selectInfo) {
+			expect(typeof selectInfo).toBe('object');
+			expect(moment.isMoment(selectInfo.start)).toBe(true);
+			expect(moment.isMoment(selectInfo.end)).toBe(true);
+			return false;
+		};
+		spyOn(options, 'selectAllow').and.callThrough();
+
+		$('#cal').fullCalendar(options);
+
+		selectTimeGrid('2016-09-04T01:00:00', '2016-09-04T05:00:00')
+			.then(function(selectInfo) {
+				expect(selectInfo).toBeFalsy();
+				expect(options.selectAllow).toHaveBeenCalled();
+				done();
+			});
+	});
+
+	it('allows selecting when returning true', function(done) {
+		options.selectAllow = function(selectInfo) {
+			return true;
+		};
+		spyOn(options, 'selectAllow').and.callThrough();
+
+		$('#cal').fullCalendar(options);
+
+		selectTimeGrid('2016-09-04T01:00:00', '2016-09-04T05:00:00')
+			.then(function(selectInfo) {
+				expect(typeof selectInfo).toBe('object');
+				expect(selectInfo.start.format()).toBe('2016-09-04T01:00:00');
+				expect(selectInfo.end.format()).toBe('2016-09-04T05:30:00');
+				expect(options.selectAllow).toHaveBeenCalled();
+				done();
+			});
+	});
+});

+ 44 - 0
tests/lib/time-grid.js

@@ -2,6 +2,50 @@
 // TODO: consolidate with scheduler
 
 
+function dragTimeGridEvent(eventEl, dropDate) {
+	return new Promise(function(resolve) {
+		var calendar = $('#cal').fullCalendar('getCalendar');
+		var modifiedEvent = null;
+
+		calendar.on('eventDragStop', function() {
+			setTimeout(function() { // wait for eventDrop to be called
+				resolve(modifiedEvent);
+			});
+		});
+		calendar.on('eventDrop', function(event) {
+			modifiedEvent = event;
+		});
+
+		eventEl.simulate('drag', {
+			localPoint: { left: '50%', top: 0 },
+			end: getTimeGridPoint(dropDate),
+		});
+	});
+}
+
+
+function selectTimeGrid(start, inclusiveEnd) {
+	return new Promise(function(resolve) {
+		var calendar = $('#cal').fullCalendar('getCalendar');
+		var selectInfo = null;
+
+		calendar.on('select', function(start, end) {
+			selectInfo = { start: start, end: end };
+		});
+
+		getTimeGridDayEls(start).simulate('drag', {
+			point: getTimeGridPoint(start),
+			end: getTimeGridPoint(inclusiveEnd),
+			onRelease: function() {
+				setTimeout(function() { // wait for eventDrop to be called
+					resolve(selectInfo);
+				});
+			}
+		});
+	});
+}
+
+
 function getTimeGridPoint(date) {
 	var date = $.fullCalendar.moment.parseZone(date);
 	var top = getTimeGridTop(date.time());