-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
ref(ui): Enhance Carousel component #54747
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
evanpurkhiser
merged 1 commit into
master
from
evanpurkhiser/ref-ui-enhance-carousel-component
Aug 15, 2023
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 |
---|---|---|
@@ -1,79 +1,140 @@ | ||
import {render, screen} from 'sentry-test/reactTestingLibrary'; | ||
import {act, render, screen, userEvent} from 'sentry-test/reactTestingLibrary'; | ||
|
||
import Carousel from 'sentry/components/carousel'; | ||
import Placeholder from 'sentry/components/placeholder'; | ||
|
||
export function setIntersectionObserver( | ||
entries: {isIntersecting: boolean; target: {id: string}}[] | ||
) { | ||
(() => { | ||
return (global.IntersectionObserver = class IntersectionObserver { | ||
[x: string]: any; | ||
constructor(cb: any) { | ||
this.cb = cb; | ||
} | ||
observe() { | ||
this.cb(entries); | ||
} | ||
unobserve() {} | ||
disconnect() {} | ||
} as any); | ||
})(); | ||
} | ||
|
||
describe('Carousel', function () { | ||
beforeEach(() => {}); | ||
it('hides arrows if content does not overflow in x', function () { | ||
setIntersectionObserver([ | ||
{target: {id: 'left-anchor'}, isIntersecting: true}, | ||
{target: {id: 'right-anchor'}, isIntersecting: true}, | ||
]); | ||
let intersectionOnbserverCb: (entries: Partial<IntersectionObserverEntry>[]) => void = | ||
jest.fn(); | ||
|
||
window.IntersectionObserver = class IntersectionObserver { | ||
root = null; | ||
rootMargin = ''; | ||
thresholds = []; | ||
takeRecords = jest.fn(); | ||
|
||
constructor(cb: IntersectionObserverCallback) { | ||
// @ts-expect-error The callback wants just way too much stuff for our simple mock | ||
intersectionOnbserverCb = cb; | ||
} | ||
observe() {} | ||
unobserve() {} | ||
disconnect() {} | ||
}; | ||
|
||
it('hides arrows if content does not overflow in x', function () { | ||
render( | ||
<Placeholder width="200px" height="100px"> | ||
<Carousel> | ||
<Placeholder width="50px" height="50px" /> | ||
</Carousel> | ||
</Placeholder> | ||
<Carousel> | ||
<div data-test-id="child-1" /> | ||
</Carousel> | ||
); | ||
|
||
expect(screen.queryByLabelText('Scroll left')).not.toBeInTheDocument(); | ||
expect(screen.queryByLabelText('Scroll right')).not.toBeInTheDocument(); | ||
}); | ||
// Child is visible | ||
act(() => | ||
intersectionOnbserverCb([ | ||
{target: screen.getByTestId('child-1'), intersectionRatio: 1}, | ||
]) | ||
); | ||
|
||
it('does not show left arrow if all the way to the left', function () { | ||
setIntersectionObserver([ | ||
{target: {id: 'left-anchor'}, isIntersecting: true}, | ||
{target: {id: 'right-anchor'}, isIntersecting: false}, | ||
]); | ||
expect(screen.queryByRole('button', {name: 'Scroll left'})).not.toBeInTheDocument(); | ||
expect(screen.queryByRole('button', {name: 'Scroll right'})).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('shows right arrow when elements exist to the right', async function () { | ||
render( | ||
<Carousel> | ||
<Placeholder /> | ||
<Placeholder /> | ||
<Placeholder /> | ||
<div data-test-id="child-1" /> | ||
<div data-test-id="child-2" /> | ||
<div data-test-id="child-3" /> | ||
</Carousel> | ||
); | ||
|
||
expect(screen.queryByLabelText('Scroll left')).not.toBeInTheDocument(); | ||
expect(screen.queryByLabelText('Scroll right')).toBeInTheDocument(); | ||
}); | ||
const elements = [ | ||
screen.getByTestId('child-1'), | ||
screen.getByTestId('child-2'), | ||
screen.getByTestId('child-3'), | ||
]; | ||
|
||
it('does not show right arrow if all the way to the right', async function () { | ||
setIntersectionObserver([ | ||
{target: {id: 'left-anchor'}, isIntersecting: false}, | ||
{target: {id: 'right-anchor'}, isIntersecting: true}, | ||
]); | ||
// Element on the right is not visible | ||
act(() => | ||
intersectionOnbserverCb([ | ||
{target: elements[0], intersectionRatio: 1}, | ||
{target: elements[1], intersectionRatio: 0.5}, | ||
{target: elements[2], intersectionRatio: 0}, | ||
]) | ||
); | ||
|
||
const rightButton = screen.getByRole('button', {name: 'Scroll right'}); | ||
expect(screen.queryByRole('button', {name: 'Scroll left'})).not.toBeInTheDocument(); | ||
|
||
// Test scroll into view, the 2nd element should have it's 'scrollIntoView' called | ||
elements[1].scrollIntoView = jest.fn(); | ||
await userEvent.click(rightButton); | ||
expect(elements[1].scrollIntoView).toHaveBeenCalled(); | ||
}); | ||
|
||
it('shows left arrow when elements exist to the left', async function () { | ||
render( | ||
<Carousel> | ||
<Placeholder /> | ||
<Placeholder /> | ||
<Placeholder /> | ||
<div data-test-id="child-1" /> | ||
<div data-test-id="child-2" /> | ||
<div data-test-id="child-3" /> | ||
</Carousel> | ||
); | ||
|
||
expect(await screen.findByLabelText('Scroll left')).toBeInTheDocument(); | ||
expect(screen.queryByLabelText('Scroll right')).not.toBeInTheDocument(); | ||
const elements = [ | ||
screen.getByTestId('child-1'), | ||
screen.getByTestId('child-2'), | ||
screen.getByTestId('child-3'), | ||
]; | ||
|
||
// Element on the left is not visible | ||
act(() => | ||
intersectionOnbserverCb([ | ||
{target: elements[0], intersectionRatio: 0}, | ||
{target: elements[1], intersectionRatio: 1}, | ||
{target: elements[2], intersectionRatio: 1}, | ||
]) | ||
); | ||
|
||
const leftButton = screen.getByRole('button', {name: 'Scroll left'}); | ||
expect(screen.queryByRole('button', {name: 'Scroll right'})).not.toBeInTheDocument(); | ||
|
||
// Test scroll into view, the 1st element should have it's 'scrollIntoView' called | ||
elements[0].scrollIntoView = jest.fn(); | ||
await userEvent.click(leftButton); | ||
expect(elements[0].scrollIntoView).toHaveBeenCalled(); | ||
}); | ||
|
||
it('skips an element when it is past the visibleRatio', async function () { | ||
render( | ||
<Carousel visibleRatio={0.9}> | ||
<div data-test-id="child-1" /> | ||
<div data-test-id="child-2" /> | ||
<div data-test-id="child-3" /> | ||
</Carousel> | ||
); | ||
|
||
const elements = [ | ||
screen.getByTestId('child-1'), | ||
screen.getByTestId('child-2'), | ||
screen.getByTestId('child-3'), | ||
]; | ||
|
||
// Second element is MOSTLY visibile, past the | ||
act(() => | ||
intersectionOnbserverCb([ | ||
{target: elements[0], intersectionRatio: 1}, | ||
{target: elements[1], intersectionRatio: 0.95}, | ||
{target: elements[2], intersectionRatio: 0}, | ||
]) | ||
); | ||
|
||
const rightButton = screen.getByRole('button', {name: 'Scroll right'}); | ||
expect(screen.queryByRole('button', {name: 'Scroll left'})).not.toBeInTheDocument(); | ||
|
||
// Test scroll into view, the 2nd element should have it's 'scrollIntoView' called | ||
elements[2].scrollIntoView = jest.fn(); | ||
await userEvent.click(rightButton); | ||
expect(elements[2].scrollIntoView).toHaveBeenCalled(); | ||
}); | ||
}); |
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
Oops, something went wrong.
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.
Do we still need this if it's in
tests/js/setup
?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 since I need to override the callback