Skip to content

Instantly share code, notes, and snippets.

@alonbardavid
Created September 2, 2019 11:46
Show Gist options
  • Save alonbardavid/6bbf3fd176f2de094c7a90ccb9bb4483 to your computer and use it in GitHub Desktop.
Save alonbardavid/6bbf3fd176f2de094c7a90ccb9bb4483 to your computer and use it in GitHub Desktop.
test('when maxLength is 1, input should reject any keypress',()=>{
//assuming we have a div in our test environment with the id #test-app
const onChangeCalls = [];
myComponent("#test-app","my-label","A",{maxLength:1},(value)=>calls.push(value));
const input = document.querySelector("#test-app input");
//this test is a simplified version, to really simulate an input event, you'll need to
//change the target value, call more events and react like the browser.
input.dispatchEvent(new Event('keypress',{bubbles:true,cancelable:true,key:"B"}))
expect(calls.length).toEqual(0);
expect(input.value).toEqual("A");
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment