From 857760c5b21ef0c145bb798a2f368e25a74efaea Mon Sep 17 00:00:00 2001 From: Jordan Harband Date: Sat, 6 Aug 2016 15:15:43 -0700 Subject: [PATCH] =?UTF-8?q?[Fix]=20include=20method=20name=20in=20?= =?UTF-8?q?=E2=80=9Csingle=20node=E2=80=9D=20errors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Per https://github.com/airbnb/enzyme/issues/184#issuecomment-238045505 --- src/ReactWrapper.js | 40 ++++++++++++++++++++-------------------- src/ShallowWrapper.js | 40 ++++++++++++++++++++-------------------- 2 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/ReactWrapper.js b/src/ReactWrapper.js index 7c103b49a..31ae4337a 100644 --- a/src/ReactWrapper.js +++ b/src/ReactWrapper.js @@ -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; @@ -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; @@ -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; @@ -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)); } /** @@ -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)); } /** @@ -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. @@ -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); } /** @@ -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, ''); @@ -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) { @@ -512,7 +512,7 @@ export default class ReactWrapper { * @returns {Object} */ props() { - return this.single(propsOfNode); + return this.single('props', propsOfNode); } /** @@ -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]; } @@ -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]; } @@ -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)); } /** @@ -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; } @@ -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); } /** @@ -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))); } /** @@ -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))); } /** @@ -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)); } /** @@ -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); diff --git a/src/ShallowWrapper.js b/src/ShallowWrapper.js index b097cb606..0b5781a8f 100644 --- a/src/ShallowWrapper.js +++ b/src/ShallowWrapper.js @@ -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]; }); @@ -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; @@ -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(); @@ -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)); } /** @@ -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)); } /** @@ -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)); } /** @@ -500,7 +500,7 @@ export default class ShallowWrapper { * @returns {String} */ text() { - return this.single(getTextFromNode); + return this.single('text', getTextFromNode); } /** @@ -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); @@ -570,7 +570,7 @@ export default class ShallowWrapper { * @returns {Object} */ props() { - return this.single(propsOfNode); + return this.single('props', propsOfNode); } /** @@ -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]; } @@ -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]; } @@ -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)); } /** @@ -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; } @@ -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)); } /** @@ -703,7 +703,7 @@ export default class ShallowWrapper { * @returns {String} */ key() { - return this.single(n => n.key); + return this.single('key', n => n.key); } /** @@ -713,7 +713,7 @@ export default class ShallowWrapper { * @returns {String|Function} */ type() { - return this.single(typeOfNode); + return this.single('type', typeOfNode); } /** @@ -724,7 +724,7 @@ export default class ShallowWrapper { * @returns {String} */ name() { - return this.single(displayNameOfNode); + return this.single('name', displayNameOfNode); } /** @@ -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)); } /** @@ -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);