From 6065f97bf287482cb7175fd42745aacb51bb66c4 Mon Sep 17 00:00:00 2001 From: Robert Fletcher Date: Fri, 27 Nov 2026 18:31:26 -0800 Subject: [PATCH] Keep stories unread through a form and Turbo Stream **What** The keep unread button is now a form that patches `/stories/:id/keep_unread`. The new `keep_unread` action saves the story and responds with a Turbo Stream that replaces the form, so the checkbox only changes once the save has worked. The form carries the saved read and keep unread state and is the `story` controller's `readState` target. When a new one connects, the controller copies that state onto the story's classes and announces the change for the unread count. Remove the `keep-unread-toggle` controller. Replace `StoryStarStream` with a generic `ReplaceStream` component used by both actions. **Why** Next step in moving story updates to server-confirmed forms, so the shared Stimulus controllers no longer talk to the server. **Notes** Merge the action button reset into their shared CSS rule, which also fixes its property order and lowers the stylelint baseline. Several system specs reloaded the page right after toggling keep unread. They now wait for the saved state first. --- .eslint_todo.ts | 8 +-- .stylelint_todo.yml | 4 +- app/assets/stylesheets/application.css | 17 +++--- app/components/replace_stream.rb | 22 +++++++ app/components/story.rb | 15 ++--- app/components/story_actions.rb | 11 +--- app/components/story_keep_unread.rb | 55 ++++++++++++++++++ app/components/story_star.rb | 9 ++- app/components/story_star_stream.rb | 23 -------- app/controllers/stories_controller.rb | 29 ++++++++-- app/javascript/controllers/index.ts | 3 - .../keep_unread_toggle_controller.ts | 54 ------------------ .../controllers/story_controller.ts | 25 +++++--- config/routes.rb | 1 + spec/components/story_keep_unread_spec.rb | 57 +++++++++++++++++++ spec/components/story_spec.rb | 6 +- .../controllers/story_controller_spec.ts | 30 +++++++--- spec/javascript/support/story_fixture.ts | 24 +++++++- spec/requests/stories_controller_spec.rb | 37 ++++++++++++ spec/system/keyboard_shortcuts_spec.rb | 1 + spec/system/stories_index_spec.rb | 16 +++--- 21 files changed, 294 insertions(+), 153 deletions(-) create mode 100644 app/components/replace_stream.rb create mode 100644 app/components/story_keep_unread.rb delete mode 100644 app/components/story_star_stream.rb delete mode 100644 app/javascript/controllers/keep_unread_toggle_controller.ts create mode 100644 spec/components/story_keep_unread_spec.rb diff --git a/.eslint_todo.ts b/.eslint_todo.ts index d512e0153..b786c984c 100644 --- a/.eslint_todo.ts +++ b/.eslint_todo.ts @@ -1,5 +1,5 @@ // This configuration was generated by `exe/eslint_autogen` -// on 2026-11-28 00:43:19 UTC. +// on 2026-11-28 02:17:20 UTC. // The point is for the user to remove these configuration records // one by one as the offenses are removed from the code base. @@ -15,10 +15,9 @@ const config: Linter.Config[] = [ "@stylistic/dot-location": "off", }, }, - // Offense count: 60 + // Offense count: 52 { files: [ - "app/javascript/controllers/keep_unread_toggle_controller.ts", "app/javascript/controllers/mark_all_as_read_controller.ts", "app/javascript/controllers/unread_count_controller.ts", "app/javascript/helpers/api.ts", @@ -44,10 +43,9 @@ const config: Linter.Config[] = [ "@stylistic/max-len": "off", }, }, - // Offense count: 9 + // Offense count: 8 { files: [ - "app/javascript/controllers/keep_unread_toggle_controller.ts", "spec/javascript/controllers/unread_count_controller_spec.ts", "spec/javascript/helpers/ensure_spec.ts", "spec/javascript/support/stimulus.ts", diff --git a/.stylelint_todo.yml b/.stylelint_todo.yml index e6959f1fd..311940d85 100644 --- a/.stylelint_todo.yml +++ b/.stylelint_todo.yml @@ -1,5 +1,5 @@ # This configuration was generated by `exe/stylelint_autogen` -# on 2026-02-19 04:45:04 UTC. +# on 2026-11-28 02:16:51 UTC. # The point is for the user to remove these configuration records # one by one as the offenses are removed from the code base. @@ -50,7 +50,7 @@ overrides: files: - app/assets/stylesheets/application.css - # Offense count: 68 + # Offense count: 67 - rules: { 'order/properties-order': null } files: - app/assets/stylesheets/application.css diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 5e35bd575..1f4a9e4db 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -264,31 +264,28 @@ li.story.open .story-lead { .story-keep-unread, .story-starred, .story-refresh { display: inline-block; + padding: 0; + margin-right: 20px; + color: inherit; cursor: pointer; - -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; - margin-right: 20px; + background: none; + border: 0; + -webkit-touch-callout: none; } li.story > turbo-frame { display: block; } -.story-refresh-form, .story-star-form { +.story-refresh-form, .story-star-form, .story-keep-unread-form { display: inline; } -.story-refresh, .story-starred { - padding: 0; - color: inherit; - background: none; - border: 0; -} - /* Turbo sets aria-busy on the refresh form while its request is in flight. */ .story-refresh-form[aria-busy="true"] .fa-refresh { animation: fa-spin 1s infinite linear; diff --git a/app/components/replace_stream.rb b/app/components/replace_stream.rb new file mode 100644 index 000000000..88d81a6a5 --- /dev/null +++ b/app/components/replace_stream.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +# A Turbo Stream response that replaces elements by id. `replacements` maps +# each target id to the component to render in its place. +class Components::ReplaceStream < Components::Base + register_element :turbo_stream + + attr_accessor :replacements + + def initialize(replacements) + super() + self.replacements = replacements + end + + def view_template + replacements.each do |target, component| + turbo_stream(action: "replace", target:) do + template { render(component) } + end + end + end +end diff --git a/app/components/story.rb b/app/components/story.rb index 63360a06e..98abc9358 100644 --- a/app/components/story.rb +++ b/app/components/story.rb @@ -7,7 +7,6 @@ class Components::Story < Components::Base # The star and refresh forms submit inside the story's frame: sample # stories skip them, and a failed refresh leaves the story as it was. ACTIONS = [ - "keep-unread-toggle:toggled->story#keepUnreadToggled", "submit->story#formSubmitted", "turbo:frame-missing->story#frameMissing" ].join(" ").freeze @@ -31,7 +30,7 @@ def classes def data { - controller: "story keep-unread-toggle", + controller: "story", action: ACTIONS, **values, story_list_target: "story", @@ -40,16 +39,10 @@ def data end def values - read_state = { is_read: story.is_read, keep_unread: story.keep_unread } { - **controller_values("story", **read_state), - **controller_values("keep_unread_toggle", **read_state) + story_id_value: story.id.to_s, + story_is_read_value: story.is_read.to_s, + story_keep_unread_value: story.keep_unread.to_s } end - - def controller_values(controller, **values) - { id: story.id, **values }.to_h do |name, value| - [:"#{controller}_#{name}_value", value.to_s] - end - end end diff --git a/app/components/story_actions.rb b/app/components/story_actions.rb index bfc5ae477..23bc73979 100644 --- a/app/components/story_actions.rb +++ b/app/components/story_actions.rb @@ -15,7 +15,7 @@ def view_template div(class: "story-actions-container") do div { span(class: "story-published") { story.pretty_date } } div(class: "story-actions") do - keep_unread_button + render(Components::StoryKeepUnread.new(story:)) render(Components::StoryStar.new(story:, placement: :actions)) refresh_button permalink_link @@ -25,15 +25,6 @@ def view_template private - def keep_unread_button - data = { action: "click->keep-unread-toggle#toggle:stop", **hotkey("m") } - div(class: "story-keep-unread", data:) do - icon = story.keep_unread ? "fa-check" : "fa-square-o" - i(class: "fa #{icon}", data: { keep_unread_toggle_target: "icon" }) - plain(" #{t('stories.keep_unread')}") - end - end - # Submits inside the story's Turbo Frame; see Components::StoryFrame. def refresh_button url = "/stories/#{story.id}/refresh" diff --git a/app/components/story_keep_unread.rb b/app/components/story_keep_unread.rb new file mode 100644 index 000000000..ff8117189 --- /dev/null +++ b/app/components/story_keep_unread.rb @@ -0,0 +1,55 @@ +# frozen_string_literal: true + +# The keep unread button for a story, which also answers the m hotkey. +# +# The button submits a form; the response is a Turbo Stream that replaces +# this form with the saved state. The form is the story controller's +# `readState` target, so when it is replaced the controller copies the saved +# read and keep unread state onto the story. +class Components::StoryKeepUnread < Components::Base + attr_accessor :story + + def self.dom_id(story) + "story_#{story.id}_keep_unread" + end + + # This button, by id, for a Turbo Stream response. + def self.replacements(story) + { dom_id(story) => new(story:) } + end + + def initialize(story:) + super() + self.story = story + end + + def view_template + url = "/stories/#{story.id}/keep_unread" + form_with(url:, method: :patch, class: "story-keep-unread-form", **attrs) do + value = (!story.keep_unread).to_s + input(type: "hidden", name: "keep_unread", value:) + keep_unread_button + end + end + + private + + def attrs + { + id: self.class.dom_id(story), + data: { + story_target: "readState", + is_read: story.is_read.to_s, + keep_unread: story.keep_unread.to_s + } + } + end + + def keep_unread_button + data = { hotkeys_target: "click", hotkey: "m" } + button(type: "submit", class: "story-keep-unread", data:) do + i(class: "fa #{story.keep_unread ? 'fa-check' : 'fa-square-o'}") + plain(" #{t('stories.keep_unread')}") + end + end +end diff --git a/app/components/story_star.rb b/app/components/story_star.rb index a76ecf01f..efb5d78e3 100644 --- a/app/components/story_star.rb +++ b/app/components/story_star.rb @@ -4,7 +4,7 @@ # one in its actions, which also answers the s hotkey. # # The button submits a form; the response is a Turbo Stream that replaces both -# buttons (see `Components::StoryStarStream`), so the icon only changes once +# buttons (see `StoriesController#star`), so the icon only changes once # the server has saved the star. class Components::StoryStar < Components::Base PLACEMENTS = [:preview, :actions].freeze @@ -15,6 +15,13 @@ def self.dom_id(story, placement) "story_#{story.id}_star_#{placement}" end + # Both of the story's star buttons, by id, for a Turbo Stream response. + def self.replacements(story) + PLACEMENTS.to_h do |placement| + [dom_id(story, placement), new(story:, placement:)] + end + end + def initialize(story:, placement:) super() self.story = story diff --git a/app/components/story_star_stream.rb b/app/components/story_star_stream.rb deleted file mode 100644 index 61088fc1a..000000000 --- a/app/components/story_star_stream.rb +++ /dev/null @@ -1,23 +0,0 @@ -# frozen_string_literal: true - -# The response to starring or unstarring a story: a Turbo Stream that -# replaces both of the story's star buttons with their updated state. -class Components::StoryStarStream < Components::Base - register_element :turbo_stream - - attr_accessor :story - - def initialize(story:) - super() - self.story = story - end - - def view_template - Components::StoryStar::PLACEMENTS.each do |placement| - target = Components::StoryStar.dom_id(story, placement) - turbo_stream(action: "replace", target:) do - template { render(Components::StoryStar.new(story:, placement:)) } - end - end - end -end diff --git a/app/controllers/stories_controller.rb b/app/controllers/stories_controller.rb index 1c1800c85..d1b7df2ff 100644 --- a/app/controllers/stories_controller.rb +++ b/app/controllers/stories_controller.rb @@ -33,16 +33,23 @@ def refresh render(Components::StoryFrame.new(story:), layout: false, status:) end - # Responds with a Turbo Stream that replaces the story's star buttons. + # Responds with a Turbo Stream that replaces both of the story's star + # buttons. def star story = authorization.check(StoryRepository.fetch(params[:id])) story.update!(is_starred: params[:starred] == "true") - render( - Components::StoryStarStream.new(story:), - layout: false, - content_type: "text/vnd.turbo-stream.html" - ) + render_replacements(Components::StoryStar.replacements(story)) + end + + # Keeping a story unread also marks it unread; turning it off marks it + # read. Responds with a Turbo Stream that replaces the keep unread button. + def keep_unread + story = authorization.check(StoryRepository.fetch(params[:id])) + keep_unread = params[:keep_unread] == "true" + story.update!(keep_unread:, is_read: !keep_unread) + + render_replacements(Components::StoryKeepUnread.replacements(story)) end def mark_all_as_read @@ -61,4 +68,14 @@ def starred stories = authorization.scope(StoryRepository.starred(params[:page])) render(Views::Stories::Starred.new(stories:)) end + + private + + def render_replacements(replacements) + render( + Components::ReplaceStream.new(replacements), + layout: false, + content_type: "text/vnd.turbo-stream.html" + ) + end end diff --git a/app/javascript/controllers/index.ts b/app/javascript/controllers/index.ts index 47fdc11ad..acc7ff93e 100644 --- a/app/javascript/controllers/index.ts +++ b/app/javascript/controllers/index.ts @@ -12,9 +12,6 @@ application.register("dialog", DialogController); import HotkeysController from "./hotkeys_controller"; application.register("hotkeys", HotkeysController); -import KeepUnreadToggleController from "./keep_unread_toggle_controller"; -application.register("keep-unread-toggle", KeepUnreadToggleController); - import MarkAllAsReadController from "./mark_all_as_read_controller"; application.register("mark-all-as-read", MarkAllAsReadController); diff --git a/app/javascript/controllers/keep_unread_toggle_controller.ts b/app/javascript/controllers/keep_unread_toggle_controller.ts deleted file mode 100644 index 816e678fb..000000000 --- a/app/javascript/controllers/keep_unread_toggle_controller.ts +++ /dev/null @@ -1,54 +0,0 @@ -import {Controller} from "@hotwired/stimulus"; - -import {updateStory} from "helpers/api"; - -export default class extends Controller { - static override values = {id: String, isRead: Boolean, keepUnread: Boolean}; - - static override targets = ["icon"]; - - declare idValue: string; - - declare isReadValue: boolean; - - declare keepUnreadValue: boolean; - - iconTargets!: HTMLElement[]; - - toggle(): void { - this.keepUnreadValue = !this.keepUnreadValue; - this.isReadValue = !this.keepUnreadValue; - - let icon = "fa fa-square-o"; - if (this.keepUnreadValue) { icon = "fa fa-check"; } - - for (const target of this.iconTargets) { - target.className = icon; - } - - this.updateStoryElement(); - this.dispatch("toggled", { - bubbles: true, - detail: {isRead: this.isReadValue, keepUnread: this.keepUnreadValue}, - }); - this.persistState(); - } - - private updateStoryElement(): void { - const storyEl = this.element.closest("li.story"); - storyEl?.classList.toggle("keepUnread", this.keepUnreadValue); - storyEl?.classList.toggle("read", this.isReadValue); - } - - private persistState(): void { - /* eslint-disable camelcase */ - const attrs = { - is_read: this.isReadValue, - keep_unread: this.keepUnreadValue, - }; - /* eslint-enable camelcase */ - updateStory(this.idValue, attrs).catch(() => { - // Optimistic UI — ignore server errors - }); - } -} diff --git a/app/javascript/controllers/story_controller.ts b/app/javascript/controllers/story_controller.ts index d92f67aee..6862cf201 100644 --- a/app/javascript/controllers/story_controller.ts +++ b/app/javascript/controllers/story_controller.ts @@ -2,8 +2,6 @@ import { Controller } from "@hotwired/stimulus"; import { updateStory } from "helpers/api"; -interface KeepUnreadDetail { isRead: boolean; keepUnread: boolean } - // Cmd, ctrl and middle clicks open the story in a background tab. function opensInTab(event: MouseEvent): boolean { return event.metaKey || event.ctrlKey || event.button === 1; @@ -17,8 +15,12 @@ function opensInTab(event: MouseEvent): boolean { export default class extends Controller { static override values = { id: String, isRead: Boolean, keepUnread: Boolean }; - // The permalink sits inside the story's Turbo Frame, so a refresh updates it. - static override targets = ["permalink"]; + /* + * The permalink sits inside the story's Turbo Frame, so a refresh updates + * it. The readState target is the keep unread form, which the server + * replaces with the saved read and keep unread state. + */ + static override targets = ["permalink", "readState"]; declare idValue: string; @@ -88,9 +90,18 @@ export default class extends Controller { }); } - keepUnreadToggled(event: CustomEvent): void { - this.isReadValue = event.detail.isRead; - this.keepUnreadValue = event.detail.keepUnread; + // Copies the saved state from a newly rendered keep unread form. + readStateTargetConnected(target: HTMLElement): void { + const isRead = target.dataset.isRead === "true"; + const keepUnread = target.dataset.keepUnread === "true"; + if (isRead === this.isReadValue && keepUnread === this.keepUnreadValue) { + return; + } + + this.isReadValue = isRead; + this.keepUnreadValue = keepUnread; + this.element.classList.toggle("read", isRead); + this.element.classList.toggle("keepUnread", keepUnread); this.dispatch("read-changed"); } diff --git a/config/routes.rb b/config/routes.rb index b36e9c981..78ce09bde 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -39,5 +39,6 @@ put "/stories/:id", to: "stories#update" post "/stories/:id/refresh", to: "stories#refresh" patch "/stories/:id/star", to: "stories#star" + patch "/stories/:id/keep_unread", to: "stories#keep_unread" post "/stories/mark_all_as_read", to: "stories#mark_all_as_read" end diff --git a/spec/components/story_keep_unread_spec.rb b/spec/components/story_keep_unread_spec.rb new file mode 100644 index 000000000..4b89bfc00 --- /dev/null +++ b/spec/components/story_keep_unread_spec.rb @@ -0,0 +1,57 @@ +# frozen_string_literal: true + +RSpec.describe Components::StoryKeepUnread do + def render_form(story) + html = + ApplicationController.render(described_class.new(story:), layout: false) + Capybara.string(html).find("form") + end + + it "patches the story's keep unread path" do + story = create(:story) + + expect(render_form(story)[:action]) + .to eq("/stories/#{story.id}/keep_unread") + end + + it "has an id the Turbo Stream response can replace" do + story = create(:story) + + expect(render_form(story)[:id]).to eq("story_#{story.id}_keep_unread") + end + + it "carries the saved read state for the story controller" do + form = render_form(create(:story, :read)) + + expect(form["data-story-target"]).to eq("readState") + end + + it "records whether the story is read and kept unread" do + form = render_form(create(:story, keep_unread: true)) + + expect([form["data-is-read"], form["data-keep-unread"]]) + .to eq(["false", "true"]) + end + + it "asks to keep a story unread when it isn't yet" do + expect(render_form(create(:story))) + .to have_field(name: "keep_unread", type: :hidden, with: "true") + end + + it "asks to stop keeping a story unread when it is" do + expect(render_form(create(:story, keep_unread: true))) + .to have_field(name: "keep_unread", type: :hidden, with: "false") + end + + it "shows a check when the story is kept unread" do + expect(render_form(create(:story, keep_unread: true))) + .to have_css("button.story-keep-unread .fa-check") + end + + it "answers the m hotkey" do + button = render_form(create(:story)).find("button") + + expect([button["data-hotkey"], button["data-hotkeys-target"]]) + .to eq(["m", "click"]) + end +end diff --git a/spec/components/story_spec.rb b/spec/components/story_spec.rb index 91fb2a5be..2f687dbb3 100644 --- a/spec/components/story_spec.rb +++ b/spec/components/story_spec.rb @@ -14,18 +14,18 @@ def story_li(story) it "sets the Stimulus controllers on the li" do li = story_li(create(:story)) - expect(li["data-controller"]).to eq("story keep-unread-toggle") + expect(li["data-controller"]).to eq("story") end def values(element) - names = ["story-id", "story-keep-unread", "keep-unread-toggle-is-read"] + names = ["story-id", "story-is-read", "story-keep-unread"] names.map { |name| element["data-#{name}-value"] } end it "sets the Stimulus values on the li" do story = create(:story, keep_unread: true) - expect(values(story_li(story))).to eq([story.id.to_s, "true", "false"]) + expect(values(story_li(story))).to eq([story.id.to_s, "false", "true"]) end it "wires the form submit and missing frame actions" do diff --git a/spec/javascript/controllers/story_controller_spec.ts b/spec/javascript/controllers/story_controller_spec.ts index b77d9f7ab..ba4ef3e94 100644 --- a/spec/javascript/controllers/story_controller_spec.ts +++ b/spec/javascript/controllers/story_controller_spec.ts @@ -3,6 +3,7 @@ import { element, listenForReadChanged, mockFetch, + replaceReadState, setupController, } from "support/story_fixture"; @@ -85,27 +86,38 @@ describe("markAsRead when it should not save", () => { }); }); -describe("keepUnreadToggled", () => { - function toggle(detail: { isRead: boolean; keepUnread: boolean }): void { - element().dispatchEvent( - new CustomEvent("keep-unread-toggle:toggled", { bubbles: true, detail }), - ); - } +describe("readStateTargetConnected", () => { + it("copies the saved state onto the story", async () => { + await setupController({ isRead: true }); + + await replaceReadState({ isRead: false, keepUnread: true }); + + expect([...element().classList]).toStrictEqual(["story", "keepUnread"]); + }); it("announces the change", async () => { await setupController({ isRead: true }); const listener = listenForReadChanged(); - toggle({ isRead: false, keepUnread: true }); + await replaceReadState({ isRead: false, keepUnread: true }); expect(listener).toHaveBeenCalledExactlyOnceWith(expect.any(CustomEvent)); }); + it("does nothing when the state is unchanged", async () => { + await setupController({ isRead: true }); + const listener = listenForReadChanged(); + + await replaceReadState({ isRead: true, keepUnread: false }); + + expect(listener).not.toHaveBeenCalled(); + }); + it("stops markAsRead while kept unread", async () => { await setupController({ isRead: true }); const fetchSpy = mockFetch(); - toggle({ isRead: false, keepUnread: true }); + await replaceReadState({ isRead: false, keepUnread: true }); controller().markAsRead(); expect(fetchSpy).not.toHaveBeenCalled(); @@ -115,7 +127,7 @@ describe("keepUnreadToggled", () => { await setupController({ keepUnread: true }); mockFetch(); - toggle({ isRead: false, keepUnread: false }); + await replaceReadState({ isRead: false, keepUnread: false }); controller().markAsRead(); expect(element().classList.contains("read")).toBe(true); diff --git a/spec/javascript/support/story_fixture.ts b/spec/javascript/support/story_fixture.ts index 1602da6dd..18f51752b 100644 --- a/spec/javascript/support/story_fixture.ts +++ b/spec/javascript/support/story_fixture.ts @@ -7,14 +7,25 @@ import { ensure } from "helpers/ensure"; interface Options { id?: string; isRead?: boolean; keepUnread?: boolean } +interface ReadState { isRead: boolean; keepUnread: boolean } + +// The keep unread form, as the server renders it with the saved state. +function readStateHTML({ isRead, keepUnread }: ReadState): string { + return [ + "
`, + " ", + "
", + ].join("\n"); +} + function setupDOM({ id = "42", isRead = false, keepUnread = false }: Options): void { // Static test fixture — safe to use innerHTML document.body.innerHTML = [ "
  • story#formSubmitted", " turbo:frame-missing->story#frameMissing'", ` data-story-id-value='${id}'`, ` data-story-is-read-value='${isRead}'`, @@ -25,6 +36,7 @@ void { " ", " ", "
    ", + readStateHTML({ isRead, keepUnread }), "
  • ", ].join("\n"); } @@ -63,6 +75,13 @@ function clickPreview(options: MouseEventInit = {}): void { ); } +// Swaps in a new keep unread form, as a Turbo Stream response would. +async function replaceReadState(state: ReadState): Promise { + const form = ensure(element().querySelector(".story-keep-unread-form")); + form.outerHTML = readStateHTML(state); + await Promise.resolve(); +} + function isOpen(): boolean { return element().classList.contains("open"); } @@ -75,5 +94,6 @@ export { listenForReadChanged, mockFetch, preview, + replaceReadState, setupController, }; diff --git a/spec/requests/stories_controller_spec.rb b/spec/requests/stories_controller_spec.rb index cf079a748..cf548461e 100644 --- a/spec/requests/stories_controller_spec.rb +++ b/spec/requests/stories_controller_spec.rb @@ -276,6 +276,43 @@ def star_targets(story) end end + describe "#keep_unread" do + def keep_unread(story, value) + patch( + "/stories/#{story.id}/keep_unread", + params: { keep_unread: value.to_s } + ) + end + + it "keeps the story unread" do + login_as(default_user) + story = create(:story, :read) + + expect { keep_unread(story, true) } + .to change_record(story, :keep_unread).from(false).to(true) + .and change_record(story, :is_read).from(true).to(false) + end + + it "marks the story read when it stops keeping it unread" do + login_as(default_user) + story = create(:story, keep_unread: true) + + expect { keep_unread(story, false) } + .to change_record(story, :keep_unread).from(true).to(false) + .and change_record(story, :is_read).from(false).to(true) + end + + it "replaces the keep unread button with a Turbo Stream" do + login_as(default_user) + story = create(:story) + + keep_unread(story, true) + + expect(rendered.find("turbo-stream", visible: :all)[:target]) + .to eq("story_#{story.id}_keep_unread") + end + end + describe "#mark_all_as_read" do it "marks all unread stories as read and reload the page" do login_as(default_user) diff --git a/spec/system/keyboard_shortcuts_spec.rb b/spec/system/keyboard_shortcuts_spec.rb index 38501a9db..9aaa5ea9b 100644 --- a/spec/system/keyboard_shortcuts_spec.rb +++ b/spec/system/keyboard_shortcuts_spec.rb @@ -120,6 +120,7 @@ def open_story_and_send(key) login_as(default_user) create_story_and_visit(title: "My Story") open_story_and_send("m") + expect(page).to have_css("li.story.cursor .story-keep-unread .fa-check") visit(news_path) expect(page).to have_text("My Story") diff --git a/spec/system/stories_index_spec.rb b/spec/system/stories_index_spec.rb index d9d35c85f..e9f3c4b2a 100644 --- a/spec/system/stories_index_spec.rb +++ b/spec/system/stories_index_spec.rb @@ -108,6 +108,7 @@ def mark_story_unread(story_title) find(".story-preview", text: story_title).click find(".story-actions .story-keep-unread").click + expect(page).to have_css(".story-actions .story-keep-unread .fa-check") end it "allows marking a story as unread" do @@ -129,16 +130,16 @@ def open_story_and_find_unread_icon(story_title) create(:story, title: "My Story") login_as(default_user) visit(news_path) + find(".story-preview", text: "My Story").click + icon = ".story-actions .story-keep-unread" - icon = open_story_and_find_unread_icon("My Story") - expect(icon[:class]).to include("fa-square-o") + expect(page).to have_css("#{icon} .fa-square-o") - find(".story-actions .story-keep-unread").click - expect(icon[:class]).to include("fa-check") - expect(icon[:class]).not_to include("fa-square-o") + find(icon).click + expect(page).to have_css("#{icon} .fa-check") - find(".story-actions .story-keep-unread").click - expect(icon[:class]).to include("fa-square-o") + find(icon).click + expect(page).to have_css("#{icon} .fa-square-o") end it "persists keep unread state across page reload" do @@ -148,6 +149,7 @@ def open_story_and_find_unread_icon(story_title) find(".story-preview", text: "My Story").click find(".story-actions .story-keep-unread").click + expect(page).to have_css(".story-actions .story-keep-unread .fa-check") visit(news_path) icon = open_story_and_find_unread_icon("My Story")