UNPKG

recoil

Version:

Recoil - A state management library for React

138 lines (114 loc) 4.98 kB
/** * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @emails oncall+recoil * @flow strict-local * @format */ 'use strict'; import type { RecoilState, RecoilValue, RecoilValueReadOnly } from '../core/Recoil_RecoilValue'; import type { Store } from '../core/Recoil_State'; // @fb-only: const ReactDOMComet = require('ReactDOMComet'); const ReactDOM = require('ReactDOMLegacy_DEPRECATED'); const { act } = require('ReactTestUtils'); const { graph } = require('../core/Recoil_Graph'); const { RecoilRoot, notifyComponents_FOR_TESTING, sendEndOfBatchNotifications_FOR_TESTING } = require('../core/Recoil_RecoilRoot.react'); const { invalidateDownstreams_FOR_TESTING } = require('../core/Recoil_RecoilValueInterface'); const { makeEmptyStoreState } = require('../core/Recoil_State'); const { useRecoilValue, useResetRecoilState, useSetRecoilState } = require('../hooks/Recoil_Hooks'); const selector = require('../recoil_values/Recoil_selector'); const invariant = require('../util/Recoil_invariant'); const nullthrows = require('../util/Recoil_nullthrows'); const stableStringify = require('../util/Recoil_stableStringify'); const React = require('react'); const { useEffect } = require('react'); const ReactDOMComet = require('ReactDOMLegacy_DEPRECATED'); // @oss-only // @fb-only: const IS_INTERNAL = true; const IS_INTERNAL = false; // @oss-only // TODO Use Snapshot for testing instead of this thunk? declare function makeStore(): Store; declare class ErrorBoundary extends React.Component<{ children: React.Node | null, ... }, { hasError: boolean, ... }> { state: any, static getDerivedStateFromError(_error: any): any, render(): any, } declare function createLegacyReactRoot(container: any, contents: any): any; declare function createConcurrentReactRoot(container: any, contents: any): any; declare function renderElementsInternal(elements: ?React.Node, createReactRoot: any): HTMLDivElement; declare function renderElements(elements: ?React.Node): HTMLDivElement; declare function renderElementsInConcurrentRoot(elements: ?React.Node): HTMLDivElement; declare function renderElementsWithSuspenseCount(elements: ?React.Node): [HTMLDivElement, JestMockFn<[], void>]; //////////////////////////////////////// // Useful RecoilValue nodes for testing //////////////////////////////////////// let id = 0; declare var errorThrowingAsyncSelector: <T, S>(msg: any, dep: ?RecoilValue<S>) => RecoilValueReadOnly<T>; declare var resolvingAsyncSelector: <T>(value: T) => RecoilValueReadOnly<T> | RecoilValueReadOnly<mixed>; declare var loadingAsyncSelector: () => any; declare function asyncSelector<T, S>(dep?: RecoilValue<S>): [RecoilValue<T>, (T) => void, (Error) => void]; ////////////////////////////////// // Useful Components for testing ////////////////////////////////// declare function ReadsAtom<T>(arg0: { atom: RecoilValue<T> }): React.Node; // Returns a tuple: [ // Component, // setValue(T), // resetValue() // ] declare function componentThatReadsAndWritesAtom<T>(atom: RecoilState<T>): [() => React.Node, (T) => void, () => void]; declare function flushPromisesAndTimers(): Promise<void>; type ReloadImports = () => void | (() => void); type AssertionsFn = (gks: Array<string>) => ?Promise<mixed>; type TestOptions = { gks?: Array<Array<string>> }; type TestFn = (string, AssertionsFn, TestOptions | void) => void; declare var testGKs: (reloadImports: ReloadImports, gks: Array<Array<string>>) => TestFn; // TODO Remove the recoil_suppress_rerender_in_callback GK checks const WWW_GKS_TO_TEST = [['recoil_suppress_rerender_in_callback'], ['recoil_suppress_rerender_in_callback', 'recoil_early_rendering_2021'], ['recoil_suppress_rerender_in_callback', 'recoil_hamt_2020'], ['recoil_suppress_rerender_in_callback', 'recoil_memory_managament_2020', 'recoil_release_on_cascading_update_killswitch_2021'], ['recoil_suppress_rerender_in_callback', 'recoil_hamt_2020', 'recoil_memory_managament_2020', 'recoil_release_on_cascading_update_killswitch_2021']]; /** * GK combinations to exclude in OSS, presumably because these combinations pass * in FB internally but not in OSS. Ideally this array would be empty. */ const OSS_GK_COMBINATION_EXCLUSIONS = []; // eslint-disable-next-line no-unused-vars const OSS_GKS_TO_TEST = WWW_GKS_TO_TEST.filter(gkCombination => !OSS_GK_COMBINATION_EXCLUSIONS.some(exclusion => exclusion.every(gk => gkCombination.includes(gk)))); declare var getRecoilTestFn: (reloadImports: ReloadImports) => TestFn; module.exports = { makeStore, renderElements, renderElementsWithSuspenseCount, renderElementsInConcurrentRoot, ReadsAtom, componentThatReadsAndWritesAtom, errorThrowingAsyncSelector, resolvingAsyncSelector, loadingAsyncSelector, asyncSelector, flushPromisesAndTimers, getRecoilTestFn, IS_INTERNAL };