-
Notifications
You must be signed in to change notification settings - Fork 842
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
Support multiple space-separated values within a single element's data-test-subj attribute #1587
Merged
cjcenizal
merged 6 commits into
elastic:master
from
cjcenizal:feature/multiple-test-subject-selectors
Mar 1, 2019
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4d384ad
Support multiple space-separated values within a single element's dat…
cjcenizal 42b6bfe
Update CHANGELOG.
cjcenizal cd7bb91
Add matcher argument to findTestSubject.
cjcenizal 774273f
Add tests.
cjcenizal 1148840
Merge branch 'master' of github.com:elastic/eui into feature/multiple…
cjcenizal d1bad33
Remove merge conflict token. Add link to matcher docs and check for v…
cjcenizal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,12 @@ | |
* Find node which matches a specific test subject selector. Returns ReactWrappers around DOM element, | ||
* https://github.com/airbnb/enzyme/tree/master/docs/api/ReactWrapper. | ||
* Common use cases include calling simulate or getDOMNode on the returned ReactWrapper. | ||
* | ||
* The ~= matcher looks for the value in space-separated list, allowing support for multiple data-test-subj | ||
* values on a single element. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add a link to the attribute selector spec or similar to give a complete view of what There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great idea! |
||
*/ | ||
export const findTestSubject = (mountedComponent, testSubjectSelector) => { | ||
// The ~ looks for the value in space-separated list, allowing support for multiple data-test-subj | ||
// values on a single element. | ||
const testSubject = mountedComponent.find(`[data-test-subj~="${testSubjectSelector}"]`); | ||
export const findTestSubject = (mountedComponent, testSubjectSelector, matcher = '~=') => { | ||
const testSubject = mountedComponent.find(`[data-test-subj${matcher}"${testSubjectSelector}"]`); | ||
|
||
// Restores Enzyme 2's find behavior, which was to only return ReactWrappers around DOM elements. | ||
// Enzyme 3 returns ReactWrappers around both DOM elements and React components. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
|
||
import { findTestSubject } from './find_test_subject'; | ||
|
||
describe('findTestSubject', () => { | ||
test('finds the specified element in a mounted component', () => { | ||
const TestComponent = () => <div data-test-subj="test" />; | ||
const component = mount(<TestComponent />); | ||
const element = findTestSubject(component, 'test'); | ||
|
||
expect(element.length).toBe(1); | ||
}); | ||
|
||
test('finds the specified element even if it has multiple identifiers', () => { | ||
const TestComponent = () => <div data-test-subj="test1 test2" />; | ||
const component = mount(<TestComponent />); | ||
const element = findTestSubject(component, 'test2'); | ||
|
||
expect(element.length).toBe(1); | ||
}); | ||
|
||
test('finds multiple elements with the same identifier', () => { | ||
const TestComponent = () => ( | ||
<div> | ||
<div data-test-subj="test" /> | ||
<div data-test-subj="test" /> | ||
</div> | ||
); | ||
const component = mount(<TestComponent />); | ||
const element = findTestSubject(component, 'test'); | ||
|
||
expect(element.length).toBe(2); | ||
}); | ||
|
||
describe('matcher optional argument', () => { | ||
test('finds multiple elements with identifiers beginning with the same string', () => { | ||
const TestComponent = () => ( | ||
<div> | ||
<div data-test-subj="test1" /> | ||
<div data-test-subj="test2" /> | ||
</div> | ||
); | ||
const component = mount(<TestComponent />); | ||
const element = findTestSubject(component, 'test', '^='); | ||
|
||
expect(element.length).toBe(2); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merge remnants
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops!