|
| 1 | +import { expect, testStep, dtl } from '@epic-web/workshop-utils/test' |
| 2 | +const { screen, fireEvent } = dtl |
| 3 | + |
| 4 | +import './index.tsx' |
| 5 | + |
| 6 | +await testStep('The user can see the posts', async () => { |
| 7 | + await screen.findByText(/caring for your feline friend/i) |
| 8 | + await screen.findByText(/the joy of owning a dog/i) |
| 9 | +}) |
| 10 | + |
| 11 | +const likeButtons = await testStep( |
| 12 | + 'The user can see like buttons', |
| 13 | + async () => { |
| 14 | + const buttons = await screen.findAllByRole('button', { |
| 15 | + name: /add favorite/i, |
| 16 | + }) |
| 17 | + expect(buttons.length).toBeGreaterThan(0) |
| 18 | + return buttons |
| 19 | + }, |
| 20 | +) |
| 21 | + |
| 22 | +const totalLikeButtons = likeButtons.length |
| 23 | + |
| 24 | +await testStep('The user can like a post', async () => { |
| 25 | + fireEvent.click(likeButtons[1]) |
| 26 | + await screen.findByRole('button', { name: /remove favorite/i }) |
| 27 | +}) |
| 28 | + |
| 29 | +await testStep('The liked post moves to the top', async () => { |
| 30 | + const posts = screen.getAllByRole('listitem') |
| 31 | + const firstPost = posts[0] |
| 32 | + expect( |
| 33 | + firstPost, |
| 34 | + 'The first post should have a remove favorite button', |
| 35 | + ).toContainElement(screen.getByRole('button', { name: /remove favorite/i })) |
| 36 | +}) |
| 37 | + |
| 38 | +await testStep('The user can unlike a post', async () => { |
| 39 | + const unlikeButton = await screen.findByRole('button', { |
| 40 | + name: /remove favorite/i, |
| 41 | + }) |
| 42 | + fireEvent.click(unlikeButton) |
| 43 | + |
| 44 | + await dtl.waitFor(() => |
| 45 | + expect( |
| 46 | + screen.queryByRole('button', { name: /remove favorite/i }), |
| 47 | + ).not.toBeInTheDocument(), |
| 48 | + ) |
| 49 | + const buttons = await screen.findAllByRole('button', { |
| 50 | + name: /add favorite/i, |
| 51 | + }) |
| 52 | + expect(buttons.length).toBe(totalLikeButtons) |
| 53 | +}) |
0 commit comments