UNPKG

recoil

Version:

Recoil - A state management library for React

94 lines (89 loc) 2.91 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 { StoreID } from '../../core/Recoil_Keys'; const { getRecoilTestFn } = require('recoil-shared/__test_utils__/Recoil_TestingUtils'); let React, renderElements, renderUnwrappedElements, useEffect, useRef, reactMode, act, RecoilRoot, useRecoilStoreID, atom, componentThatReadsAndWritesAtom, useRecoilBridgeAcrossReactRoots; const testRecoil = getRecoilTestFn(() => { React = require('react'); ({ useEffect, useRef } = React); ({ act } = require('ReactTestUtils')); ({ reactMode } = require('../../core/Recoil_ReactMode')); ({ renderElements, renderUnwrappedElements, componentThatReadsAndWritesAtom } = require('recoil-shared/__test_utils__/Recoil_TestingUtils')); ({ RecoilRoot, useRecoilStoreID } = require('../../core/Recoil_RecoilRoot')); atom = require('../../recoil_values/Recoil_atom'); useRecoilBridgeAcrossReactRoots = require('../Recoil_useRecoilBridgeAcrossReactRoots'); }); declare function NestedReactRoot(arg0: any): any; testRecoil('useRecoilBridgeAcrossReactRoots - create a context bridge', async ({ concurrentMode }) => { // Test fails with useRecoilBridgeAcrossReactRoots() and useMutableSource(). // It only reproduces if act() is used in renderElements() for the nested // root, so it may just be a testing environment issue. if (concurrentMode && reactMode().mode === 'MUTABLE_SOURCE') { return; } const myAtom = atom({ key: 'useRecoilBridgeAcrossReactRoots - context bridge', default: 'DEFAULT' }); declare function initializeState(arg0: any): any; const [ReadWriteAtom, setAtom] = componentThatReadsAndWritesAtom(myAtom); const container = renderElements(<RecoilRoot initializeState={initializeState}> <ReadWriteAtom /> <NestedReactRoot> <ReadWriteAtom /> </NestedReactRoot> </RecoilRoot>); expect(container.textContent).toEqual('"INITIALIZE""INITIALIZE"'); act(() => setAtom('SET')); expect(container.textContent).toEqual('"SET""SET"'); }); testRecoil('StoreID matches bridged store', () => { declare function RecoilStoreID(arg0: { storeIDRef: { current: ?StoreID } }): any; const rootStoreIDRef = { current: null }; const nestedStoreIDRef = { current: null }; const c = renderElements(<> <RecoilStoreID storeIDRef={rootStoreIDRef} /> <NestedReactRoot> <RecoilStoreID storeIDRef={nestedStoreIDRef} /> </NestedReactRoot> RENDER </>); expect(c.textContent).toEqual('RENDER'); expect(rootStoreIDRef.current).toBe(nestedStoreIDRef.current); expect(rootStoreIDRef.current).not.toBe(null); });