Skip to content

Commit

Permalink
Corrects XHR handling to pass test
Browse files Browse the repository at this point in the history
  • Loading branch information
njpearman committed Nov 13, 2020
1 parent 8fa15b1 commit bfd7a75
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 69 deletions.
42 changes: 12 additions & 30 deletions packages/__tests__/src/legacy/eventSourcesICalFeed.js
Expand Up @@ -2,6 +2,8 @@ import XHRMock from 'xhr-mock'
import { CalendarWrapper } from '../lib/wrappers/CalendarWrapper'

describe('addICalEventSource', function() {
const ICAL_MIME_TYPE = 'text/calendar'

// 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 = `
Expand Down Expand Up @@ -45,43 +47,23 @@ END:VEVENT
XHRMock.teardown()
})

it('correctly adds an array source', function(done) {
XHRMock.get(/^mock.ics/, function(req, res) {
it('correctly adds an ical feed source', async (done) => {
XHRMock.get('/mock.ics', function(req, res) {
expect(req.url().query).toEqual({})

done()
return res.status(200)
.header('content-type', 'application/json')
.header('content-type', ICAL_MIME_TYPE)
.body(ICAL_FEED)
})
go(
function() {
currentCalendar.addEventSource(iCalFeedSource)
},
null,
done
)
})

function go(addFunc, extraTestFunc, doneFunc) {
initCalendar()
addFunc()

checkAllEvents()
if (extraTestFunc) {
extraTestFunc()
}

setTimeout(function() {

const calendar = initCalendar()

calendar.addEventSource(iCalFeedSource)
setTimeout(() => {
checkAllEvents()
if (extraTestFunc) {
extraTestFunc()
}

doneFunc()
}, 0)
}
done()
}, 200)
})

// Checks to make sure all events have been rendered and that the calendar
// has internal info on all the events.
Expand Down
84 changes: 45 additions & 39 deletions packages/common/src/event-sources/ical-feed-event-source.ts
@@ -1,70 +1,76 @@
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'

export function requestICal(url: string, successCallback, failureCallback) {

const xhr = new XMLHttpRequest()
xhr.open('GET', url, true)

xhr.onload = function() {
if (xhr.status >= 200 && xhr.status < 400) {

const iCalFeed = xhr.responseText
console.log(iCalFeed)

successCallback(iCalFeed, xhr)
} else {
failureCallback('Request failed', xhr)
}
}

xhr.onerror = () => failureCallback('Request failed', xhr)

xhr.send(null)
}


interface ICalFeedMeta {
feedUrl: string
method: string
extraParams?: any
}


let eventSourceDef: EventSourceDef<ICalFeedMeta> = {

parseMeta(refined) {
if (refined.feedUrl) {
return {
feedUrl: refined.feedUrl,
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 })
}
)
}

return new Promise((resolve, reject) => {
requestICal(meta.feedUrl,
(_, xhr) => {
const rawEvents = [
{
title: 'id-123',
start: '2019-04-10T10:30:00Z',
end: '2019-04-13T17:00Z',
},
]

success({ rawEvents, xhr })
resolve()
},
(errorMessage, xhr) => {
failure({ message: errorMessage, xhr })
reject()
},
)
})
}
}


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
}

0 comments on commit bfd7a75

Please sign in to comment.