Skip to content

Commit a8771e9

Browse files
committed
Try to handle browser JSON responses more robustly
Adds `assert_json_response` and `json_response` helper method. Sticking to Capybara's API for getting document text should (in theory) make this less likely to break in the event of a browser changing how it renders plain text/JSON responses.
1 parent 7567de1 commit a8771e9

File tree

4 files changed

+47
-35
lines changed

4 files changed

+47
-35
lines changed

test/system/alpha/action_menu_test.rb

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class IntegrationActionMenuTest < System::TestCase
77
include Primer::ClipboardTestHelpers
88
include Primer::JsTestHelpers
99
include Primer::KeyboardTestHelpers
10+
include Primer::JsonResponseTestHelpers
1011

1112
###### HELPER METHODS ######
1213

@@ -409,9 +410,8 @@ def test_single_select_form_submission
409410

410411
find("input[type=submit]").click
411412

412-
# for some reason the JSON response is wrapped in HTML, I have no idea why
413-
response = JSON.parse(find("pre").text)
414-
assert_equal "fast_forward", response["value"]
413+
assert_json_response
414+
assert_equal "fast_forward", json_response["value"]
415415
end
416416

417417
def test_single_select_form_uses_label_if_no_value_provided
@@ -422,9 +422,8 @@ def test_single_select_form_uses_label_if_no_value_provided
422422

423423
find("input[type=submit]").click
424424

425-
# for some reason the JSON response is wrapped in HTML, I have no idea why
426-
response = JSON.parse(find("pre").text)
427-
assert_equal "Resolve", response["value"]
425+
assert_json_response
426+
assert_equal "Resolve", json_response["value"]
428427
end
429428

430429
def test_multiple_select_form_submission
@@ -439,11 +438,10 @@ def test_multiple_select_form_submission
439438

440439
find("input[type=submit]").click
441440

442-
# for some reason the JSON response is wrapped in HTML, I have no idea why
443-
response = JSON.parse(find("pre").text)
441+
assert_json_response
444442

445443
# "ours" is pre-selected
446-
assert_equal %w[fast_forward recursive ours], response["value"]
444+
assert_equal %w[fast_forward recursive ours], json_response["value"]
447445
end
448446

449447
def test_multiple_select_form_uses_label_if_no_value_provided
@@ -458,11 +456,10 @@ def test_multiple_select_form_uses_label_if_no_value_provided
458456

459457
find("input[type=submit]").click
460458

461-
# for some reason the JSON response is wrapped in HTML, I have no idea why
462-
response = JSON.parse(find("pre").text)
459+
assert_json_response
463460

464461
# "ours" is pre-selected
465-
assert_equal %w[fast_forward ours Resolve], response["value"]
462+
assert_equal %w[fast_forward ours Resolve], json_response["value"]
466463
end
467464

468465
def test_multiple_select_does_not_set_dynamic_label_for_preselected_item
@@ -476,8 +473,8 @@ def test_individual_items_can_submit_post_requests_via_forms
476473
click_on_invoker_button
477474
click_on_fourth_item
478475

479-
response = JSON.parse(find("pre").text)
480-
assert_equal "bar", response["value"]
476+
assert_json_response
477+
assert_equal "bar", json_response["value"]
481478
end
482479

483480
def test_single_select_items_can_submit_forms
@@ -486,9 +483,8 @@ def test_single_select_items_can_submit_forms
486483
click_on_invoker_button
487484
click_on_first_item
488485

489-
# for some reason the JSON response is wrapped in HTML, I have no idea why
490-
response = JSON.parse(find("pre").text)
491-
assert_equal "group-by-repository", response["value"]
486+
assert_json_response
487+
assert_equal "group-by-repository", json_response["value"]
492488
end
493489

494490
def test_single_select_items_can_submit_forms_on_enter
@@ -499,9 +495,8 @@ def test_single_select_items_can_submit_forms_on_enter
499495
# "click" first item
500496
keyboard.type(:enter)
501497

502-
# for some reason the JSON response is wrapped in HTML, I have no idea why
503-
response = JSON.parse(find("pre").text)
504-
assert_equal "group-by-repository", response["value"]
498+
assert_json_response
499+
assert_equal "group-by-repository", json_response["value"]
505500
end
506501

