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

Throwing an error on missing selector in test harness trigger #320

Merged
merged 3 commits into from Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/testing/harness.ts
Expand Up @@ -133,16 +133,19 @@ export function harness(
trigger(selector: string, functionSelector: string | FunctionalSelector, ...args: any[]): any {
_tryRender();
const [firstItem] = select(selector, _getRender());
if (firstItem) {
let triggerFunction: Function | undefined;
if (typeof functionSelector === 'string') {
triggerFunction = (firstItem.properties as any)[functionSelector];
} else {
triggerFunction = functionSelector(firstItem);
}
if (triggerFunction) {
return triggerFunction.apply(widget, args);
}

if (!firstItem) {
throw new Error(`Cannot find node with selector ${selector}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this error message. It might be good to make this and the assertion template errors consistent.

throw Error('Node not found');

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't disagree, but updating the message there is a bit more complicated. I've made the change though, we'll see how it goes 😄

}

let triggerFunction: Function | undefined;
if (typeof functionSelector === 'string') {
triggerFunction = (firstItem.properties as any)[functionSelector];
} else {
triggerFunction = functionSelector(firstItem);
}
if (triggerFunction) {
return triggerFunction.apply(widget, args);
}
},
getRender(index?: number): DNode | DNode[] {
Expand Down
24 changes: 4 additions & 20 deletions tests/testing/unit/harness.ts
Expand Up @@ -271,18 +271,7 @@ describe('harness', () => {
w<ChildWidget>('registry-item', { key: 'registry', id: 'random-id' })
])
);
h.trigger('*[key="other"]', 'onclick');
h.expect(() =>
v('div', { classes: ['root', 'other'], onclick: () => {} }, [
v(
'span',
{ key: 'span', classes: 'span', style: 'width: 100px', id: 'random-id', onclick: () => {} },
['hello 0']
),
w(ChildWidget, { key: 'widget', id: 'random-id', func: noop }),
w<ChildWidget>('registry-item', { key: 'registry', id: 'random-id' })
])
);
assert.throws(() => h.trigger('*[key="other"]', 'onclick'));
});

it('trigger by pseudo selector', () => {
Expand Down Expand Up @@ -520,14 +509,9 @@ describe('harness', () => {

it('trigger with non matching selector', () => {
const h = harness(() => w(ArrayWidget, {}));
h.trigger('*[key="other"]', 'onclick');
h.expect(() => [
v('span', { key: 'span', classes: 'span', style: 'width: 100px', id: 'random-id', onclick: () => {} }, [
'hello 0'
]),
w(ChildWidget, { key: 'widget', id: 'random-id' }),
w<ChildWidget>('registry-item', { key: 'registry', id: 'random-id' })
]);
assert.throws(() => {
h.trigger('*[key="other"]', 'onclick');
});
});

it('custom compare for VNode', () => {
Expand Down
13 changes: 3 additions & 10 deletions tests/testing/unit/harnessWithTsx.tsx
@@ -1,4 +1,5 @@
const { describe, it } = intern.getInterface('bdd');
const { assert } = intern.getPlugin('chai');

import { harness } from '../../../src/testing/harness';
import { WidgetBase } from '../../../src/widget-core/WidgetBase';
Expand Down Expand Up @@ -181,16 +182,8 @@ describe('harness with tsx', () => {

it('trigger with non matching selector', () => {
const h = harness(() => <MyWidget />);
h.trigger('*[key="other"]', 'onclick');
h.expect(() => (
<div classes={['root', 'other']} onclick={noop}>
<span key="span" classes="span" style="widget: 100px" id="random-id" onclick={noop}>
hello 0
</span>
<ChildWidget key="widget" id="random-id" />
<RegistryWidget key="registry" id="random-id" />
</div>
));

assert.throws(() => h.trigger('*[key="other"]', 'onclick'));
});

it('custom compare for VNode', () => {
Expand Down