-
Notifications
You must be signed in to change notification settings - Fork 392
fix(CardHeightMatching): Fix debounce invocation in CardHeightMatching #301
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
Merged
jeff-phillips-18
merged 1 commit into
patternfly:master
from
dmiller9911:300-CardHeightMatchingFix
Apr 16, 2018
Merged
Changes from all commits
Commits
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 hidden or 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,7 @@ | ||
| export const mockDetach = jest.fn(); | ||
| export const ResizeSensor = jest.fn(function MockSlider(el, cb) { | ||
| this.el = el; | ||
| this.cb = cb; | ||
|
|
||
| this.detach = mockDetach; | ||
| }); |
This file contains hidden or 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 hidden or 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,126 @@ | ||
| import React from 'react'; | ||
| import { mount } from 'enzyme'; | ||
| import { ResizeSensor, mockDetach } from 'css-element-queries'; | ||
| import CardHeightMatching from './CardHeightMatching'; | ||
| import * as helpers from '../../common/helpers'; | ||
|
|
||
| jest.mock('css-element-queries'); | ||
|
|
||
| jest.spyOn(helpers, 'debounce').mockImplementation(v => v); | ||
| const mockQuerySelectorAll = jest.fn(); | ||
| HTMLElement.prototype.querySelectorAll = mockQuerySelectorAll; | ||
|
|
||
| const mockElement = { | ||
| scrollHeight: 0, | ||
| style: { | ||
| height: 0 | ||
| } | ||
| }; | ||
|
|
||
| const props = { | ||
| selector: ['first', 'second'], | ||
| children: 'children' | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| mockDetach.mockClear(); | ||
| ResizeSensor.mockClear(); | ||
| helpers.debounce.mockClear(); | ||
| mockQuerySelectorAll.mockReturnValue([mockElement]); | ||
| }); | ||
|
|
||
| test('creates a ResizeSensor for each selector', () => { | ||
| mount(<CardHeightMatching {...props} />); | ||
|
Contributor
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.
|
||
| expect(ResizeSensor).toHaveBeenCalledTimes(props.selector.length); | ||
| expect(ResizeSensor).toBeCalledWith( | ||
| expect.arrayContaining([mockElement]), | ||
| expect.any(Function) | ||
| ); | ||
| }); | ||
|
|
||
| test('allows selector to be a string', () => { | ||
| mount(<CardHeightMatching {...props} selector="selector" />); | ||
| expect(ResizeSensor).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| test('debounces resize events', () => { | ||
| mount(<CardHeightMatching {...props} />); | ||
| expect(helpers.debounce).toBeCalledWith(expect.any(Function), 200); | ||
| }); | ||
|
|
||
| test('sets max height on mount', () => { | ||
| const firstElement = { | ||
| ...mockElement, | ||
| scrollHeight: 0 | ||
| }; | ||
| const secondElement = { | ||
| ...mockElement, | ||
| scrollHeight: 10 | ||
| }; | ||
| mockQuerySelectorAll.mockReturnValue([firstElement, secondElement]); | ||
| jest.useFakeTimers(); | ||
| mount(<CardHeightMatching {...props} />); | ||
| jest.runAllTimers(); | ||
| expect(firstElement.style.height).toBe(`${secondElement.scrollHeight}px`); | ||
| expect(secondElement.style.height).toBe(`${secondElement.scrollHeight}px`); | ||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| test('sets max height on update', () => { | ||
| const firstElement = { | ||
| ...mockElement, | ||
| scrollHeight: 0 | ||
| }; | ||
| const secondElement = { | ||
| ...mockElement, | ||
| scrollHeight: 10 | ||
| }; | ||
| mockQuerySelectorAll.mockReturnValue([firstElement, secondElement]); | ||
| jest.useFakeTimers(); | ||
| const view = mount(<CardHeightMatching {...props} />); | ||
| jest.runAllTimers(); | ||
| secondElement.scrollHeight = 20; | ||
| view.setProps(props); | ||
| jest.runAllTimers(); | ||
| expect(firstElement.style.height).toBe(`${secondElement.scrollHeight}px`); | ||
| expect(secondElement.style.height).toBe(`${secondElement.scrollHeight}px`); | ||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| test('detaches on unmount', () => { | ||
| const view = mount(<CardHeightMatching {...props} />); | ||
| view.unmount(); | ||
| expect(mockDetach).toHaveBeenCalledTimes(props.selector.length); | ||
| }); | ||
|
|
||
| test('handles resize trigger without a container', () => { | ||
| let debounceHandler; | ||
| helpers.debounce.mockImplementationOnce(handler => { | ||
| debounceHandler = handler; | ||
| return handler; | ||
| }); | ||
| jest.useFakeTimers(); | ||
| const view = mount(<CardHeightMatching {...props} />); | ||
| jest.runAllTimers(); | ||
| view.unmount(); | ||
| mockQuerySelectorAll.mockClear(); | ||
| debounceHandler(); | ||
| expect(mockQuerySelectorAll).not.toBeCalled(); | ||
| jest.useRealTimers(); | ||
| }); | ||
|
|
||
| test('sets height on update', () => { | ||
| let debounceHandler; | ||
| helpers.debounce.mockImplementationOnce(handler => { | ||
| debounceHandler = handler; | ||
| return handler; | ||
| }); | ||
| jest.useFakeTimers(); | ||
| const view = mount(<CardHeightMatching {...props} />); | ||
| jest.runAllTimers(); | ||
| view.unmount(); | ||
| mockQuerySelectorAll.mockClear(); | ||
| debounceHandler(); | ||
| expect(mockQuerySelectorAll).not.toBeCalled(); | ||
| jest.useRealTimers(); | ||
| }); | ||
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.
just in case...it doesn't hurt...thanks!
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.
yeah, this was the source of the error @michaelkro mentioned before. Since _matchHeights is called inside of debounce, this can be called after the component has unmounted which will make _container null. I didn't see a good way to cancel the debounced call, unfortunately.