507502
def test_single_select_items_can_submit_forms_on_keydown_space
@@ -512,9 +507,8 @@ def test_single_select_items_can_submit_forms_on_keydown_space
512507
# "click" first item
513508
keyboard.type(:space)
514509

515-
# for some reason the JSON response is wrapped in HTML, I have no idea why
516-
response = JSON.parse(find("pre").text)
517-
assert_equal "group-by-repository", response["value"]
510+
assert_json_response
511+
assert_equal "group-by-repository", json_response["value"]
518512
end
519513

520514
def test_single_select_items_can_submit_forms_with_multiple_fields
@@ -523,9 +517,8 @@ def test_single_select_items_can_submit_forms_with_multiple_fields
523517
click_on_invoker_button
524518
click_on_first_item
525519

526-
# for some reason the JSON response is wrapped in HTML, I have no idea why
527-
response = JSON.parse(find("pre").text)
528-
assert_equal "query", response.dig("other_params", "query")
520+
assert_json_response
521+
assert_equal "query", json_response.dig("other_params", "query")
529522
end
530523

531524
def test_deferred_loading

test/system/alpha/select_panel_test.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ module Alpha
77
class IntegrationSelectPanelTest < System::TestCase
88
include Primer::KeyboardTestHelpers
99
include Primer::JsTestHelpers
10+
include Primer::JsonResponseTestHelpers
1011

1112
###### HELPER METHODS ######
1213

@@ -1221,9 +1222,8 @@ def test_single_select_form
12211222

12221223
click_on "Submit"
12231224

1224-
# for some reason the JSON response is wrapped in HTML, I have no idea why
1225-
response = JSON.parse(find("pre").text)
1226-
assert_equal "item2", response.dig(*%w(form_params item))
1225+
assert_json_response
1226+
assert_equal "item2", json_response.dig(*%w(form_params item))
12271227
end
12281228

12291229
def test_single_select_form_submits_pre_selected_item
@@ -1232,9 +1232,8 @@ def test_single_select_form_submits_pre_selected_item
12321232
# the first item has been pre-selected, so there's no need to select any items
12331233
click_on "Submit"
12341234

1235-
# for some reason the JSON response is wrapped in HTML, I have no idea why
1236-
response = JSON.parse(find("pre").text)
1237-
assert_equal "item1", response.dig(*%w(form_params item))
1235+
assert_json_response
1236+
assert_equal "item1", json_response.dig(*%w(form_params item))
12381237
end
12391238

12401239
def test_multi_select_form
@@ -1246,11 +1245,10 @@ def test_multi_select_form
12461245

12471246
click_on "Submit"
12481247

1249-
# for some reason the JSON response is wrapped in HTML, I have no idea why
1250-
response = JSON.parse(find("pre").text)
1248+
assert_json_response
12511249

12521250
# first item is pre-selected
1253-
assert_equal ["item1", "item2"], response.dig(*%w(form_params item))
1251+
assert_equal ["item1", "item2"], json_response.dig(*%w(form_params item))
12541252
end
12551253

12561254
########## ANNOUNCEMENT TESTS ############

test/test_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
require "test_helpers/mouse_test_helpers"
2626
require "test_helpers/js_test_helpers"
2727
require "test_helpers/window_test_helpers"
28+
require "test_helpers/json_response_test_helpers"
2829
require "action_controller/railtie"
2930
require "rails/test_unit/railtie"
3031
require "active_model/railtie"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# frozen_string_literal: true
2+
3+
module Primer
4+
module JsonResponseTestHelpers
5+
def assert_json_response
6+
assert_current_path(/\.json$/)
7+
assert_equal json_mime_type, page.evaluate_script("document.contentType")
8+
end
9+
10+
def json_response
11+
@json_response ||= JSON.parse(page.document.text)
12+
end
13+
14+
private
15+
16+
def json_mime_type
17+
@json_mime_type ||= Mime::Type.lookup("application/json")
18+
end
19+
end
20+
end

0 commit comments

Comments
 (0)