Skip to content

Commit 1b3fbca

Browse files
committed
Section 32: Fake DOM Events
1 parent 9ff606d commit 1b3fbca

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

32-The-Basics-of-Testing/06-movies/test/autocomplete.test.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
it('Shows an autocomplete', () => {
1+
beforeEach(() => {
2+
document.querySelectorAll('#target').innerHTML = ''
23
createAutoComplete({
34
root: document.querySelector('#target'),
45
fetchData() {
@@ -10,9 +11,11 @@ it('Shows an autocomplete', () => {
1011
},
1112
renderOption(movie) {
1213
return movie.Title
13-
}
14+
},
1415
})
16+
})
1517

18+
it('Dropdown starts closed', () => {
1619
const dropdown = document.querySelector('.dropdown')
1720

1821
// no real, we don't have access to the assert library inside of the browser
@@ -23,3 +26,13 @@ it('Shows an autocomplete', () => {
2326
// Fail the test
2427
// expect(dropdown.className).to.include('is-active')
2528
})
29+
30+
it('After searching, dropdown opens up', () => {
31+
const input = document.querySelector('input')
32+
input.value = 'avengers'
33+
input.dispatchEvent(new Event('input'))
34+
35+
const dropdown = document.querySelector('.dropdown')
36+
37+
expect(dropdown.className).to.include('is-active')
38+
})

32-The-Basics-of-Testing/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,10 @@ Is what we call an expectation library or an assertion library is the library th
4747

4848
https://www.chaijs.com/guide/styles/#expect
4949

50+
51+
## 13. 15 Fake DOM Events
52+
53+
`document.querySelector('input').value = 'Avenger'`
54+
55+
Faking the event with mocha
56+
`document.querySelector('input').dispatchEvent(new Event('input'))`

0 commit comments

Comments
 (0)