Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions packages/preact/src/hydration.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import {
act
} from '@testing-library/preact'
import {
type InjectionContext,
signal,
hydratable
} from '@nano_kit/store'
import {
InjectionContextProvider,
useInjectionContext,
useInject,
useSignal
} from './core.js'
Expand All @@ -36,6 +38,12 @@ function Test() {
)
}

function Context({ contexts }: { contexts: (InjectionContext | undefined)[] }) {
contexts.push(useInjectionContext())

return null
}

describe('preact', () => {
describe('hydration', () => {
describe('HydrationProvider', () => {
Expand Down Expand Up @@ -86,6 +94,40 @@ describe('preact', () => {

expect(container.innerHTML).toBe('<div>empty</div>')
})

it('should reuse the parent hydration context by default', () => {
const contexts: (InjectionContext | undefined)[] = []

render(
<InjectionContextProvider>
<HydrationProvider>
<Context contexts={contexts}/>
<HydrationProvider>
<Context contexts={contexts}/>
</HydrationProvider>
</HydrationProvider>
</InjectionContextProvider>
)

expect(contexts[1]).toBe(contexts[0])
})

it('should create a child context when reuse is disabled', () => {
const contexts: (InjectionContext | undefined)[] = []

render(
<InjectionContextProvider>
<HydrationProvider>
<Context contexts={contexts}/>
<HydrationProvider reuse={false}>
<Context contexts={contexts}/>
</HydrationProvider>
</HydrationProvider>
</InjectionContextProvider>
)

expect(contexts[1]).not.toBe(contexts[0])
})
})

describe('StaticHydrationProvider', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/preact/src/hydration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface HydrationProviderProps extends InjectionContextProps {
context?: InjectionProvider[]
/**
* Whether to reuse an existing InjectionContext or create a new one.
* `true` by default.
*/
reuse?: boolean
}
Expand All @@ -36,7 +37,7 @@ export interface HydrationProviderProps extends InjectionContextProps {
export function HydrationProvider({
dehydrated,
context = [],
reuse,
reuse = true,
children
}: HydrationProviderProps) {
const currentContext = useInjectionContext()
Expand Down
2 changes: 1 addition & 1 deletion website/src/content/docs/store-integrations/preact.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ function App({ dehydrated }) {
Props:
- `dehydrated?` — dehydrated key-value pairs `[string, unknown][]`, or a falsy value to skip hydration
- `context?` — additional injection providers
- `reuse?` — reuse an existing `InjectionContext` instead of creating a new one; set to `true` if you want the hydration context to be shared with the rest of your app instead of isolated
- `reuse?` — reuse an existing `InjectionContext` instead of creating a new one; `true` by default

#### `StaticHydrationProvider`

Expand Down