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

feat: add exact mode option for toHaveClass (#176) #217

Merged
merged 4 commits into from Mar 26, 2020

Conversation

SergiCL
Copy link
Contributor

@SergiCL SergiCL commented Mar 17, 2020

Added exact mode option for toHaveClass matcher, as discussed in #176

How

<div data-test-id='test-element' class="hello world foo"></div>
const testElement = getByTestId('test-element')

// toHaveClas is true becose has EXACTLY defined set of classes
expect(testElement).toHaveClass('hello', 'world', 'foo', {exact: true})

// toHaveClas is false because has te set of classes, but it also has 'foo'
expect(testElement).not.toHaveClass('hello', 'world', {exact: true})

// if exact option is not passed or is set as false, it will continue working as before.
expect(testElement).toHaveClass('hello', 'world')
expect(testElement).toHaveClass('hello', 'world', {exact: false})

If exact option is not passed or is false, it will continue working as before these changes.

Checklist:

  • Documentation
  • Tests
  • Updated Type Definitions
  • Ready to be merged

@gnapse
Copy link
Member

gnapse commented Mar 18, 2020

Oh no @SergiCL, I'm sorry it was not clear enough, but the API we decided when discussing this in #176 was to add a new matcher:

expect(element).toHaveClassList('class1', 'class2', 'class3')

Could you change this PR to add that instead, and leave toHaveClass intact?

@SergiCL
Copy link
Contributor Author

SergiCL commented Mar 18, 2020

Sure, no problem. I didn't understand it correctly.

expect(element).toHaveClassList('class1', 'class2', 'class3')

I think the name toHaveClassList can lead to confusion. jest-extended library makes a distinction between array matchers that could be used here:

  • .toIncludeAllMembers([members])
  • .toIncludeSameMembers([members])
  • .toIncludeAnyMembers([members])

In my view something like toHaveSameClass could be clearer, and leaves the door open for a possible toHaveAnyClass if necessary in the future.

By the way, do you know why Travis' validation is failing?
When I run it locally with the same version of node and npm it works without problem.

@gnapse
Copy link
Member

gnapse commented Mar 18, 2020

Can you bring up the naming change suggestion in the issue #176 (just so that the people that originally discuss this can chime in). I'll post my thoughts about it over there, but it kinda make sense your suggestion. Let's see what others think.

Copy link
Member

@benmonro benmonro left a comment

Choose a reason for hiding this comment

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

🎉 Thanks for this!

src/to-have-class.js Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Mar 21, 2020

Codecov Report

Merging #217 into master will not change coverage by %.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master      #217   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           19        19           
  Lines          245       257   +12     
  Branches        59        62    +3     
=========================================
+ Hits           245       257   +12     
Impacted Files Coverage Δ
src/to-have-class.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update eb51c17...01e9eeb. Read the comment docs.

@gnapse gnapse merged commit cc8721e into testing-library:master Mar 26, 2020
@gnapse
Copy link
Member

gnapse commented Mar 26, 2020

@all-contributors please add @SergiCL for code, test

@allcontributors
Copy link
Contributor

@gnapse

I've put up a pull request to add @SergiCL! 🎉

@gnapse
Copy link
Member

gnapse commented Mar 26, 2020

🎉 This PR is included in version 5.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Successfully merging this pull request may close these issues.

None yet

3 participants