From 99381de753bb040126c5a54a56df8fbc4457f688 Mon Sep 17 00:00:00 2001 From: Tim Yung Date: Sat, 12 Nov 2022 08:35:29 -0800 Subject: [PATCH] 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 --- .../ScrollView/__tests__/ScrollView-test.js | 65 ++++++++++++------- .../__snapshots__/ScrollView-test.js.snap | 8 +-- 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/Libraries/Components/ScrollView/__tests__/ScrollView-test.js b/Libraries/Components/ScrollView/__tests__/ScrollView-test.js index 6081d8db9c19c7..1af5e9d197bdb6 100644 --- a/Libraries/Components/ScrollView/__tests__/ScrollView-test.js +++ b/Libraries/Components/ScrollView/__tests__/ScrollView-test.js @@ -18,8 +18,12 @@ const ScrollView = require('../ScrollView'); const React = require('react'); const ReactTestRenderer = require('react-test-renderer'); -describe('', () => { - it('should render as expected', () => { +describe('ScrollView', () => { + beforeEach(() => { + jest.resetModules(); + }); + + it('renders its children', () => { ReactNativeTestTools.expectRendersMatchingSnapshot( 'ScrollView', () => ( @@ -34,34 +38,51 @@ describe('', () => { }, ); }); - it('should mock native methods and instance methods when mocked', () => { - jest.resetModules(); + + it('mocks native methods and instance methods', () => { jest.mock('../ScrollView'); - const ref = React.createRef(); + const ref = React.createRef(); ReactTestRenderer.create(); - expect(ref.current != null && ref.current.measure).toBeInstanceOf( - jest.fn().constructor, - ); - expect(ref.current != null && ref.current.scrollTo).toBeInstanceOf( - jest.fn().constructor, - ); + expect(ref.current?.measure).toBeInstanceOf(jest.fn().constructor); + expect(ref.current?.scrollTo).toBeInstanceOf(jest.fn().constructor); }); - it('getInnerViewRef for case where it returns a native view', () => { - jest.resetModules(); - jest.unmock('../ScrollView'); - const scrollViewRef = React.createRef(null); + describe('ref', () => { + it('receives an instance or null', () => { + jest.dontMock('../ScrollView'); + + const scrollViewRef = jest.fn(); + const testRendererInstance = ReactTestRenderer.create( + , + ); + + expect(scrollViewRef).toHaveBeenLastCalledWith( + expect.objectContaining({_nativeTag: expect.any(Number)}), + ); + + testRendererInstance.unmount(); + + expect(scrollViewRef).toHaveBeenLastCalledWith(null); + }); + }); - ReactTestRenderer.create(); + describe('getInnerViewRef', () => { + it('returns an instance', () => { + jest.dontMock('../ScrollView'); - const innerViewRef = scrollViewRef.current.getInnerViewRef(); + const scrollViewRef = React.createRef(null); + ReactTestRenderer.create(); + 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); + // 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, + ); + }); }); }); diff --git a/Libraries/Components/ScrollView/__tests__/__snapshots__/ScrollView-test.js.snap b/Libraries/Components/ScrollView/__tests__/__snapshots__/ScrollView-test.js.snap index e4723b567bc504..be423b0986f156 100644 --- a/Libraries/Components/ScrollView/__tests__/__snapshots__/ScrollView-test.js.snap +++ b/Libraries/Components/ScrollView/__tests__/__snapshots__/ScrollView-test.js.snap @@ -1,6 +1,6 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP -exports[` should render as expected: should deep render when mocked (please verify output manually) 1`] = ` +exports[`ScrollView renders its children: should deep render when mocked (please verify output manually) 1`] = ` @@ -12,7 +12,7 @@ exports[` should render as expected: should deep render when mocke `; -exports[` should render as expected: should deep render when not mocked (please verify output manually) 1`] = ` +exports[`ScrollView renders its children: should deep render when not mocked (please verify output manually) 1`] = ` should render as expected: should deep render when not m `; -exports[` should render as expected: should shallow render as when mocked 1`] = ` +exports[`ScrollView renders its children: should shallow render as when mocked 1`] = ` @@ -75,7 +75,7 @@ exports[` should render as expected: should shallow render as `; -exports[` should render as expected: should shallow render as when not mocked 1`] = ` +exports[`ScrollView renders its children: should shallow render as when not mocked 1`] = `