Skip to content

Commit b8a66e2

Browse files
authored
♻️ Hide tutorials on other pages (#1181)
* ♻️ Hide tutorials on other pages * ✅ Update Tutorials.test * ✅ Update Tutorials.test.tsx
1 parent 146d421 commit b8a66e2

3 files changed

Lines changed: 28 additions & 79 deletions

File tree

src/organisms/TopBar/Tutorials/Tutorials.styles.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ export const Container = styled.div`
77
flex-direction: column;
88
padding: ${spacings.large};
99
width: 520px;
10-
> h4 {
11-
margin-bottom: ${spacings.x_large};
12-
}
13-
> hr {
10+
height: 100%;
11+
> h4:not(:last-child) {
1412
margin-bottom: ${spacings.x_large};
1513
}
1614
`;

src/organisms/TopBar/Tutorials/Tutorials.test.tsx

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -47,30 +47,23 @@ test('Renders expected items when opening the tutorials menu', async ({
4747
<Tutorials />
4848
</TutorialProvider>,
4949
{
50-
routes: ['/tutorial'],
51-
initialEntries: ['/tutorial'],
50+
routes: ['/'],
51+
initialEntries: ['/'],
5252
},
5353
{ wrapper: Providers }
5454
);
5555
const user = userEvent.setup();
5656

57-
await user.click(screen.getByRole('button'));
57+
await user.click(await screen.findByRole('button'));
5858

5959
await waitFor(() =>
60-
expect(
61-
screen.getByText(`Available Tutorials (${tutorials.length})`)
62-
).toBeInTheDocument()
60+
expect(screen.getByText(`Available Tutorials (1)`)).toBeInTheDocument()
6361
);
6462

6563
expect(screen.getByText('For current page')).toBeInTheDocument();
66-
expect(screen.getByText('For other pages')).toBeInTheDocument();
67-
expect(screen.getByRole('separator')).toBeInTheDocument();
68-
69-
for (const tutorial of tutorials) {
70-
expect(
71-
screen.getByRole('button', { name: tutorial.name })
72-
).toBeInTheDocument();
73-
}
64+
expect(
65+
screen.getByRole('button', { name: tutorials[0].name })
66+
).toBeInTheDocument();
7467
});
7568

7669
test('Hides expected tutorials when providing filter fn', async ({
@@ -83,8 +76,8 @@ test('Hides expected tutorials when providing filter fn', async ({
8376
<Tutorials filterTutorials={filterFunction} />
8477
</TutorialProvider>,
8578
{
86-
routes: ['/tutorial'],
87-
initialEntries: ['/tutorial'],
79+
routes: ['/'],
80+
initialEntries: ['/'],
8881
},
8982
{ wrapper: Providers }
9083
);
@@ -95,91 +88,72 @@ test('Hides expected tutorials when providing filter fn', async ({
9588
await waitFor(() =>
9689
expect(
9790
screen.getByText(
98-
`Available Tutorials (${tutorials.filter(filterFunction).length})`
91+
`Available Tutorials (${tutorials.filter((t) => t.path === '/').filter(filterFunction).length})`
9992
)
10093
).toBeInTheDocument()
10194
);
102-
103-
for (const tutorial of tutorials) {
104-
if (tutorial.willPopUp) {
105-
expect(
106-
screen.getByRole('button', { name: tutorial.name })
107-
).toBeInTheDocument();
108-
} else {
109-
expect(
110-
screen.queryByRole('button', { name: tutorial.name })
111-
).not.toBeInTheDocument();
112-
}
113-
}
11495
});
11596

11697
test('Clicking TutorialItem triggers callback, so we can navigate / do whatever we need', async ({
11798
worker,
11899
}) => {
119100
worker.use(tutorialHandler);
120101
const callback = vi.fn();
121-
122102
await renderWithRouter(
123103
<TutorialProvider>
124104
<Tutorials onTutorialStart={callback} />
125105
</TutorialProvider>,
126106
{
127-
routes: ['/tutorial'],
128-
initialEntries: ['/tutorial'],
107+
routes: ['/'],
108+
initialEntries: ['/'],
129109
},
130110
{ wrapper: Providers }
131111
);
132112
const user = userEvent.setup();
133113

134-
await user.click(screen.getByRole('button'));
114+
await user.click(await screen.findByRole('button'));
135115

136116
await waitFor(() =>
137-
expect(
138-
screen.getByText(`Available Tutorials (${tutorials.length})`)
139-
).toBeInTheDocument()
117+
expect(screen.getByText(`Available Tutorials (1)`)).toBeInTheDocument()
140118
);
141119

142-
const randomTutorial = faker.helpers.arrayElement(tutorials);
143120
const tutorialItem = screen.getByRole('button', {
144-
name: randomTutorial.name,
121+
name: tutorials[0].name,
145122
});
146123

147124
await user.click(tutorialItem);
148125

149-
expect(callback).toHaveBeenCalledWith(randomTutorial.id);
126+
expect(callback).toHaveBeenCalledWith(tutorials[0].id);
150127
});
151128

152129
test('Shows "completed" if tutorial has been seen', async ({ worker }) => {
153-
worker.use(tutorialHandler);
154-
const randomTutorial = faker.helpers.arrayElement(tutorials);
155130
window.localStorage.setItem(
156131
`sam-seen-tutorials-${environment.getAppName(import.meta.env.VITE_NAME)}`,
157-
JSON.stringify([randomTutorial.id])
132+
JSON.stringify([tutorials[0].id])
158133
);
134+
worker.use(tutorialHandler);
159135
const callback = vi.fn();
160136
await renderWithRouter(
161137
<TutorialProvider>
162138
<Tutorials onTutorialStart={callback} />
163139
</TutorialProvider>,
164140
{
165-
routes: ['/tutorial'],
166-
initialEntries: ['/tutorial'],
141+
routes: ['/'],
142+
initialEntries: ['/'],
167143
},
168144
{ wrapper: Providers }
169145
);
170146
const user = userEvent.setup();
171147

172-
await user.click(screen.getByRole('button'));
148+
await user.click(await screen.findByRole('button'));
173149

174150
await waitFor(() =>
175-
expect(
176-
screen.getByText(`Available Tutorials (${tutorials.length})`)
177-
).toBeInTheDocument()
151+
expect(screen.getByText(`Available Tutorials (1)`)).toBeInTheDocument()
178152
);
179153

180154
expect(
181155
screen.getByRole('button', {
182-
name: `COMPLETED ${randomTutorial.name}`,
156+
name: `COMPLETED ${tutorials[0].name}`,
183157
})
184158
).toBeInTheDocument();
185159
});

src/organisms/TopBar/Tutorials/Tutorials.tsx

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FC, HTMLAttributes, useRef, useState } from 'react';
22

3-
import { Divider, Icon, Typography } from '@equinor/eds-core-react';
3+
import { Icon, Typography } from '@equinor/eds-core-react';
44
import { youtube_alt } from '@equinor/eds-icons';
55
import {
66
MyTutorialDto,
@@ -27,22 +27,14 @@ export const Tutorials: FC<TutorialsProps> = ({
2727
onTutorialStart,
2828
...rest
2929
}) => {
30-
const { allTutorials, tutorialsOnThisPage } = useTutorials();
30+
const { tutorialsOnThisPage } = useTutorials();
3131
const [open, setOpen] = useState(false);
3232
const buttonRef = useRef<HTMLButtonElement | null>(null);
3333

34-
const filteredAllTutorials = filterTutorials
35-
? allTutorials.filter(filterTutorials)
36-
: allTutorials;
3734
const filteredTutorialsOnThisPage = filterTutorials
3835
? tutorialsOnThisPage.filter(filterTutorials)
3936
: tutorialsOnThisPage;
4037

41-
const tutorialsOnOtherPages = filteredAllTutorials.filter(
42-
(tutorial) =>
43-
filteredTutorialsOnThisPage.findIndex((t) => t.id === tutorial.id) === -1
44-
);
45-
4638
const handleToggleOpen = () => setOpen((prev) => !prev);
4739

4840
return (
@@ -63,7 +55,7 @@ export const Tutorials: FC<TutorialsProps> = ({
6355
>
6456
<Container>
6557
<Typography variant="h4">
66-
Available Tutorials ({filteredAllTutorials.length})
58+
Available Tutorials ({filteredTutorialsOnThisPage.length})
6759
</Typography>
6860
{filteredTutorialsOnThisPage.length > 0 && (
6961
<TutorialList>
@@ -78,21 +70,6 @@ export const Tutorials: FC<TutorialsProps> = ({
7870
))}
7971
</TutorialList>
8072
)}
81-
{filteredTutorialsOnThisPage.length > 0 &&
82-
filteredTutorialsOnThisPage.length > 0 && <Divider />}
83-
{tutorialsOnOtherPages.length > 0 && (
84-
<TutorialList>
85-
<Typography variant="caption">For other pages</Typography>
86-
{tutorialsOnOtherPages.map((tutorial) => (
87-
<TutorialItem
88-
key={tutorial.id}
89-
onTutorialStart={onTutorialStart}
90-
onClose={handleToggleOpen}
91-
{...tutorial}
92-
/>
93-
))}
94-
</TutorialList>
95-
)}
9673
</Container>
9774
</TopBarMenu>
9875
)}

0 commit comments

Comments
 (0)