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

Adding better errors when passing null or undefined to modifiers #37

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions addon/modifiers/did-insert.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { assert } from '@ember/debug';
import { setModifierManager, capabilities } from '@ember/modifier';

/**
Expand Down Expand Up @@ -54,11 +55,16 @@ export default setModifierManager(
_state,
element,
{
positional: [fn, ...args],
positional: [didInsertFunction, ...args],
named,
}
) {
fn(element, args, named);
assert(
`You need to pass a function as the first argument to \`did-insert\` you passed ${didInsertFunction}`,
typeof didInsertFunction === 'function'
);

didInsertFunction(element, args, named);
},

updateModifier() {},
Expand Down
10 changes: 8 additions & 2 deletions addon/modifiers/did-update.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { assert } from '@ember/debug';
import { setModifierManager, capabilities } from '@ember/modifier';

/**
Expand Down Expand Up @@ -69,9 +70,14 @@ export default setModifierManager(
},

updateModifier({ element }, args) {
let [fn, ...positional] = args.positional;
let [didUpdateFunction, ...positional] = args.positional;

fn(element, positional, args.named);
assert(
`You need to pass a function as the first argument to \`did-update\` you passed ${didUpdateFunction}`,
typeof didUpdateFunction === 'function'
);

didUpdateFunction(element, positional, args.named);
},

destroyModifier() {},
Expand Down
10 changes: 8 additions & 2 deletions addon/modifiers/will-destroy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { assert } from '@ember/debug';
import { setModifierManager, capabilities } from '@ember/modifier';

/**
Expand Down Expand Up @@ -53,9 +54,14 @@ export default setModifierManager(
updateModifier() {},

destroyModifier({ element }, args) {
let [fn, ...positional] = args.positional;
let [willDestroyFunction, ...positional] = args.positional;

fn(element, positional, args.named);
assert(
`You need to pass a function as the first argument to \`will-destroy\` you passed ${willDestroyFunction}`,
typeof willDestroyFunction === 'function'
);

willDestroyFunction(element, positional, args.named);
},
}),
class WillDestroyModifier {}
Expand Down
17 changes: 16 additions & 1 deletion tests/integration/modifiers/did-insert-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import { render, setupOnerror } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import Component from '@ember/component';

Expand Down Expand Up @@ -76,4 +76,19 @@ module('Integration | Modifier | did-insert', function(hooks) {

assert.dom('.alert').hasClass('fade-in');
});

test('throws meaningful error when did-insert does not receive a function', async function(assert) {
assert.expect(1);

this.notAFunction = null;

setupOnerror(function(error) {
assert.equal(
error.message,
'Assertion Failed: You need to pass a function as the first argument to `did-insert` you passed null'
);
});

await render(hbs`<div {{did-insert this.notAFunction}}></div>`);
});
});
19 changes: 18 additions & 1 deletion tests/integration/modifiers/did-update-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import { render, setupOnerror } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';

module('Integration | Modifier | did-update', function(hooks) {
Expand All @@ -24,4 +24,21 @@ module('Integration | Modifier | did-update', function(hooks) {

this.set('boundValue', 'update');
});

test('throws meaningful error when did-update does not receive a function', async function(assert) {
assert.expect(1);

this.notAFunction = null;
this.set('boundValue', 'initial');

setupOnerror(function(error) {
assert.equal(
error.message,
'Assertion Failed: You need to pass a function as the first argument to `did-update` you passed null'
);
});

await render(hbs`<div {{did-update this.notAFunction this.boundValue}}></div>`);
this.set('boundValue', 'update');
});
});
21 changes: 20 additions & 1 deletion tests/integration/modifiers/will-destroy-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import { render, setupOnerror } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';

module('Integration | Modifier | will-destroy', function(hooks) {
Expand Down Expand Up @@ -43,4 +43,23 @@ module('Integration | Modifier | will-destroy', function(hooks) {
// trigger destroy
this.set('show', false);
});

test('throws meaningful error when will-destroy does not receive a function', async function(assert) {
assert.expect(2);
kiwiupover marked this conversation as resolved.
Show resolved Hide resolved

this.notAFunction = null;
this.set('show', true);

setupOnerror(function(error) {
assert.equal(
error.message,
'Assertion Failed: You need to pass a function as the first argument to `will-destroy` you passed null'
);
});

await render(hbs`{{#if show}}<div {{will-destroy this.notAFunction}}></div>{{/if}}`);

// trigger destroy
this.set('show', false);
});
});