Skip to content

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

11.0 KB
Raw Blame
import { getOwner } from "@ember/owner";
import { click, render, waitFor } from "@ember/test-helpers";
import { module, test } from "qunit";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import DiscoursePostEvent from "discourse/plugins/discourse-events/discourse/components/discourse-post-event";
import DiscoursePostEventEvent from "discourse/plugins/discourse-events/discourse/models/discourse-post-event-event";

function buildEvent(overrides = {}) {
  return DiscoursePostEventEvent.create({
    id: 1,
    // 2026-06-04 is a Thursday
    starts_at: "2026-06-04T14:00:00Z",
    ends_at: "2026-06-04T15:00:00Z",
    timezone: "UTC",
    status: "public",
    name: "Product team office hours",
    post: { id: 1, url: "/t/foo/1", topic: { id: 1 } },
    creator: { username: "bob", id: 5 },
    should_display_invitees: false,
    sample_invitees: [],
    ...overrides,
  });
}

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

  function stubApi(event) {
    getOwner(this).unregister("service:discourse-post-event-api");
    getOwner(this).register(
      "service:discourse-post-event-api",
      {
        async event() {
          return event;
        },
      },
      { instantiate: false }
    );
  }

  test("read-only mode keeps the card but drops its controls", async function (assert) {
    const attrs = {
      can_update_attendance: true,
      stats: { going: 1, interested: 0, invited: 0, not_going: 0 },
      should_display_invitees: true,
    };

    const event = buildEvent(attrs);
    await render(<template><DiscoursePostEvent @event={{event}} /></template>);

    assert.dom(".going-button").exists("the interactive card offers an RSVP");
    assert.dom(".event-invitees").exists();
    assert
      .dom(
        '.event-header__controls [data-identifier="discourse-post-event-more-menu"]'
      )
      .exists();

    const readOnlyEvent = buildEvent(attrs);
    await render(
      <template>
        <DiscoursePostEvent @event={{readOnlyEvent}} @readOnly={{true}} />
      </template>
    );

    assert.dom(".event-date .day").exists("the date box is still shown");
    assert.dom(".event-dates").exists("the dates are still listed");
    assert.dom(".going-button").doesNotExist();
    assert.dom(".event-invitees").doesNotExist();
    assert
      .dom(
        '.event-header__controls [data-identifier="discourse-post-event-more-menu"]'
      )
      .doesNotExist();
  });

  test("read-only mode neither fetches details nor subscribes to updates", async function (assert) {
    let fetched = 0;
    getOwner(this).unregister("service:discourse-post-event-api");
    getOwner(this).register(
      "service:discourse-post-event-api",
      {
        async event() {
          fetched++;
          return buildEvent();
        },
      },
      { instantiate: false }
    );
    const messageBus = getOwner(this).lookup("service:message-bus");
    const subscribed = [];
    const originalSubscribe = messageBus.subscribe;
    messageBus.subscribe = (path) => subscribed.push(path);

    try {
      const event = buildEvent({ creator: null });
      await render(
        <template>
          <DiscoursePostEvent @event={{event}} @readOnly={{true}} />
        </template>
      );

      assert.dom(".event-date .day").exists("the card still renders");
      assert.strictEqual(fetched, 0, "a creator-less event is not refetched");
      assert.deepEqual(subscribed, [], "no message-bus subscription is made");
    } finally {
      messageBus.subscribe = originalSubscribe;
    }
  });

  test("interpolates the weekday into the recurrence label", async function (assert) {
    stubApi.call(this, buildEvent({ recurrence: "every_week" }));

    const event = { id: 1, startsAt: "2026-06-04T14:00:00Z" };
    await render(<template><DiscoursePostEvent @event={{event}} /></template>);
    await waitFor(".event-recurrence");

    assert
      .dom(".event-recurrence")
      .hasText(
        "Every Thursday",
        "renders the weekday instead of a missing placeholder"
      );
  });

  test("lists additional event hosts in a menu", async function (assert) {
    stubApi.call(
      this,
      buildEvent({
        hosts: [
          { id: 6, username: "alex" },
          { id: 7, username: "blake" },
          { id: 8, username: "casey" },
          { id: 9, username: "drew" },
        ],
      })
    );

    const event = { id: 1 };
    await render(<template><DiscoursePostEvent @event={{event}} /></template>);
    await waitFor(".event-hosts");

    assert
      .dom(".event-host")
      .exists({ count: 2 }, "shows the first two hosts initially");
    assert.dom(".event-hosts__toggle").hasText("and 2 others");

    await click(".event-hosts__toggle");

    assert
      .dom(".event-host")
      .exists({ count: 2 }, "keeps the row compact while the menu is open");
    assert.dom(".event-hosts-menu__title").hasText("4 hosts");
    assert
      .dom(".event-hosts-menu__host")
      .exists({ count: 4 }, "lists every host in the menu");
    assert
      .dom(".event-hosts-menu__host[data-user-card='drew']")
      .hasText("drew");
  });

  test("interpolates the ordinal and weekday into the monthly recurrence label", async function (assert) {
    stubApi.call(this, buildEvent({ recurrence: "every_month" }));

    const event = { id: 1, startsAt: "2026-06-04T14:00:00Z" };
    await render(<template><DiscoursePostEvent @event={{event}} /></template>);
    await waitFor(".event-recurrence");

    assert
      .dom(".event-recurrence")
      .hasText(
        "The first Thursday of every month",
        "renders both the ordinal and weekday placeholders"
      );
  });

  test("wires up the event image lightbox inside a topic", async function (assert) {
    stubApi.call(
      this,
      buildEvent({
        image_upload: { url: "/uploads/default/original/1X/event.png" },
      })
    );

    const event = { id: 1, startsAt: "2026-06-04T14:00:00Z" };
    await render(<template><DiscoursePostEvent @event={{event}} /></template>);
    await waitFor(".event-image a.lightbox");
    await click(".event-image a.lightbox");
    await waitFor(".pswp--open");

    assert.dom(".pswp--open").exists("opens the PhotoSwipe lightbox");

    await click(".pswp__button--close");
  });

  test("links the event image to the post when linkToPost is set", async function (assert) {
    stubApi.call(
      this,
      buildEvent({
        image_upload: { url: "/uploads/default/original/1X/event.png" },
      })
    );

    const event = { id: 1, startsAt: "2026-06-04T14:00:00Z" };
    await render(
      <template>
        <DiscoursePostEvent @event={{event}} @linkToPost={{true}} />
      </template>
    );
    await waitFor(".event-image");

    assert
      .dom(".event-image a.lightbox")
      .doesNotExist("does not wire up a lightbox in calendar contexts");
    assert
      .dom(".event-image a")
      .hasAttribute(
        "href",
        "/t/foo/1",
        "links the image to the event topic instead"
      );
  });

  test("derives the weekday from the date for all-day events, ignoring the timezone offset", async function (assert) {
    // Midnight UTC in a negative-offset timezone rolls back to the previous
    // day; an all-day event's weekday must come from the date, not the offset.
    stubApi.call(
      this,
      buildEvent({
        recurrence: "every_week",
        all_day: true,
        timezone: "America/New_York",
        starts_at: "2026-06-04T00:00:00Z",
      })
    );

    const event = { id: 1, startsAt: "2026-06-04T00:00:00Z" };
    await render(<template><DiscoursePostEvent @event={{event}} /></template>);
    await waitFor(".event-recurrence");

    assert
      .dom(".event-recurrence")
      .hasText("Every Thursday", "uses the date's weekday, not the prior day");
  });

  test("labels a fourth weekday as fourth even when the month has no fifth", async function (assert) {
    // 2026-02-26 is both the fourth and the final Thursday of February
    stubApi.call(
      this,
      buildEvent({
        recurrence: "every_month",
        starts_at: "2026-02-26T14:00:00Z",
        ends_at: "2026-02-26T15:00:00Z",
      })
    );

    const event = { id: 1, startsAt: "2026-02-26T14:00:00Z" };
    await render(<template><DiscoursePostEvent @event={{event}} /></template>);
    await waitFor(".event-recurrence");

    assert
      .dom(".event-recurrence")
      .hasText(
        "The fourth Thursday of every month",
        "matches the fourth-weekday rule the server generates"
      );
  });

  test("renders location and url side by side when they differ", async function (assert) {
    stubApi.call(
      this,
      buildEvent({
        location: "Discourse HQ, Denver",
        location_html: "Discourse HQ, Denver",
        url: "https://ti.to/discourse/meetup",
        url_restates_location: false,
      })
    );

    const event = { id: 1 };
    await render(<template><DiscoursePostEvent @event={{event}} /></template>);
    await waitFor(".event-location");

    assert.dom(".event-location").includesText("Discourse HQ, Denver");
    assert
      .dom(".event-url a")
      .hasAttribute(
        "href",
        "https://ti.to/discourse/meetup",
        "a venue plus a registration page is a legitimate pairing"
      );
  });

  test("shows a chat icon for events with a channel", async function (assert) {
    stubApi.call(
      this,
      buildEvent({
        channel: {
          id: 2,
          title: "Product team chat",
          slug: "product-team-chat",
          chatable_type: "Category",
          chatable: { color: "0088cc", read_restricted: false },
        },
      })
    );

    const event = { id: 1 };
    await render(<template><DiscoursePostEvent @event={{event}} /></template>);
    await waitFor(".event-chat-channel");

    assert
      .dom(".event-chat-channel > .d-icon-comment")
      .exists("shows the chat icon beside the channel link");
  });

  test("hides the url row when it carries the zoom livestream", async function (assert) {
    stubApi.call(
      this,
      buildEvent({
        url: "https://zoom.us/j/123",
        livestream: true,
        livestream_url: "https://zoom.us/j/123",
        is_zoom_livestream: true,
        url_restates_location: false,
      })
    );

    const event = { id: 1 };
    await render(<template><DiscoursePostEvent @event={{event}} /></template>);
    await waitFor(".event-header");

    assert
      .dom(".event-url")
      .doesNotExist(
        "the zoom join UI presents the link, like a location-carried livestream"
      );
  });

  test("renders the link once when url only restates location", async function (assert) {
    stubApi.call(
      this,
      buildEvent({
        location: "zoom.us/j/123",
        location_html:
          '<a href="http://zoom.us/j/123" rel="nofollow ugc">zoom.us/j/123</a>',
        url: "https://zoom.us/j/123/",
        url_restates_location: true,
      })
    );

    const event = { id: 1 };
    await render(<template><DiscoursePostEvent @event={{event}} /></template>);
    await waitFor(".event-location");

    assert
      .dom(".event-url")
      .doesNotExist("the same link is not worth two rows");
  });
});