event-dnd.js 11.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
describe('eventDrop', function() {
	var options;

	beforeEach(function() {
		options = {
			defaultDate: '2014-06-11',
			editable: true,
			dragScroll: false
		};
		affix('#cal');
	});

	afterEach(function() {
		$('#cal').fullCalendar('destroy');
	});

	describe('when in month view', function() {
		beforeEach(function() {
			options.defaultView = 'month';
		});

		describe('when dragging an all-day event to another day', function() {
			it('should be given correct arguments, with whole-day delta', function(done) {
				options.events = [ {
					title: 'all-day event',
					start: '2014-06-11',
					allDay: true
				} ];

				init(
					function() {
						$('.fc-event').simulate('drag-n-drop', {
							dx: $('.fc-day').width() * 2,
							dy: $('.fc-day').height()
						});
					},
					function(event, delta, revertFunc) {
						expect(delta.asDays()).toBe(9);
						expect(delta.hours()).toBe(0);
						expect(delta.minutes()).toBe(0);
						expect(delta.seconds()).toBe(0);
						expect(delta.milliseconds()).toBe(0);

						expect(event.start).toEqualMoment('2014-06-20');
						expect(event.end).toBeNull();
						revertFunc();
						expect(event.start).toEqualMoment('2014-06-11');
						expect(event.end).toBeNull();

						done();
					}
				);
			});
		});

		describe('when gragging a timed event to another day', function() {
			it('should be given correct arguments, with whole-day delta', function(done) {
				options.events = [ {
					title: 'timed event',
					start: '2014-06-11T06:00:00',
					allDay: false
				} ];

				init(
					function() {
						$('.fc-event').simulate('drag-n-drop', {
							dx: $('.fc-day').width() * -2,
							dy: $('.fc-day').height()
						});
					},
					function(event, delta, revertFunc) {
						expect(delta.asDays()).toBe(5);
						expect(delta.hours()).toBe(0);
						expect(delta.minutes()).toBe(0);
						expect(delta.seconds()).toBe(0);
						expect(delta.milliseconds()).toBe(0);

						expect(event.start).toEqualMoment('2014-06-16T06:00:00');
						expect(event.end).toBeNull();
						revertFunc();
						expect(event.start).toEqualMoment('2014-06-11T06:00:00');
						expect(event.end).toBeNull();

						done();
					}
				);
			});
		});

		// TODO: tests for eventMouseover/eventMouseout firing correctly when no dragging
		it('should not fire any eventMouseover/eventMouseout events while dragging', function(done) { // issue 1297
			options.events = [
				{
					title: 'all-day event',
					start: '2014-06-11',
					allDay: true,
					className: 'event1'
				},
				{
					title: 'event2',
					start: '2014-06-10',
					allDay: true,
					className: 'event2'
				}
			];
			options.eventMouseover = function() { };
			options.eventMouseout = function() { };
			spyOn(options, 'eventMouseover');
			spyOn(options, 'eventMouseout');

			init(
				function() {
					$('.event1').simulate('drag-n-drop', {
						dx: $('.fc-day').width() * 2,
						dy: $('.fc-day').height(),
						interpolation: {
							stepCount: 10,
							duration: 1000
						}
					});
					setTimeout(function() { // wait until half way through drag
						$('.event2')
							.simulate('mouseover')
							.simulate('mouseenter')
							.simulate('mouseout')
							.simulate('mouseleave');
					}, 500);
				},
				function(event, delta, revertFunc) {
					expect(options.eventMouseover).not.toHaveBeenCalled();
					expect(options.eventMouseout).not.toHaveBeenCalled();
					done();
				}
			);
		});
	});

	describe('when in agenda view', function() {
		beforeEach(function() {
			options.defaultView = 'agendaWeek';
		});

		describe('when dragging a timed event to another time on a different day', function() {
			it('should be given correct arguments and delta with days/time', function(done) {
				options.events = [ {
					title: 'timed event',
					start: '2014-06-11T06:00:00',
					allDay: false
				} ];

				init(
					function() {
						$('.fc-event .fc-time').simulate('drag-n-drop', {
							dx: $('th.fc-wed').width(), // 1 day
							dy: $('.fc-slats tr:eq(1)').outerHeight() * 2.9 // 1.5 hours
						});
					},
					function(event, delta, revertFunc) {
						expect(delta.days()).toBe(1);
						expect(delta.hours()).toBe(1);
						expect(delta.minutes()).toBe(30);
						expect(delta.seconds()).toBe(0);
						expect(delta.milliseconds()).toBe(0);

						expect(event.start).toEqualMoment('2014-06-12T07:30:00');
						expect(event.end).toBeNull();
						revertFunc();
						expect(event.start).toEqualMoment('2014-06-11T06:00:00');
						expect(event.end).toBeNull();

						done();
					}
				);
			});
		});

		describe('when dragging an all-day event to another all-day', function() {
			it('should be given correct arguments, with whole-day delta', function(done) {
				options.events = [ {
					title: 'all-day event',
					start: '2014-06-11',
					allDay: true
				} ];

				init(
					function() {
						$('.fc-event').simulate('drag-n-drop', {
							dx: $('th.fc-wed').width() * 2 // 2 days
						});
					},
					function(event, delta, revertFunc) {
						expect(delta.days()).toBe(2);
						expect(delta.hours()).toBe(0);
						expect(delta.minutes()).toBe(0);
						expect(delta.seconds()).toBe(0);
						expect(delta.milliseconds()).toBe(0);

						expect(event.start).toEqualMoment('2014-06-13');
						expect(event.end).toBeNull();
						revertFunc();
						expect(event.start).toEqualMoment('2014-06-11');
						expect(event.end).toBeNull();

						done();
					}
				);
			});
		});

		describe('when dragging an all-day event to a time slot on a different day', function() {
			it('should be given correct arguments and delta with days/time', function(done) {
				options.scrollTime = '01:00:00';
				options.height = 400; // short enough to make scrolling happen
				options.events = [ {
					title: 'all-day event',
					start: '2014-06-11',
					allDay: true
				} ];

				init(
					function() {
						var allDayGrid = $('.fc-agenda-view .fc-day-grid');
						var hr = allDayGrid.next('hr');
						$('.fc-event').simulate('drag-n-drop', {
							dx: $('th.fc-wed').width() * -1,
							dy: allDayGrid.outerHeight() + hr.outerHeight()
						});
					},
					function(event, delta, revertFunc) {
						expect(delta.days()).toBe(-1);
						expect(delta.hours()).toBe(1);
						expect(delta.minutes()).toBe(0);
						expect(delta.seconds()).toBe(0);
						expect(delta.milliseconds()).toBe(0);

						expect(event.start).toEqualMoment('2014-06-10T01:00:00');
						expect(event.end).toBeNull();
						expect(event.allDay).toBe(false);
						revertFunc();
						expect(event.start).toEqualMoment('2014-06-11');
						expect(event.end).toBeNull();
						expect(event.allDay).toBe(true);

						done();
					}
				);
			});
		});

		describe('when dragging a timed event to an all-day slot on a different day', function() {
			it('should be given correct arguments, with whole-day delta', function(done) {
				var eventElm;

				options.scrollTime = '01:00:00';
				options.height = 400; // short enough to make scrolling happen
				options.events = [ {
					title: 'timed event',
					start: '2014-06-11T01:00:00',
					allDay: false
				} ];

				init(
					function() {
						eventElm = $('.fc-event .fc-time').simulate('drag', { // grabs the top of the event
							dx: $('th.fc-wed').width() * -1,
							dy: -$('.fc-agenda-view .fc-day-grid').outerHeight(),
							callback: function() {
								// the all day slot works off of mouse-moving coordinates
								var offset = eventElm.offset();
								$('.fc-agenda-allday .fc-day-content')
									.simulate('mouseover', {
										clientX: offset.left + 10,
										clientY: offset.top + 10
									})
									.simulate('mousemove', {
										clientX: offset.left + 10,
										clientY: offset.top + 10
									});
								setTimeout(function() {
									eventElm.simulate('drop');
								}, 100);
							}
						});
					},
					function(event, delta, revertFunc) {
						expect(delta.days()).toBe(-1);
						expect(delta.hours()).toBe(0);
						expect(delta.minutes()).toBe(0);
						expect(delta.seconds()).toBe(0);
						expect(delta.milliseconds()).toBe(0);

						expect(event.start).toEqualMoment('2014-06-10');
						expect(event.end).toBeNull();
						expect(event.allDay).toBe(true);
						revertFunc();
						expect(event.start).toEqualMoment('2014-06-11T01:00:00');
						expect(event.end).toBeNull();
						expect(event.allDay).toBe(false);

						done();
					}
				);
			});
		});

		describe('when dragging a timed event with no end time', function() {
			it('should continue to only show the updated start time', function(done) {
				var dragged = false;
				var eventElm;

				options.scrollTime = '01:00:00';
				options.height = 400; // short enough to make scrolling happen
				options.events = [ {
					title: 'timed event',
					start: '2014-06-11T01:00:00',
					allDay: false
				} ];

				init(
					function() {
						eventElm = $('.fc-event .fc-time').simulate('drag', {
							dy: $('.fc-slats tr:eq(1)').height() * 2.9, // 1.5 hours
							callback: function() {
								dragged = true;
								expect($('.fc-event.fc-helper .fc-time')).toHaveText('2:30');
								eventElm.simulate('drop');
							}
						});
					},
					function() {
						expect(dragged).toBe(true);
						done();
					}
				);
			});
		});

		describe('when dragging a timed event with an end time', function() {
			it('should continue to show the updated start and end time', function(done) {
				var dragged = false;
				var eventElm;

				options.scrollTime = '01:00:00';
				options.height = 400; // short enough to make scrolling happen
				options.events = [ {
					title: 'timed event',
					start: '2014-06-11T01:00:00',
					end: '2014-06-11T02:00:00',
					allDay: false
				} ];

				init(
					function() {
						eventElm = $('.fc-event .fc-time').simulate('drag', {
							dy: $('.fc-slats tr:eq(1)').height() * 2.9, // 1.5 hours
							callback: function() {
								dragged = true;
								expect($('.fc-event.fc-helper .fc-time')).toHaveText('2:30 - 3:30');
								eventElm.simulate('drop');
							}
						});
					},
					function() {
						expect(dragged).toBe(true);
						done();
					}
				);
			});
		});
	});

	// Initialize a calendar, run a drag, and do type-checking of all arguments for all handlers.
	// TODO: more descrimination instead of just checking for 'object'
	function init(dragFunc, dropHandler) {
		var eventsRendered = false;

		options.eventAfterAllRender = function() {
			if (!eventsRendered) { // because event rerendering will happen upon drop
				dragFunc();
				eventsRendered = true;
			}
		};
		options.eventDragStart = function(event, jsEvent, uiEvent, view) {
			expect(this instanceof Element).toBe(true);
			expect(this).toHaveClass('fc-event');
			expect(typeof event).toBe('object');
			expect(typeof jsEvent).toBe('object');
			expect(typeof uiEvent).toBe('object');
			expect(typeof view).toBe('object');
		};
		options.eventDragStop = function(event, jsEvent, uiEvent, view) {
			expect(options.eventDragStart).toHaveBeenCalled();

			expect(this instanceof Element).toBe(true);
			expect(this).toHaveClass('fc-event');
			expect(typeof event).toBe('object');
			expect(typeof jsEvent).toBe('object');
			expect(typeof uiEvent).toBe('object');
			expect(typeof view).toBe('object');
		};
		options.eventDrop = function(event, delta, revertFunc, jsEvent, uiEvent, view) {
			expect(options.eventDragStop).toHaveBeenCalled();

			expect(this instanceof Element).toBe(true);
			expect(this).toHaveClass('fc-event');
			expect(typeof event).toBe('object');
			expect(moment.isDuration(delta)).toBe(true);
			expect(typeof revertFunc).toBe('function');
			expect(typeof jsEvent).toBe('object');
			expect(typeof uiEvent).toBe('object');
			expect(typeof view).toBe('object');

			dropHandler.apply(this, arguments);
		};

		spyOn(options, 'eventDragStart').and.callThrough();
		spyOn(options, 'eventDragStop').and.callThrough();

		setTimeout(function() { // hack. agenda view scroll state would get messed up between tests
			$('#cal').fullCalendar(options);
		}, 0);
	}
});