Skip to content

Commit dc07b43

Browse files
committed
pat-calendar: Do not include properties not existent in the event source. Avoids problem of setting a href attribute of undefined in the calendar.
1 parent eb30880 commit dc07b43

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

src/pat/calendar/calendar.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,11 @@ export default Base.extend({
252252
contact_email: event.contact_email,
253253
event_url: event.event_url,
254254
};
255+
for (const prop in ret) {
256+
if (!ret[prop]) {
257+
delete ret[prop];
258+
}
259+
}
255260
return ret;
256261
},
257262

src/pat/calendar/calendar.test.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ const mockFetch = () =>
1313
end: "2020-10-10T12:00:00Z",
1414
},
1515
{
16-
title: "Event 2",
17-
start: "2020-10-12",
18-
end: "2020-10-12",
16+
"title": "Event 2",
17+
"start": "2020-10-12",
18+
"end": "2020-10-12",
19+
"@id": "./test_event.html",
1920
},
2021
{
2122
title: "Event 3",
@@ -228,6 +229,33 @@ describe("Calendar tests", () => {
228229
done();
229230
});
230231

232+
it("Loads events and does not set the href if not present", async (done) => {
233+
const el = document.querySelector(".pat-calendar");
234+
el.setAttribute(
235+
"data-pat-calendar",
236+
"initial-date: 2020-10-10; url: ./test.json;"
237+
);
238+
239+
global.fetch = jest.fn().mockImplementation(mockFetch);
240+
241+
registry.scan(document.body);
242+
await utils.timeout(1); // wait a tick for async to settle.
243+
244+
const events = [...document.querySelectorAll(".fc-event-title")];
245+
246+
const event1 = events.filter((it) => it.textContent === "Event 1")[0].closest(".fc-event"); // prettier-ignore
247+
const event2 = events.filter((it) => it.textContent === "Event 2")[0].closest(".fc-event"); // prettier-ignore
248+
const event3 = events.filter((it) => it.textContent === "Event 3")[0].closest(".fc-event"); // prettier-ignore
249+
250+
expect(event1.href).toBeFalsy();
251+
expect(event2.href).toBe("http://localhost/test_event.html");
252+
expect(event3.href).toBeFalsy();
253+
254+
global.fetch.mockClear();
255+
delete global.fetch;
256+
done();
257+
});
258+
231259
it("Loads correct date if set in query string", async (done) => {
232260
const el = document.querySelector(".pat-calendar");
233261
el.setAttribute("data-pat-calendar", "timezone: Europe/Berlin");

src/pat/calendar/test_event_source.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
"title": "Event 1",
55
"start": "2020-10-10T10:00:00",
66
"end": "2020-10-10T12:00:00",
7-
"class": "hello-class-1 hello-class-2 calendar-section1-category1"
7+
"class": "hello-class-1 hello-class-2 calendar-section1-category1",
8+
"@id": "./test_event.html"
89
},
910
{
1011
"title": "Event 2",
1112
"start": "2020-10-12",
1213
"end": "2020-10-12",
13-
"class": "calendar-section1-category2"
14+
"class": "calendar-section1-category2",
15+
"@id": "./test_event.html"
1416
},
1517
{
1618
"title": "Event 3",

0 commit comments

Comments
 (0)