recoil
Version:
Recoil - A state management library for React
85 lines (81 loc) • 2.79 kB
Flow
/**
* 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
*/
;
const {
getRecoilTestFn
} = require('../../__test_utils__/Recoil_TestingUtils');
let React, useRef, useState, act, atom, counterAtom, renderElements, useRecoilInterface;
const testRecoil = getRecoilTestFn(() => {
React = require('react');
({
useRef,
useState
} = require('react'));
({
act
} = require('ReactTestUtils'));
atom = require('../../recoil_values/Recoil_atom');
({
renderElements
} = require('../../__test_utils__/Recoil_TestingUtils'));
({
useRecoilInterface
} = require('../Recoil_Hooks'));
counterAtom = atom({
key: `counterAtom`,
default: 0
});
});
testRecoil('Interface for non-react code - useRecoilState', () => {
declare function nonReactCode(recoilInterface: any): any;
let updateValue;
declare var Component: () => any;
const container = renderElements(<Component />);
expect(container.textContent).toEqual('0');
act(() => updateValue(1));
expect(container.textContent).toEqual('1');
});
testRecoil('Interface for non-react code - useRecoilStateNoThrow', () => {
declare function nonReactCode(recoilInterface: any): any;
let updateValue;
declare var Component: () => any;
const container = renderElements(<Component />);
expect(container.textContent).toEqual('0');
act(() => updateValue(1));
expect(container.textContent).toEqual('1');
});
testRecoil('Interface for non-react code - useRecoilValue, useSetRecoilState', () => {
declare function nonReactCode(recoilInterface: any): any;
let updateValue;
declare var Component: () => any;
const container = renderElements(<Component />);
expect(container.textContent).toEqual('0');
act(() => updateValue(1));
expect(container.textContent).toEqual('1');
});
testRecoil('Interface for non-react code - useRecoilValueNoThrow', () => {
declare function nonReactCode(recoilInterface: any): any;
let updateValue;
declare var Component: () => any;
const container = renderElements(<Component />);
expect(container.textContent).toEqual('0');
act(() => updateValue(1));
expect(container.textContent).toEqual('1');
}); // Test that we always get a consistent instance of the interface object and
// hooks from useRecoilInterface() (at least for a given <AppRoot> store)
testRecoil('Consistent interface object', () => {
let setValue;
declare var Component: () => any;
const out = renderElements(<Component />);
expect(out.textContent).toBe('0');
act(() => setValue(1)); // Force a re-render of the Component
expect(out.textContent).toBe('1');
});