|
| 1 | +module ForestLiana |
| 2 | + describe DecorationHelper do |
| 3 | + describe '.detect_match_and_decorate' do |
| 4 | + let(:record) do |
| 5 | + { |
| 6 | + 'type' => 'User', |
| 7 | + 'id' => '123', |
| 8 | + 'attributes' => { |
| 9 | + 'id' => 123, |
| 10 | + 'name' => 'John Doe', |
| 11 | + |
| 12 | + }, |
| 13 | + 'links' => { 'self' => '/forest/user/123' }, |
| 14 | + 'relationships' => {} |
| 15 | + } |
| 16 | + end |
| 17 | + let(:index) { 0 } |
| 18 | + let(:field_name) { 'name' } |
| 19 | + let(:value) { 'John Doe' } |
| 20 | + let(:search_value) { 'john' } |
| 21 | + let(:match_fields) { {} } |
| 22 | + |
| 23 | + context 'when value matches search_value' do |
| 24 | + it 'creates new match entry when none exists' do |
| 25 | + described_class.detect_match_and_decorate(record, index, field_name, value, search_value, match_fields) |
| 26 | + |
| 27 | + expect(match_fields[index]).to eq({ |
| 28 | + id: '123', |
| 29 | + search: ['name'] |
| 30 | + }) |
| 31 | + end |
| 32 | + |
| 33 | + it 'appends to existing match entry' do |
| 34 | + match_fields[index] = { id: '123', search: ['email'] } |
| 35 | + |
| 36 | + described_class.detect_match_and_decorate(record, index, field_name, value, search_value, match_fields) |
| 37 | + |
| 38 | + expect(match_fields[index][:search]).to contain_exactly('email', 'name') |
| 39 | + end |
| 40 | + |
| 41 | + it 'performs case-insensitive matching' do |
| 42 | + search_value = 'JOHN' |
| 43 | + |
| 44 | + described_class.detect_match_and_decorate(record, index, field_name, value, search_value, match_fields) |
| 45 | + |
| 46 | + expect(match_fields[index]).not_to be_nil |
| 47 | + expect(match_fields[index][:search]).to include('name') |
| 48 | + end |
| 49 | + |
| 50 | + it 'matches partial strings' do |
| 51 | + search_value = 'oe' |
| 52 | + |
| 53 | + described_class.detect_match_and_decorate(record, index, field_name, value, search_value, match_fields) |
| 54 | + |
| 55 | + expect(match_fields[index][:search]).to include('name') |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + context 'when value does not match search_value' do |
| 60 | + let(:search_value) { 'jane' } |
| 61 | + |
| 62 | + it 'does not create match entry' do |
| 63 | + described_class.detect_match_and_decorate(record, index, field_name, value, search_value, match_fields) |
| 64 | + |
| 65 | + expect(match_fields).to be_empty |
| 66 | + end |
| 67 | + |
| 68 | + it 'does not modify existing match_fields' do |
| 69 | + existing_data = { id: '456', search: ['other_field'] } |
| 70 | + match_fields[1] = existing_data.dup |
| 71 | + |
| 72 | + described_class.detect_match_and_decorate(record, index, field_name, value, search_value, match_fields) |
| 73 | + |
| 74 | + expect(match_fields[1]).to eq(existing_data) |
| 75 | + expect(match_fields[index]).to be_nil |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + context 'when regex matching raises an exception' do |
| 80 | + let(:search_value) { '[invalid_regex' } |
| 81 | + |
| 82 | + it 'handles the exception gracefully' do |
| 83 | + expect { |
| 84 | + described_class.detect_match_and_decorate(record, index, field_name, value, search_value, match_fields) |
| 85 | + }.not_to raise_error |
| 86 | + |
| 87 | + expect(match_fields).to be_empty |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + context 'with special regex characters in search_value' do |
| 92 | + let(:search_value) { '.' } |
| 93 | + let(:value) { '[email protected]' } |
| 94 | + |
| 95 | + it 'treats special characters as literal characters' do |
| 96 | + described_class.detect_match_and_decorate(record, index, field_name, value, search_value, match_fields) |
| 97 | + |
| 98 | + expect(match_fields[index][:search]).to include('name') |
| 99 | + end |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + describe '.decorate_for_search' do |
| 104 | + let(:search_value) { 'john' } |
| 105 | + let(:field_names) { ['name', 'email'] } |
| 106 | + |
| 107 | + context 'with valid records' do |
| 108 | + let(:records_serialized) do |
| 109 | + { |
| 110 | + 'data' => [ |
| 111 | + { |
| 112 | + 'type' => 'User', |
| 113 | + 'id' => '1', |
| 114 | + 'attributes' => { |
| 115 | + 'id' => 1, |
| 116 | + 'name' => 'John Doe', |
| 117 | + |
| 118 | + }, |
| 119 | + 'links' => { 'self' => '/forest/user/1' }, |
| 120 | + 'relationships' => {} |
| 121 | + }, |
| 122 | + { |
| 123 | + 'type' => 'User', |
| 124 | + 'id' => '2', |
| 125 | + 'attributes' => { |
| 126 | + 'id' => 2, |
| 127 | + 'name' => 'Jane Smith', |
| 128 | + |
| 129 | + }, |
| 130 | + 'links' => { 'self' => '/forest/user/2' }, |
| 131 | + 'relationships' => {} |
| 132 | + } |
| 133 | + ] |
| 134 | + } |
| 135 | + end |
| 136 | + |
| 137 | + it 'returns match fields for matching records' do |
| 138 | + result = described_class.decorate_for_search(records_serialized, field_names, search_value) |
| 139 | + |
| 140 | + expect(result).to eq({ |
| 141 | + 0 => { |
| 142 | + id: '1', |
| 143 | + search: %w[name email] |
| 144 | + } |
| 145 | + }) |
| 146 | + end |
| 147 | + |
| 148 | + it 'includes ID field in search when ID matches' do |
| 149 | + search_value = '2' |
| 150 | + |
| 151 | + result = described_class.decorate_for_search(records_serialized, field_names, search_value) |
| 152 | + |
| 153 | + expect(result[1][:search]).to include('id') |
| 154 | + end |
| 155 | + |
| 156 | + it 'handles multiple matches across different records' do |
| 157 | + records_serialized['data'][1]['attributes']['name'] = 'Johnny Cash' |
| 158 | + |
| 159 | + result = described_class.decorate_for_search(records_serialized, field_names, search_value) |
| 160 | + |
| 161 | + expect(result).to have_key(0) |
| 162 | + expect(result).to have_key(1) |
| 163 | + expect(result[0][:search]).to contain_exactly('name', 'email') |
| 164 | + expect(result[1][:search]).to contain_exactly('name') |
| 165 | + end |
| 166 | + |
| 167 | + it 'skips fields with nil values' do |
| 168 | + records_serialized['data'][0]['attributes']['email'] = nil |
| 169 | + |
| 170 | + result = described_class.decorate_for_search(records_serialized, field_names, search_value) |
| 171 | + |
| 172 | + expect(result[0][:search]).to eq(['name']) |
| 173 | + end |
| 174 | + |
| 175 | + it 'skips fields with empty string values' do |
| 176 | + records_serialized['data'][0]['attributes']['email'] = '' |
| 177 | + |
| 178 | + result = described_class.decorate_for_search(records_serialized, field_names, search_value) |
| 179 | + |
| 180 | + expect(result[0][:search]).to eq(['name']) |
| 181 | + end |
| 182 | + end |
| 183 | + |
| 184 | + context 'when no matches are found' do |
| 185 | + let(:records_serialized) do |
| 186 | + { |
| 187 | + 'data' => [ |
| 188 | + { |
| 189 | + 'type' => 'User', |
| 190 | + 'id' => '1', |
| 191 | + 'attributes' => { |
| 192 | + 'id' => 1, |
| 193 | + 'name' => 'Jane Doe', |
| 194 | + |
| 195 | + }, |
| 196 | + 'links' => { 'self' => '/forest/user/1' }, |
| 197 | + 'relationships' => {} |
| 198 | + } |
| 199 | + ] |
| 200 | + } |
| 201 | + end |
| 202 | + |
| 203 | + it 'returns nil' do |
| 204 | + result = described_class.decorate_for_search(records_serialized, field_names, search_value) |
| 205 | + |
| 206 | + expect(result).to be_nil |
| 207 | + end |
| 208 | + end |
| 209 | + |
| 210 | + context 'with invalid record structure' do |
| 211 | + let(:records_serialized) do |
| 212 | + { |
| 213 | + 'data' => [ |
| 214 | + { |
| 215 | + 'type' => 'User', |
| 216 | + 'id' => '1', |
| 217 | + 'links' => { 'self' => '/forest/user/1' }, |
| 218 | + 'relationships' => { |
| 219 | + 'claim' => { 'links' => { 'related' => {} } } |
| 220 | + } |
| 221 | + } |
| 222 | + ] |
| 223 | + } |
| 224 | + end |
| 225 | + |
| 226 | + it 'raises ArgumentError with descriptive message' do |
| 227 | + expect { |
| 228 | + described_class.decorate_for_search(records_serialized, field_names, search_value) |
| 229 | + }.to raise_error(ArgumentError, "Missing 'attributes' key in record #{records_serialized['data'][0]}") |
| 230 | + end |
| 231 | + end |
| 232 | + end |
| 233 | + end |
| 234 | +end |
0 commit comments