module JSONAPI::Testing::StiHelpers

def expect_sti_resources_in_data(sti_type:, expected_ids:)

Other tags:
    Example: Verify specific messages are in data -

Returns:
  • (void) -

Parameters:
  • expected_ids (Array) -- Expected resource IDs
  • sti_type (String, Symbol) -- The STI subtype
def expect_sti_resources_in_data(sti_type:, expected_ids:)
  resources = find_sti_resources_in_data(sti_type)
  actual_ids = resources.map { |r| r["id"] }
  expected_ids_str = expected_ids.map(&:to_s)
  expect(actual_ids).to match_array(expected_ids_str),
                        "Expected #{sti_type} resources with IDs #{expected_ids_str}, got: #{actual_ids}"
end

def expect_sti_resources_included(sti_type:, expected_ids:)

Other tags:
    Example: Verify specific messages are included -

Returns:
  • (void) -

Parameters:
  • expected_ids (Array) -- Expected resource IDs
  • sti_type (String, Symbol) -- The STI subtype
def expect_sti_resources_included(sti_type:, expected_ids:)
  resources = find_sti_resources_in_included(sti_type)
  actual_ids = resources.map { |r| r["id"] }
  expected_ids_str = expected_ids.map(&:to_s)
  expect(actual_ids).to match_array(expected_ids_str),
                        "Expected #{sti_type} resources with IDs #{expected_ids_str}, got: #{actual_ids}"
end

def expect_sti_types_in_data(expected_subtypes:, exclude_base_type: nil)

Other tags:
    Example: Verify data contains correct STI types -

Returns:
  • (void) -

Parameters:
  • exclude_base_type (String, Symbol, nil) -- Base type that should NOT be present (optional)
  • expected_subtypes (Array) -- Expected STI subtype names
def expect_sti_types_in_data(expected_subtypes:, exclude_base_type: nil)
  data = response.parsed_body["data"]
  data = [data] unless data.is_a?(Array)
  types = data.pluck("type").uniq
  expected_subtypes.each do |subtype|
    expect(types).to include(subtype.to_s),
                     "Expected data to contain STI subtype '#{subtype}', got: #{types}"
  end
  return if exclude_base_type.blank?
  expect(types).not_to include(exclude_base_type.to_s),
                       "Expected data NOT to contain base type '#{exclude_base_type}', but it did"
end

def expect_sti_types_in_included(expected_subtypes:, exclude_base_type: nil)

Other tags:
    Example: Verify subtypes and exclude base type -
    Example: Verify subtypes are present -

Returns:
  • (void) -

Parameters:
  • exclude_base_type (String, Symbol, nil) -- Base type that should NOT be present (optional)
  • expected_subtypes (Array) -- Expected STI subtype names
def expect_sti_types_in_included(expected_subtypes:, exclude_base_type: nil)
  included = response.parsed_body["included"] || []
  included_types = included.pluck("type").uniq
  expected_subtypes.each do |subtype|
    expect(included_types).to include(subtype.to_s),
                              "Expected included to contain STI type '#{subtype}', got: #{included_types}"
  end
  return if exclude_base_type.blank?
  expect(included_types).not_to include(exclude_base_type.to_s),
                                "Expected included NOT to contain base type '#{exclude_base_type}', but it did"
end

def find_sti_resources_in_data(sti_type)

Other tags:
    Example: Find all user messages in data -

Returns:
  • (Array) - Array of resource objects matching the STI type

Parameters:
  • sti_type (String, Symbol) -- The STI subtype to find
def find_sti_resources_in_data(sti_type)
  data = response.parsed_body["data"]
  data = [data] unless data.is_a?(Array)
  data.select { |item| item["type"] == sti_type.to_s }
end

def find_sti_resources_in_included(sti_type)

Other tags:
    Example: Find all user messages in included -

Returns:
  • (Array) - Array of resource objects matching the STI type

Parameters:
  • sti_type (String, Symbol) -- The STI subtype to find
def find_sti_resources_in_included(sti_type)
  included = response.parsed_body["included"] || []
  included.select { |item| item["type"] == sti_type.to_s }
end