-
-
Notifications
You must be signed in to change notification settings - Fork 259
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
Implement test helper logging #745
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import Target from './-target'; | ||
|
||
/** | ||
* Logs a debug message to the console if the `testHelperLogging` query | ||
* parameter is set. | ||
* | ||
* @private | ||
* @param {string} helperName Name of the helper | ||
* @param {string|Element} target The target element or selector | ||
*/ | ||
export function log(helperName: string, target: Target, ...args: any[]) { | ||
if (loggingEnabled()) { | ||
// eslint-disable-next-line no-console | ||
console.log(`${helperName}(${[elementToString(target), ...args.filter(Boolean)].join(', ')})`); | ||
} | ||
} | ||
|
||
/** | ||
* Returns whether the test helper logging is enabled or not via the | ||
* `testHelperLogging` query parameter. | ||
* | ||
* @private | ||
* @returns {boolean} true if enabled | ||
*/ | ||
function loggingEnabled(): boolean { | ||
return typeof location !== 'undefined' && location.search.indexOf('testHelperLogging') !== -1; | ||
} | ||
|
||
/** | ||
* This generates a human-readable description to a DOM element. | ||
* | ||
* @private | ||
* @param {*} el The element that should be described | ||
* @returns {string} A human-readable description | ||
*/ | ||
export function elementToString(el: unknown) { | ||
let desc: string; | ||
if (el instanceof NodeList) { | ||
if (el.length === 0) { | ||
return 'empty NodeList'; | ||
} | ||
desc = Array.prototype.slice | ||
.call(el, 0, 5) | ||
.map(elementToString) | ||
.join(', '); | ||
return el.length > 5 ? `${desc}... (+${el.length - 5} more)` : desc; | ||
} | ||
if (!(el instanceof HTMLElement || el instanceof SVGElement)) { | ||
return String(el); | ||
} | ||
|
||
desc = el.tagName.toLowerCase(); | ||
if (el.id) { | ||
desc += `#${el.id}`; | ||
} | ||
if (el.className && !(el.className instanceof SVGAnimatedString)) { | ||
desc += `.${String(el.className).replace(/\s+/g, '.')}`; | ||
} | ||
Array.prototype.forEach.call(el.attributes, function(attr: Attr) { | ||
if (attr.name !== 'class' && attr.name !== 'id') { | ||
desc += `[${attr.name}${attr.value ? `="${attr.value}"]` : ']'}`; | ||
} | ||
}); | ||
return desc; | ||
} |
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 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 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,97 @@ | ||
import { module, test } from 'qunit'; | ||
import { | ||
render, | ||
settled, | ||
setupContext, | ||
setupRenderingContext, | ||
teardownContext, | ||
teardownRenderingContext, | ||
} from '@ember/test-helpers'; | ||
import hasEmberVersion from '@ember/test-helpers/has-ember-version'; | ||
import { elementToString } from '@ember/test-helpers/dom/-logging'; | ||
import hbs from 'htmlbars-inline-precompile'; | ||
|
||
module('elementToString()', function(hooks) { | ||
if (!hasEmberVersion(2, 4)) { | ||
return; | ||
} | ||
|
||
hooks.beforeEach(async function() { | ||
await setupContext(this); | ||
await setupRenderingContext(this); | ||
}); | ||
|
||
hooks.afterEach(async function() { | ||
await settled(); | ||
await teardownRenderingContext(this); | ||
await teardownContext(this); | ||
}); | ||
|
||
module('NodeLists', function() { | ||
test('empty NodeList', async function(assert) { | ||
await render(hbs``); | ||
assert.equal(elementToString(this.element.querySelectorAll('h1')), 'empty NodeList'); | ||
}); | ||
|
||
test('with single element', async function(assert) { | ||
await render(hbs`<h1></h1>`); | ||
assert.equal(elementToString(this.element.querySelectorAll('h1')), 'h1'); | ||
}); | ||
|
||
test('with three elements', async function(assert) { | ||
await render(hbs`<h1></h1><h1></h1><h1 class="foo"></h1>`); | ||
assert.equal(elementToString(this.element.querySelectorAll('h1')), 'h1, h1, h1.foo'); | ||
}); | ||
|
||
test('with five elements', async function(assert) { | ||
await render(hbs`<h1></h1><h1></h1><h1 class="foo"></h1><h1></h1><h1></h1>`); | ||
assert.equal(elementToString(this.element.querySelectorAll('h1')), 'h1, h1, h1.foo, h1, h1'); | ||
}); | ||
|
||
test('with six elements', async function(assert) { | ||
await render(hbs`<h1></h1><h1></h1><h1 class="foo"></h1><h1></h1><h1></h1><h1></h1>`); | ||
assert.equal( | ||
elementToString(this.element.querySelectorAll('h1')), | ||
'h1, h1, h1.foo, h1, h1... (+1 more)' | ||
); | ||
}); | ||
|
||
test('with ten elements', async function(assert) { | ||
await render( | ||
hbs`<h1></h1><h1></h1><h1 class="foo"></h1><h1></h1><h1></h1><h1></h1><h1></h1><h1></h1><h1></h1><h1></h1>` | ||
); | ||
assert.equal( | ||
elementToString(this.element.querySelectorAll('h1')), | ||
'h1, h1, h1.foo, h1, h1... (+5 more)' | ||
); | ||
}); | ||
}); | ||
|
||
test('strings', async function(assert) { | ||
assert.equal(elementToString('h1'), 'h1'); | ||
assert.equal(elementToString('[data-test-foo]'), '[data-test-foo]'); | ||
}); | ||
|
||
module('HTMLElements', function() { | ||
test('IDs', async function(assert) { | ||
await render(hbs`<div id="map"></div>`); | ||
assert.equal(elementToString(this.element.querySelector('div')), 'div#map'); | ||
}); | ||
|
||
test('CSS classes', async function(assert) { | ||
await render(hbs`<div class="foo bar"></div>`); | ||
assert.equal(elementToString(this.element.querySelector('div')), 'div.foo.bar'); | ||
}); | ||
|
||
test('attributes', async function(assert) { | ||
await render(hbs`<input type="password">`); | ||
assert.equal(elementToString(this.element.querySelector('input')), 'input[type="password"]'); | ||
|
||
await render(hbs`<input data-test-username>`); | ||
assert.equal( | ||
elementToString(this.element.querySelector('input')), | ||
'input[data-test-username]' | ||
); | ||
}); | ||
}); | ||
}); |
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.
FWIW, I wonder if this should actually be private. Seems like it might be nice for folks authoring custom helpers too...
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.
I'd say let's try it out and see if it works properly before we commit to making it a public API