Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmhaig committed Oct 20, 2023
1 parent e001e13 commit 4d33c4b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
8 changes: 3 additions & 5 deletions app/webpack/javascripts/modules/selectAll.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@ export class SelectAll {
*/
init () {
// Check that required elements are present
if (!this.$module) {
return
}
if (!this.$module) { return }

const selector = this.$module.dataset.selectAllClass
const collection = this.$module.dataset.collectionClass

this.selectAllBox = this.$module.getElementsByClassName(selector)[0]
this.selectAllBox = this.$module.querySelector(`.${selector}`)
this.selectAllBox.addEventListener('change', () => this.toggleSelection())
this.checkBoxes = document.getElementsByClassName(collection)
this.checkBoxes = document.querySelectorAll(`.${collection}`)
}

toggleSelection = () => {
Expand Down
26 changes: 12 additions & 14 deletions app/webpack/javascripts/modules/selectAll_spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,11 @@ describe('SelectAll', () => {
testArea.appendChild(selectAllDiv)

// <input class="pick-me" type="checkbox" name="box1" checked=true>
selectedBox = document.createElement('input')
selectedBox.type = 'checkbox'
selectedBox.classList.add('pick-me')
selectedBox.name = 'box1'
selectedBox.checked = true
selectedBox = createCheckbox('pick-me', true, 'box1')
testArea.appendChild(selectedBox)

// <input class="pick-me" type="checkbox" name="box2">
unselectedBox = document.createElement('input')
unselectedBox.type = 'checkbox'
unselectedBox.classList.add('pick-me')
unselectedBox.name = 'box2'
unselectedBox.checked = false
unselectedBox = createCheckbox('pick-me', true, 'box1')
testArea.appendChild(unselectedBox)

const selectAll = new SelectAll(selectAllDiv)
Expand All @@ -52,6 +44,15 @@ describe('SelectAll', () => {
testArea.remove()
})

const createCheckbox = (className, checked = false, name = '') => {
const checkbox = document.createElement('input')
checkbox.type = 'checkbox'
checkbox.classList.add(className)
checkbox.name = name
checkbox.checked = checked
return checkbox
}

const toggleSelectAll = () => {
const event = new Event('change')
selectAllBox.checked = !selectAllBox.checked
Expand All @@ -74,10 +75,7 @@ describe('SelectAll', () => {
})

it('should select newly created boxes', () => {
const newBox = document.createElement('input')
newBox.type = 'checkbox'
newBox.classList.add('pick-me')
newBox.checked = false
const newBox = createCheckbox('pick-me', false, 'new-box')
document.body.appendChild(newBox)

toggleSelectAll()
Expand Down

0 comments on commit 4d33c4b

Please sign in to comment.