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/time-utils-test.js
Martin Brennan 3e3f3f7b7e
DEV: Add time shortcut picker component and libs and refactor bookmark modal controller into component which uses time shortcut picker (#11802)
This PR moves all of the time picking functionality from the bookmark modal and controller into a reusable time-shortcut-picker component, which will be used for the topic timer UI revamp. All of the utility JS for getting dates like tomorrow/next week/next month etc. have also been moved into a separate utility lib.

The time-shortcut-picker has a couple of options that can be passed in:

* prefilledDatetime - The date and time to parse and prefill into the custom date and time section, useful for editing interfaces.
* onTimeSelected (callback) - Called when one of the time shortcuts is clicked, and passes the type of the shortcut (e.g. tomorrow) and the datetime selected.
* additionalOptionsToShow - An array of option ids to show (by default `later_today` and `later_this_week` are hidden)
* hiddenOptions - An array of option ids to hide
* customOptions - An array of custom options to display (e.g. the option to select a post date for the bookmarks modal). The options should have the below properties:
    * id
    * icon
    * label (I18n key)
    * time (moment datetime object)
    * timeFormatted
    * hidden

The other major work in this PR is moving all of the bookmark functionality out of the bookmark modal controller and into its own component, where it makes more sense to be able to access elements on the page via `document`. Tests have been added to accompany this move, and existing acceptance tests for bookmark are all passing.
2021-02-01 09:03:41 +10:00

108 lines
2.8 KiB
JavaScript

import {
discourseModule,
fakeTime,
} from "discourse/tests/helpers/qunit-helpers";
import {
laterThisWeek,
laterToday,
nextMonth,
nextWeek,
startOfDay,
tomorrow,
} from "discourse/lib/time-utils";
import { test } from "qunit";
const timezone = "Australia/Brisbane";
function mockMomentTz(dateString) {
fakeTime(dateString, timezone);
}
discourseModule("Unit | lib | timeUtils", function () {
test("nextWeek gets next week correctly", function (assert) {
mockMomentTz("2019-12-11T08:00:00");
assert.equal(nextWeek(timezone).format("YYYY-MM-DD"), "2019-12-18");
});
test("nextMonth gets next month correctly", function (assert) {
mockMomentTz("2019-12-11T08:00:00");
assert.equal(nextMonth(timezone).format("YYYY-MM-DD"), "2020-01-11");
});
test("laterThisWeek gets 2 days from now", function (assert) {
mockMomentTz("2019-12-10T08:00:00");
assert.equal(laterThisWeek(timezone).format("YYYY-MM-DD"), "2019-12-12");
});
test("tomorrow gets tomorrow correctly", function (assert) {
mockMomentTz("2019-12-11T08:00:00");
assert.equal(tomorrow(timezone).format("YYYY-MM-DD"), "2019-12-12");
});
test("startOfDay changes the time of the provided date to 8:00am correctly", function (assert) {
let dt = moment.tz("2019-12-11T11:37:16", timezone);
assert.equal(
startOfDay(dt).format("YYYY-MM-DD HH:mm:ss"),
"2019-12-11 08:00:00"
);
});
test("laterToday gets 3 hours from now and if before half-past, it rounds down", function (assert) {
mockMomentTz("2019-12-11T08:13:00");
assert.equal(
laterToday(timezone).format("YYYY-MM-DD HH:mm:ss"),
"2019-12-11 11:00:00"
);
});
test("laterToday gets 3 hours from now and if after half-past, it rounds up to the next hour", function (assert) {
mockMomentTz("2019-12-11T08:43:00");
assert.equal(
laterToday(timezone).format("YYYY-MM-DD HH:mm:ss"),
"2019-12-11 12:00:00"
);
});
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(
laterToday(timezone).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(
laterToday(timezone).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(
laterToday(timezone).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(
laterToday(timezone).format("YYYY-MM-DD HH:mm:ss"),
"2019-12-11 18:00:00",
"4:59pm should max to 6pm"
);
});
});