Skip to content

Commit

Permalink
fix the test that was corrupting the shared repo
Browse files Browse the repository at this point in the history
  • Loading branch information
pvh committed Jan 6, 2025
1 parent da5ed07 commit 4d5f507
Showing 1 changed file with 36 additions and 34 deletions.
70 changes: 36 additions & 34 deletions packages/automerge-repo-react-hooks/test/useDocHandle.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,40 +99,6 @@ describe("useHandle", () => {
await waitFor(() => expect(onHandle2).not.toHaveBeenCalledWith(undefined))
})

it("suspends while loading a handle", async () => {
const { handleA, wrapper } = await setup()
const onHandle = vi.fn()
let promiseResolve: (value: DocHandle<ExampleDoc>) => void

// Mock find to return a delayed promise
const originalFind = repo.find.bind(repo)
repo.find = vi.fn().mockImplementation(
() =>
new Promise(resolve => {
promiseResolve = resolve
})
)

render(
<Suspense fallback={<div data-testid="loading">Loading...</div>}>
<Component url={handleA.url} onHandle={onHandle} />
</Suspense>,
{ wrapper }
)

// Should show loading state
expect(screen.getByTestId("loading")).toBeInTheDocument()
expect(onHandle).not.toHaveBeenCalled()

// Resolve the find
promiseResolve!(await originalFind(handleA.url))

// Should show content
await waitFor(() => {
expect(onHandle).toHaveBeenCalledWith(handleA)
})
})

it("handles unavailable documents correctly", async () => {
const { repo, wrapper } = await setup()
const url = generateAutomergeUrl()
Expand Down Expand Up @@ -196,6 +162,42 @@ describe("useHandle", () => {
expect(screen.queryByTestId("error")).not.toBeInTheDocument()
})

it("suspends while loading a handle", async () => {
const { handleA, wrapper } = await setup()
const onHandle = vi.fn()
let promiseResolve: (value: DocHandle<ExampleDoc>) => void

// Mock find to return a delayed promise
const originalFind = repo.find.bind(repo)
repo.find = vi.fn().mockImplementation(
() =>
new Promise(resolve => {
promiseResolve = resolve
})
)

render(
<Suspense fallback={<div data-testid="loading">Loading...</div>}>
<Component url={handleA.url} onHandle={onHandle} />
</Suspense>,
{ wrapper }
)

// Should show loading state
expect(screen.getByTestId("loading")).toBeInTheDocument()
expect(onHandle).not.toHaveBeenCalled()

// Resolve the find
promiseResolve!(await originalFind(handleA.url))

// Should show content
await waitFor(() => {
expect(onHandle).toHaveBeenCalledWith(handleA)
// return repo.find to its natural state
repo.find = originalFind
})
})

it("handles rapid url changes during loading", async () => {
const { handleA, handleB, wrapper } = await setup()
const onHandle = vi.fn()
Expand Down

0 comments on commit 4d5f507

Please sign in to comment.