forked from help-me-mom/ng-mocks
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#315): ngMocks.trigger and ngMocks.click
- Loading branch information
Showing
12 changed files
with
878 additions
and
70 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
5 changes: 5 additions & 0 deletions
5
libs/ng-mocks/src/lib/mock-helper/events/mock-helper.click.ts
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,5 @@ | ||
import mockHelperTrigger from './mock-helper.trigger'; | ||
|
||
export default (selector: any, payload?: object) => { | ||
mockHelperTrigger(selector, 'click', payload); | ||
}; |
162 changes: 162 additions & 0 deletions
162
libs/ng-mocks/src/lib/mock-helper/events/mock-helper.event.ts
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,162 @@ | ||
import mockHelperStub from '../mock-helper.stub'; | ||
|
||
/** | ||
* @see https://developer.mozilla.org/de/docs/Web/Events | ||
*/ | ||
const preventBubble = ['focus', 'blur', 'load', 'unload', 'change', 'reset', 'scroll']; | ||
|
||
// istanbul ignore next | ||
function customEvent(event: string, params?: EventInit) { | ||
const initParams = { | ||
bubbles: false, | ||
cancelable: false, | ||
...params, | ||
}; | ||
const eventObj = document.createEvent('CustomEvent'); | ||
eventObj.initCustomEvent(event, initParams.bubbles, initParams.cancelable, null); | ||
|
||
return eventObj; | ||
} | ||
|
||
const eventCtor = | ||
typeof (Event as any) === 'function' | ||
? (event: string, init?: EventInit): Event => new CustomEvent(event, init) | ||
: /* istanbul ignore next */ customEvent; | ||
|
||
const keyMap: Record<string, object> = { | ||
alt: { | ||
altKey: true, | ||
code: 'AltLeft', | ||
key: 'Alt', | ||
location: 1, | ||
which: 18, | ||
}, | ||
arrowdown: { | ||
code: 'ArrowDown', | ||
key: 'ArrowDown', | ||
location: 0, | ||
which: 40, | ||
}, | ||
arrowleft: { | ||
code: 'ArrowLeft', | ||
key: 'ArrowLeft', | ||
location: 0, | ||
which: 37, | ||
}, | ||
arrowright: { | ||
code: 'ArrowRight', | ||
key: 'ArrowRight', | ||
location: 0, | ||
which: 39, | ||
}, | ||
arrowup: { | ||
code: 'ArrowUp', | ||
key: 'ArrowUp', | ||
location: 0, | ||
which: 38, | ||
}, | ||
backspace: { | ||
code: 'Backspace', | ||
key: 'Backspace', | ||
location: 0, | ||
which: 8, | ||
}, | ||
control: { | ||
code: 'ControlLeft', | ||
ctrlKey: true, | ||
key: 'Control', | ||
location: 1, | ||
which: 17, | ||
}, | ||
enter: { | ||
code: 'Enter', | ||
key: 'Enter', | ||
location: 0, | ||
which: 13, | ||
}, | ||
esc: { | ||
code: 'Escape', | ||
key: 'Escape', | ||
location: 0, | ||
which: 27, | ||
}, | ||
meta: { | ||
code: 'MetaLeft', | ||
key: 'Meta', | ||
location: 1, | ||
metaKey: true, | ||
which: 91, | ||
}, | ||
shift: { | ||
code: 'ShiftLeft', | ||
key: 'Shift', | ||
location: 1, | ||
shiftKey: true, | ||
which: 16, | ||
}, | ||
space: { | ||
code: 'Space', | ||
key: ' ', | ||
location: 0, | ||
which: 32, | ||
}, | ||
tab: { | ||
code: 'Tab', | ||
key: 'Tab', | ||
location: 0, | ||
which: 9, | ||
}, | ||
}; | ||
for (let f = 1; f <= 12; f += 1) { | ||
keyMap[`f${f}`] = { | ||
code: `F${f}`, | ||
key: `F${f}`, | ||
location: 0, | ||
which: f + 111, | ||
}; | ||
} | ||
|
||
const applyPayload = (event: Event, payload?: string): void => { | ||
const keyData: object = {}; | ||
for (const key of payload ? payload.split('.') : []) { | ||
let map = keyMap[key]; | ||
if (!map && key.length === 1) { | ||
map = { | ||
code: `Key${key.toUpperCase()}`, | ||
key, | ||
which: key.charCodeAt(0), | ||
}; | ||
} | ||
|
||
if (!map) { | ||
throw new Error(`Unknown event part ${key}`); | ||
} | ||
|
||
mockHelperStub(keyData, map); | ||
} | ||
|
||
if (payload) { | ||
mockHelperStub(event, keyData); | ||
} | ||
}; | ||
|
||
export default ( | ||
event: string, | ||
init?: EventInit, | ||
overrides?: Partial<UIEvent | KeyboardEvent | MouseEvent | TouchEvent | Event>, | ||
): Event => { | ||
const dot = event.indexOf('.'); | ||
const [eventName, eventPayload] = dot === -1 ? [event] : [event.substr(0, dot), event.substr(dot + 1)]; | ||
const eventObj = eventCtor(eventName, { | ||
bubbles: preventBubble.indexOf(event) === -1, | ||
cancelable: true, | ||
...init, | ||
}); | ||
applyPayload(eventObj, eventPayload); | ||
|
||
if (overrides) { | ||
mockHelperStub(eventObj, overrides); | ||
} | ||
|
||
return eventObj; | ||
}; |
56 changes: 56 additions & 0 deletions
56
libs/ng-mocks/src/lib/mock-helper/events/mock-helper.trigger.ts
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,56 @@ | ||
import { DebugElement } from '@angular/core'; | ||
|
||
import mockHelperStub from '../mock-helper.stub'; | ||
|
||
import mockHelperEvent from './mock-helper.event'; | ||
|
||
/** | ||
* @see https://developer.mozilla.org/de/docs/Web/Events | ||
*/ | ||
const preventBubble = ['focus', 'blur', 'load', 'unload', 'change', 'reset', 'scroll']; | ||
|
||
const toEventObj = ( | ||
event: string | UIEvent | KeyboardEvent | MouseEvent | TouchEvent | Event, | ||
debugElement?: DebugElement | null, | ||
): Event => { | ||
const eventObj = | ||
typeof event === 'string' | ||
? mockHelperEvent(event, { | ||
bubbles: preventBubble.indexOf(event) === -1, | ||
cancelable: true, | ||
}) | ||
: event; | ||
if (!eventObj.target && debugElement) { | ||
mockHelperStub(eventObj, { | ||
target: debugElement.nativeElement, | ||
}); | ||
} | ||
|
||
return eventObj; | ||
}; | ||
|
||
export default ( | ||
debugElement: DebugElement | undefined | null, | ||
eventName: string | UIEvent | KeyboardEvent | MouseEvent | TouchEvent | Event, | ||
payload?: Partial<UIEvent | KeyboardEvent | MouseEvent | TouchEvent | Event>, | ||
) => { | ||
if (!debugElement) { | ||
throw new Error( | ||
`Cannot trigger ${typeof eventName === 'string' ? eventName : eventName.type} event undefined element`, | ||
); | ||
} | ||
|
||
// nothing to emit on disabled elements | ||
if (debugElement.nativeElement.disabled) { | ||
return; | ||
} | ||
|
||
const callback = (event: Event) => { | ||
if (payload) { | ||
mockHelperStub(event, payload); | ||
} | ||
}; | ||
debugElement.nativeElement.addEventListener(eventName, callback, true); | ||
debugElement.nativeElement.dispatchEvent(toEventObj(eventName, debugElement)); | ||
debugElement.nativeElement.removeEventListener(eventName, callback, true); | ||
}; |
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
Oops, something went wrong.