import { setupTest } from "ember-qunit"; import { module, test } from "qunit"; import freezeTime from "../helpers/freeze-time"; import { applyLocalDates } from "../initializers/discourse-local-dates"; module("Unit | discourse-local-dates", function (hooks) { setupTest(hooks); function createElementFromHTML(htmlString) { const div = document.createElement("div"); div.innerHTML = htmlString.trim(); // we need "element", not "node", since `.dataset` isn't available on nodes return div.firstElementChild; } const fromElement = () => createElementFromHTML( "' + "" ); const toElement = () => createElementFromHTML( "' + "" ); test("applyLocalDates sets formatted relative time", function (assert) { const from = fromElement(); const to = toElement(); const dateElements = [from, to]; freezeTime( { date: "2022-10-07T10:10:10", timezone: "Asia/Singapore" }, () => { applyLocalDates(dateElements, { discourse_local_dates_enabled: true, }); assert.dom(".relative-time", from).hasText("Yesterday 5:21 PM"); assert.dom(".relative-time", to).hasText("10:22 PM (Singapore)"); } ); }); test("applyLocalDates does not fail when a date element has no time", function (assert) { const from = fromElement(); const to = toElement(); delete to.dataset.time; const dateElements = [from, to]; freezeTime( { date: "2022-10-07T10:10:10", timezone: "Asia/Singapore" }, () => { applyLocalDates(dateElements, { discourse_local_dates_enabled: true, }); assert.dom(".relative-time", from).hasText("Yesterday 5:21 PM"); assert.dom(".relative-time", to).hasText("Yesterday"); } ); }); });