|
| 1 | +import React from 'react'; |
| 2 | +import thunk from 'redux-thunk'; |
| 3 | +import configureStore from 'redux-mock-store'; |
| 4 | +import { reduxRender, screen, fireEvent, act } from '../../../test-utils'; |
| 5 | +import { initialTestState } from '../../../testData/testReduxStore'; |
| 6 | +import CollectionItemRow from './CollectionItemRow'; |
| 7 | + |
| 8 | +jest.mock('../../../i18n'); |
| 9 | + |
| 10 | +const mockStore = configureStore([thunk]); |
| 11 | +const store = mockStore(initialTestState); |
| 12 | + |
| 13 | +let subjectProps = { |
| 14 | + collection: { |
| 15 | + id: 'collection-123', |
| 16 | + name: 'Test Collection' |
| 17 | + }, |
| 18 | + item: { |
| 19 | + createdAt: '2026-01-15T10:30:00.000Z', |
| 20 | + isDeleted: false, |
| 21 | + project: { |
| 22 | + id: 'project-456', |
| 23 | + name: 'My Sketch', |
| 24 | + user: { username: 'testuser' }, |
| 25 | + visibility: 'Public' |
| 26 | + } |
| 27 | + }, |
| 28 | + isOwner: true |
| 29 | +}; |
| 30 | + |
| 31 | +const subject = () => { |
| 32 | + reduxRender( |
| 33 | + <table> |
| 34 | + <tbody> |
| 35 | + <CollectionItemRow {...subjectProps} /> |
| 36 | + </tbody> |
| 37 | + </table>, |
| 38 | + { store } |
| 39 | + ); |
| 40 | +}; |
| 41 | + |
| 42 | +describe('<CollectionItemRow />', () => { |
| 43 | + afterEach(() => { |
| 44 | + store.clearActions(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('renders the sketch name as a link', () => { |
| 48 | + subject(); |
| 49 | + const link = screen.getByRole('link', { name: 'My Sketch' }); |
| 50 | + expect(link).toBeInTheDocument(); |
| 51 | + expect(link).toHaveAttribute('href', '/testuser/sketches/project-456'); |
| 52 | + }); |
| 53 | + |
| 54 | + it('renders the owner username', () => { |
| 55 | + subject(); |
| 56 | + expect(screen.getByText('testuser')).toBeInTheDocument(); |
| 57 | + }); |
| 58 | + |
| 59 | + it('shows the remove button when user is the owner', () => { |
| 60 | + subject(); |
| 61 | + expect(screen.getByRole('button', { name: /remove/i })).toBeInTheDocument(); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('when user is not the owner', () => { |
| 65 | + beforeAll(() => { |
| 66 | + subjectProps = { ...subjectProps, isOwner: false }; |
| 67 | + }); |
| 68 | + |
| 69 | + afterAll(() => { |
| 70 | + subjectProps = { ...subjectProps, isOwner: true }; |
| 71 | + }); |
| 72 | + |
| 73 | + it('does not show the remove button', () => { |
| 74 | + subject(); |
| 75 | + expect( |
| 76 | + screen.queryByRole('button', { name: /remove/i }) |
| 77 | + ).not.toBeInTheDocument(); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('wraps the remove button with a tooltip', async () => { |
| 82 | + subject(); |
| 83 | + |
| 84 | + const button = screen.getByRole('button', { name: /remove/i }); |
| 85 | + await act(async () => { |
| 86 | + fireEvent.mouseEnter(button); |
| 87 | + }); |
| 88 | + expect(button).toHaveClass('tooltipped'); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('when the project is deleted', () => { |
| 92 | + beforeAll(() => { |
| 93 | + subjectProps = { |
| 94 | + ...subjectProps, |
| 95 | + item: { |
| 96 | + ...subjectProps.item, |
| 97 | + isDeleted: true, |
| 98 | + project: undefined |
| 99 | + } |
| 100 | + }; |
| 101 | + }); |
| 102 | + |
| 103 | + it('shows the deleted sketch label', () => { |
| 104 | + subject(); |
| 105 | + expect(screen.getByText('Sketch deleted')).toBeInTheDocument(); |
| 106 | + }); |
| 107 | + }); |
| 108 | +}); |
0 commit comments