Skip to content

Commit

Permalink
fix: fixed types for batchReplaceState, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BowlingX committed Nov 21, 2023
1 parent 3e7663d commit 2f1f828
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,6 @@
"prettier": {
"singleQuote": true,
"semi": false
}
},
"packageManager": "[email protected]"
}
49 changes: 47 additions & 2 deletions src/__tests__/static-render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { render, cleanup, screen, act } from '@testing-library/react'
import userEventImport from '@testing-library/user-event'
import React from 'react'
import Geschichte from '../lib/adapters/static/index.js'
import { factoryParameters } from '../lib/store.js'
import {
factoryParameters,
InferNamespaceValues,
useBatchQuery,
} from '../lib/store.js'
import { pm } from '../lib/utils.js'
import { serializers } from '../lib/serializers.js'

Expand All @@ -14,18 +18,23 @@ const userEvent = userEventImport.default || userEventImport
afterEach(cleanup)

describe('<StaticGeschichteProvider />', () => {
const defaults = { someParameter: 'test' }
const { useQuery } = factoryParameters(
{
someParameter: pm('parameter', serializers.string),
},
{ someParameter: 'test' }
defaults
)
const ComponentThatRendersSomethingStatically = () => {
const {
pushState,
replaceState,
values: { someParameter },
} = useQuery()
type FullStore = {
default: InferNamespaceValues<typeof useQuery>
}
const { batchPushState, batchReplaceState } = useBatchQuery<FullStore>()
return (
<>
<span role="content">{someParameter}</span>
Expand All @@ -38,6 +47,28 @@ describe('<StaticGeschichteProvider />', () => {
}
></button>

<button
role="batchPush"
onClick={() =>
void batchPushState(['default'], (state) => {
if (state) {
state.someParameter = 'nextBatchPushState'
}
})
}
></button>

<button
role="batchReplace"
onClick={() =>
void batchReplaceState(['default'], (state) => {
if (state) {
state.someParameter = 'nextBatchPushState'
}
})
}
></button>

<button
role="replace"
onClick={() =>
Expand Down Expand Up @@ -74,4 +105,18 @@ describe('<StaticGeschichteProvider />', () => {
})
expect(screen.getByRole('content').textContent).toEqual('nextReplaceState')
})

it('should support batchPushState', async () => {
render(
<Geschichte>
<ComponentThatRendersSomethingStatically />
</Geschichte>
)
await act(async () => {
await userEvent.click(screen.getByRole('batchPush'))
})
expect(screen.getByRole('content').textContent).toEqual(
'nextBatchPushState'
)
})
})
2 changes: 1 addition & 1 deletion src/lib/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export interface StoreState<
readonly updateFromQuery: (query: string | URLSearchParams) => void
readonly batchReplaceState: <Keys extends keyof Namespaces>(
ns: Keys[],
fn: (...args: Namespaces[Keys]['values'][]) => void,
fn: (...args: (Namespaces[Keys]['values'] | undefined)[]) => void,
routerOptions?: RouterOptions
) => Promise<unknown>
readonly batchPushState: <Keys extends keyof Namespaces>(
Expand Down

0 comments on commit 2f1f828

Please sign in to comment.