recoil
Version:
Recoil - A state management library for React
54 lines (51 loc) • 1.44 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('../../testing/Recoil_TestingUtils');
let unstable_batchedUpdates, batchUpdates, getBatcher, setBatcher;
const testRecoil = getRecoilTestFn(() => {
({
unstable_batchedUpdates
} = require('ReactDOM'));
({
batchUpdates,
getBatcher,
setBatcher
} = require('../Recoil_Batching'));
});
/**
* Cleanup function that will reset the batcher back
* to ReactDOM's resetBatcherToDefault.
*
* Call this at the end of a test that calls setBatcher
* to maintain test purity.
*/
declare var resetBatcherToDefault: () => any;
describe('batcher', () => {
testRecoil('default batcher is ReactDOM unstable_batchedUpdates', () => {
expect(getBatcher()).toEqual(unstable_batchedUpdates);
});
testRecoil('setBatcher sets the batcher function', () => {
const batcherFn = jest.fn();
setBatcher(batcherFn);
expect(getBatcher()).toEqual(batcherFn);
resetBatcherToDefault();
});
testRecoil('batchUpdates calls the batcher', () => {
const batcherFn = jest.fn();
setBatcher(batcherFn);
batchUpdates(() => {});
expect(batcherFn).toHaveBeenCalledTimes(1);
resetBatcherToDefault();
});
});