-
Notifications
You must be signed in to change notification settings - Fork 5
feat: findClosest* test utilities
#115
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
Changes from all commits
b8bd469
4569665
b414b3f
1eb27da
5d9dd42
1aadd26
5d9ea02
b0af06d
362fd07
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -291,6 +291,75 @@ describe('DOM test utils', () => { | |
| expect(components[1].getElement().textContent).toBe('Old Button'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('findClosestComponent()', () => { | ||
| class ComponentAWrapper extends ComponentWrapper { | ||
| static rootSelector = 'component-a'; | ||
| } | ||
|
|
||
| class ComponentBWrapper extends ComponentWrapper { | ||
| static rootSelector = 'component-b'; | ||
| } | ||
|
|
||
| class UnrelatedWrapper extends ComponentWrapper { | ||
| static rootSelector = 'unrelated-component'; | ||
| } | ||
|
|
||
| const nestedElements = document.createElement('div'); | ||
| nestedElements.innerHTML = ` | ||
| <div class="component-a" data-testid="component-a-outer"> | ||
| <div class="component-b" data-testid="component-b-outer"> | ||
| <div class="component-a" data-testid="component-a-inner"> | ||
| <div class="component-b" data-testid="component-b-inner"> | ||
| <span data-testid="deep-child">target</span> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| <div class="component-b" data-testid="component-b-sibling"/> | ||
| `; | ||
|
|
||
| beforeEach(() => { | ||
| document.body.appendChild(nestedElements); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| document.body.removeChild(nestedElements); | ||
| }); | ||
|
|
||
| it('returns the closest parent matching the component type, skipping nearer components of different types', () => { | ||
| const deepChild = nestedElements.querySelector('[data-testid="deep-child"]')!; | ||
| const childWrapper = new ElementWrapper(deepChild); | ||
|
|
||
| const closestInner = childWrapper.findClosestComponent(ComponentAWrapper); | ||
| expect(closestInner).toBeInstanceOf(ComponentAWrapper); | ||
| expect(closestInner!.getElement().getAttribute('data-testid')).toBe('component-a-inner'); | ||
| }); | ||
|
|
||
| it('returns self when the element itself matches the component type', () => { | ||
SpyZzey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const inner = nestedElements.querySelector('[data-testid="component-b-inner"]')!; | ||
| const innerWrapper = new ElementWrapper(inner); | ||
|
|
||
| const closestInner = innerWrapper.findClosestComponent(ComponentBWrapper); | ||
| expect(closestInner!.getElement().getAttribute('data-testid')).toBe('component-b-inner'); | ||
| }); | ||
|
|
||
| it('returns null when no ancestor matches the component type', () => { | ||
| const deepChild = nestedElements.querySelector('[data-testid="deep-child"]')!; | ||
| const childWrapper = new ElementWrapper(deepChild); | ||
|
|
||
| const result = childWrapper.findClosestComponent(UnrelatedWrapper); | ||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it('does not match elements outside of the parent chain', () => { | ||
| const deepChild = nestedElements.querySelector('[data-testid="component-a-outer"]')!; | ||
| const childWrapper = new ElementWrapper(deepChild); | ||
|
|
||
| const result = childWrapper.findClosestComponent(ComponentBWrapper); | ||
| expect(result).toBeNull(); | ||
| }); | ||
| }); | ||
|
Member
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. I'd suggest adding test coverage for sibling structures like: When looking up the closest component for Component4 or Component3, the result should not match Component1 or Component2 as they're in a separate subtree
Member
Author
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. Added coverage for this in b0af06d.
Member
Author
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. And added a specific test for this in the latest commit |
||
| }); | ||
|
|
||
| describe('createWrapper', () => { | ||
|
|
||
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.
Deduplicated this logic which exists for both dom and selectors