-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1085 from InseeFr/feat/codes-panel-add-button
feat: convert some files to typescript
- Loading branch information
Showing
13 changed files
with
279 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
107 changes: 107 additions & 0 deletions
107
src/packages/modules-codelists/components/codelist-detail/codes-panel-add-button.spec.tsx
This file contains 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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import { Mock, vi } from 'vitest'; | ||
|
||
import { ADMIN, CODELIST_CONTRIBUTOR } from '../../../auth/roles'; | ||
import { usePermission } from '../../../redux/hooks/usePermission'; | ||
import { CodesPanelAddButton } from './codes-panel-add-button'; | ||
|
||
vi.mock('../../../redux/hooks/usePermission', () => ({ | ||
usePermission: vi.fn(), | ||
})); | ||
|
||
describe('CodesPanelAddButton', () => { | ||
const mockOnHandlePanel = vi.fn(); | ||
|
||
afterEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('should not render if codelist.lastCodeUriSegment is missing', () => { | ||
(usePermission as Mock).mockReturnValue({ | ||
stamp: 'test-stamp', | ||
roles: [ADMIN], | ||
}); | ||
|
||
render( | ||
<CodesPanelAddButton codelist={{}} onHandlePanel={mockOnHandlePanel} />, | ||
); | ||
|
||
expect(screen.queryByRole('button', { name: /add/i })).toBeNull(); | ||
}); | ||
|
||
it('should render the button if user is an admin', () => { | ||
(usePermission as Mock).mockReturnValue({ | ||
stamp: 'test-stamp', | ||
roles: [ADMIN], | ||
}); | ||
|
||
render( | ||
<CodesPanelAddButton | ||
codelist={{ lastCodeUriSegment: 'segment' }} | ||
onHandlePanel={mockOnHandlePanel} | ||
/>, | ||
); | ||
|
||
screen.getByRole('button', { name: /add/i }); | ||
}); | ||
|
||
it('should render the button if user has contributor rights based on stamp', () => { | ||
(usePermission as Mock).mockReturnValue({ | ||
stamp: 'test-contributor', | ||
roles: [CODELIST_CONTRIBUTOR], | ||
}); | ||
|
||
render( | ||
<CodesPanelAddButton | ||
codelist={{ | ||
lastCodeUriSegment: 'segment', | ||
contributor: 'test-contributor', | ||
}} | ||
onHandlePanel={mockOnHandlePanel} | ||
/>, | ||
); | ||
|
||
screen.getByRole('button', { name: /add/i }); | ||
}); | ||
|
||
it('should not render the button if user lacks the required permissions', () => { | ||
(usePermission as Mock).mockReturnValue({ | ||
stamp: 'test-stamp', | ||
roles: ['OTHER_ROLE'], | ||
}); | ||
|
||
render( | ||
<CodesPanelAddButton | ||
codelist={{ | ||
lastCodeUriSegment: 'segment', | ||
contributor: 'test-contributor', | ||
}} | ||
onHandlePanel={mockOnHandlePanel} | ||
/>, | ||
); | ||
|
||
expect(screen.queryByRole('button', { name: /add/i })).toBeNull(); | ||
}); | ||
|
||
it('should trigger onHandlePanel when the button is clicked', () => { | ||
(usePermission as Mock).mockReturnValue({ | ||
stamp: 'test-contributor', | ||
roles: [CODELIST_CONTRIBUTOR], | ||
}); | ||
|
||
render( | ||
<CodesPanelAddButton | ||
codelist={{ | ||
lastCodeUriSegment: 'segment', | ||
contributor: 'test-contributor', | ||
}} | ||
onHandlePanel={mockOnHandlePanel} | ||
/>, | ||
); | ||
|
||
const button = screen.getByRole('button', { name: /add/i }); | ||
fireEvent.click(button); | ||
|
||
expect(mockOnHandlePanel).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
This file contains 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
File renamed without changes.
This file contains 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
72 changes: 72 additions & 0 deletions
72
src/packages/modules-concepts/collections/home-container.spec.tsx
This file contains 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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { render, screen, waitFor } from '@testing-library/react'; | ||
import { vi } from 'vitest'; | ||
|
||
import { CollectionApi } from '@sdk/collection-api'; | ||
|
||
import CollectionsHome from './home'; | ||
import { Component } from './home-container'; | ||
|
||
vi.mock('@components/loading', () => ({ | ||
Loading: vi.fn(() => <div>Loading...</div>), | ||
})); | ||
|
||
vi.mock('./home', () => ({ | ||
default: vi.fn(() => <div>CollectionsHome</div>), | ||
})); | ||
|
||
vi.mock('@sdk/collection-api', () => ({ | ||
CollectionApi: { | ||
getCollectionList: vi.fn(), | ||
}, | ||
})); | ||
|
||
describe('Component', () => { | ||
const mockCollections = [ | ||
{ id: '1', label: 'Collection 1' }, | ||
{ id: '2', label: 'Collection 2' }, | ||
]; | ||
|
||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('should show the loading spinner initially', () => { | ||
// Simuler une promesse qui ne se résout pas immédiatement | ||
CollectionApi.getCollectionList.mockImplementation( | ||
() => new Promise(() => {}), | ||
); | ||
|
||
render(<Component />); | ||
|
||
screen.getByText('Loading...'); | ||
}); | ||
|
||
it('should render CollectionsHome after data is loaded', async () => { | ||
CollectionApi.getCollectionList.mockResolvedValue(mockCollections); | ||
|
||
render(<Component />); | ||
|
||
screen.getByText('Loading...'); | ||
|
||
await waitFor(() => | ||
expect(CollectionApi.getCollectionList).toHaveBeenCalled(), | ||
); | ||
|
||
screen.getByText('CollectionsHome'); | ||
}); | ||
|
||
it('should pass the collections data to CollectionsHome', async () => { | ||
CollectionApi.getCollectionList.mockResolvedValue(mockCollections); | ||
|
||
render(<Component />); | ||
|
||
await waitFor(() => screen.getByText('CollectionsHome')); | ||
|
||
expect(CollectionsHome).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
collections: mockCollections, | ||
}), | ||
{}, | ||
); | ||
}); | ||
}); |
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { render, screen } from '@testing-library/react'; | ||
import { vi } from 'vitest'; | ||
|
||
import { SearchableList } from '@components/searchable-list'; | ||
|
||
import { useTitle } from '@utils/hooks/useTitle'; | ||
|
||
import D from '../../deprecated-locales'; | ||
import CollectionsHome from './home'; | ||
|
||
vi.mock('@components/page-title', () => ({ | ||
PageTitle: vi.fn(() => <div>PageTitle</div>), | ||
})); | ||
vi.mock('@components/layout', () => ({ | ||
Row: vi.fn(({ children }) => <div>{children}</div>), | ||
})); | ||
vi.mock('@components/searchable-list', () => ({ | ||
SearchableList: vi.fn(() => <div>SearchableList</div>), | ||
})); | ||
|
||
vi.mock('@utils/hooks/useTitle'); | ||
|
||
vi.mock('./menu', () => ({ | ||
Menu: vi.fn(() => <div>Menu</div>), | ||
})); | ||
|
||
describe('CollectionsHome', () => { | ||
const collectionsMock = [ | ||
{ id: '1', label: 'Collection 1' }, | ||
{ id: '2', label: 'Collection 2' }, | ||
]; | ||
|
||
it('should render the page title and the searchable list with correct data', () => { | ||
render(<CollectionsHome collections={collectionsMock} />); | ||
|
||
expect(useTitle).toHaveBeenCalledWith(D.conceptsTitle, D.collectionsTitle); | ||
|
||
screen.getByText('PageTitle'); | ||
screen.getByText('SearchableList'); | ||
}); | ||
|
||
it('should render the Menu component', () => { | ||
render(<CollectionsHome collections={collectionsMock} />); | ||
screen.getByText('Menu'); | ||
}); | ||
|
||
it('should pass collections prop to SearchableList', () => { | ||
render(<CollectionsHome collections={collectionsMock} />); | ||
expect(SearchableList).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
items: collectionsMock, | ||
childPath: 'concepts/collections', | ||
autoFocus: true, | ||
}), | ||
{}, | ||
); | ||
}); | ||
}); |
This file contains 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
File renamed without changes.
File renamed without changes.
This file contains 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