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

Add interaction tests for pagelayout sticky #2241

Merged
merged 8 commits into from
Aug 31, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 32 additions & 20 deletions src/PageLayout/PageLayout.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,9 @@ NestedScrollContainer.argTypes = {

export const CustomStickyHeader: Story = args => (
// a box to create a sticky top element that will be on the consumer side and outside of the PageLayout component
<Box>
<Box data-testid="story-window">
<Box
data-testid="sticky-header"
sx={{
position: 'sticky',
top: 0,
Expand All @@ -617,29 +618,40 @@ export const CustomStickyHeader: Story = args => (
</Box>
<PageLayout rowGap="none" columnGap="none" padding="none" containerWidth="full">
<PageLayout.Content padding="normal" width="large">
<Box sx={{display: 'grid', gap: 3}}>
{Array.from({length: args.numParagraphsInContent}).map((_, i) => (
<Box key={i} as="p" sx={{margin: 0}}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam at enim id lorem tempus egestas a non ipsum.
Maecenas imperdiet ante quam, at varius lorem molestie vel. Sed at eros consequat, varius tellus et,
auctor felis. Donec pulvinar lacinia urna nec commodo. Phasellus at imperdiet risus. Donec sit amet massa
purus. Nunc sem lectus, bibendum a sapien nec, tristique tempus felis. Ut porttitor auctor tellus in
imperdiet. Ut blandit tincidunt augue, quis fringilla nunc tincidunt sed. Vestibulum auctor euismod nisi.
Nullam tincidunt est in mi tincidunt dictum. Sed consectetur aliquet velit ut ornare.
</Box>
))}
<Box sx={{display: 'grid', gap: 3}} data-testid="scrollContainer">
{Array.from({length: args.numParagraphsInContent}).map((_, i) => {
const testId = `content${i}`
return (
<Box key={i} as="p" sx={{margin: 0}}>
<span data-testid={testId}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam at enim id lorem tempus egestas a non
ipsum. Maecenas imperdiet ante quam, at varius lorem molestie vel. Sed at eros consequat, varius
tellus et, auctor felis. Donec pulvinar lacinia urna nec commodo. Phasellus at imperdiet risus. Donec
sit amet massa purus. Nunc sem lectus, bibendum a sapien nec, tristique tempus felis. Ut porttitor
auctor tellus in imperdiet. Ut blandit tincidunt augue, quis fringilla nunc tincidunt sed. Vestibulum
auctor euismod nisi. Nullam tincidunt est in mi tincidunt dictum. Sed consectetur aliquet velit ut
ornare.
</span>
</Box>
)
})}
</Box>
</PageLayout.Content>
<PageLayout.Pane position="start" padding="normal" divider="line" sticky stickyTop={args.stickyTop}>
<Box sx={{display: 'grid', gap: 3}}>
{Array.from({length: args.numParagraphsInPane}).map((_, i) => (
<Box key={i} as="p" sx={{margin: 0}}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam at enim id lorem tempus egestas a non ipsum.
Maecenas imperdiet ante quam, at varius lorem molestie vel. Sed at eros consequat, varius tellus et,
auctor felis. Donec pulvinar lacinia urna nec commodo. Phasellus at imperdiet risus. Donec sit amet massa
purus.
</Box>
))}
{Array.from({length: args.numParagraphsInPane}).map((_, i) => {
const testId = `paragraph${i}`
return (
<Box key={i} as="p" sx={{margin: 0}}>
<span data-testid={testId}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam at enim id lorem tempus egestas a non
ipsum. Maecenas imperdiet ante quam, at varius lorem molestie vel. Sed at eros consequat, varius
tellus et, auctor felis. Donec pulvinar lacinia urna nec commodo. Phasellus at imperdiet risus. Donec
sit amet massa purus.
</span>
</Box>
)
})}
</Box>
</PageLayout.Pane>
<PageLayout.Footer padding="normal" divider="line">
Expand Down
43 changes: 40 additions & 3 deletions src/PageLayout/interaction.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {Meta} from '@storybook/react'
import {StickyPane} from './PageLayout.stories'
import {within} from '@storybook/testing-library'
import {StickyPane, CustomStickyHeader} from './PageLayout.stories'
import {within, fireEvent} from '@storybook/testing-library'
import {expect} from '@storybook/jest'
import {setTimeout} from 'timers/promises'
Copy link
Member

Choose a reason for hiding this comment

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

removed it in the next commit


const meta: Meta = {
title: 'Layout/PageLayout/interactions',
Expand Down Expand Up @@ -389,5 +390,41 @@ NonStickyPane.play = async ({canvasElement}: {canvasElement: HTMLElement}) => {
const paragraphRect = paragraph0.getBoundingClientRect()
expect(isInViewPort(paragraphRect)).toBe(false)
}

CustomStickyHeader.argTypes = {
sticky: {
type: 'boolean',
defaultValue: true
},
stickyTop: {
type: 'string',
defaultValue: '8rem'
},
numParagraphsInPane: {
type: 'number',
defaultValue: 10
},
numParagraphsInContent: {
type: 'number',
defaultValue: 30
}
}

CustomStickyHeader.play = async ({canvasElement}: {canvasElement: HTMLElement}) => {
const canvas = within(canvasElement)
const contentToScroll = await canvas.getByTestId('content3')
contentToScroll.scrollIntoView()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Do you need to scroll twice? The fireEvent.scroll below would be ideal. I would fix my tests to work on that if it works for you.

Copy link
Member

Choose a reason for hiding this comment

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

No it doesn't work :( This is what I mean here Maybe I am missing something?

Because the fireEvent.scroll didn't work I included contentToScroll. I'll give another go today and let you know if I make it work.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah! I had the same experience. It would have been so much better if fireEvent scroll worked in this case.Maybe we aren't using the scrollcontainer effectively?

Copy link
Member

Choose a reason for hiding this comment

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

I tried it again but no luck 😢 I agree, I think it is about the scroll container. I tried the outer box, the page layout content as scroll containers but none of them are scrollable. (el.scrollHeight-el.clientHight is 0) and I tried giving auto overflow but that doesn't seem to work either. I'll remove this fireEvent for now and maybe down the track, it will be possible to do with user events from SB.


// fireEvent alternative?
const storyWindow = await canvas.getByTestId('story-window')
await fireEvent.scroll(storyWindow, {top: 600})
Copy link
Member

Choose a reason for hiding this comment

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

I wanted to give it a go to more descriptive event names, but can't seem to make this work :( See the doc Maybe I am missing something? @pksjce

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

They don't yet have a descriptive name feature. However, storybook 7.0.0 is going to support sections within interaction tests I believe!

Copy link
Member

Choose a reason for hiding this comment

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

Ahh cool! I'll remove this then, and we can try later when available.


const stickyPaneFirstParagraph = await canvas.getByTestId('paragraph0')
const paragraphBoundaries = stickyPaneFirstParagraph.getBoundingClientRect()
const stickyHeader = await canvas.getByTestId('sticky-header')
const stickyHeaderBoundaries = stickyHeader.getBoundingClientRect()
expect(isInViewPort(paragraphBoundaries)).toBe(true)
expect(isInViewPort(stickyHeaderBoundaries)).toBe(true)
}
export default meta
export {StickyPane, NonStickyPane}
export {StickyPane, NonStickyPane, CustomStickyHeader}