Skip to content

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

9.8 KB
Raw Blame
import {
  fillIn,
  findAll,
  render,
  triggerEvent,
  typeIn,
} from "@ember/test-helpers";
import { module, test } from "qunit";
import { setupRenderingTest } from "discourse/tests/helpers/component-test";
import CompactEventEditor from "discourse/plugins/discourse-events/discourse/components/compact-event-editor";
import { defaultEventState } from "discourse/plugins/discourse-events/discourse/lib/raw-event-helper";

async function renderEditor(initialState, onChange = () => {}) {
  await render(
    <template>
      <CompactEventEditor
        @initialState={{initialState}}
        @onChange={{onChange}}
      />
    </template>
  );
}

function stateWith(overrides) {
  return {
    ...defaultEventState(),
    name: "My event",
    timezone: "UTC",
    startsAt: moment("2026-07-01T10:00:00Z"),
    endsAt: moment("2026-07-01T11:00:00Z"),
    ...overrides,
  };
}

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

  test("exposes an existing url so it can be edited", async function (assert) {
    const initialState = stateWith({ url: "https://meet.example.com/legacy" });
    await renderEditor(initialState);

    assert
      .dom(".composer-event__url-input")
      .hasValue(
        "https://meet.example.com/legacy",
        "a defined url is reachable on the compact screen, not just in advanced"
      );
  });

  test("offers a recording field once a livestream has ended", async function (assert) {
    await renderEditor(
      stateWith({
        livestream: true,
        startsAt: moment("2020-07-01T10:00:00Z"),
        endsAt: moment("2020-07-01T11:00:00Z"),
      })
    );

    assert.dom(".composer-event__recording-input").exists();
  });

  test("offers no recording field before a livestream has ended", async function (assert) {
    await renderEditor(
      stateWith({
        livestream: true,
        startsAt: moment("2099-07-01T10:00:00Z"),
        endsAt: moment("2099-07-01T11:00:00Z"),
      })
    );

    assert.dom(".composer-event__recording-input").doesNotExist();
  });

  test("emits the recording as it is typed", async function (assert) {
    let state;
    await renderEditor(
      stateWith({
        livestream: true,
        startsAt: moment("2020-07-01T10:00:00Z"),
        endsAt: moment("2020-07-01T11:00:00Z"),
      }),
      (newState) => (state = newState)
    );

    await fillIn(".composer-event__recording-input", "https://youtu.be/abc");

    assert.strictEqual(state.recording, "https://youtu.be/abc");
  });

  test("offers no url row for an event that has none", async function (assert) {
    const initialState = stateWith({ location: "Room 5" });
    await renderEditor(initialState);

    assert
      .dom(".composer-event__url-input")
      .doesNotExist("adding a url is an advanced-screen action");
    assert
      .dom(".composer-event__location-input")
      .hasAttribute(
        "placeholder",
        "Add location or URL",
        "location covers both meanings while it is the only link field"
      );
  });

  test("clearing the url keeps the row so it can be retyped", async function (assert) {
    const initialState = stateWith({ url: "https://meet.example.com/legacy" });
    let lastState = null;
    await renderEditor(initialState, (state) => (lastState = state));

    await fillIn(".composer-event__url-input", "");

    assert.strictEqual(
      lastState.url,
      null,
      "the cleared value is emitted so buildParams drops it"
    );
    assert
      .dom(".composer-event__url-input")
      .exists("the input is not yanked out from under the cursor");
  });

  test("offers the livestream toggle when the url carries the livestream link", async function (assert) {
    this.siteSettings.chat_enabled = true;

    const initialState = stateWith({ url: "https://zoom.us/j/123456789" });
    await renderEditor(initialState);

    assert
      .dom(".composer-event__livestream")
      .exists("a livestream link is recognized in either field");
  });

  test("does not offer livestream for a venue with a companion link", async function (assert) {
    this.siteSettings.chat_enabled = true;

    const initialState = stateWith({
      location: "Room 5",
      url: "https://zoom.us/j/123456789",
    });
    await renderEditor(initialState);

    assert
      .dom(".composer-event__livestream")
      .doesNotExist("the location takes precedence as the livestream source");
  });

  test("keeps the livestream flag when only the companion url changes", async function (assert) {
    this.siteSettings.chat_enabled = true;

    const initialState = stateWith({
      location: "https://zoom.us/j/123456789",
      url: "https://example.com/tickets",
      livestream: true,
    });
    let lastState = null;
    await renderEditor(initialState, (state) => (lastState = state));

    await fillIn(".composer-event__url-input", "https://example.com/register");

    assert
      .dom(".composer-event__livestream")
      .exists("the location still carries the stream");
    assert.true(lastState.livestream, "the flag survives a companion url edit");
  });

  test("ignores a whitespace-only location when the url carries the stream", async function (assert) {
    this.siteSettings.chat_enabled = true;

    const initialState = stateWith({
      url: "https://zoom.us/j/123456789",
      livestream: true,
    });
    let lastState = null;
    await renderEditor(initialState, (state) => (lastState = state));

    await fillIn(".composer-event__location-input", " ");

    assert
      .dom(".composer-event__livestream")
      .exists("a blank location does not shadow the url");
    assert.true(
      lastState.livestream,
      "the flag matches what the save path would keep"
    );
  });

  test("drops the livestream flag when the url stops being the livestream", async function (assert) {
    this.siteSettings.chat_enabled = true;

    const initialState = stateWith({
      url: "https://zoom.us/j/123456789",
      livestream: true,
    });
    let lastState = null;
    await renderEditor(initialState, (state) => (lastState = state));

    await fillIn(".composer-event__url-input", "https://example.com/tickets");

    assert.false(
      lastState.livestream,
      "livestream is reset so it is not submitted for a non-livestream url"
    );
  });

  test("narrows the location placeholder once url has its own row", async function (assert) {
    const initialState = stateWith({
      location: "Room 5",
      url: "https://meet.example.com/legacy",
    });
    await renderEditor(initialState);

    assert
      .dom(".composer-event__location-input")
      .hasAttribute(
        "placeholder",
        "Add location",
        "location stops advertising URLs once url is a separate row"
      );
  });

  test("defaults the end date to the day after the start date", async function (assert) {
    const initialState = stateWith({
      allDay: true,
      startsAt: moment("2027-07-14T00:00:00Z"),
      endsAt: null,
    });
    let lastState = null;
    await renderEditor(initialState, (state) => (lastState = state));

    assert
      .dom(findAll(".composer-event__date-input")[1])
      .hasAttribute("min", "2027-07-14");

    const endDateInput = findAll(".composer-event__date-input")[1];
    endDateInput.showPicker = () => {};
    await triggerEvent(endDateInput, "focus");

    assert.strictEqual(lastState.endsAt.format("YYYY-MM-DD"), "2027-07-15");
    assert.dom(endDateInput).hasValue("2027-07-15");

    const startDateInput = findAll(".composer-event__date-input")[0];
    startDateInput.value = "2027-08-20";
    await triggerEvent(startDateInput, "change");

    assert
      .dom(findAll(".composer-event__date-input")[1])
      .hasAttribute("min", "2027-08-20");
  });

  test("clamps end an hour after start when the picked end time is not after start", async function (assert) {
    const initialState = stateWith({
      endsAt: moment("2026-07-01T12:00:00Z"),
    });
    let lastState = null;
    await renderEditor(initialState, (state) => (lastState = state));

    await fillIn(findAll(".composer-event__time-input")[1], "10:00");

    assert.strictEqual(
      lastState.endsAt.toISOString(),
      "2026-07-01T11:00:00.000Z",
      "an end time at or before start is pushed an hour past start"
    );
  });

  test("the end date picker is bounded by the start date", async function (assert) {
    const initialState = stateWith({
      allDay: true,
      startsAt: moment("2026-07-01"),
      endsAt: null,
    });
    await renderEditor(initialState);

    assert
      .dom(findAll(".composer-event__date-input")[1])
      .hasAttribute(
        "min",
        "2026-07-01",
        "days before the start are greyed out"
      );
  });

  test("picking the start day as the end day keeps a one-day all-day event", async function (assert) {
    const initialState = stateWith({
      allDay: true,
      startsAt: moment("2026-07-01"),
      endsAt: null,
    });
    let lastState = null;
    await renderEditor(initialState, (state) => (lastState = state));

    await fillIn(findAll(".composer-event__date-input")[1], "2026-07-01");

    assert.strictEqual(
      lastState.endsAt.format("YYYY-MM-DD"),
      "2026-07-01",
      "an equal boundary is a valid one-day range"
    );
    assert
      .dom(findAll(".composer-event__date-input")[1])
      .hasValue("2026-07-01", "the picked day stays in the field");
  });

  test("typing a multi-digit reminder value keeps every digit", async function (assert) {
    const initialState = stateWith({
      reminders: [
        { type: "notification", value: 1, unit: "minutes", period: "before" },
      ],
    });
    let lastState = null;
    await renderEditor(initialState, (state) => (lastState = state));

    await typeIn(".composer-event__reminder-value", "55");

    assert.strictEqual(
      lastState.reminders[0].value,
      155,
      "each keystroke lands in the reminder value"
    );
  });
});