Created
October 13, 2019 04:46
-
-
Save takahirohonda/2846c93f0cdabd643e00c6c93d392b50 to your computer and use it in GitHub Desktop.
karma-unit-test-input-eventlistener-4.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import FormEventHandler from '../FormEventHandler'; | |
describe('FormEventHandler.spec.ts', () => { | |
beforeEach(() => { | |
const fixture = `<form id="fixture"> | |
<div role="radiogroup"> | |
<h3>Radio buttons</h3> | |
<div role="radio" class="radio-input-ac" | |
value="option1" id="option1"> | |
Option 1 | |
</div> | |
<div role="radio" class="radio-input-ac" | |
value="option2" id="option2"> | |
Option 2 | |
</div> | |
<div role="radio" class="radio-input-ac" | |
value="option3" id="option3"> | |
Option 3 | |
</div> | |
</div> | |
<div class="select-section"> | |
<span role="checkbox" aria-checked="false" | |
class="checkbox-input-ac" | |
id="subscribe" name="subscribe" /></span> | |
<label for="subscribe">subscribe</label> | |
</div> | |
</form>`; | |
document.body.insertAdjacentHTML('afterbegin', fixture); | |
}); | |
afterEach(() => { | |
document.body.removeChild((document.getElementById('fixture') as HTMLElement)); | |
}); | |
it('should set correct aria-checked attritube on radio input', () => { | |
// Arrange | |
const formElement = document.querySelector('#fixture') as HTMLElement; | |
const formEventHandler = new FormEventHandler(formElement); | |
// Act | |
formEventHandler.init(); | |
const clickedElement = document.getElementById('option1') as HTMLInputElement; | |
clickedElement.checked = true; | |
clickedElement.click(); | |
// Assert | |
const radioGroupElements = document.querySelectorAll('.radio-input-ac') as any; | |
for (let elem of radioGroupElements) { | |
if (elem.getAttribute('id') === 'option1') { | |
expect(elem.getAttribute('aria-checked')).toBe('true'); | |
} else { | |
expect(elem.getAttribute('aria-checked')).toBe('false'); | |
} | |
} | |
}); | |
it('should set aria-checked to true on checkbox input when ticked', () => { | |
// Arrange | |
const formElement = document.querySelector('#fixture') as HTMLElement; | |
const formEventHandler = new FormEventHandler(formElement); | |
// Act | |
formEventHandler.init(); | |
const clickedElement = document.getElementById('subscribe') as HTMLInputElement; | |
clickedElement.checked = true; | |
clickedElement.click(); | |
// Assert | |
expect(clickedElement.getAttribute('aria-checked')).toBe('true'); | |
}); | |
it('should set aria-checked to false on checkbox input when not ticked', () => { | |
// Arrange | |
const formElement = document.querySelector('#fixture') as HTMLElement; | |
const formEventHandler = new FormEventHandler(formElement); | |
// Act | |
formEventHandler.init(); | |
const clickedElement = document.getElementById('subscribe') as HTMLInputElement; | |
clickedElement.click(); | |
// Assert | |
expect(clickedElement.getAttribute('aria-checked')).toBe('false'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment