Resolve host new-worktree route action before commit (#3242)
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
|
||||
import {
|
||||
createInitialHostRouteActionState,
|
||||
resolveHostRouteActionState,
|
||||
setHostRouteNewWorktreeVisible
|
||||
} from './host-route-action-state'
|
||||
|
||||
describe('host route action state', () => {
|
||||
it('opens new worktree modal on an initial newWorktree action', () => {
|
||||
expect(createInitialHostRouteActionState('newWorktree')).toEqual({
|
||||
routeAction: 'newWorktree',
|
||||
showNewWorktree: true
|
||||
})
|
||||
})
|
||||
|
||||
it('keeps initial non-action routes closed', () => {
|
||||
expect(createInitialHostRouteActionState(undefined)).toEqual({
|
||||
routeAction: undefined,
|
||||
showNewWorktree: false
|
||||
})
|
||||
})
|
||||
|
||||
it('opens once when route action changes to newWorktree', () => {
|
||||
expect(
|
||||
resolveHostRouteActionState({ routeAction: undefined, showNewWorktree: false }, 'newWorktree')
|
||||
).toEqual({
|
||||
routeAction: 'newWorktree',
|
||||
showNewWorktree: true
|
||||
})
|
||||
})
|
||||
|
||||
it('does not reopen after user closes while route action is unchanged', () => {
|
||||
const closed = setHostRouteNewWorktreeVisible(
|
||||
{ routeAction: 'newWorktree', showNewWorktree: true },
|
||||
false
|
||||
)
|
||||
|
||||
expect(resolveHostRouteActionState(closed, 'newWorktree')).toBe(closed)
|
||||
})
|
||||
|
||||
it('preserves an already-open modal when route action changes away', () => {
|
||||
expect(
|
||||
resolveHostRouteActionState({ routeAction: 'newWorktree', showNewWorktree: true }, undefined)
|
||||
).toEqual({
|
||||
routeAction: undefined,
|
||||
showNewWorktree: true
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,36 @@
|
||||
export type HostRouteActionState = {
|
||||
routeAction: string | undefined
|
||||
showNewWorktree: boolean
|
||||
}
|
||||
|
||||
export function createInitialHostRouteActionState(
|
||||
routeAction: string | undefined
|
||||
): HostRouteActionState {
|
||||
return {
|
||||
routeAction,
|
||||
showNewWorktree: routeAction === 'newWorktree'
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveHostRouteActionState(
|
||||
current: HostRouteActionState,
|
||||
routeAction: string | undefined
|
||||
): HostRouteActionState {
|
||||
if (current.routeAction === routeAction) {
|
||||
return current
|
||||
}
|
||||
return {
|
||||
routeAction,
|
||||
showNewWorktree: current.showNewWorktree || routeAction === 'newWorktree'
|
||||
}
|
||||
}
|
||||
|
||||
export function setHostRouteNewWorktreeVisible(
|
||||
current: HostRouteActionState,
|
||||
showNewWorktree: boolean
|
||||
): HostRouteActionState {
|
||||
if (current.showNewWorktree === showNewWorktree) {
|
||||
return current
|
||||
}
|
||||
return { ...current, showNewWorktree }
|
||||
}
|
||||
Reference in New Issue
Block a user