:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import '@testing-library/jest-dom'
import { act, render, screen } from '@testing-library/react'
import { MakeLogicType, actions, getContext, kea, path, reducers, useValues } from 'kea'
import { initKeaTests } from '~/test/init'
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface counterTestLogicValues {
count: number
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface counterTestLogicActions {
increment: () => {
value: true
}
}
export type counterTestLogicType = MakeLogicType<counterTestLogicValues, counterTestLogicActions>
const counterTestLogic = kea<counterTestLogicType>([
path(['lib', 'keaMidDispatchSnapshot', 'counterTestLogic']),
actions({ increment: true }),
reducers({ count: [0, { increment: (state) => state + 1 }] }),
])
function Count(): JSX.Element {
const { count } = useValues(counterTestLogic)
return <span>count is {count}</span>
}
// Guards patches/[email protected]: React can read a render snapshot while a redux
// dispatch is still running (seen on Firefox), where getState() throws and crashes the
// component tree. The patched useSelector must fall back to the last good snapshot.
describe('kea useSelector mid-dispatch snapshot guard', () => {
beforeEach(() => {
initKeaTests()
})
it('survives getState throwing mid-dispatch and recovers on the next read', () => {
render(<Count />)
expect(screen.getByText('count is 0')).toBeInTheDocument()
const store = getContext().store
const realGetState = store.getState
let thrown = false
store.getState = () => {
if (!thrown) {
thrown = true
throw new Error('You may not call store.getState() while the reducer is executing.')
}
return realGetState()
}
try {
expect(() => {
act(() => {
counterTestLogic.actions.increment()
})
}).not.toThrow()
} finally {
store.getState = realGetState
}
expect(thrown).toBe(true)
act(() => {
counterTestLogic.actions.increment()
})
expect(screen.getByText('count is 2')).toBeInTheDocument()
})
})