2020-03-23 21:39:09 -04:00
|
|
|
import { logIn } from "helpers/qunit-helpers";
|
|
|
|
import User from "discourse/models/user";
|
2020-04-14 03:28:51 -04:00
|
|
|
import KeyboardShortcutInitializer from "discourse/initializers/keyboard-shortcuts";
|
2020-04-16 21:08:07 -04:00
|
|
|
import { REMINDER_TYPES } from "discourse/lib/bookmark";
|
2020-05-06 19:10:32 -04:00
|
|
|
import { fakeTime } from "helpers/qunit-helpers";
|
2019-12-10 23:04:02 -05:00
|
|
|
let BookmarkController;
|
|
|
|
|
|
|
|
moduleFor("controller:bookmark", {
|
|
|
|
beforeEach() {
|
2020-03-23 21:39:09 -04:00
|
|
|
logIn();
|
2020-04-14 03:28:51 -04:00
|
|
|
KeyboardShortcutInitializer.initialize(Discourse.__container__);
|
2020-03-23 21:39:09 -04:00
|
|
|
BookmarkController = this.subject({ currentUser: User.current() });
|
2020-04-01 19:57:48 -04:00
|
|
|
BookmarkController.onShow();
|
2019-12-11 04:00:37 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
afterEach() {
|
|
|
|
sandbox.restore();
|
2019-12-10 23:04:02 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
function mockMomentTz(dateString) {
|
2020-05-06 19:10:32 -04:00
|
|
|
fakeTime(dateString, BookmarkController.userTimezone);
|
2019-12-10 23:04:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
QUnit.test("showLaterToday when later today is tomorrow do not show", function(
|
|
|
|
assert
|
|
|
|
) {
|
2020-03-16 02:05:44 -04:00
|
|
|
mockMomentTz("2019-12-11T22:00:00");
|
2019-12-10 23:04:02 -05:00
|
|
|
|
|
|
|
assert.equal(BookmarkController.get("showLaterToday"), false);
|
|
|
|
});
|
|
|
|
|
2020-04-14 03:28:51 -04:00
|
|
|
QUnit.test(
|
|
|
|
"showLaterToday when later today is after 5pm but before 6pm",
|
|
|
|
function(assert) {
|
|
|
|
mockMomentTz("2019-12-11T15:00:00");
|
|
|
|
assert.equal(BookmarkController.get("showLaterToday"), true);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
QUnit.test("showLaterToday when now is after the cutoff time (5pm)", function(
|
|
|
|
assert
|
|
|
|
) {
|
|
|
|
mockMomentTz("2019-12-11T17:00:00");
|
2020-03-16 02:05:44 -04:00
|
|
|
assert.equal(BookmarkController.get("showLaterToday"), false);
|
|
|
|
});
|
|
|
|
|
2019-12-10 23:04:02 -05:00
|
|
|
QUnit.test(
|
|
|
|
"showLaterToday when later today is before the end of the day, show",
|
|
|
|
function(assert) {
|
2020-03-16 02:05:44 -04:00
|
|
|
mockMomentTz("2019-12-11T10:00:00");
|
2019-12-10 23:04:02 -05:00
|
|
|
|
|
|
|
assert.equal(BookmarkController.get("showLaterToday"), true);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
QUnit.test("nextWeek gets next week correctly", function(assert) {
|
2020-03-16 02:05:44 -04:00
|
|
|
mockMomentTz("2019-12-11T08:00:00");
|
2019-12-10 23:04:02 -05:00
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
BookmarkController.nextWeek().format("YYYY-MM-DD"),
|
|
|
|
"2019-12-18"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test("nextMonth gets next month correctly", function(assert) {
|
2020-03-16 02:05:44 -04:00
|
|
|
mockMomentTz("2019-12-11T08:00:00");
|
2019-12-10 23:04:02 -05:00
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
BookmarkController.nextMonth().format("YYYY-MM-DD"),
|
|
|
|
"2020-01-11"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2020-04-01 19:57:48 -04:00
|
|
|
QUnit.test("laterThisWeek gets 2 days from now", function(assert) {
|
|
|
|
mockMomentTz("2019-12-10T08:00:00");
|
2019-12-10 23:04:02 -05:00
|
|
|
|
2020-04-01 19:57:48 -04:00
|
|
|
assert.equal(
|
|
|
|
BookmarkController.laterThisWeek().format("YYYY-MM-DD"),
|
|
|
|
"2019-12-12"
|
|
|
|
);
|
|
|
|
});
|
2019-12-10 23:04:02 -05:00
|
|
|
|
2020-04-01 19:57:48 -04:00
|
|
|
QUnit.test("laterThisWeek returns null if we are at Thursday already", function(
|
|
|
|
assert
|
|
|
|
) {
|
|
|
|
mockMomentTz("2019-12-12T08:00:00");
|
2019-12-10 23:04:02 -05:00
|
|
|
|
2020-04-01 19:57:48 -04:00
|
|
|
assert.equal(BookmarkController.laterThisWeek(), null);
|
|
|
|
});
|
2019-12-10 23:04:02 -05:00
|
|
|
|
2020-04-01 19:57:48 -04:00
|
|
|
QUnit.test("showLaterThisWeek returns true if < Thursday", function(assert) {
|
|
|
|
mockMomentTz("2019-12-10T08:00:00");
|
2019-12-10 23:04:02 -05:00
|
|
|
|
2020-04-01 19:57:48 -04:00
|
|
|
assert.equal(BookmarkController.showLaterThisWeek, true);
|
|
|
|
});
|
2019-12-10 23:04:02 -05:00
|
|
|
|
2020-04-01 19:57:48 -04:00
|
|
|
QUnit.test("showLaterThisWeek returns false if > Thursday", function(assert) {
|
|
|
|
mockMomentTz("2019-12-12T08:00:00");
|
2019-12-10 23:04:02 -05:00
|
|
|
|
2020-04-01 19:57:48 -04:00
|
|
|
assert.equal(BookmarkController.showLaterThisWeek, false);
|
|
|
|
});
|
2019-12-10 23:04:02 -05:00
|
|
|
QUnit.test("tomorrow gets tomorrow correctly", function(assert) {
|
2020-03-16 02:05:44 -04:00
|
|
|
mockMomentTz("2019-12-11T08:00:00");
|
2019-12-10 23:04:02 -05:00
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
BookmarkController.tomorrow().format("YYYY-MM-DD"),
|
|
|
|
"2019-12-12"
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test(
|
|
|
|
"startOfDay changes the time of the provided date to 8:00am correctly",
|
|
|
|
function(assert) {
|
|
|
|
let dt = moment.tz(
|
2020-03-16 02:05:44 -04:00
|
|
|
"2019-12-11T11:37:16",
|
2020-05-10 21:01:47 -04:00
|
|
|
BookmarkController.currentUser.resolvedTimezone(
|
|
|
|
BookmarkController.currentUser
|
|
|
|
)
|
2019-12-10 23:04:02 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
BookmarkController.startOfDay(dt).format("YYYY-MM-DD HH:mm:ss"),
|
|
|
|
"2019-12-11 08:00:00"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
QUnit.test(
|
2020-04-14 03:28:51 -04:00
|
|
|
"laterToday gets 3 hours from now and if before half-past, it rounds down",
|
2019-12-10 23:04:02 -05:00
|
|
|
function(assert) {
|
2020-03-16 02:05:44 -04:00
|
|
|
mockMomentTz("2019-12-11T08:13:00");
|
2019-12-10 23:04:02 -05:00
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
BookmarkController.laterToday().format("YYYY-MM-DD HH:mm:ss"),
|
2020-04-14 03:28:51 -04:00
|
|
|
"2019-12-11 11:00:00"
|
2019-12-10 23:04:02 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
QUnit.test(
|
|
|
|
"laterToday gets 3 hours from now and if after half-past, it rounds up to the next hour",
|
|
|
|
function(assert) {
|
2020-03-16 02:05:44 -04:00
|
|
|
mockMomentTz("2019-12-11T08:43:00");
|
2019-12-10 23:04:02 -05:00
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
BookmarkController.laterToday().format("YYYY-MM-DD HH:mm:ss"),
|
2020-03-16 02:05:44 -04:00
|
|
|
"2019-12-11 12:00:00"
|
2019-12-10 23:04:02 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
2020-03-16 02:05:44 -04:00
|
|
|
|
2020-04-14 03:28:51 -04:00
|
|
|
QUnit.test(
|
|
|
|
"laterToday is capped to 6pm. later today at 3pm = 6pm, 3:30pm = 6pm, 4pm = 6pm, 4:59pm = 6pm",
|
|
|
|
function(assert) {
|
|
|
|
mockMomentTz("2019-12-11T15:00:00");
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
BookmarkController.laterToday().format("YYYY-MM-DD HH:mm:ss"),
|
|
|
|
"2019-12-11 18:00:00",
|
|
|
|
"3pm should max to 6pm"
|
|
|
|
);
|
|
|
|
|
|
|
|
mockMomentTz("2019-12-11T15:31:00");
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
BookmarkController.laterToday().format("YYYY-MM-DD HH:mm:ss"),
|
|
|
|
"2019-12-11 18:00:00",
|
|
|
|
"3:30pm should max to 6pm"
|
|
|
|
);
|
|
|
|
|
|
|
|
mockMomentTz("2019-12-11T16:00:00");
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
BookmarkController.laterToday().format("YYYY-MM-DD HH:mm:ss"),
|
|
|
|
"2019-12-11 18:00:00",
|
|
|
|
"4pm should max to 6pm"
|
|
|
|
);
|
|
|
|
|
|
|
|
mockMomentTz("2019-12-11T16:59:00");
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
BookmarkController.laterToday().format("YYYY-MM-DD HH:mm:ss"),
|
|
|
|
"2019-12-11 18:00:00",
|
|
|
|
"4:59pm should max to 6pm"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
QUnit.test("showLaterToday returns false if >= 5PM", function(assert) {
|
|
|
|
mockMomentTz("2019-12-11T17:00:01");
|
|
|
|
assert.equal(BookmarkController.showLaterToday, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
QUnit.test("showLaterToday returns false if >= 5PM", function(assert) {
|
|
|
|
mockMomentTz("2019-12-11T17:00:01");
|
|
|
|
assert.equal(BookmarkController.showLaterToday, false);
|
|
|
|
});
|
|
|
|
|
2020-04-01 19:57:48 -04:00
|
|
|
QUnit.test(
|
|
|
|
"reminderAt - custom - defaults to 8:00am if the time is not selected",
|
|
|
|
function(assert) {
|
|
|
|
BookmarkController.customReminderDate = "2028-12-12";
|
|
|
|
BookmarkController.selectedReminderType =
|
|
|
|
BookmarkController.reminderTypes.CUSTOM;
|
2020-04-19 23:30:04 -04:00
|
|
|
const reminderAt = BookmarkController._reminderAt();
|
2020-04-01 19:57:48 -04:00
|
|
|
assert.equal(BookmarkController.customReminderTime, "08:00");
|
|
|
|
assert.equal(
|
|
|
|
reminderAt.toString(),
|
|
|
|
moment
|
|
|
|
.tz(
|
|
|
|
"2028-12-12 08:00",
|
2020-05-10 21:01:47 -04:00
|
|
|
BookmarkController.currentUser.resolvedTimezone(
|
|
|
|
BookmarkController.currentUser
|
|
|
|
)
|
2020-04-01 19:57:48 -04:00
|
|
|
)
|
|
|
|
.toString(),
|
|
|
|
"the custom date and time are parsed correctly with default time"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-03-16 02:05:44 -04:00
|
|
|
QUnit.test(
|
|
|
|
"loadLastUsedCustomReminderDatetime fills the custom reminder date + time if present in localStorage",
|
|
|
|
function(assert) {
|
|
|
|
mockMomentTz("2019-12-11T08:00:00");
|
|
|
|
localStorage.lastCustomBookmarkReminderDate = "2019-12-12";
|
|
|
|
localStorage.lastCustomBookmarkReminderTime = "08:00";
|
|
|
|
|
2020-04-19 23:30:04 -04:00
|
|
|
BookmarkController._loadLastUsedCustomReminderDatetime();
|
2020-03-16 02:05:44 -04:00
|
|
|
|
|
|
|
assert.equal(BookmarkController.lastCustomReminderDate, "2019-12-12");
|
|
|
|
assert.equal(BookmarkController.lastCustomReminderTime, "08:00");
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
QUnit.test(
|
|
|
|
"loadLastUsedCustomReminderDatetime does not fills the custom reminder date + time if the datetime in localStorage is < now",
|
|
|
|
function(assert) {
|
|
|
|
mockMomentTz("2019-12-11T08:00:00");
|
|
|
|
localStorage.lastCustomBookmarkReminderDate = "2019-12-11";
|
|
|
|
localStorage.lastCustomBookmarkReminderTime = "07:00";
|
|
|
|
|
2020-04-19 23:30:04 -04:00
|
|
|
BookmarkController._loadLastUsedCustomReminderDatetime();
|
2020-03-16 02:05:44 -04:00
|
|
|
|
|
|
|
assert.equal(BookmarkController.lastCustomReminderDate, null);
|
|
|
|
assert.equal(BookmarkController.lastCustomReminderTime, null);
|
|
|
|
}
|
|
|
|
);
|
2020-03-17 21:12:23 -04:00
|
|
|
|
2020-03-23 21:39:09 -04:00
|
|
|
QUnit.test("user timezone updates when the modal is shown", function(assert) {
|
|
|
|
User.current().changeTimezone(null);
|
|
|
|
let stub = sandbox.stub(moment.tz, "guess").returns("Europe/Moscow");
|
|
|
|
BookmarkController.onShow();
|
|
|
|
assert.equal(BookmarkController.userHasTimezoneSet, true);
|
|
|
|
assert.equal(
|
|
|
|
BookmarkController.userTimezone,
|
|
|
|
"Europe/Moscow",
|
|
|
|
"the user does not have their timezone set and a timezone is guessed"
|
|
|
|
);
|
|
|
|
User.current().changeTimezone("Australia/Brisbane");
|
|
|
|
BookmarkController.onShow();
|
|
|
|
assert.equal(BookmarkController.userHasTimezoneSet, true);
|
|
|
|
assert.equal(
|
|
|
|
BookmarkController.userTimezone,
|
|
|
|
"Australia/Brisbane",
|
|
|
|
"the user does their timezone set"
|
|
|
|
);
|
|
|
|
stub.restore();
|
|
|
|
});
|
2020-04-16 21:08:07 -04:00
|
|
|
|
|
|
|
QUnit.test(
|
|
|
|
"opening the modal with an existing bookmark with reminder at prefills the custom reminder type",
|
|
|
|
function(assert) {
|
|
|
|
let name = "test";
|
|
|
|
let reminderAt = "2020-05-15T09:45:00";
|
|
|
|
BookmarkController.model = { id: 1, name: name, reminderAt: reminderAt };
|
|
|
|
BookmarkController.onShow();
|
|
|
|
assert.equal(
|
|
|
|
BookmarkController.selectedReminderType,
|
|
|
|
REMINDER_TYPES.CUSTOM
|
|
|
|
);
|
|
|
|
assert.equal(BookmarkController.customReminderDate, "2020-05-15");
|
|
|
|
assert.equal(BookmarkController.customReminderTime, "09:45");
|
|
|
|
assert.equal(BookmarkController.model.name, name);
|
|
|
|
}
|
|
|
|
);
|