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/timeframes-builder-test.js
Andrei Prigorshnev 62029ec4eb
DEV: remove the includeMidFuture option on future-date-input (#15818)
This option was always on. Essentially, we set it only in two places and always use `|| true` with it.

Note that we're going to switch future-date-input-selector to another source of time shortcuts(https://github.com/discourse/discourse/blob/main/app/assets/javascripts/discourse/app/lib/time-shortcut.js) and also change its API to make it more customizable. Removing the includeMidFuture option is a part of that change.
2022-02-04 21:33:11 +04:00

152 lines
4.5 KiB
JavaScript

import { module, test } from "qunit";
import { fakeTime } from "discourse/tests/helpers/qunit-helpers";
import buildTimeframes from "discourse/lib/timeframes-builder";
const DEFAULT_OPTIONS = {
includeWeekend: null,
includeFarFuture: null,
includeDateTime: null,
canScheduleNow: false,
};
function buildOptions(now, opts) {
return Object.assign(
{},
DEFAULT_OPTIONS,
{ now, day: now.day(), canScheduleToday: 24 - now.hour() > 6 },
opts
);
}
module("Unit | Lib | timeframes-builder", function (hooks) {
hooks.afterEach(function () {
if (this.clock) {
this.clock.restore();
}
});
test("default options", function (assert) {
const timezone = moment.tz.guess();
this.clock = fakeTime("2100-06-07T08:00:00", timezone, true); // Monday
const expected = [
"later_today",
"tomorrow",
"next_week",
"two_weeks",
"next_month",
"two_months",
"three_months",
"four_months",
"six_months",
];
assert.deepEqual(
buildTimeframes(buildOptions(moment())).mapBy("id"),
expected
);
});
test("doesn't output 'Next Week' on Sundays", function (assert) {
const timezone = moment.tz.guess();
this.clock = fakeTime("2100-06-13T08:00:00", timezone, true); // Sunday
assert.ok(
!buildTimeframes(buildOptions(moment())).mapBy("id").includes("next_week")
);
});
test("outputs 'This Weekend' if it's enabled", function (assert) {
const timezone = moment.tz.guess();
this.clock = fakeTime("2100-06-07T08:00:00", timezone, true); // Monday
assert.ok(
buildTimeframes(buildOptions(moment(), { includeWeekend: true }))
.mapBy("id")
.includes("this_weekend")
);
});
test("doesn't output 'This Weekend' on Fridays", function (assert) {
const timezone = moment.tz.guess();
this.clock = fakeTime("2100-04-23 18:00:00", timezone, true); // Friday
assert.ok(
!buildTimeframes(buildOptions(moment(), { includeWeekend: true }))
.mapBy("id")
.includes("this_weekend")
);
});
test("doesn't show 'This Weekend' on Sundays", function (assert) {
/*
We need this test to avoid regressions.
We tend to write such conditions and think that
they mean the beginning of work week
(Monday, Tuesday and Wednesday in this specific case):
if (date.day <= 3) {
...
}
In fact, Sunday will pass this check too, because
in moment.js 0 stands for Sunday.
*/
const timezone = moment.tz.guess();
this.clock = fakeTime("2100-04-25 18:00:00", timezone, true); // Sunday
assert.ok(
!buildTimeframes(buildOptions(moment(), { includeWeekend: true }))
.mapBy("id")
.includes("this_weekend")
);
});
test("outputs 'Later This Week' instead of 'Later Today' at the end of the day", function (assert) {
const timezone = moment.tz.guess();
this.clock = fakeTime("2100-04-19 18:00:00", timezone, true); // Monday evening
const timeframes = buildTimeframes(buildOptions(moment())).mapBy("id");
assert.not(timeframes.includes("later_today"));
assert.ok(timeframes.includes("later_this_week"));
});
test("doesn't output 'Later This Week' on Tuesdays", function (assert) {
const timezone = moment.tz.guess();
this.clock = fakeTime("2100-04-22 18:00:00", timezone, true); // Tuesday evening
const timeframes = buildTimeframes(buildOptions(moment())).mapBy("id");
assert.not(timeframes.includes("later_this_week"));
});
test("doesn't output 'Later This Week' on Sundays", function (assert) {
/*
We need this test to avoid regressions.
We tend to write such conditions and think that
they mean the beginning of business week
(Monday, Tuesday and Wednesday in this specific case):
if (date.day < 3) {
...
}
In fact, Sunday will pass this check too, because
in moment.js 0 stands for Sunday.
*/
const timezone = moment.tz.guess();
this.clock = fakeTime("2100-04-25 18:00:00", timezone, true); // Sunday evening
const timeframes = buildTimeframes(buildOptions(moment())).mapBy("id");
assert.not(timeframes.includes("later_this_week"));
});
test("doesn't output 'Next Month' on the last day of the month", function (assert) {
const timezone = moment.tz.guess();
this.clock = fakeTime("2100-04-30 18:00:00", timezone, true); // The last day of April
const timeframes = buildTimeframes(buildOptions(moment())).mapBy("id");
assert.not(timeframes.includes("next_month"));
});
});