Skip to content

Commit

Permalink
Implement test helper logging if the testHelperLogging query param …
Browse files Browse the repository at this point in the history
…is set
  • Loading branch information
Turbo87 committed Dec 6, 2019
1 parent aa061b7 commit 9738f74
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 0 deletions.
28 changes: 28 additions & 0 deletions addon-test-support/@ember/test-helpers/dom/-logging.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
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.
*
Expand Down
3 changes: 3 additions & 0 deletions addon-test-support/@ember/test-helpers/dom/blur.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import settled from '../settled';
import isFocusable from './-is-focusable';
import { nextTickPromise } from '../-utils';
import Target from './-target';
import { log } from '@ember/test-helpers/dom/-logging';

/**
@private
Expand Down Expand Up @@ -51,6 +52,8 @@ export function __blur__(element: HTMLElement | SVGElement): void {
blur('input');
*/
export default function blur(target: Target = document.activeElement!): Promise<void> {
log('blur', target);

return nextTickPromise().then(() => {
let element = getElement(target);
if (!element) {
Expand Down
3 changes: 3 additions & 0 deletions addon-test-support/@ember/test-helpers/dom/click.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import isFocusable from './-is-focusable';
import { nextTickPromise } from '../-utils';
import isFormControl from './-is-form-control';
import Target from './-target';
import { log } from '@ember/test-helpers/dom/-logging';

/**
@private
Expand Down Expand Up @@ -69,6 +70,8 @@ export function __click__(element: Element | Document, options: MouseEventInit):
click('button', { shiftKey: true });
*/
export default function click(target: Target, options: MouseEventInit = {}): Promise<void> {
log('click', target);

return nextTickPromise().then(() => {
if (!target) {
throw new Error('Must pass an element or selector to `click`.');
Expand Down
3 changes: 3 additions & 0 deletions addon-test-support/@ember/test-helpers/dom/double-click.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import settled from '../settled';
import isFocusable from './-is-focusable';
import { nextTickPromise } from '../-utils';
import Target from './-target';
import { log } from '@ember/test-helpers/dom/-logging';

/**
@private
Expand Down Expand Up @@ -80,6 +81,8 @@ export function __doubleClick__(element: Element | Document, options: MouseEvent
doubleClick('button', { shiftKey: true });
*/
export default function doubleClick(target: Target, options: MouseEventInit = {}): Promise<void> {
log('doubleClick', target);

return nextTickPromise().then(() => {
if (!target) {
throw new Error('Must pass an element or selector to `doubleClick`.');
Expand Down
3 changes: 3 additions & 0 deletions addon-test-support/@ember/test-helpers/dom/fill-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import settled from '../settled';
import fireEvent from './fire-event';
import { nextTickPromise } from '../-utils';
import Target from './-target';
import { log } from '@ember/test-helpers/dom/-logging';

/**
Fill the provided text into the `value` property (or set `.innerHTML` when
Expand All @@ -24,6 +25,8 @@ import Target from './-target';
fillIn('input', 'hello world');
*/
export default function fillIn(target: Target, text: string): Promise<void> {
log('fillIn', target, text);

return nextTickPromise().then(() => {
if (!target) {
throw new Error('Must pass an element or selector to `fillIn`.');
Expand Down
3 changes: 3 additions & 0 deletions addon-test-support/@ember/test-helpers/dom/focus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import settled from '../settled';
import isFocusable from './-is-focusable';
import { nextTickPromise } from '../-utils';
import Target from './-target';
import { log } from '@ember/test-helpers/dom/-logging';

/**
@private
Expand Down Expand Up @@ -54,6 +55,8 @@ export function __focus__(element: HTMLElement | SVGElement): void {
focus('input');
*/
export default function focus(target: Target): Promise<void> {
log('focus', target);

return nextTickPromise().then(() => {
if (!target) {
throw new Error('Must pass an element or selector to `focus`.');
Expand Down
3 changes: 3 additions & 0 deletions addon-test-support/@ember/test-helpers/dom/tap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { __click__ } from './click';
import settled from '../settled';
import { nextTickPromise } from '../-utils';
import Target from './-target';
import { log } from '@ember/test-helpers/dom/-logging';

/**
Taps on the specified target.
Expand Down Expand Up @@ -48,6 +49,8 @@ import Target from './-target';
tap('button');
*/
export default function tap(target: Target, options: object = {}): Promise<void> {
log('tap', target);

return nextTickPromise().then(() => {
if (!target) {
throw new Error('Must pass an element or selector to `tap`.');
Expand Down
3 changes: 3 additions & 0 deletions addon-test-support/@ember/test-helpers/dom/trigger-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import fireEvent from './fire-event';
import settled from '../settled';
import { nextTickPromise } from '../-utils';
import Target from './-target';
import { log } from '@ember/test-helpers/dom/-logging';

/**
* Triggers an event on the specified target.
Expand Down Expand Up @@ -52,6 +53,8 @@ export default function triggerEvent(
eventType: string,
options?: object
): Promise<void> {
log('triggerEvent', target, eventType);

return nextTickPromise().then(() => {
if (!target) {
throw new Error('Must pass an element or selector to `triggerEvent`.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import settled from '../settled';
import { KEYBOARD_EVENT_TYPES, KeyboardEventType, isKeyboardEventType } from './fire-event';
import { nextTickPromise, isNumeric } from '../-utils';
import Target from './-target';
import { log } from '@ember/test-helpers/dom/-logging';

export interface KeyModifiers {
ctrlKey?: boolean;
Expand Down Expand Up @@ -183,6 +184,8 @@ export default function triggerKeyEvent(
key: number | string,
modifiers: KeyModifiers = DEFAULT_MODIFIERS
): Promise<void> {
log('triggerKeyEvent', target, eventType, key);

return nextTickPromise().then(() => {
if (!target) {
throw new Error('Must pass an element or selector to `triggerKeyEvent`.');
Expand Down
3 changes: 3 additions & 0 deletions addon-test-support/@ember/test-helpers/dom/type-in.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Promise } from 'rsvp';
import fireEvent from './fire-event';
import Target from './-target';
import { __triggerKeyEvent__ } from './trigger-key-event';
import { log } from '@ember/test-helpers/dom/-logging';

export interface Options {
delay?: number;
Expand Down Expand Up @@ -38,6 +39,8 @@ export interface Options {
* typeIn('hello world');
*/
export default function typeIn(target: Target, text: string, options: Options = {}): Promise<void> {
log('typeIn', target, text);

return nextTickPromise().then(() => {
if (!target) {
throw new Error('Must pass an element or selector to `typeIn`.');
Expand Down

0 comments on commit 9738f74

Please sign in to comment.