Skip to content

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

16.7 KB
Raw Blame
# frozen_string_literal: true

describe "Composer - Drafts" do
  fab!(:topic, :topic_with_op)
  fab!(:current_user, :admin)

  let(:toasts) { PageObjects::Components::Toasts.new }
  let(:topic_page) { PageObjects::Pages::Topic.new }
  let(:composer) { PageObjects::Components::Composer.new }
  let(:discard_draft_modal) { PageObjects::Modals::DiscardDraft.new }

  before { sign_in(current_user) }

  context "when clicking X (save and close)" do
    it "saves the draft and shows a toast" do
      visit "/new-topic"

      expect(composer).to be_opened
      composer.fill_title("this is a test topic")
      composer.fill_content("a b c d e f g")
      composer.close

      expect(toasts).to have_success(I18n.t("js.composer.draft_saved"))
      expect(Draft.where(user: current_user).count).to eq(1)
    end

    context "when only a title and category is specified" do
      fab!(:category_1, :category)
      fab!(:category_2, :category)

      it "saves the draft and shows a toast" do
        visit "/new-topic"

        expect(composer).to be_opened
        composer.fill_title("this is a test topic")
        composer.switch_category(category_1.name)
        composer.close

        expect(toasts).to have_success(I18n.t("js.composer.draft_saved"))
        expect(Draft.where(user: current_user).count).to eq(1)
      end
    end

    context "when only title is specified and it is less than min_topic_title_length" do
      it "does saves the draft and shows a toast" do
        visit "/new-topic"

        expect(composer).to be_opened
        composer.fill_title("x")
        composer.close
        expect(composer).to be_closed

        expect(toasts).to have_success(I18n.t("js.composer.draft_saved"))
        expect(Draft.where(user: current_user).count).to eq(1)
      end
    end

    context "when only body is specified and it is less than min_post_length" do
      it "does saves the draft and shows a toast" do
        visit "/new-topic"

        expect(composer).to be_opened
        composer.fill_content("x")
        composer.close
        expect(composer).to be_closed

        expect(toasts).to have_success(I18n.t("js.composer.draft_saved"))
        expect(Draft.where(user: current_user).count).to eq(1)
      end
    end
  end

  context "when starting a topic with the keyboard shortcut on a topic page" do
    let(:dialog) { PageObjects::Components::Dialog.new }
    let(:drafts_page) { PageObjects::Pages::UserActivityDrafts.new }

    it "lets the user save a new topic draft next to an older one" do
      # an older topic draft on the shared key, whose sequence has moved on
      DraftSequence.next!(current_user, Draft::NEW_TOPIC)
      Draft.set(
        current_user,
        Draft::NEW_TOPIC,
        1,
        { title: "An older topic draft", reply: "Started before the new one" }.to_json,
      )

      topic_page.visit_topic(topic)
      send_keys("c")

      expect(composer).to be_opened
      composer.fill_title("A new topic started from a topic page")
      composer.fill_content("This draft must not conflict with the older one")
      composer.close

      expect(toasts).to have_success(I18n.t("js.composer.draft_saved"))
      expect(dialog).to be_closed

      drafts_page.visit(current_user)
      expect(drafts_page).to have_drafts(count: 2)
    end
  end

  context "when clicking discard" do
    let(:dialog) { PageObjects::Components::Dialog.new }

    before { Jobs.run_immediately! }

    it "does not show confirmation if there is no user input in the composer" do
      visit "/new-topic"

      expect(composer).to be_opened
      composer.discard

      expect(discard_draft_modal).to be_closed
      expect(composer).to be_closed
    end

    it "destroys draft after discard confirmation" do
      visit "/new-topic"

      composer.fill_title("this is a test topic")
      composer.fill_content("a b c d e f g")

      try_until_success(reason: "Relies on an Ember debounce to update the draft") do
        expect(Draft.where(user: current_user).count).to eq(1)
      end

      composer.discard

      expect(discard_draft_modal).to be_open
      discard_draft_modal.click_discard

      expect(discard_draft_modal).to be_closed
      expect(composer).to be_closed
      expect(Draft.where(user: current_user).count).to eq(0)
    end

    context "when only a title and category is specified" do
      fab!(:category_1, :category)
      fab!(:category_2, :category)

      it "shows Discard draft confirmation modal and hides it on Cancel button click" do
        visit "/new-topic"

        expect(composer).to be_opened
        composer.fill_title("this is a test topic")
        composer.switch_category(category_1.name)
        composer.discard

        expect(discard_draft_modal).to be_open
        discard_draft_modal.click_cancel

        expect(discard_draft_modal).to be_closed
      end
    end
  end

  context "when discarding while editing a post" do
    fab!(:post_1) { Fabricate(:post, topic:, user: current_user) }

    before { Jobs.run_immediately! }

    it "does not show the discard modal when there are no changes" do
      topic_page.visit_topic(post_1.topic)
      topic_page.click_post_action_button(post_1, :edit)

      expect(composer).to be_opened
      composer.discard

      expect(discard_draft_modal).to be_closed
      expect(composer).to be_closed
    end

    it "does not show the discard modal when editing the first post with no changes" do
      first_post = topic.first_post
      first_post.update!(user: current_user)

      topic_page.visit_topic(topic)
      topic_page.click_post_action_button(first_post, :edit)

      expect(composer).to be_opened
      composer.discard

      expect(discard_draft_modal).to be_closed
      expect(composer).to be_closed
    end

    it "shows the discard modal with editing-specific text when there are changes" do
      topic_page.visit_topic(post_1.topic)
      topic_page.click_post_action_button(post_1, :edit)
      composer.fill_content("edited content here")
      composer.discard

      expect(discard_draft_modal).to be_open
      expect(page).to have_css(
        ".discard-draft-modal",
        text: I18n.t("js.post.cancel_composer.confirm_edit"),
      )
      expect(page).to have_css(
        ".discard-draft-modal__discard-btn",
        text: I18n.t("js.post.cancel_composer.discard_edit"),
      )
    end

    it "shows the discard modal with editing-specific text when editing the first post" do
      first_post = topic.first_post
      first_post.update!(user: current_user)

      topic_page.visit_topic(topic)
      topic_page.click_post_action_button(first_post, :edit)
      composer.fill_content("edited content here")
      composer.discard

      expect(discard_draft_modal).to be_open
      expect(page).to have_css(
        ".discard-draft-modal",
        text: I18n.t("js.post.cancel_composer.confirm_edit"),
      )
    end

    it "shows the cancel edit button instead of discard" do
      topic_page.visit_topic(post_1.topic)
      topic_page.click_post_action_button(post_1, :edit)

      expect(composer).to be_opened
      expect(page).to have_css(".discard-button", text: I18n.t("js.composer.cancel_edit"))
    end
  end

  context "when discarding while creating a post" do
    before { Jobs.run_immediately! }

    it "shows the discard modal with post-specific text when there are changes" do
      visit "/new-topic"

      composer.fill_title("this is a test topic")
      composer.fill_content("a b c d e f g")
      composer.discard

      expect(discard_draft_modal).to be_open
      expect(page).to have_css(
        ".discard-draft-modal",
        text: I18n.t("js.post.cancel_composer.confirm"),
      )
      expect(page).to have_css(
        ".discard-draft-modal__discard-btn",
        text: I18n.t("js.post.cancel_composer.discard"),
      )
    end

    it "shows the discard button" do
      visit "/new-topic"

      expect(composer).to be_opened
      expect(page).to have_css(".discard-button", text: I18n.t("js.composer.discard"))
    end
  end

  context "when editing different post" do
    fab!(:post_1) { Fabricate(:post, topic:, user: current_user) }
    fab!(:post_2) { Fabricate(:post, topic:, user: current_user) }

    it "shows the discard modal when there are changes in the composer" do
      topic_page.visit_topic(post_1.topic)
      topic_page.click_post_action_button(post_1, :edit)
      composer.fill_content("a b c d e f g")
      composer.minimize

      topic_page.click_post_action_button(post_2, :edit)

      expect(discard_draft_modal).to be_open
    end

    it "doesn't show the discard modal when there are no changes in the composer" do
      topic_page.visit_topic(post_1.topic)
      topic_page.click_post_action_button(post_1, :edit)
      composer.minimize

      topic_page.click_post_action_button(post_2, :edit)

      expect(discard_draft_modal).to be_closed
      expect(composer).to be_opened
    end
  end

  context "when editing the same post" do
    fab!(:post_1) { Fabricate(:post, topic:, user: current_user) }

    it "doesn’t show the discard modal even if there are changes in the composer" do
      topic_page.visit_topic(post_1.topic)
      topic_page.click_post_action_button(post_1, :edit)
      composer.fill_content("a b c d e f g")
      composer.minimize

      topic_page.click_post_action_button(post_1, :edit)

      expect(discard_draft_modal).to be_closed
      expect(composer).to be_opened
      expect(composer).to have_content("a b c d e f g")

      composer.minimize
      expect(composer).to be_minimized

      topic_page.click_post_action_button(post_1, :edit)

      expect(discard_draft_modal).to be_closed
      expect(composer).to be_opened
    end

    it "doesn’t show the discard modal when there are no changes in the composer" do
      topic_page.visit_topic(post_1.topic)
      topic_page.click_post_action_button(post_1, :edit)
      composer.minimize

      topic_page.click_post_action_button(post_1, :edit)

      expect(discard_draft_modal).to be_closed
      expect(composer).to be_opened
    end
  end

  context "when resuming a saved edit draft" do
    fab!(:post_1) { Fabricate(:post, topic:, user: current_user, raw: "original post content") }
    fab!(:post_2) { Fabricate(:post, topic:, user: current_user, raw: "another post content") }

    before do
      topic_page.visit_topic(topic)
      topic_page.click_post_action_button(post_1, :edit)
      composer.fill_content("edited post content")
      composer.close

      expect(toasts).to have_success(I18n.t("js.composer.draft_saved"))
    end

    it "lets the user finish their edit after clicking reply" do
      topic_page.click_reply_button

      expect(composer).to have_content("edited post content")
      expect(composer.button_label).to have_text(I18n.t("js.composer.save_edit"))

      composer.create

      expect(composer).to be_closed
      expect(topic_page).to have_post_content(
        post_number: post_1.post_number,
        content: "edited post content",
      )
    end

    it "shows the user their saved edit when they edit the same post again" do
      topic_page.click_post_action_button(post_1, :edit)

      expect(composer).to have_content("edited post content")
      expect(composer.button_label).to have_text(I18n.t("js.composer.save_edit"))
    end

    it "shows the user the original content when they edit a different post" do
      topic_page.click_post_action_button(post_2, :edit)

      expect(composer).to have_content("another post content")
    end
  end

  context "when replying to a different topic with an active draft" do
    fab!(:other_topic, :topic_with_op)

    let(:topic_reply_choice_dialog) { PageObjects::Components::TopicReplyChoiceDialog.new }
    let(:topic_list) { PageObjects::Components::TopicList.new }

    before do
      topic.first_post.update!(raw: "This is the original topic OP content.")
      topic.first_post.rebake!

      other_topic.first_post.update!(raw: "This is the other topic OP content.")
      other_topic.first_post.rebake!
    end

    def visit_topic_and_save_draft
      topic_page.visit_topic(topic)
      topic_page.click_reply_button

      expect(composer).to be_opened
      composer.fill_content("a b c d e f g")

      composer.close

      expect(toasts).to have_success(I18n.t("js.composer.draft_saved"))
      expect(Draft.where(user: current_user).count).to eq(1)

      topic_page.visit_topic(topic)
      topic_page.click_reply_button

      expect(composer).to be_opened
      expect(composer).to have_content("a b c d e f g")
    end

    context "when clicking the original topic in the topic reply choice dialog" do
      it "replies with the current content to the original topic" do
        visit_topic_and_save_draft

        # We have to navigate by clicking through the app to keep the
        # composer open.
        click_logo
        expect(topic_list).to have_topic(other_topic)
        topic_list.visit_topic(other_topic)

        expect(topic_page).to have_post_content(
          post_number: 1,
          content: "This is the other topic OP content.",
        )

        composer.create

        expect(topic_reply_choice_dialog).to be_open
        expect(topic_reply_choice_dialog).to have_reply_on_original_topic(topic)
        topic_reply_choice_dialog.click_reply_on_original

        expect(topic_reply_choice_dialog).to be_closed
        expect(composer).to be_closed

        expect(topic_page).to have_post_content(
          post_number: 1,
          content: "This is the original topic OP content.",
        )
        expect(topic_page).to have_post_content(post_number: 2, content: "a b c d e f g")
        expect(topic.reload.posts.last.raw).to eq("a b c d e f g")
      end
    end

    context "when clicking the new topic in the topic reply choice dialog" do
      it "replies with the current content to the new topic" do
        visit_topic_and_save_draft

        # We have to navigate by clicking through the app to keep the
        # composer open.
        click_logo
        expect(topic_list).to have_topic(other_topic)
        topic_list.visit_topic(other_topic)

        expect(topic_page).to have_post_content(
          post_number: 1,
          content: "This is the other topic OP content.",
        )

        composer.create

        expect(topic_reply_choice_dialog).to be_open
        expect(topic_reply_choice_dialog).to have_reply_here_topic(other_topic)
        topic_reply_choice_dialog.click_reply_here

        expect(topic_reply_choice_dialog).to be_closed
        expect(composer).to be_closed

        expect(topic_page).to have_post_content(
          post_number: 1,
          content: "This is the other topic OP content.",
        )
        expect(topic_page).to have_post_content(post_number: 2, content: "a b c d e f g")
        expect(other_topic.reload.posts.last.raw).to eq("a b c d e f g")
      end
    end

    context "when clicking Cancel in the topic reply choice dialog" do
      it "saves the current draft and will save future changes to the draft" do
        visit_topic_and_save_draft

        # We have to navigate by clicking through the app to keep the
        # composer open.
        click_logo
        expect(topic_list).to have_topic(other_topic)
        topic_list.visit_topic(other_topic)

        expect(topic_page).to have_post_content(
          post_number: 1,
          content: "This is the other topic OP content.",
        )

        composer.create

        expect(topic_reply_choice_dialog).to be_open
        topic_reply_choice_dialog.click_cancel
        expect(topic_reply_choice_dialog).to be_closed

        composer.fill_content("This is my updated draft content, wow very impressive.")

        try_until_success(reason: "Relies on waiting a few seconds for the draft to autosave") do
          draft = Draft.where(user: current_user).first
          expect(JSON.parse(draft.data)["reply"]).to eq(
            "This is my updated draft content, wow very impressive.",
          )
        end
      end
    end
  end

  context "when replying as a linked topic" do
    fab!(:category)

    it "does not leave a stale reply draft after posting a linked topic" do
      topic_page.visit_topic(topic)
      topic_page.click_reply_button

      expect(composer).to be_opened
      composer.fill_content("this is a reply draft")

      try_until_success(reason: "wait for draft to be saved") do
        expect(Draft.where(user: current_user).count).to eq(1)
      end

      composer.open_composer_actions
      composer.select_action_by_id("reply_as_new_topic")

      expect(composer).to be_opened
      composer.fill_title("This is a linked topic title for testing")
      composer.fill_content("this is a linked topic body content for testing")
      composer.switch_category(category.name)
      composer.create

      expect(composer).to be_closed
      expect(Topic.last.title).to eq("This is a linked topic title for testing")
      expect(Draft.where(user: current_user).count).to eq(0)
    end
  end
end