This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/tests/unit/lib/download-calendar-test.js
Krzysztof Kotlarek 9062fd9b7a
FIX: improvements for download local dates (#14588)
* FIX: do not display add to calendar for past dates

There is no value in saving past dates into calendar

* FIX: remove postId and move ICS to frontend

PostId is not necessary and will make the solution more generic for dates which doesn't belong to a specific post.

Also, ICS file can be generated in JavaScript to avoid calling backend.
2021-10-14 09:22:44 +11:00

72 lines
1.6 KiB
JavaScript

import { module, test } from "qunit";
import {
downloadGoogle,
formatDates,
generateIcsData,
} from "discourse/lib/download-calendar";
import sinon from "sinon";
module("Unit | Utility | download-calendar", function (hooks) {
hooks.beforeEach(function () {
let win = { focus: function () {} };
sinon.stub(window, "open").returns(win);
sinon.stub(win, "focus");
});
test("correct data for Ics", function (assert) {
const data = generateIcsData("event test", [
{
startsAt: "2021-10-12T15:00:00.000Z",
endsAt: "2021-10-12T16:00:00.000Z",
},
]);
assert.ok(
data,
`
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Discourse//EN
BEGIN:VEVENT
UID:1634050800000_1634054400000
DTSTAMP:20213312T223320Z
DTSTART:20210012T150000Z
DTEND:20210012T160000Z
SUMMARY:event2
END:VEVENT
END:VCALENDAR
`
);
});
test("correct url for Google", function (assert) {
downloadGoogle("event", [
{
startsAt: "2021-10-12T15:00:00.000Z",
endsAt: "2021-10-12T16:00:00.000Z",
},
]);
assert.ok(
window.open.calledWith(
"https://www.google.com/calendar/event?action=TEMPLATE&text=event&dates=20211012T150000Z/20211012T160000Z",
"_blank",
"noopener",
"noreferrer"
)
);
});
test("calculates end date when none given", function (assert) {
let dates = formatDates([{ startsAt: "2021-10-12T15:00:00.000Z" }]);
assert.deepEqual(
dates,
[
{
startsAt: "2021-10-12T15:00:00.000Z",
endsAt: "2021-10-12T16:00:00Z",
},
],
"endsAt is one hour after startsAt"
);
});
});