Skip to content

Commit

Permalink
Creates stub for plugin, with test file
Browse files Browse the repository at this point in the history
  • Loading branch information
njpearman committed Nov 13, 2020
1 parent 364e135 commit 79fb1c0
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 1 deletion.
94 changes: 94 additions & 0 deletions packages/__tests__/src/legacy/eventSourcesICalFeed.js
@@ -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)
}
})
@@ -0,0 +1,3 @@
export const ICAL_FEED_EVENT_SOURCE_REFINERS = {
feedUrl: String,
}
70 changes: 70 additions & 0 deletions packages/common/src/event-sources/ical-feed-event-source.ts
@@ -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
}
3 changes: 2 additions & 1 deletion packages/common/src/structs/event-source-parse.ts
Expand Up @@ -2,6 +2,7 @@ import { EventInput, EventInputTransformer } from './event-parse'
import { EventSourceFunc } from '../event-sources/func-event-source'
import { EventSource, EventSourceSuccessResponseHandler, EventSourceErrorResponseHandler } from './event-source'
import { JSON_FEED_EVENT_SOURCE_REFINERS } from '../event-sources/json-feed-event-source-refiners'
import { ICAL_FEED_EVENT_SOURCE_REFINERS } from '../event-sources/ical-feed-event-source-refiners'
import { CalendarContext } from '../CalendarContext'
import { guid } from '../util/misc'
import { EVENT_UI_REFINERS, createEventUi, EventUiInput, EventUiRefined } from '../component/event-ui'
Expand All @@ -19,7 +20,7 @@ const EVENT_SOURCE_REFINERS = { // does NOT include EVENT_UI_REFINERS
failure: identity as Identity<EventSourceErrorResponseHandler>,
}

type BuiltInEventSourceRefiners = typeof EVENT_SOURCE_REFINERS & typeof JSON_FEED_EVENT_SOURCE_REFINERS
type BuiltInEventSourceRefiners = typeof EVENT_SOURCE_REFINERS & typeof JSON_FEED_EVENT_SOURCE_REFINERS & typeof ICAL_FEED_EVENT_SOURCE_REFINERS

export interface EventSourceRefiners extends BuiltInEventSourceRefiners {
// for extending
Expand Down

0 comments on commit 79fb1c0

Please sign in to comment.