-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved "checkbox" tests into unit tests.
- Loading branch information
1 parent
cfda0b3
commit e21e548
Showing
12 changed files
with
135 additions
and
112 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,103 @@ | ||
import {FormToObject} from "../../src/FormToObject"; | ||
|
||
describe('checkbox', () => { | ||
it('unchecked checkboxes, should return {}', () => { | ||
const $form = document.createElement('form'); | ||
$form.innerHTML = ` | ||
<input type="checkbox" name="single" value="single" /> | ||
<input type="checkbox" name="many[]" value="many 0" /> | ||
<input type="checkbox" name="many[]" value="many 1" /> | ||
<input type="checkbox" name="more[first]" value="more first" /> | ||
<input type="checkbox" name="more[second]" value="more second" /> | ||
`; | ||
|
||
const formToObject = new FormToObject($form) | ||
|
||
expect(formToObject.convertToObj()).toEqual({}); | ||
}); | ||
|
||
it('a single checkbox without value attribute, should return the default value "on" as a string.', () => { | ||
const $form = document.createElement('form'); | ||
$form.innerHTML = ` | ||
<input type="checkbox" name="terms" checked/> | ||
`; | ||
|
||
const formToObject = new FormToObject($form); | ||
|
||
expect(formToObject.convertToObj()).toEqual({ | ||
terms: "on" | ||
}); | ||
}); | ||
|
||
it('two checkboxes with the same name and different values should return the checked element value as an array', () => { | ||
const $form = document.createElement('form'); | ||
$form.innerHTML = ` | ||
<input type="checkbox" name="checkbox" value="first" checked/> | ||
<input type="checkbox" name="checkbox" value="second"/> | ||
`; | ||
|
||
const formToObject = new FormToObject($form); | ||
|
||
expect(formToObject.convertToObj()).toEqual({ | ||
checkbox: ["first"] | ||
}); | ||
}); | ||
|
||
it('two checkboxes with the same name and different values, both checked, should return value as an array', () => { | ||
const $form = document.createElement('form'); | ||
$form.innerHTML = ` | ||
<input type="checkbox" name="checkbox" value="first" checked/> | ||
<input type="checkbox" name="checkbox" value="second" checked/> | ||
`; | ||
|
||
const formToObject = new FormToObject($form); | ||
|
||
expect(formToObject.convertToObj()).toEqual({ | ||
checkbox: ["first", "second"] | ||
}); | ||
}); | ||
|
||
it('checkboxes named checkbox[] should return an array of values, by default', () => { | ||
const $form = document.createElement('form'); | ||
$form.innerHTML = ` | ||
<input type="checkbox" name="checkbox[]" value="first" checked /> | ||
<input type="checkbox" name="checkbox[]" value="second" checked /> | ||
`; | ||
|
||
const formToObject = new FormToObject($form); | ||
|
||
expect(formToObject.convertToObj()).toEqual({ | ||
checkbox: ["first", "second"] | ||
}); | ||
}); | ||
|
||
it('checkboxes named checkbox[] should return an array of values, when checkBoxNameWithEmptyBracketsReturnsArray = false', () => { | ||
const $form = document.createElement('form'); | ||
$form.innerHTML = ` | ||
<input type="checkbox" name="checkbox[]" value="first" checked /> | ||
<input type="checkbox" name="checkbox[]" value="second" checked /> | ||
`; | ||
|
||
const formToObject = new FormToObject($form, { checkBoxNameWithEmptyBracketsReturnsArray: false }); | ||
|
||
expect(formToObject.convertToObj()).toEqual({ | ||
checkbox: {0: "first", 1: "second" } | ||
}); | ||
}); | ||
|
||
it('checkboxes named checkbox[a] and checkbox[b] return an object', () => { | ||
const $form = document.createElement('form'); | ||
$form.innerHTML = ` | ||
<input type="checkbox" name="checkbox[a]" value="a" checked /> | ||
<input type="checkbox" name="checkbox[b]" value="b" checked /> | ||
`; | ||
|
||
const formToObject = new FormToObject($form); | ||
|
||
expect(formToObject.convertToObj()).toEqual({ | ||
checkbox: {"a": "a", "b": "b" } | ||
}); | ||
}); | ||
}); | ||
|
||
|
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