Skip to content

add initatedBy#162

Merged
kyeotic merged 2 commits into
mainfrom
use-location-iniate
Mar 10, 2026
Merged

add initatedBy#162
kyeotic merged 2 commits into
mainfrom
use-location-iniate

Conversation

@kyeotic
Copy link
Copy Markdown
Owner

@kyeotic kyeotic commented Mar 9, 2026

closes #161

Adds initiatedBy to useLocationChange to distinguish between push and pop navigations

@kyeotic kyeotic merged commit 9d12631 into main Mar 10, 2026
6 checks passed
@kyeotic kyeotic deleted the use-location-iniate branch March 10, 2026 21:32
Comment thread src/location.ts
Comment on lines +142 to +144
const method = (event as any).__method as string | undefined
const initiatedBy: NavigationInitiator =
method === 'push' ? 'push' : method === 'replace' ? 'replace' : 'pop'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const initiatedBy: NavigationInitiator = (event as any).__method ?? 'pop'

is enough

Comment thread src/main.ts
Comment on lines +21 to +26
export type {
LocationChangeSetFn,
LocationChangeOptionParams,
RavigerLocation,
NavigationInitiator,
} from './location'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we export RavigerHistory too?

Comment thread test/location.spec.tsx
act(() => navigate('/home'))
expect(watcher).toHaveBeenCalledWith('/')
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

describe('useLocationChange action', () => {
  function RouteWithLocation({
    onChange,
    onInitial = false,
  }: {
    onChange: (location: RavigerLocation) => void
    onInitial?: boolean
  }) {
    useLocationChange(onChange, { onInitial })
    return null
  }

  test('action is "push" on regular navigate', () => {
    const watcher = jest.fn()
    render(<RouteWithLocation onChange={watcher} />)

    act(() => navigate('/foo'))
    expect(watcher).toHaveBeenCalledWith(expect.objectContaining({ action: 'push' }))
  })

  test('action is "replace" on replace navigate', () => {
    const watcher = jest.fn()
    render(<RouteWithLocation onChange={watcher} />)

    act(() => navigate('/foo', { replace: true }))
    expect(watcher).toHaveBeenCalledWith(expect.objectContaining({ action: 'replace' }))
  })

  test('action is undefined on initial mount (onInitial: true) — not a history navigation', () => {
    const watcher = jest.fn()
    render(<RouteWithLocation onChange={watcher} onInitial />)

    expect(watcher).toHaveBeenCalledWith(expect.objectContaining({ action: undefined }))
  })

  test('action is "push" on subsequent regular navigate after replace', () => {
    const watcher = jest.fn()
    render(<RouteWithLocation onChange={watcher} />)

    act(() => navigate('/foo', { replace: true }))
    act(() => navigate('/bar'))
    expect(watcher).toHaveBeenLastCalledWith(expect.objectContaining({ action: 'push' }))
  })
})

@n33pm
Copy link
Copy Markdown
Contributor

n33pm commented Mar 11, 2026

sry, I forgot to submit my review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

useLocationChange: Unable to know whether the location was changed using pushState/replaceState/popState

2 participants