-
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 isVisible and isNotVisible assertion helpers #67
Changes from all commits
bcb8d35
1e611d6
94ba9da
5b177c0
3b59b4f
5f5db28
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* eslint-env jest */ | ||
|
||
import TestAssertions from '../helpers/test-assertions'; | ||
|
||
describe('assert.dom(...).isNotVisible()', () => { | ||
let assert; | ||
|
||
beforeEach(() => { | ||
assert = new TestAssertions(); | ||
}); | ||
|
||
describe('selector only', () => { | ||
test('fails if element is missing', () => { | ||
document.body.innerHTML = '<h1 class="baz">foo</h1>bar'; | ||
|
||
assert.dom('h2').isNotVisible(); | ||
|
||
expect(assert.results).toEqual([{ | ||
message: 'Element h2 exists', | ||
result: false, | ||
}]); | ||
}); | ||
}); | ||
|
||
describe('custom messages', () => { | ||
test('shows custom messages', () => { | ||
document.body.innerHTML = '<h1 class="baz">foo</h1>bar'; | ||
|
||
assert.dom('h1').isNotVisible('foo'); | ||
|
||
expect(assert.results).toEqual([{ | ||
actual: 'Element h1 is not visible', | ||
expected: 'Element h1 is not visible', | ||
message: 'foo', | ||
result: true, | ||
}]); | ||
}); | ||
}); | ||
|
||
test('throws for unexpected parameter types', () => { | ||
expect(() => assert.dom(5).isNotVisible()).toThrow('Unexpected Parameter: 5'); | ||
expect(() => assert.dom(true).isNotVisible()).toThrow('Unexpected Parameter: true'); | ||
expect(() => assert.dom(undefined).isNotVisible()).toThrow('Unexpected Parameter: undefined'); | ||
expect(() => assert.dom({}).isNotVisible()).toThrow('Unexpected Parameter: [object Object]'); | ||
expect(() => assert.dom(document).isNotVisible()).toThrow('Unexpected Parameter: [object HTMLDocument]'); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* eslint-env jest */ | ||
|
||
import TestAssertions from '../helpers/test-assertions'; | ||
|
||
describe('assert.dom(...).isVisible()', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we add a comment here explaining why this is difficult to test in the jsdom-based testing system and possibly a reference to the Ember-based tests? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can do that for sure. |
||
let assert; | ||
|
||
beforeEach(() => { | ||
assert = new TestAssertions(); | ||
}); | ||
|
||
describe('selector only', () => { | ||
test('fails if element is missing', () => { | ||
document.body.innerHTML = '<h1 class="baz">foo</h1>bar'; | ||
|
||
assert.dom('h2').isVisible(); | ||
|
||
expect(assert.results).toEqual([{ | ||
message: 'Element h2 exists', | ||
result: false, | ||
}]); | ||
}); | ||
}); | ||
|
||
describe('custom messages', () => { | ||
test('shows custom messages', () => { | ||
document.body.innerHTML = '<h1 class="baz">foo</h1>bar'; | ||
|
||
assert.dom('h1').isVisible('foo'); | ||
|
||
expect(assert.results).toEqual([{ | ||
actual: 'Element h1 is not visible', | ||
expected: 'Element h1 is visible', | ||
message: 'foo', | ||
result: false, | ||
}]); | ||
}); | ||
}); | ||
|
||
test('throws for unexpected parameter types', () => { | ||
expect(() => assert.dom(5).isVisible()).toThrow('Unexpected Parameter: 5'); | ||
expect(() => assert.dom(true).isVisible()).toThrow('Unexpected Parameter: true'); | ||
expect(() => assert.dom(undefined).isVisible()).toThrow('Unexpected Parameter: undefined'); | ||
expect(() => assert.dom({}).isVisible()).toThrow('Unexpected Parameter: [object Object]'); | ||
expect(() => assert.dom(document).isVisible()).toThrow('Unexpected Parameter: [object HTMLDocument]'); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import visible from '../helpers/visible'; | ||
|
||
export default function isNotVisible(message) { | ||
let element = this.findTargetElement(); | ||
if (!element) return; | ||
|
||
let result = !visible(element); | ||
let actual = result | ||
? `Element ${this.target} is not visible` | ||
: `Element ${this.target} is visible`; | ||
let expected = `Element ${this.target} is not visible`; | ||
|
||
if (!message) { | ||
message = expected; | ||
} | ||
|
||
this.pushResult({ result, actual, expected, message }); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import visible from '../helpers/visible'; | ||
|
||
export default function isVisible(message) { | ||
let element = this.findTargetElement(); | ||
if (!element) return; | ||
|
||
let result = visible(element); | ||
let actual = result | ||
? `Element ${this.target} is visible` | ||
: `Element ${this.target} is not visible`; | ||
let expected = `Element ${this.target} is visible`; | ||
|
||
if (!message) { | ||
message = expected; | ||
} | ||
|
||
this.pushResult({ result, actual, expected, message }); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Visible logic based on jQuery's | ||
// https://github.com/jquery/jquery/blob/4a2bcc27f9c3ee24b3effac0fbe1285d1ee23cc5/src/css/hiddenVisibleSelectors.js#L11-L13 | ||
|
||
export default function visible(el) { | ||
return el !== null && (el.offsetWidth !== 0 || el.offsetHeight !== 0 || el.getClientRects().length !== 0); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,11 @@ | ||
<h2 id="title"> | ||
Welcome to <b>Ember</b> | ||
</h2> | ||
<p id="display-none" style="display: none;">You can't see me.</p> | ||
<div style="display: none;"> | ||
<p id="display-descendant" style="display: block;">You can't see me.</p> | ||
</div> | ||
<p id="display-block" style="display: block;">You can see me.</p> | ||
<input id="hidden-input" type="hidden"> | ||
|
||
{{outlet}} |
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.
just to clarify, this means it's visible on the page, but not necessarily that it's within the currently rendered viewport, correct?
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.
Yes, that's exactly what it means.
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.
cool, can we add that to the docs so that they read a little easier? 😉
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.
Yes. Absolutely.
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.
Done.