|
1 | 1 | RSpec.describe 'errors shared automatically', type: :request do
|
| 2 | + context 'always_include_errors_hash configuration' do |
| 3 | + let(:server_version) { 1.0 } |
| 4 | + let(:headers) { { 'X-Inertia' => true } } |
| 5 | + |
| 6 | + after { InertiaRails.configure { |c| c.always_include_errors_hash = nil } } |
| 7 | + |
| 8 | + context 'when always_include_errors_hash is true' do |
| 9 | + before { InertiaRails.configure { |c| c.always_include_errors_hash = true } } |
| 10 | + |
| 11 | + it 'includes empty errors hash when no errors present' do |
| 12 | + get empty_test_path, headers: headers |
| 13 | + expect(response.body).to include({ errors: {} }.to_json) |
| 14 | + end |
| 15 | + |
| 16 | + it 'still includes actual errors when they exist' do |
| 17 | + post redirect_with_inertia_errors_path, headers: headers |
| 18 | + get response.headers['Location'], headers: headers |
| 19 | + expect(response.body).to include({ errors: { uh: 'oh' } }.to_json) |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + context 'when always_include_errors_hash is false' do |
| 24 | + before { InertiaRails.configure { |c| c.always_include_errors_hash = false } } |
| 25 | + |
| 26 | + it 'does not include errors hash when no errors present' do |
| 27 | + get empty_test_path, headers: headers |
| 28 | + expect(response.body).not_to include('"errors"') |
| 29 | + end |
| 30 | + |
| 31 | + it 'still includes actual errors when they exist' do |
| 32 | + post redirect_with_inertia_errors_path, headers: headers |
| 33 | + get response.headers['Location'], headers: headers |
| 34 | + expect(response.body).to include({ errors: { uh: 'oh' } }.to_json) |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + context 'when always_include_errors_hash is nil (default)' do |
| 39 | + before { InertiaRails.configure { |c| c.always_include_errors_hash = nil } } |
| 40 | + |
| 41 | + it 'shows deprecation warning and does not include empty errors hash' do |
| 42 | + expect { get empty_test_path, headers: headers } |
| 43 | + .to output(/To comply with the Inertia protocol/).to_stderr |
| 44 | + expect(response.body).not_to include('"errors"') |
| 45 | + end |
| 46 | + |
| 47 | + it 'still includes actual errors when they exist' do |
| 48 | + post redirect_with_inertia_errors_path, headers: headers |
| 49 | + get response.headers['Location'], headers: headers |
| 50 | + expect(response.body).to include({ errors: { uh: 'oh' } }.to_json) |
| 51 | + end |
| 52 | + end |
| 53 | + end |
| 54 | + |
2 | 55 | context 'rendering errors across redirects' do
|
3 | 56 | let(:server_version){ 1.0 }
|
4 | 57 | let(:headers){ { 'X-Inertia' => true, 'X-Inertia-Version' => server_version } }
|
|
45 | 98 | expect(response.body).to include(CGI::escape_html({ errors: { uh: 'oh' } }.to_json))
|
46 | 99 | expect(session[:inertia_errors]).not_to be
|
47 | 100 | end
|
| 101 | + |
| 102 | + context 'with partial update' do |
| 103 | + let(:headers) do |
| 104 | + { |
| 105 | + 'X-Inertia' => true, |
| 106 | + 'X-Inertia-Version' => server_version, |
| 107 | + 'X-Inertia-Partial-Component' => 'EmptyTestComponent', |
| 108 | + 'X-Inertia-Partial-Data' => 'foo', |
| 109 | + } |
| 110 | + end |
| 111 | + |
| 112 | + it 'keeps errors when partial inertia request redirects' do |
| 113 | + post redirect_with_inertia_errors_path, headers: headers |
| 114 | + expect(response.headers['Location']).to eq(empty_test_url) |
| 115 | + expect(session[:inertia_errors]).to include({ uh: 'oh' }) |
| 116 | + |
| 117 | + # Follow the redirect |
| 118 | + get response.headers['Location'], headers: headers |
| 119 | + expect(response.body).to include({ errors: { uh: 'oh' } }.to_json) |
| 120 | + expect(session[:inertia_errors]).not_to be |
| 121 | + end |
| 122 | + end |
48 | 123 | end
|
49 | 124 | end
|
0 commit comments