Skip to content

A platform for community discussion. Free, open, simple.

5.4 KB
Raw Blame
import { getOwner } from "@ember/owner";
import { render, settled } from "@ember/test-helpers";
import { module, test } from "qunit";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import { fakeTime } from "discourse/tests/helpers/qunit-helpers";
import EventDate from "discourse/plugins/discourse-events/discourse/components/event-date";

module("Integration | Component | EventDate", function (hooks) {
  setupRenderingTest(hooks);

  hooks.beforeEach(function () {
    this.siteSettings.discourse_post_event_enabled = true;
    this.siteSettings.use_local_event_date = true;
    this.originalGuess = moment.tz.guess;
  });

  hooks.afterEach(function () {
    moment.tz.guess = this.originalGuess;
    this.clock?.restore();
  });

  test("uses event timezone when show_local_time is true", async function (assert) {
    moment.tz.guess = () => "Africa/Harare";
    this.clock = fakeTime("2026-02-02T12:00:00Z", "Africa/Harare", true);

    // 15:00 UTC on Feb 2 is:
    // - Feb 3, 02:00 in Sydney (UTC+11)
    // - Feb 2, 17:00 in Harare (UTC+2)
    // So the date should show Feb 3 (Sydney) not Feb 2 (Harare)
    const topic = {
      event_starts_at: "2026-02-02T15:00:00.000Z",
      event_timezone: "Australia/Sydney",
      event_show_local_time: true,
    };

    await render(<template><EventDate @topic={{topic}} /></template>);

    assert
      .dom(".event-date")
      .hasAttribute(
        "title",
        /February 3, 2026/,
        "displays Feb 3 (Sydney timezone) not Feb 2 (browser Harare timezone)"
      );
  });

  test("uses browser timezone when show_local_time is false", async function (assert) {
    moment.tz.guess = () => "Africa/Harare";
    this.clock = fakeTime("2026-02-02T12:00:00Z", "Africa/Harare", true);

    // 15:00 UTC on Feb 2 is:
    // - Feb 3, 02:00 in Sydney (UTC+11)
    // - Feb 2, 17:00 in Harare (UTC+2)
    // When show_local_time is false, should use browser timezone (Harare)
    const topic = {
      event_starts_at: "2026-02-02T15:00:00.000Z",
      event_timezone: "Australia/Sydney",
      event_show_local_time: false,
    };

    await render(<template><EventDate @topic={{topic}} /></template>);

    assert
      .dom(".event-date")
      .hasAttribute(
        "title",
        /February 2, 2026.*5:00 PM/,
        "displays Feb 2 (browser Harare timezone) not Feb 3 (event Sydney timezone)"
      );
  });

  test("falls back to browser timezone when event has no timezone", async function (assert) {
    moment.tz.guess = () => "Africa/Harare";
    this.clock = fakeTime("2026-02-02T12:00:00Z", "Africa/Harare", true);

    // 15:00 UTC on Feb 2 is Feb 2, 17:00 in Harare (UTC+2)
    const topic = {
      event_starts_at: "2026-02-02T15:00:00.000Z",
    };

    await render(<template><EventDate @topic={{topic}} /></template>);

    assert
      .dom(".event-date")
      .hasAttribute(
        "title",
        /February 2, 2026.*5:00 PM/,
        "displays date in browser timezone (Harare) when no event timezone"
      );
  });

  test("updates as time passes without topic changes", async function (assert) {
    this.siteSettings.use_local_event_date = false;
    moment.tz.guess = () => "UTC";
    this.clock = fakeTime("2026-09-04T13:12:00Z", "UTC", true);

    const topic = {
      event_starts_at: "2026-09-04T13:11:00.000Z",
      event_ends_at: "2026-09-04T13:13:00.000Z",
    };

    await render(<template><EventDate @topic={{topic}} /></template>);

    assert
      .dom(".event-relative-date.current")
      .exists("shows the event as current before it ends");

    this.clock.tick(2 * 60 * 1000);
    getOwner(this).lookup("service:a11y").autoUpdatingRelativeDateRef =
      new Date();

    await settled();

    assert
      .dom(".event-relative-date.past")
      .exists("updates to past after the event ends");
  });

  test("updates when topic event dates are rehydrated", async function (assert) {
    moment.tz.guess = () => "UTC";
    this.clock = fakeTime("2026-09-04T12:28:00Z", "UTC", true);

    const store = getOwner(this).lookup("service:store");
    const topic = store.createRecord("topic", {
      id: 1234,
      event_starts_at: "2026-09-04T11:25:00.000Z",
      event_ends_at: "2026-09-04T11:27:00.000Z",
    });

    await render(<template><EventDate @topic={{topic}} /></template>);

    assert
      .dom(".event-date")
      .hasAttribute(
        "title",
        /September 4, 2026/,
        "shows the initial event date"
      );

    store.createRecord("topic", {
      id: 1234,
      event_starts_at: "2026-09-11T11:25:00.000Z",
      event_ends_at: "2026-09-11T11:27:00.000Z",
    });

    await settled();

    assert
      .dom(".event-date")
      .hasAttribute(
        "title",
        /September 11, 2026/,
        "rerenders when the existing topic is rehydrated"
      );
  });

  test("all-day event does not shift date across timezones", async function (assert) {
    // Use a western timezone where UTC midnight would shift to the previous day
    moment.tz.guess = () => "America/Chicago";
    this.clock = fakeTime("2026-03-11T12:00:00Z", "America/Chicago", true);

    const topic = {
      event_starts_at: "2026-03-12",
      event_ends_at: "2026-03-14",
      event_all_day: true,
    };

    await render(<template><EventDate @topic={{topic}} /></template>);

    assert
      .dom(".event-date")
      .hasAttribute(
        "title",
        /March 12, 2026/,
        "displays March 12 (correct date) not March 11 (timezone-shifted)"
      );
  });
});