-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add isRequired and isNotRequired assertions #65
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
44cc617
Adding assertion for isRequired
scalvert 286b20b
Adding tests
scalvert 93aaf88
oops
scalvert 0ec1efc
Adding isNotRequired
scalvert 5640d35
Fixing doc string
scalvert 3470f2e
Regenerating docs
scalvert 8a33075
Reusing cached value
scalvert 0eae31f
Adding check to ensure correct element type is passed.
scalvert 82548e2
Covering all types that support required
scalvert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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,112 @@ | ||
/* eslint-env jest */ | ||
|
||
import TestAssertions from "../helpers/test-assertions"; | ||
|
||
describe('assert.dom(...).isNotRequired()', () => { | ||
let assert; | ||
|
||
beforeEach(() => { | ||
assert = new TestAssertions(); | ||
}); | ||
|
||
test('with custom message', () => { | ||
document.body.innerHTML = '<input type="text" required>'; | ||
|
||
assert.dom('input').isNotRequired('custom message'); | ||
|
||
expect(assert.results).toEqual([{ | ||
actual: 'required', | ||
expected: 'not required', | ||
message: 'custom message', | ||
result: false, | ||
}]); | ||
}); | ||
|
||
describe('with HTMLElement', () => { | ||
let element; | ||
|
||
beforeEach(() => { | ||
document.body.innerHTML = '<input type="text">'; | ||
element = document.querySelector('input'); | ||
}); | ||
|
||
test('succeeds if element is not required', () => { | ||
assert.dom(element).isNotRequired(); | ||
|
||
expect(assert.results).toEqual([{ | ||
actual: 'not required', | ||
expected: 'not required', | ||
message: 'Element input[type=\"text\"] is not required', | ||
result: true, | ||
}]); | ||
}); | ||
|
||
test('fails if element is required', () => { | ||
element.required = true; | ||
assert.dom(element).isNotRequired(); | ||
|
||
expect(assert.results).toEqual([{ | ||
actual: 'required', | ||
expected: 'not required', | ||
message: 'Element input[type=\"text\"][required] is not required', | ||
result: false, | ||
}]); | ||
}); | ||
|
||
test('fails for missing element', () => { | ||
assert.dom(null).isNotRequired(); | ||
|
||
expect(assert.results).toEqual([{ | ||
message: 'Element <unknown> exists', | ||
result: false, | ||
}]); | ||
}); | ||
}); | ||
|
||
describe('with selector', () => { | ||
beforeEach(() => { | ||
document.body.innerHTML = '<input type="text">'; | ||
}); | ||
|
||
test('succeeds if element is not required', () => { | ||
assert.dom('input').isNotRequired(); | ||
|
||
expect(assert.results).toEqual([{ | ||
actual: 'not required', | ||
expected: 'not required', | ||
message: 'Element input is not required', | ||
result: true, | ||
}]); | ||
}); | ||
|
||
test('fails if element is required', () => { | ||
document.querySelector('input').required = true; | ||
assert.dom('input').isNotRequired(); | ||
|
||
expect(assert.results).toEqual([{ | ||
actual: 'required', | ||
expected: 'not required', | ||
message: 'Element input is not required', | ||
result: false, | ||
}]); | ||
}); | ||
|
||
test('fails for missing element', () => { | ||
assert.dom('input[type="password"]').isNotRequired(); | ||
|
||
expect(assert.results).toEqual([{ | ||
message: 'Element input[type="password"] exists', | ||
result: false, | ||
}]); | ||
}); | ||
}); | ||
|
||
test('throws for unexpected parameter types', () => { | ||
expect(() => assert.dom(5).isNotRequired()).toThrow('Unexpected Parameter: 5'); | ||
expect(() => assert.dom(true).isNotRequired()).toThrow('Unexpected Parameter: true'); | ||
expect(() => assert.dom(undefined).isNotRequired()).toThrow('Unexpected Parameter: undefined'); | ||
expect(() => assert.dom({}).isNotRequired()).toThrow('Unexpected Parameter: [object Object]'); | ||
expect(() => assert.dom(document).isNotRequired()).toThrow('Unexpected Parameter: [object HTMLDocument]'); | ||
}); | ||
|
||
}); |
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,111 @@ | ||
/* eslint-env jest */ | ||
|
||
import TestAssertions from "../helpers/test-assertions"; | ||
|
||
describe('assert.dom(...).isRequired()', () => { | ||
let assert; | ||
|
||
beforeEach(() => { | ||
assert = new TestAssertions(); | ||
}); | ||
|
||
test('with custom message', () => { | ||
document.body.innerHTML = '<input>'; | ||
|
||
assert.dom('input').isRequired('custom message'); | ||
|
||
expect(assert.results).toEqual([{ | ||
actual: 'not required', | ||
expected: 'required', | ||
message: 'custom message', | ||
result: false, | ||
}]); | ||
}); | ||
|
||
describe('with HTMLElement', () => { | ||
let element; | ||
|
||
beforeEach(() => { | ||
document.body.innerHTML = '<input type="text" required>'; | ||
element = document.querySelector('input'); | ||
}); | ||
|
||
test('succeeds if element is required', () => { | ||
assert.dom(element).isRequired(); | ||
|
||
expect(assert.results).toEqual([{ | ||
actual: 'required', | ||
expected: 'required', | ||
message: 'Element input[type=\"text\"][required] is required', | ||
result: true, | ||
}]); | ||
}); | ||
|
||
test('fails if element is not required', () => { | ||
element.required = false; | ||
assert.dom(element).isRequired(); | ||
|
||
expect(assert.results).toEqual([{ | ||
actual: 'not required', | ||
expected: 'required', | ||
message: 'Element input[type=\"text\"] is required', | ||
result: false, | ||
}]); | ||
}); | ||
|
||
test('fails for missing element', () => { | ||
assert.dom(null).isRequired(); | ||
|
||
expect(assert.results).toEqual([{ | ||
message: 'Element <unknown> exists', | ||
result: false, | ||
}]); | ||
}); | ||
}); | ||
|
||
describe('with selector', () => { | ||
beforeEach(() => { | ||
document.body.innerHTML = '<input type="text" required>'; | ||
}); | ||
|
||
test('succeeds if element is required', () => { | ||
assert.dom('input').isRequired(); | ||
|
||
expect(assert.results).toEqual([{ | ||
actual: 'required', | ||
expected: 'required', | ||
message: 'Element input is required', | ||
result: true, | ||
}]); | ||
}); | ||
|
||
test('fails if element is not required', () => { | ||
document.querySelector('input').required = false; | ||
assert.dom('input').isRequired(); | ||
|
||
expect(assert.results).toEqual([{ | ||
actual: 'not required', | ||
expected: 'required', | ||
message: 'Element input is required', | ||
result: false, | ||
}]); | ||
}); | ||
|
||
test('fails for missing element', () => { | ||
assert.dom('input[type="password"]').isRequired(); | ||
|
||
expect(assert.results).toEqual([{ | ||
message: 'Element input[type="password"] exists', | ||
result: false, | ||
}]); | ||
}); | ||
}); | ||
|
||
test('throws for unexpected parameter types', () => { | ||
expect(() => assert.dom(5).isRequired()).toThrow('Unexpected Parameter: 5'); | ||
expect(() => assert.dom(true).isRequired()).toThrow('Unexpected Parameter: true'); | ||
expect(() => assert.dom(undefined).isRequired()).toThrow('Unexpected Parameter: undefined'); | ||
expect(() => assert.dom({}).isRequired()).toThrow('Unexpected Parameter: [object Object]'); | ||
expect(() => assert.dom(document).isRequired()).toThrow('Unexpected Parameter: [object HTMLDocument]'); | ||
}); | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import elementToString from '../helpers/element-to-string'; | ||
|
||
export default function notRequired(message) { | ||
let element = this.findTargetElement(); | ||
if (!element) return; | ||
|
||
let result = element.required === false; | ||
let actual = !result ? 'required' : 'not required'; | ||
let expected = 'not required'; | ||
|
||
if (!message) { | ||
message = `Element ${elementToString(this.target)} is not required`; | ||
} | ||
|
||
this.pushResult({ result, actual, expected, message }); | ||
} |
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,16 @@ | ||
import elementToString from '../helpers/element-to-string'; | ||
|
||
export default function required(message) { | ||
let element = this.findTargetElement(); | ||
if (!element) return; | ||
|
||
let result = element.required === true; | ||
let actual = result ? 'required' : 'not required'; | ||
let expected = 'required'; | ||
|
||
if (!message) { | ||
message = `Element ${elementToString(this.target)} is required`; | ||
} | ||
|
||
this.pushResult({ result, actual, expected, message }); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happens if the element is not an
<input>
element? wouldrequired
beundefined
then and fail this check?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. I've added a conditional, which will throw if an element is passed that is not one of
HTMLInputElement
,HTMLTextAreaElement
, orHTMLSelectElement
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And a test...