Skip to content

Commit

Permalink
fix scroll into view testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Warren Day authored and Warren Day committed Jan 12, 2024
1 parent 39653af commit bfb8e7c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 5 deletions.
14 changes: 11 additions & 3 deletions src/hooks/useMark.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { render } from "@testing-library/react"
import { useMark } from "./useMark"

const TestComponent = (props: { search: string }) => {
const ref = useMark(props.search)
const TestComponent = (props: { search: string; done?: () => void }) => {
const ref = useMark(props.search, "", props.done)
return (
<div ref={ref} data-testid="target">
The quick brown fox jumps over the lazy dog again
Expand Down Expand Up @@ -32,10 +32,18 @@ describe("useMark", () => {

const markedElements = container.querySelectorAll("mark")

// There are three "a"s in the test text and they should all be marked
expect(markedElements).toHaveLength(1)
markedElements.forEach((element) => {
expect(element).toHaveTextContent("brown fox")
})
})

it("should call done callback when mark finished", () => {
const search = "Brown Fox"
const done = jest.fn()

render(<TestComponent search={search} done={done} />)

expect(done).toHaveBeenCalledTimes(1)
})
})
5 changes: 3 additions & 2 deletions src/hooks/useMark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const useMark = (
mark.mark(searchQuery, {
caseSensitive: false,
separateWordSearch: false,
done,
done: () => done?.(),
})

return () => {
Expand All @@ -37,7 +37,8 @@ export const useMark = (

/**
* Mark text that has been searched from the search provider.
* @param content Optional content to listen for changes to causing mark to re-run.
*
* @param content Optional string to cause mark to re-run when changed.
*
* @returns ref to the element to mark
*/
Expand Down
2 changes: 2 additions & 0 deletions src/setupTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ Object.defineProperty(window, "matchMedia", {
dispatchEvent: jest.fn(),
}),
})

window.HTMLElement.prototype.scrollIntoView = function () {}

0 comments on commit bfb8e7c

Please sign in to comment.