Module: Inertia::RSpec::RequestHelpers

Defined in:
lib/inertia/rspec.rb

Instance Method Summary collapse

Instance Method Details

#get(inertia: nil, 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: nil)

    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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/inertia/rspec.rb', line 97

def get(*, inertia: nil, headers: {}, **)
  if inertia
    inertia = {} unless inertia.is_a?(Hash)

    if inertia.except(:only, :except).any?
      raise ArgumentError, "Unknown :inertia option. Supported values are :only and :except"
    end

    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