Skip to content
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 force argument to triggerEvent #1521

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion addon/src/dom/trigger-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ registerHook('triggerEvent', 'start', (target: Target, eventType: string) => {
* @param {string|Element|IDOMElementDescriptor} target the element, selector, or descriptor to trigger the event on
* @param {string} eventType the type of event to trigger
* @param {Object} options additional properties to be set on the event
* @param {boolean} force if true, will bypass availability checks (false by default)
* @return {Promise<void>} resolves when the application is settled
*
* @example
Expand Down Expand Up @@ -58,6 +59,7 @@ export default function triggerEvent(
target: Target,
eventType: string,
options?: Record<string, unknown>,
force: boolean = false,
): Promise<void> {
return Promise.resolve()
.then(() => {
Expand All @@ -82,7 +84,7 @@ export default function triggerEvent(
);
}

if (isFormControl(element) && element.disabled) {
if (!force && isFormControl(element) && element.disabled) {
throw new Error(`Can not \`triggerEvent\` on disabled ${element}`);
}

Expand Down
18 changes: 18 additions & 0 deletions test-app/tests/unit/dom/trigger-event-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,24 @@ module('DOM Helper: triggerEvent', function (hooks) {
);
});

test('allows triggering events on disabled form control when `force` is true', async function (assert) {
element = buildInstrumentedElement('textarea');
element.setAttribute('disabled', true);

element.addEventListener('fliberty', (e) => {
assert.step('fliberty');
assert.ok(
e instanceof Event,
`fliberty listener receives a native event`
);
});

await setupContext(context);
await triggerEvent(element, 'fliberty', {}, true);

assert.verifySteps(['fliberty']);
});

test('events properly bubble upwards', async function (assert) {
await setupContext(context);
element = buildInstrumentedElement('div');
Expand Down
3 changes: 2 additions & 1 deletion type-tests/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ expectTypeOf(triggerEvent).toEqualTypeOf<
(
target: Target,
eventType: string,
options?: Record<string, unknown>
options?: Record<string, unknown>,
force?: boolean,
) => Promise<void>
>();
expectTypeOf(triggerKeyEvent).toEqualTypeOf<
Expand Down