Skip to content

Commit

Permalink
All requests from ToggleSwitch are being made with a Turbo Accept h…
Browse files Browse the repository at this point in the history
…eader (#3012)
  • Loading branch information
orhantoy authored Aug 16, 2024
1 parent 205cb16 commit adcdddd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .changeset/quick-falcons-cheat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@primer/view-components": patch
---

Fix a bug where all requests from the `ToggleSwitch` view component are being made with a Turbo Accept header.
This started happening after the change in #2964 because `data-turbo=false` gets interpreted as a truthy value.
3 changes: 2 additions & 1 deletion app/components/primer/alpha/toggle_switch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ def before_render

@system_arguments[:data] = merge_data(
@system_arguments,
{ data: { csrf: @csrf_token, turbo: @turbo } }
{ data: { csrf: @csrf_token } }
)
@system_arguments[:data][:turbo] = true if @turbo
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/components/alpha/toggle_switch_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ def test_csrf_token
assert_selector("[data-csrf]")
end

def test_turbo_default
render_inline(Primer::Alpha::ToggleSwitch.new(src: "/foo"))

refute_selector("[data-turbo]")
end

def test_turbo
render_inline(Primer::Alpha::ToggleSwitch.new(src: "/foo", turbo: true))

Expand Down
10 changes: 10 additions & 0 deletions test/system/alpha/toggle_switch_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,17 @@ def test_fetch_made_with_turbo
assert_equal "text/vnd.turbo-stream.html", ToggleSwitchController.last_request.headers["HTTP_ACCEPT"]
end

def test_fetch_made_without_turbo
visit_preview(:default)

refute_selector(".ToggleSwitch--checked")
find("toggle-switch").click
assert_selector(".ToggleSwitch--checked")

wait_for_request

assert_equal "*/*", ToggleSwitchController.last_request.headers["HTTP_ACCEPT"]
end

private

Expand Down

0 comments on commit adcdddd

Please sign in to comment.