-
Notifications
You must be signed in to change notification settings - Fork 305
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(storage-browser): Add control hooks for consistency (#6209)
- Loading branch information
Showing
17 changed files
with
193 additions
and
129 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
30 changes: 0 additions & 30 deletions
30
packages/react-storage/src/components/StorageBrowser/controls/SearchControl.tsx
This file was deleted.
Oops, something went wrong.
13 changes: 13 additions & 0 deletions
13
packages/react-storage/src/components/StorageBrowser/controls/SearchFieldControl.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,13 @@ | ||
import React from 'react'; | ||
|
||
import { SearchField } from '../composables/SearchField'; | ||
|
||
import { useResolvedComposable } from './hooks/useResolvedComposable'; | ||
import { useSearchField } from './hooks/useSearchField'; | ||
|
||
export const SearchFieldControl = (): React.JSX.Element => { | ||
const props = useSearchField(); | ||
const Resolved = useResolvedComposable(SearchField, 'SearchField'); | ||
|
||
return <Resolved {...props} />; | ||
}; |
50 changes: 12 additions & 38 deletions
50
...react-storage/src/components/StorageBrowser/controls/__tests__/PaginationControl.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 |
---|---|---|
@@ -1,60 +1,34 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { PaginationControl } from '../PaginationControl'; | ||
import { useControlsContext } from '../context'; | ||
import { usePagination } from '../hooks/usePagination'; | ||
import { useResolvedComposable } from '../hooks/useResolvedComposable'; | ||
|
||
jest.mock('../context'); | ||
jest.mock('../hooks/usePagination'); | ||
jest.mock('../hooks/useResolvedComposable'); | ||
jest.mock('../../composables/Pagination', () => ({ | ||
Pagination: () => <div data-testid="pagination" />, | ||
})); | ||
|
||
describe('PaginationControl', () => { | ||
// assert mocks | ||
const mockUseControlsContext = useControlsContext as jest.Mock; | ||
const mockUseResolvedComposable = useResolvedComposable as jest.Mock; | ||
const mockUsePagination = jest.mocked(usePagination); | ||
const mockUseResolvedComposable = jest.mocked(useResolvedComposable); | ||
|
||
beforeAll(() => { | ||
mockUseResolvedComposable.mockImplementation( | ||
(component: React.JSX.Element) => component | ||
(component) => component as () => React.JSX.Element | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
mockUseControlsContext.mockReset(); | ||
mockUseResolvedComposable.mockReset(); | ||
mockUsePagination.mockClear(); | ||
}); | ||
|
||
it('renders the PaginationControl', async () => { | ||
mockUseControlsContext.mockReturnValue({ | ||
data: { | ||
paginationData: { | ||
hasNextPage: true, | ||
highestPageVisited: 1, | ||
onPaginate: jest.fn(), | ||
page: 1, | ||
}, | ||
}, | ||
}); | ||
|
||
it('renders', () => { | ||
render(<PaginationControl />); | ||
|
||
const nav = screen.getByRole('navigation'); | ||
const list = screen.getByRole('list'); | ||
const listItems = await screen.findAllByRole('listitem'); | ||
const nextButton = screen.getByRole('button', { name: 'Go to next page' }); | ||
const prevButton = screen.getByRole('button', { | ||
name: 'Go to previous page', | ||
}); | ||
const nextIcon = nextButton.querySelector('svg'); | ||
const prevIcon = nextButton.querySelector('svg'); | ||
const pagination = screen.getByTestId('pagination'); | ||
|
||
expect(nextButton).toBeInTheDocument(); | ||
expect(prevButton).toBeInTheDocument(); | ||
expect(nextIcon).toBeInTheDocument(); | ||
expect(prevIcon).toBeInTheDocument(); | ||
expect(nextIcon).toHaveAttribute('aria-hidden', 'true'); | ||
expect(prevIcon).toHaveAttribute('aria-hidden', 'true'); | ||
expect(nav).toBeInTheDocument(); | ||
expect(list).toBeInTheDocument(); | ||
expect(listItems).toHaveLength(3); | ||
expect(pagination).toBeInTheDocument(); | ||
}); | ||
}); |
40 changes: 0 additions & 40 deletions
40
...ges/react-storage/src/components/StorageBrowser/controls/__tests__/SearchControl.spec.tsx
This file was deleted.
Oops, something went wrong.
34 changes: 34 additions & 0 deletions
34
...eact-storage/src/components/StorageBrowser/controls/__tests__/SearchFieldControl.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,34 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { SearchFieldControl } from '../SearchFieldControl'; | ||
import { useSearchField } from '../hooks/useSearchField'; | ||
import { useResolvedComposable } from '../hooks/useResolvedComposable'; | ||
|
||
jest.mock('../hooks/useSearchField'); | ||
jest.mock('../hooks/useResolvedComposable'); | ||
jest.mock('../../composables/SearchField', () => ({ | ||
SearchField: () => <div data-testid="search-field" />, | ||
})); | ||
|
||
describe('SearchFieldControl', () => { | ||
const mockUseSearchField = jest.mocked(useSearchField); | ||
const mockUseResolvedComposable = jest.mocked(useResolvedComposable); | ||
|
||
beforeAll(() => { | ||
mockUseResolvedComposable.mockImplementation( | ||
(component) => component as () => React.JSX.Element | ||
); | ||
}); | ||
|
||
afterEach(() => { | ||
mockUseSearchField.mockClear(); | ||
}); | ||
|
||
it('renders', () => { | ||
render(<SearchFieldControl />); | ||
|
||
const searchField = screen.getByTestId('search-field'); | ||
|
||
expect(searchField).toBeInTheDocument(); | ||
}); | ||
}); |
35 changes: 35 additions & 0 deletions
35
...eact-storage/src/components/StorageBrowser/controls/hooks/__tests__/usePagination.spec.ts
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,35 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { PaginationProps } from '../../../composables/Pagination'; | ||
import { useControlsContext } from '../../../controls/context'; | ||
import { usePagination } from '../usePagination'; | ||
|
||
jest.mock('../../../controls/context'); | ||
|
||
describe('usePagination', () => { | ||
const data = { | ||
paginationData: { hasNextPage: true, highestPageVisited: 1, page: 1 }, | ||
}; | ||
|
||
const mockUseControlsContext = jest.mocked(useControlsContext); | ||
|
||
beforeEach(() => { | ||
mockUseControlsContext.mockReturnValue({ data, onPaginate: jest.fn() }); | ||
}); | ||
|
||
afterEach(() => { | ||
mockUseControlsContext.mockReset(); | ||
}); | ||
|
||
it('returns Pagination props', () => { | ||
const { result } = renderHook(() => usePagination()); | ||
|
||
const expected: PaginationProps = { | ||
hasNextPage: data.paginationData.hasNextPage, | ||
highestPageVisited: data.paginationData.highestPageVisited, | ||
page: data.paginationData.page, | ||
onPaginate: expect.any(Function), | ||
}; | ||
|
||
expect(result.current).toStrictEqual(expected); | ||
}); | ||
}); |
46 changes: 46 additions & 0 deletions
46
...act-storage/src/components/StorageBrowser/controls/hooks/__tests__/useSearchField.spec.ts
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,46 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { SearchFieldProps } from '../../../composables/SearchField'; | ||
import { useControlsContext } from '../../../controls/context'; | ||
import { useSearchField } from '../useSearchField'; | ||
|
||
jest.mock('../../../controls/context'); | ||
|
||
describe('useSearchField', () => { | ||
const data = { | ||
searchClearLabel: 'search-clear-label', | ||
searchPlaceholder: 'search-placeholder', | ||
searchSubmitLabel: 'search-submit-label', | ||
searchQuery: 'search-query', | ||
}; | ||
|
||
const mockUseControlsContext = jest.mocked(useControlsContext); | ||
|
||
beforeEach(() => { | ||
mockUseControlsContext.mockReturnValue({ | ||
data, | ||
onSearch: jest.fn(), | ||
onSearchClear: jest.fn(), | ||
onSearchQueryChange: jest.fn(), | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
mockUseControlsContext.mockReset(); | ||
}); | ||
|
||
it('returns useSearchField data', () => { | ||
const { result } = renderHook(() => useSearchField()); | ||
|
||
const expected: SearchFieldProps = { | ||
clearLabel: data.searchClearLabel, | ||
placeholder: data.searchPlaceholder, | ||
query: data.searchQuery, | ||
submitLabel: data.searchSubmitLabel, | ||
onClear: expect.any(Function), | ||
onQueryChange: expect.any(Function), | ||
onSearch: expect.any(Function), | ||
}; | ||
|
||
expect(result.current).toStrictEqual(expected); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/react-storage/src/components/StorageBrowser/controls/hooks/usePagination.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,9 @@ | ||
import { PaginationProps } from '../../composables/Pagination'; | ||
import { useControlsContext } from '../../controls/context'; | ||
|
||
export const usePagination = (): PaginationProps => { | ||
const { data, onPaginate } = useControlsContext(); | ||
const { paginationData } = data; | ||
|
||
return { ...paginationData, onPaginate }; | ||
}; |
23 changes: 23 additions & 0 deletions
23
packages/react-storage/src/components/StorageBrowser/controls/hooks/useSearchField.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,23 @@ | ||
import { SearchFieldProps } from '../../composables/SearchField'; | ||
import { useControlsContext } from '../../controls/context'; | ||
|
||
export const useSearchField = (): SearchFieldProps => { | ||
const { data, onSearch, onSearchClear, onSearchQueryChange } = | ||
useControlsContext(); | ||
const { | ||
searchPlaceholder, | ||
searchClearLabel, | ||
searchQuery, | ||
searchSubmitLabel, | ||
} = data; | ||
|
||
return { | ||
clearLabel: searchClearLabel, | ||
placeholder: searchPlaceholder, | ||
query: searchQuery, | ||
submitLabel: searchSubmitLabel, | ||
onClear: onSearchClear, | ||
onQueryChange: onSearchQueryChange, | ||
onSearch, | ||
}; | ||
}; |
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
Oops, something went wrong.