Here's a quick example of how to get a 1-1 reproduction of storybook fixtures in jest.
Note: If you use storybook, definitely do this - it removes the need to set up context manually for jest testing
// .storybook/preview.tsx| const isIntrospectionQuery = (arg: string) => { | |
| const query = parse(arg); | |
| const opDefs = query.definitions.filter(d => d.kind == "OperationDefinition") as OperationDefinitionNode[]; | |
| // Must only have one definition | |
| if (opDefs.length > 1) { | |
| return false; | |
| } | |
| const selections = opDefs[0].selectionSet.selections; |
.storybook/preview.tsx
import { addDecorator, DecoratorFn } from '@storybook/react';
/** Adds react-router context */
const routerDecorator: DecoratorFn = (Story, context) => {
const router = context.parameters.router;| import { useState, useCallback, useRef } from 'react'; | |
| export const useSynchronousReducer = <S, A>( | |
| reducer: (s: S, a: A) => S, | |
| initialState: S | |
| ) => { | |
| const stateRef = useRef(initialState); | |
| const [state, setState] = useState<S>(stateRef.current); | |
| const dispatch = useCallback<(a: A) => S>((action) => { |
| export const ttlCache = <K extends string, V = any>({ ttl, maxSize }: { ttl: number, maxSize?: number }) => { | |
| let cache: Record<K, { value: V; expiresAt: number } | undefined> = Object.create(null); | |
| return { | |
| set: (key: K, value: V) => { | |
| const keys = Object.keys(cache) as K[]; | |
| if (maxSize && keys.length === maxSize) { | |
| delete cache[keys[0]]; | |
| } |
| export type Event<K extends string = string, V extends any = any> = { | |
| key: K; | |
| value: V; | |
| }; | |
| export class EventTarget<T extends Event> { | |
| private listeners: { [key in T["key"]]?: Array<(event: any) => void> } = {}; | |
| public addEventListener<K extends T["key"]>( | |
| type: K, |
| Wiphy phy1 | |
| wiphy index: 1 | |
| max # scan SSIDs: 4 | |
| max scan IEs length: 2190 bytes | |
| max # sched scan SSIDs: 0 | |
| max # match sets: 0 | |
| Retry short limit: 7 | |
| Retry long limit: 4 | |
| Coverage class: 0 (up to 0m) | |
| Device supports AP-side u-APSD. |