-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
131 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* Component name | ||
*/ | ||
export class SelectAll { | ||
/** | ||
* @param {Element} $module - HTML element to use for component | ||
*/ | ||
constructor ($module) { | ||
if (($module instanceof window.HTMLElement) && document.body.classList.contains('govuk-frontend-supported')) { | ||
this.$module = $module | ||
} | ||
} | ||
|
||
/** | ||
* Initialise component | ||
*/ | ||
init () { | ||
// Check that required elements are present | ||
if (!this.$module) { | ||
return | ||
} | ||
|
||
this.selectAllBox = this.$module.querySelector('input[name="select-all"]') | ||
this.selectAllBox.addEventListener('change', () => this.toggleSelection()) | ||
this.checkBoxes = document.getElementsByClassName('select-all-box') | ||
} | ||
|
||
toggleSelection = () => { | ||
this.checkBoxes.forEach((box) => { box.checked = this.selectAllBox.checked }) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { SelectAll } from './selectAll.mjs' | ||
|
||
describe('SelectAll', () => { | ||
let selectAllBox = null | ||
let selectedBox = null | ||
let unselectedBox = null | ||
let testArea = null | ||
|
||
beforeEach(() => { | ||
document.body.classList.add('govuk-frontend-supported') | ||
|
||
testArea = document.createElement('div') | ||
document.body.appendChild(testArea) | ||
|
||
// <div data-module="govuk-select-all"> | ||
// <input name="select-all" type="checkbox" /> | ||
// </div> | ||
const selectAllDiv = document.createElement('div') | ||
selectAllDiv.setAttribute('data-module', 'govuk-select-all') | ||
selectAllBox = document.createElement('input') | ||
selectAllBox.type = 'checkbox' | ||
selectAllBox.name = 'select-all' | ||
selectAllBox.checked = false | ||
selectAllDiv.appendChild(selectAllBox) | ||
testArea.appendChild(selectAllDiv) | ||
|
||
// <input class="select-all-box" type="checkbox" name="box1" checked=true> | ||
selectedBox = document.createElement('input') | ||
selectedBox.type = 'checkbox' | ||
selectedBox.classList.add('select-all-box') | ||
selectedBox.name = 'box1' | ||
selectedBox.checked = true | ||
testArea.appendChild(selectedBox) | ||
|
||
// <input class="select-all-box" type="checkbox" name="box2"> | ||
unselectedBox = document.createElement('input') | ||
unselectedBox.type = 'checkbox' | ||
unselectedBox.classList.add('select-all-box') | ||
unselectedBox.name = 'box2' | ||
unselectedBox.checked = false | ||
testArea.appendChild(unselectedBox) | ||
|
||
const selectAll = new SelectAll(selectAllDiv) | ||
selectAll.init() | ||
}) | ||
|
||
afterEach(() => { | ||
document.body.classList.remove('govuk-frontend-supported') | ||
testArea.remove() | ||
}) | ||
|
||
const toggleSelectAll = () => { | ||
const event = new Event('change') | ||
selectAllBox.checked = !selectAllBox.checked | ||
selectAllBox.dispatchEvent(event) | ||
} | ||
|
||
describe('toggleSelection', () => { | ||
it('should mark all checkboxes as checked', () => { | ||
toggleSelectAll() | ||
|
||
expect(selectedBox.checked).toBeTrue() | ||
expect(unselectedBox.checked).toBeTrue() | ||
}) | ||
|
||
it('should mark all checkboxes as unchecked', () => { | ||
toggleSelectAll() | ||
toggleSelectAll() | ||
|
||
expect(selectedBox.checked).toBeFalse() | ||
expect(unselectedBox.checked).toBeFalse() | ||
}) | ||
|
||
it('should select newly created boxes', () => { | ||
const newBox = document.createElement('input') | ||
newBox.type = 'checkbox' | ||
newBox.classList.add('select-all-box') | ||
newBox.checked = false | ||
document.body.appendChild(newBox) | ||
|
||
toggleSelectAll() | ||
|
||
expect(newBox.checked).toBeTrue() | ||
}) | ||
}) | ||
}) |
This file contains 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
This file contains 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