Skip to content

Commit

Permalink
[Fix] include method name in “single node” errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Aug 6, 2016
1 parent 13f84a3 commit 857760c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
40 changes: 20 additions & 20 deletions src/ReactWrapper.js
Expand Up @@ -152,7 +152,7 @@ export default class ReactWrapper {
// TODO(lmr): this requirement may not be necessary for the ReactWrapper
throw new Error('ReactWrapper::update() can only be called on the root');
}
this.single(() => {
this.single('update', () => {
this.component.forceUpdate();
});
return this;
Expand All @@ -168,7 +168,7 @@ export default class ReactWrapper {
if (this.root !== this) {
throw new Error('ReactWrapper::unmount() can only be called on the root');
}
this.single(() => {
this.single('unmount', () => {
this.component.setState({ mount: false });
});
return this;
Expand All @@ -184,7 +184,7 @@ export default class ReactWrapper {
if (this.root !== this) {
throw new Error('ReactWrapper::mount() can only be called on the root');
}
this.single(() => {
this.single('mount', () => {
this.component.setState({ mount: true });
});
return this;
Expand Down Expand Up @@ -271,7 +271,7 @@ export default class ReactWrapper {
* @returns {Boolean}
*/
matchesElement(node) {
return this.single(() => instEqual(node, this.node, (a, b) => a <= b));
return this.single('matchesElement', () => instEqual(node, this.node, (a, b) => a <= b));
}

/**
Expand Down Expand Up @@ -387,7 +387,7 @@ export default class ReactWrapper {
*/
is(selector) {
const predicate = buildInstPredicate(selector);
return this.single(n => predicate(n));
return this.single('is', n => predicate(n));
}

/**
Expand All @@ -396,7 +396,7 @@ export default class ReactWrapper {
* @returns {boolean}
*/
isEmptyRender() {
return this.single((n) => {
return this.single('isEmptyRender', (n) => {
// Stateful components and stateless function components have different internal structures,
// so we need to find the correct internal instance, and validate the rendered node type
// equals 2, which is the `ReactNodeTypes.EMPTY` value.
Expand Down Expand Up @@ -453,7 +453,7 @@ export default class ReactWrapper {
* @returns {String}
*/
text() {
return this.single(n => findDOMNode(n).textContent);
return this.single('text', n => findDOMNode(n).textContent);
}

/**
Expand All @@ -464,7 +464,7 @@ export default class ReactWrapper {
* @returns {String}
*/
html() {
return this.single(n => {
return this.single('html', n => {
const node = findDOMNode(n);
return node === null ? null :
node.outerHTML.replace(/\sdata-(reactid|reactroot)+="([^"]*)+"/g, '');
Expand Down Expand Up @@ -492,7 +492,7 @@ export default class ReactWrapper {
* @returns {ReactWrapper}
*/
simulate(event, mock = {}) {
this.single(n => {
this.single('simulate', n => {
const mappedEvent = mapNativeEventNames(event);
const eventFn = Simulate[mappedEvent];
if (!eventFn) {
Expand All @@ -512,7 +512,7 @@ export default class ReactWrapper {
* @returns {Object}
*/
props() {
return this.single(propsOfNode);
return this.single('props', propsOfNode);
}

/**
Expand All @@ -528,7 +528,7 @@ export default class ReactWrapper {
if (this.root !== this) {
throw new Error('ReactWrapper::state() can only be called on the root');
}
const _state = this.single(() => this.instance().state);
const _state = this.single('state', () => this.instance().state);
if (name !== undefined) {
return _state[name];
}
Expand All @@ -548,7 +548,7 @@ export default class ReactWrapper {
if (this.root !== this) {
throw new Error('ReactWrapper::context() can only be called on the root');
}
const _context = this.single(() => this.instance().context);
const _context = this.single('context', () => this.instance().context);
if (name !== undefined) {
return _context[name];
}
Expand All @@ -573,7 +573,7 @@ export default class ReactWrapper {
* @returns {ReactWrapper}
*/
childAt(index) {
return this.single(() => this.children().at(index));
return this.single('childAt', () => this.children().at(index));
}

/**
Expand All @@ -586,7 +586,7 @@ export default class ReactWrapper {
* @returns {ReactWrapper}
*/
parents(selector) {
const allParents = this.wrap(this.single(n => parentsOfInst(n, this.root.node)));
const allParents = this.wrap(this.single('parents', n => parentsOfInst(n, this.root.node)));
return selector ? allParents.filter(selector) : allParents;
}

Expand Down Expand Up @@ -624,7 +624,7 @@ export default class ReactWrapper {
* @returns {String}
*/
key() {
return this.single(n => getNode(n).key);
return this.single('key', n => getNode(n).key);
}

/**
Expand All @@ -634,7 +634,7 @@ export default class ReactWrapper {
* @returns {String|Function}
*/
type() {
return this.single(n => typeOfNode(getNode(n)));
return this.single('type', n => typeOfNode(getNode(n)));
}

/**
Expand All @@ -645,7 +645,7 @@ export default class ReactWrapper {
* @returns {String}
*/
name() {
return this.single(n => displayNameOfNode(getNode(n)));
return this.single('name', n => displayNameOfNode(getNode(n)));
}

/**
Expand All @@ -664,7 +664,7 @@ export default class ReactWrapper {
'hasClass() expects a class name, not a CSS selector.'
);
}
return this.single(n => instHasClassName(n, className));
return this.single('hasClass', n => instHasClassName(n, className));
}

/**
Expand Down Expand Up @@ -847,10 +847,10 @@ export default class ReactWrapper {
* @param {Function} fn
* @returns {*}
*/
single(fn) {
single(name, fn) {
if (this.length !== 1) {
throw new Error(
`This method is only meant to be run on single node. ${this.length} found instead.`
`Method “${name} is only meant to be run on a single node. ${this.length} found instead.`
);
}
return fn.call(this, this.node);
Expand Down
40 changes: 20 additions & 20 deletions src/ShallowWrapper.js
Expand Up @@ -132,7 +132,7 @@ export default class ShallowWrapper {
if (this.root !== this) {
throw new Error('ShallowWrapper::update() can only be called on the root');
}
this.single(() => {
this.single('update', () => {
this.node = this.renderer.getRenderOutput();
this.nodes = [this.node];
});
Expand All @@ -150,7 +150,7 @@ export default class ShallowWrapper {
* @returns {ShallowWrapper}
*/
rerender(props, context) {
this.single(() => {
this.single('rerender', () => {
withSetStateAllowed(() => {
const instance = this.instance();
const state = instance.state;
Expand Down Expand Up @@ -250,7 +250,7 @@ export default class ShallowWrapper {
if (isFunctionalComponent(this.instance())) {
throw new Error('ShallowWrapper::setState() can only be called on class components');
}
this.single(() => {
this.single('setState', () => {
withSetStateAllowed(() => {
this.instance().setState(state);
this.update();
Expand Down Expand Up @@ -398,7 +398,7 @@ export default class ShallowWrapper {
* @returns {Boolean}
*/
equals(node) {
return this.single(() => nodeEqual(this.node, node));
return this.single('equals', () => nodeEqual(this.node, node));
}

/**
Expand All @@ -419,7 +419,7 @@ export default class ShallowWrapper {
* @returns {Boolean}
*/
matchesElement(node) {
return this.single(() => nodeEqual(node, this.node, (a, b) => a <= b));
return this.single('matchesElement', () => nodeEqual(node, this.node, (a, b) => a <= b));
}

/**
Expand All @@ -442,7 +442,7 @@ export default class ShallowWrapper {
*/
is(selector) {
const predicate = buildPredicate(selector);
return this.single(n => predicate(n));
return this.single('is', n => predicate(n));
}

/**
Expand Down Expand Up @@ -500,7 +500,7 @@ export default class ShallowWrapper {
* @returns {String}
*/
text() {
return this.single(getTextFromNode);
return this.single('text', getTextFromNode);
}

/**
Expand All @@ -511,7 +511,7 @@ export default class ShallowWrapper {
* @returns {String}
*/
html() {
return this.single(n => {
return this.single('html', n => {
// NOTE: splitting this into two statements is required to make the linter happy.
const isNull = this.type() === null;
return isNull ? null : renderToStaticMarkup(n);
Expand Down Expand Up @@ -570,7 +570,7 @@ export default class ShallowWrapper {
* @returns {Object}
*/
props() {
return this.single(propsOfNode);
return this.single('props', propsOfNode);
}

/**
Expand All @@ -589,7 +589,7 @@ export default class ShallowWrapper {
if (isFunctionalComponent(this.instance())) {
throw new Error('ShallowWrapper::state() can only be called on class components');
}
const _state = this.single(() => this.instance().state);
const _state = this.single('state', () => this.instance().state);
if (name !== undefined) {
return _state[name];
}
Expand All @@ -615,7 +615,7 @@ export default class ShallowWrapper {
'a context option'
);
}
const _context = this.single(() => this.instance().context);
const _context = this.single('context', () => this.instance().context);
if (name) {
return _context[name];
}
Expand All @@ -640,7 +640,7 @@ export default class ShallowWrapper {
* @returns {ShallowWrapper}
*/
childAt(index) {
return this.single(() => this.children().at(index));
return this.single('childAt', () => this.children().at(index));
}

/**
Expand All @@ -653,7 +653,7 @@ export default class ShallowWrapper {
* @returns {ShallowWrapper}
*/
parents(selector) {
const allParents = this.wrap(this.single(n => parentsOfNode(n, this.root.node)));
const allParents = this.wrap(this.single('parents', n => parentsOfNode(n, this.root.node)));
return selector ? allParents.filter(selector) : allParents;
}

Expand Down Expand Up @@ -684,7 +684,7 @@ export default class ShallowWrapper {
* @returns {ShallowWrapper}
*/
shallow(options) {
return this.single(n => new ShallowWrapper(n, null, options));
return this.single('shallow', n => new ShallowWrapper(n, null, options));
}

/**
Expand All @@ -703,7 +703,7 @@ export default class ShallowWrapper {
* @returns {String}
*/
key() {
return this.single(n => n.key);
return this.single('key', n => n.key);
}

/**
Expand All @@ -713,7 +713,7 @@ export default class ShallowWrapper {
* @returns {String|Function}
*/
type() {
return this.single(typeOfNode);
return this.single('type', typeOfNode);
}

/**
Expand All @@ -724,7 +724,7 @@ export default class ShallowWrapper {
* @returns {String}
*/
name() {
return this.single(displayNameOfNode);
return this.single('name', displayNameOfNode);
}

/**
Expand All @@ -743,7 +743,7 @@ export default class ShallowWrapper {
'hasClass() expects a class name, not a CSS selector.'
);
}
return this.single(n => hasClassName(n, className));
return this.single('hasClass', n => hasClassName(n, className));
}

/**
Expand Down Expand Up @@ -927,10 +927,10 @@ export default class ShallowWrapper {
* @param fn
* @returns {*}
*/
single(fn) {
single(name, fn) {
if (this.length !== 1) {
throw new Error(
`This method is only meant to be run on single node. ${this.length} found instead.`
`Method “${name} is only meant to be run on a single node. ${this.length} found instead.`
);
}
return fn.call(this, this.node);
Expand Down

0 comments on commit 857760c

Please sign in to comment.