Module: InertiaRails::Testing::Assertions
- Defined in:
- lib/inertia_rails/testing.rb
Class Method Summary collapse
- .validate_component(inertia, expected) ⇒ Object
- .validate_deferred_props(inertia, *expected_keys, group: nil) ⇒ Object
- .validate_exact_match(inertia, field, expected) ⇒ Object
- .validate_inertia_response(inertia) ⇒ Object
- .validate_key_absent(inertia, field, key) ⇒ Object
- .validate_partial_match(inertia, field, expected) ⇒ Object
- .validate_with_block(inertia, field, &block) ⇒ Object
Class Method Details
.validate_component(inertia, expected) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/inertia_rails/testing.rb', line 117 def validate_component(inertia, expected) actual = inertia&.component negated = "expected component not to be #{expected.inspect}" if actual == expected { passed: true, negated_message: negated } else { passed: false, message: "expected component to be #{expected.inspect}, got #{actual.nil? ? 'nothing' : actual.inspect}", negated_message: negated, } end end |
.validate_deferred_props(inertia, *expected_keys, group: nil) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/inertia_rails/testing.rb', line 151 def validate_deferred_props(inertia, *expected_keys, group: nil) actual = inertia&.deferred_props || {} if expected_keys.empty? negated = 'expected no deferred props to be present' return { passed: true, negated_message: negated } if actual.present? return { passed: false, message: 'expected deferred props to be present', negated_message: negated } end group ||= :default expected_sorted = expected_keys.map(&:to_s).sort actual_keys = actual[group] || actual[group.to_s] || [] actual_sorted = actual_keys.map(&:to_s).sort negated = "expected #{expected_keys.inspect} not to be deferred in group #{group.inspect}" missing = expected_sorted - actual_sorted if missing.empty? { passed: true, negated_message: negated } else { passed: false, message: "expected #{missing.map(&:to_sym).inspect} to be deferred in group #{group.inspect}, " \ "but group has #{actual_keys.inspect}", negated_message: negated, } end end |
.validate_exact_match(inertia, field, expected) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/inertia_rails/testing.rb', line 88 def validate_exact_match(inertia, field, expected) actual = inertia&.public_send(field) actual_sym = actual&.to_h&.deep_symbolize_keys || {} expected_sym = expected.deep_symbolize_keys negated = "expected #{field} not to equal #{expected.inspect}" if actual_sym == expected_sym { passed: true, negated_message: negated } else { passed: false, message: "expected #{field} to equal #{expected.inspect}, got #{actual || 'nothing'}", negated_message: negated, } end end |
.validate_inertia_response(inertia) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/inertia_rails/testing.rb', line 132 def validate_inertia_response(inertia) negated = 'expected response not to be an Inertia response' unless inertia.nil? || inertia.is_a?(InertiaRails::Testing::TestResponse) return { passed: false, message: "expected `inertia` helper, got #{inertia.class}. " \ 'Use the `inertia` helper instead of passing the response directly.', negated_message: negated, } end if inertia&.component.present? { passed: true, negated_message: negated } else { passed: false, message: 'expected an Inertia response', negated_message: negated } end end |
.validate_key_absent(inertia, field, key) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/inertia_rails/testing.rb', line 105 def validate_key_absent(inertia, field, key) actual = inertia&.public_send(field) || {} negated = "expected #{field} to have key #{key.inspect}" if actual.key?(key) { passed: false, message: "expected #{field} not to have key #{key.inspect}", negated_message: negated, } else { passed: true, negated_message: negated } end end |
.validate_partial_match(inertia, field, expected) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/inertia_rails/testing.rb', line 68 def validate_partial_match(inertia, field, expected) actual = inertia&.public_send(field) negated = "expected #{field} not to include #{expected.inspect}" return { passed: false, message: "expected #{field} to be present", negated_message: negated } if actual.nil? actual_sym = actual.to_h.deep_symbolize_keys expected_sym = expected.deep_symbolize_keys if expected_sym.all? { |k, v| actual_sym[k] == v } { passed: true, negated_message: negated } else { passed: false, message: "expected #{field} to include #{expected.inspect}\ngot: #{actual.inspect}", negated_message: negated, } end end |
.validate_with_block(inertia, field, &block) ⇒ Object
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/inertia_rails/testing.rb', line 57 def validate_with_block(inertia, field, &block) actual = inertia&.public_send(field) negated = "expected #{field} block to return false" if block.call(actual) { passed: true, negated_message: negated } else { passed: false, message: "#{field} block validation failed", negated_message: negated } end end |