diff --git a/CHANGELOG.md b/CHANGELOG.md index a9b226f4..71b8257e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - `Node#select_option` and `Node#unselect_option` didn't fire the `input` event on the select [#223] - `Node#trigger` silently dispatched nothing for events other than mouse, focus and `submit`, such as `input`, `change` or custom events [#197] +- `Node#set` with an empty string cleared a text field without firing the `input` event [#164] ### Removed diff --git a/lib/capybara/cuprite/javascripts/index.js b/lib/capybara/cuprite/javascripts/index.js index dd0636cf..1aef8e68 100644 --- a/lib/capybara/cuprite/javascripts/index.js +++ b/lib/capybara/cuprite/javascripts/index.js @@ -220,6 +220,10 @@ class Cuprite { this.keyupdowned(node, "keyup", char, keyCode); } + + if (valueBefore !== "" && node.value === "") { + this.input(node, "deleteContentBackward"); + } } if (valueBefore !== node.value) { @@ -238,8 +242,8 @@ class Cuprite { return nativeTextareaValueSetter.call(node, value); } - input(node) { - let event = new InputEvent("input", { inputType: "insertText", bubbles: true, cancelable: false }); + input(node, inputType = "insertText") { + let event = new InputEvent("input", { inputType: inputType, bubbles: true, cancelable: false }); node.dispatchEvent(event); } diff --git a/spec/features/driver_spec.rb b/spec/features/driver_spec.rb index 8666ccf1..12965cfc 100644 --- a/spec/features/driver_spec.rb +++ b/spec/features/driver_spec.rb @@ -1575,6 +1575,19 @@ def create_screenshot(file, *args) input.set("a") expect(output.text).to eq("keydown keypress input keyup change keydown keypress input keyup") end + + it "calls the input event when clearing the value" do + input.set("a") + input.set("") + expect(output.text).to eq("keydown keypress input keyup change input change") + expect(output["data-input-type"]).to eq("deleteContentBackward") + expect(input.value).to be_empty + end + + it "doesn't call the input event when clearing an empty value" do + input.set("") + expect(output.text).to be_empty + end end end