|
226 | 226 | expect(page).to have_content(item.name) |
227 | 227 | end |
228 | 228 | end |
| 229 | + |
| 230 | + describe "when duplicate items" do |
| 231 | + it "detects duplicate items and shows modal", js: true do |
| 232 | + # Disable server-side validation to test JS modal |
| 233 | + #allow_any_instance_of(Audit).to receive(:line_items_unique_by_item_id) |
| 234 | + visit new_kit_path |
| 235 | + click_link "New Kit" |
| 236 | + |
| 237 | + kit_traits = attributes_for(:kit) |
| 238 | + fill_in "Name", with: kit_traits[:name] |
| 239 | + find(:css, '#kit_value_in_dollars').set('10.10') |
| 240 | + |
| 241 | + item = Item.last |
| 242 | + |
| 243 | + # Add first entry for the item |
| 244 | + select item.name, from: "item_line_items_attributes_0_item_id" |
| 245 | + fill_in "item_line_items_attributes_0_quantity", with: "10" |
| 246 | + |
| 247 | + # Add a new line item row |
| 248 | + find("[data-form-input-target='addButton']").click |
| 249 | + |
| 250 | + # Add second entry for the same item |
| 251 | + within all('.line_item_section').last do |
| 252 | + item_select = find('select[name*="[item_id]"]') |
| 253 | + select item.name, from: item_select[:id] |
| 254 | + quantity_input = find('input[name*="[quantity]"]') |
| 255 | + fill_in quantity_input[:id], with: "15" |
| 256 | + end |
| 257 | + |
| 258 | + # Try to save - should trigger duplicate detection modal |
| 259 | + click_button "Save" |
| 260 | + |
| 261 | + # JavaScript modal should appear |
| 262 | + expect(page).to have_css("#duplicateItemsModal", visible: true) |
| 263 | + expect(page).to have_content("Multiple Item Entries Detected") |
| 264 | + expect(page).to have_content("Merge Items") |
| 265 | + expect(page).to have_content("Make Changes") |
| 266 | + |
| 267 | + |
| 268 | + # Test merge functionality |
| 269 | + click_button "Merge Items" |
| 270 | + |
| 271 | + expect(page.find(".alert")).to have_content "Kit created successfully" |
| 272 | + expect(page).to have_content(kit_traits[:name]) |
| 273 | + expect(page).to have_content("25 #{item.name}") |
| 274 | + end |
| 275 | + end |
229 | 276 | end |
0 commit comments