Files
react-native/Libraries/Components/ScrollView/__tests__/ScrollView-test.js
T
Tim YungandFacebook GitHub Bot 99381de753 ScrollView: Cleanup Jest Tests
Summary:
Cleans up `ScrollView-test.js` to be easier to read and to include a unit test for `ref`.

This prepares the Jest test suite to be extended with an additional test to validate a bug with `ref` invalidation.

Changelog:
[Internal]

Reviewed By: sammy-SC

Differential Revision: D41208894

fbshipit-source-id: 6d79adf131602292f7aa3180b1c230643e9baf16
2022-11-12 08:35:29 -08:00

89 lines
2.5 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow-strict
* @format
* @oncall react_native
*/
'use strict';
const Text = require('../../../Text/Text');
const ReactNativeTestTools = require('../../../Utilities/ReactNativeTestTools');
const View = require('../../View/View');
const ScrollView = require('../ScrollView');
const React = require('react');
const ReactTestRenderer = require('react-test-renderer');
describe('ScrollView', () => {
beforeEach(() => {
jest.resetModules();
});
it('renders its children', () => {
ReactNativeTestTools.expectRendersMatchingSnapshot(
'ScrollView',
() => (
<ScrollView>
<View>
<Text>Hello World!</Text>
</View>
</ScrollView>
),
() => {
jest.dontMock('../ScrollView');
},
);
});
it('mocks native methods and instance methods', () => {
jest.mock('../ScrollView');
const ref = React.createRef();
ReactTestRenderer.create(<ScrollView ref={ref} />);
expect(ref.current?.measure).toBeInstanceOf(jest.fn().constructor);
expect(ref.current?.scrollTo).toBeInstanceOf(jest.fn().constructor);
});
describe('ref', () => {
it('receives an instance or null', () => {
jest.dontMock('../ScrollView');
const scrollViewRef = jest.fn();
const testRendererInstance = ReactTestRenderer.create(
<ScrollView ref={scrollViewRef} />,
);
expect(scrollViewRef).toHaveBeenLastCalledWith(
expect.objectContaining({_nativeTag: expect.any(Number)}),
);
testRendererInstance.unmount();
expect(scrollViewRef).toHaveBeenLastCalledWith(null);
});
});
describe('getInnerViewRef', () => {
it('returns an instance', () => {
jest.dontMock('../ScrollView');
const scrollViewRef = React.createRef(null);
ReactTestRenderer.create(<ScrollView ref={scrollViewRef} />);
const innerViewRef = scrollViewRef.current.getInnerViewRef();
// This is checking if the ref acts like a host component. If we had an
// `isHostComponent(ref)` method, that would be preferred.
expect(innerViewRef.measure).toBeInstanceOf(jest.fn().constructor);
expect(innerViewRef.measureLayout).toBeInstanceOf(jest.fn().constructor);
expect(innerViewRef.measureInWindow).toBeInstanceOf(
jest.fn().constructor,
);
});
});
});