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

toHaveClass should wrap existing matcher #1

Closed
jcarlson opened this issue Mar 9, 2017 · 4 comments
Closed

toHaveClass should wrap existing matcher #1

jcarlson opened this issue Mar 9, 2017 · 4 comments

Comments

@jcarlson
Copy link

jcarlson commented Mar 9, 2017

My test suite already has a custom matcher called toHaveClass (and also toNotHaveClass). So expect-enzyme clashes with this existing matcher and breaks tests which are expecting the non-enzymified version of the matcher.

Looks like it should be trivial to addEnzymeSupport to resolve this...

@PsychoLlama
Copy link
Owner

Hey @jcarlson! Sorry things are breaking. You're right, it should check for an existing method before registering a new one. What type of value is .toHaveClass expecting on your side?

@jcarlson
Copy link
Author

jcarlson commented Mar 9, 2017

My matchers are expecting a node and a string to assert that the node has that class. Such as:

class MyComponent extends Component {
  render () {
    return <div className='my-component'>Hello!</div>
  }
}

let rendered = ReactDOM.render(<MyComponent />, document.createElement('div'))
let node = ReactDOM.findDOMNode(rendered)
expect(node).toHaveClass('my-component')

Here's my matchers:

expect.extend({
  toHaveClass (className) {
    expect.assert(
      this.actual.classList ?
        this.actual.classList.contains(className) :
        false,
      'Expected class list %s to contain  %s.',
      this.actual.classList ?
        Array.from(this.actual.classList) :
        '(not a DOM element)',
      className
    )
    return this
  },

  toNotHaveClass (className) {
    expect.assert(
      this.actual.classList ?
        !this.actual.classList.contains(className) :
        true,
      'Expected class list %s to not contain  %s.',
      this.actual.classList ?
        Array.from(this.actual.classList) :
        '(not a DOM element)',
      className
    )
    return this
  }
})

@PsychoLlama
Copy link
Owner

Sweet, thanks!
I'm hoping to add a few new features this weekend, I'll be sure to prioritize this.

PsychoLlama added a commit that referenced this issue Mar 11, 2017
If a user of this library already has custom assertions of the same
name, previously, this library would completely overwrite them. Now it
Attempts to wrap the existing method, falling back if `actual` is not an
enzyme wrapper. If the built-in doesn't exist, the previous behavior is
kept (assert actual is enzyme).

Please refer to issue #1 for more details.
@PsychoLlama
Copy link
Owner

I've just pushed out v0.13.2 which should resolve your issue. Just be sure you import (and extend) your custom assertions before loading expect-enzyme. I'm closing this issue for now, but feel free to re-open if you need!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants