Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/jest-preset/jest/MockNativeMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,35 @@ const MockNativeMethods = {
setNativeProps: jest.fn(),
focus: jest.fn(),
blur: jest.fn(),
getBoundingClientRect: jest.fn(function () {
return {
x: 0,
y: 0,
width: 0,
height: 0,
top: 0,
left: 0,
right: 0,
bottom: 0,
};
}),
} as {
measure: () => void,
measureInWindow: () => void,
measureLayout: () => void,
setNativeProps: () => void,
focus: () => void,
blur: () => void,
getBoundingClientRect: () => {
x: number,
y: number,
width: number,
height: number,
top: number,
left: number,
right: number,
bottom: number,
},
};

export default MockNativeMethods;
59 changes: 59 additions & 0 deletions packages/jest-preset/jest/__tests__/getBoundingClientRect-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* 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-local
* @format
*/

import * as React from 'react';
import View from 'react-native/Libraries/Components/View/View';
import TestRenderer from 'react-test-renderer';

describe('getBoundingClientRect on mocked host components', () => {
test('View ref has getBoundingClientRect as a function', async () => {
let viewRef: $FlowFixMe = null;

await TestRenderer.act(() => {
TestRenderer.create(
<View
ref={(ref: $FlowFixMe) => {
viewRef = ref;
}}
/>,
);
});

expect(viewRef).not.toBeNull();
expect(typeof viewRef.getBoundingClientRect).toBe('function');
});

test('calling getBoundingClientRect on a View ref does not throw', async () => {
let rect: $FlowFixMe = null;

await TestRenderer.act(() => {
TestRenderer.create(
<View
ref={(ref: $FlowFixMe) => {
if (ref) {
rect = ref.getBoundingClientRect();
}
}}
/>,
);
});

expect(rect).toEqual({
x: 0,
y: 0,
width: 0,
height: 0,
top: 0,
left: 0,
right: 0,
bottom: 0,
});
});
});
57 changes: 57 additions & 0 deletions packages/jest-preset/jest/__tests__/mockNativeMethods-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* 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-local
* @format
*/

import MockNativeMethods from '../MockNativeMethods';

describe('MockNativeMethods', () => {
test('provides mock for measure', () => {
expect(jest.isMockFunction(MockNativeMethods.measure)).toBe(true);
});

test('provides mock for measureInWindow', () => {
expect(jest.isMockFunction(MockNativeMethods.measureInWindow)).toBe(true);
});

test('provides mock for measureLayout', () => {
expect(jest.isMockFunction(MockNativeMethods.measureLayout)).toBe(true);
});

test('provides mock for setNativeProps', () => {
expect(jest.isMockFunction(MockNativeMethods.setNativeProps)).toBe(true);
});

test('provides mock for focus', () => {
expect(jest.isMockFunction(MockNativeMethods.focus)).toBe(true);
});

test('provides mock for blur', () => {
expect(jest.isMockFunction(MockNativeMethods.blur)).toBe(true);
});

test('provides mock for getBoundingClientRect', () => {
expect(jest.isMockFunction(MockNativeMethods.getBoundingClientRect)).toBe(
true,
);
});

test('getBoundingClientRect returns a DOMRect-like object with zero values', () => {
const rect = MockNativeMethods.getBoundingClientRect();
expect(rect).toEqual({
x: 0,
y: 0,
width: 0,
height: 0,
top: 0,
left: 0,
right: 0,
bottom: 0,
});
});
});
21 changes: 21 additions & 0 deletions packages/jest-preset/jest/mockNativeComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ export default function mockNativeComponent<TProps extends {...}>(
measureInWindow: () => void = jest.fn();
measureLayout: () => void = jest.fn();
setNativeProps: () => void = jest.fn();
getBoundingClientRect: () => {
x: number,
y: number,
width: number,
height: number,
top: number,
left: number,
right: number,
bottom: number,
} = jest.fn(function () {
return {
x: 0,
y: 0,
width: 0,
height: 0,
top: 0,
left: 0,
right: 0,
bottom: 0,
};
});
};

if (viewName === 'RCTView') {
Expand Down
Loading