forked from octokatherine/readme.so
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature Request: Search box above section list octokatherine#277
Add new SectionFilter component to filter slugs based on section names Update SectionsColumn to display filtered sections Add units tests for SectionFilter component
- Loading branch information
Showing
3 changed files
with
115 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useEffect, useState } from 'react' | ||
|
||
const SectionFilter = ({ sectionSlugs, getTemplate, filterSections }) => { | ||
const [section, setSection] = useState('') | ||
|
||
const getAutoCompleteResults = (section) => { | ||
const suggestedSlugs = sectionSlugs.filter((slug) => { | ||
return getTemplate(slug).name.toLowerCase().includes(section.toLowerCase()) | ||
}) | ||
|
||
return suggestedSlugs.length ? suggestedSlugs : [undefined] | ||
} | ||
|
||
useEffect(() => { | ||
if (!section) { | ||
filterSections([]) | ||
return | ||
} | ||
|
||
const suggestedSlugs = getAutoCompleteResults(section) | ||
|
||
filterSections(suggestedSlugs) | ||
}, [section]) | ||
|
||
return ( | ||
<input | ||
type="text" | ||
placeholder="Search for a section" | ||
className="mb-3 space-y-3 w-full py-2 pl-3 pr-6 bg-white dark:bg-gray-200 rounded-md shadow cursor-pointer focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-emerald-400" | ||
data-testid="slugs-filter" | ||
value={section} | ||
onChange={(e) => setSection(e.target.value)} | ||
/> | ||
) | ||
} | ||
|
||
export default SectionFilter |
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,42 @@ | ||
import { render, screen } from '@testing-library/react' | ||
import userEvent from '@testing-library/user-event' | ||
|
||
import SectionFilter from '../SectionFilter' | ||
|
||
import { en_EN } from '../../data/section-templates-en_EN' | ||
|
||
jest.mock('next-i18next', () => ({ | ||
useTranslation: () => ({ t: jest.fn() }), | ||
})) | ||
|
||
describe('<SectionFilter />', () => { | ||
const props = { | ||
sectionSlugs: ['api', 'appendix'], | ||
getTemplate: (slug) => en_EN.find((t) => t.slug === slug), | ||
filterSections: jest.fn(), | ||
} | ||
|
||
it('should render', () => { | ||
const { container } = render(<SectionFilter {...props} />) | ||
expect(container).toBeInTheDocument() | ||
}) | ||
|
||
it('should call the callBack function with suggested slugs', () => { | ||
render(<SectionFilter {...props} />) | ||
|
||
const input = screen.getByTestId('slugs-filter') | ||
expect(input).toBeInTheDocument() | ||
expect(input).toHaveAttribute('type', 'text') | ||
expect(input).toHaveAttribute('placeholder', 'Search for a section') | ||
|
||
userEvent.type(input, 'app') | ||
expect(screen.getByTestId('slugs-filter')).toHaveValue('app') | ||
|
||
expect(props.filterSections).toHaveBeenCalledTimes(5) | ||
expect(props.filterSections).toHaveBeenNthCalledWith(1, []) | ||
expect(props.filterSections).toHaveBeenNthCalledWith(2, []) | ||
expect(props.filterSections).toHaveBeenNthCalledWith(3, ['api', 'appendix']) | ||
expect(props.filterSections).toHaveBeenNthCalledWith(4, ['api', 'appendix']) | ||
expect(props.filterSections).toHaveBeenNthCalledWith(5, ['appendix']) | ||
}) | ||
}) |