Module: Inertia::RSpec::RequestHelpers

Defined in:
lib/inertia/rspec.rb

Instance Method Summary collapse

Instance Method Details

#get(inertia: {}, headers: {}) ⇒ Object

Performs a GET request with optional Inertia partial reload headers.

Examples:

Request only specific props

get "/users/1", inertia: { only: [:user, :permissions] }

Request all props except some

get "/users/1", inertia: { except: :audit_log }

Parameters:

  • inertia (Hash) (defaults to: {})

    partial reload options

  • headers (Hash) (defaults to: {})

    additional request headers

Options Hash (inertia:):

  • :only (String, Symbol, Array<String, Symbol>)

    request only these props

  • :except (String, Symbol, Array<String, Symbol>)

    request all props except these

Raises:

  • (ArgumentError)

    if unknown options are passed in the inertia hash



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/inertia/rspec.rb', line 88

def get(*, inertia: {}, headers: {}, **)
  if inertia.except(:only, :except).any?
    raise ArgumentError, "Unknown :inertia option. Supported values are :only and :except"
  end

  if inertia.any?
    headers = headers.merge({
      "X-Inertia" => "true",
      "X-Inertia-Partial-Component" => self.inertia&.component || double(:== => true)
    })

    if only = inertia[:only]
      headers["X-Inertia-Partial-Data"] = Array(only).join(",")
    elsif except = inertia[:except]
      headers["X-Inertia-Partial-Except"] = Array(except).join(",")
    end
  end

  super(*, headers:, **)
end