Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creates stub for plugin, with test file
- Loading branch information
Showing
4 changed files
with
169 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import XHRMock from 'xhr-mock' | ||
import { CalendarWrapper } from '../lib/wrappers/CalendarWrapper' | ||
|
||
describe('addICalEventSource', function() { | ||
// This is a bit gross to dump straight in the test. Could it be pulled out | ||
// to a separate file, or put somewhere neater? | ||
const ICAL_FEED = ` | ||
BEGIN:VCALENDAR | ||
VERSION:2.0 | ||
CALSCALE:GREGORIAN | ||
METHOD:PUBLISH | ||
X-WR-CALNAME:events@fullcalendar.test | ||
X-WR-TIMEZONE:Europe/Paris | ||
BEGIN:VEVENT | ||
DTSTART;VALUE=DATE:20190410 | ||
DTEND;VALUE=DATE:20190413 | ||
DTSTAMP:20201006T124223Z | ||
UID:5pll5td7cag5rkdm988j2d0vc7@google.com | ||
CREATED:20190408T110429Z | ||
DESCRIPTION: | ||
LAST-MODIFIED:20190409T110738Z | ||
LOCATION: | ||
SEQUENCE:0 | ||
STATUS:CONFIRMED | ||
SUMMARY:Multi-day conference | ||
TRANSP:OPAQUE | ||
END:VEVENT | ||
` | ||
|
||
const iCalFeedSource = { | ||
feedUrl: '/mock.ics', | ||
method: 'GET', | ||
} | ||
|
||
pushOptions({ | ||
initialDate: '2019-04-10', // the start of the three-day event in the feed | ||
initialView: 'dayGridMonth' | ||
}) | ||
|
||
beforeEach(function() { | ||
XHRMock.setup() | ||
}) | ||
|
||
afterEach(function() { | ||
XHRMock.teardown() | ||
}) | ||
|
||
it('correctly adds an array source', function(done) { | ||
XHRMock.get(/^mock.ics/, function(req, res) { | ||
expect(req.url().query).toEqual({}) | ||
|
||
done() | ||
return res.status(200) | ||
.header('content-type', 'application/json') | ||
.body(ICAL_FEED) | ||
}) | ||
go( | ||
function() { | ||
currentCalendar.addEventSource(iCalFeedSource) | ||
}, | ||
null, | ||
done | ||
) | ||
}) | ||
|
||
function go(addFunc, extraTestFunc, doneFunc) { | ||
initCalendar() | ||
addFunc() | ||
|
||
checkAllEvents() | ||
if (extraTestFunc) { | ||
extraTestFunc() | ||
} | ||
|
||
setTimeout(function() { | ||
|
||
checkAllEvents() | ||
if (extraTestFunc) { | ||
extraTestFunc() | ||
} | ||
|
||
doneFunc() | ||
}, 0) | ||
} | ||
|
||
// Checks to make sure all events have been rendered and that the calendar | ||
// has internal info on all the events. | ||
function checkAllEvents() { | ||
expect(currentCalendar.getEvents().length).toEqual(1) | ||
|
||
let calendarWrapper = new CalendarWrapper(currentCalendar) | ||
expect(calendarWrapper.getEventEls().length).toEqual(1) | ||
} | ||
}) |
3 changes: 3 additions & 0 deletions
3
packages/common/src/event-sources/ical-feed-event-source-refiners.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const ICAL_FEED_EVENT_SOURCE_REFINERS = { | ||
feedUrl: String, | ||
} |
70 changes: 70 additions & 0 deletions
70
packages/common/src/event-sources/ical-feed-event-source.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import { requestJson } from '../util/requestJson' | ||
import { EventSourceDef } from '../structs/event-source-def' | ||
import { __assign } from 'tslib' | ||
import { createPlugin } from '../plugin-system' | ||
import { ICAL_FEED_EVENT_SOURCE_REFINERS } from './ical-feed-event-source-refiners' | ||
|
||
interface ICalFeedMeta { | ||
feedUrl: string | ||
method: string | ||
extraParams?: any | ||
} | ||
|
||
let eventSourceDef: EventSourceDef<ICalFeedMeta> = { | ||
|
||
parseMeta(refined) { | ||
if (refined.feedUrl) { | ||
return { | ||
feedUrl: refined.url, | ||
method: (refined.method || 'GET').toUpperCase(), | ||
} | ||
} | ||
return null | ||
}, | ||
|
||
fetch(arg, success, failure) { | ||
let meta: ICalFeedMeta = arg.eventSource.meta | ||
let requestParams = buildRequestParams(meta) | ||
|
||
requestJson( | ||
meta.method, meta.feedUrl, requestParams, | ||
function(_, xhr) { | ||
const rawEvents = [ | ||
{ | ||
title: 'id-123', | ||
start: '2019-04-10T10:30:00Z', | ||
end: '2019-04-13T17:00Z', | ||
}, | ||
] | ||
|
||
success({ rawEvents, xhr }) | ||
}, | ||
function(errorMessage, xhr) { | ||
failure({ message: errorMessage, xhr }) | ||
} | ||
) | ||
} | ||
|
||
} | ||
|
||
|
||
export const iCalFeedEventSourcePlugin = createPlugin({ | ||
eventSourceRefiners: ICAL_FEED_EVENT_SOURCE_REFINERS, | ||
eventSourceDefs: [ eventSourceDef ] | ||
}) | ||
|
||
|
||
function buildRequestParams(meta: ICalFeedMeta) { | ||
let customRequestParams | ||
let params = {} | ||
|
||
if (typeof meta.extraParams === 'function') { | ||
customRequestParams = meta.extraParams() | ||
} else { | ||
customRequestParams = meta.extraParams || {} | ||
} | ||
|
||
__assign(params, customRequestParams) | ||
|
||
return params | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters