diff --git a/src/components/Highlight.test.tsx b/src/components/Highlight.test.tsx new file mode 100644 index 0000000..5d1fbc3 --- /dev/null +++ b/src/components/Highlight.test.tsx @@ -0,0 +1,15 @@ +import { render } from '@testing-library/react' +import React, { ComponentProps } from 'react' +import { Highlight } from './Highlight' + +function test(title: string, text: string, match?: ComponentProps['match']) { + it(title, () => { + expect(render().container.textContent).toBe(text) + }) +} + +test('sample', 'abc', undefined) +test('sample', 'abc', /./) +test('sample', 'abc', /../) +test('sample', 'abc', /.../) +test('sample', 'abc', /..../) diff --git a/src/components/Highlight.tsx b/src/components/Highlight.tsx index cb91e29..0102ef7 100644 --- a/src/components/Highlight.tsx +++ b/src/components/Highlight.tsx @@ -1,17 +1,11 @@ -import * as React from 'react' +import * as React from 'react'; -export const Highlight = React.memo(function Highlight(props: { - text: string - match?: RegExp | string -}) { +export const Highlight = React.memo(function Highlight(props: { text: string; match?: RegExp }) { const { text, match } = props const $match = React.useMemo(() => { - if (match) { - if (match instanceof RegExp) { - if (match.flags.includes('g')) return match - return new RegExp(match.source, 'g' + match.flags) - } - return new RegExp(match, 'g') + if (match instanceof RegExp) { + if (match.flags.includes('g')) return match + return new RegExp(match.source, 'g' + match.flags) } return null }, [match])