Skip to content

Commit 66e15ee

Browse files
committed
RN: Flowify packages/react-native/jest
1 parent 7ef278a commit 66e15ee

File tree

13 files changed

+165
-121
lines changed

13 files changed

+165
-121
lines changed

flow-typed/npm/jest.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,10 @@ type JestObjectType = {
853853
* Returns the number of fake timers still left to run.
854854
*/
855855
getTimerCount(): number,
856+
/**
857+
* Returns the time in ms of the current clock.
858+
*/
859+
now(): number,
856860
/**
857861
* Set the current system time used by fake timers.
858862
* Simulates a user changing the system clock while your program is running.

packages/react-native/jest/MockNativeMethods.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @flow strict
78
* @format
89
*/
910

10-
'use strict';
11+
import type {LegacyHostInstanceMethods} from "../src/private/types/HostInstance";
1112

1213
const MockNativeMethods = {
1314
measure: jest.fn(),
@@ -16,6 +17,13 @@ const MockNativeMethods = {
1617
setNativeProps: jest.fn(),
1718
focus: jest.fn(),
1819
blur: jest.fn(),
20+
} as {
21+
measure: () => void,
22+
measureInWindow: () => void,
23+
measureLayout: () => void,
24+
setNativeProps: () => void,
25+
focus: () => void,
26+
blur: () => void,
1927
};
2028

21-
module.exports = MockNativeMethods;
29+
export default MockNativeMethods;

packages/react-native/jest/RefreshControlMock.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ const RCTRefreshControl: HostComponent<{}> = requireNativeComponent<{}>(
2121

2222
export default class RefreshControlMock extends React.Component<{...}> {
2323
static latestRef: ?RefreshControlMock;
24+
25+
render(): React.Node {
26+
return <RCTRefreshControl />;
27+
}
28+
2429
componentDidMount() {
2530
RefreshControlMock.latestRef = this;
2631
}
27-
render(): React.MixedElement {
28-
return <RCTRefreshControl />;
29-
}
3032
}

packages/react-native/jest/assetFileTransformer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @noflow
78
* @format
89
*/
910

packages/react-native/jest/local-setup.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,25 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @flow strict-local
78
* @format
89
*/
910

1011
// Global setup for tests local to the react-native repo. This setup is not
1112
// included in the react-native Jest preset.
1213

13-
'use strict';
14-
15-
require('./setup');
14+
import './setup';
1615

1716
const consoleError = console.error;
1817
const consoleWarn = console.warn;
1918

19+
// $FlowIgnore[cannot-write]
2020
console.error = (...args) => {
2121
consoleError(...args);
2222
throw new Error('console.error() was called (see error above)');
2323
};
2424

25+
// $FlowIgnore[cannot-write]
2526
console.warn = (...args) => {
2627
consoleWarn(...args);
2728
throw new Error('console.warn() was called (see warning above)');

packages/react-native/jest/mockComponent.js

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,50 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @flow strict
78
* @format
89
*/
910

10-
'use strict';
11+
import * as React from 'react';
12+
import {createElement} from 'react';
1113

12-
module.exports = (moduleName, instanceMethods, isESModule) => {
13-
const RealComponent = isESModule
14-
? jest.requireActual(moduleName).default
15-
: jest.requireActual(moduleName);
16-
const React = require('react');
14+
type Modulish<T> = T | $ReadOnly<{default: T}>;
1715

18-
const SuperClass =
16+
type TComponentType = React.ComponentType<$ReadOnly<{children?: React.Node}>>;
17+
18+
export default function mockComponent<TComponentModule: Modulish<TComponentType>>(
19+
moduleName: string,
20+
instanceMethods: ?interface {},
21+
isESModule: boolean,
22+
): (typeof isESModule extends true ? TComponentModule['default'] : TComponentModule) & typeof instanceMethods {
23+
const RealComponent =
24+
isESModule
25+
// $FlowIgnore[incompatible-cast]
26+
? (jest.requireActual<TComponentModule>(moduleName) as $ReadOnly<{default: TComponentType}>).default
27+
// $FlowIgnore[incompatible-cast]
28+
: jest.requireActual<TComponentModule>(moduleName) as TComponentType;
29+
30+
const SuperClass: typeof React.Component<React.ElementProps<typeof RealComponent>> =
1931
typeof RealComponent === 'function' &&
2032
RealComponent.prototype.constructor instanceof React.Component
2133
? RealComponent
2234
: React.Component;
2335

2436
const name =
25-
RealComponent.displayName ||
26-
RealComponent.name ||
27-
(RealComponent.render // handle React.forwardRef
28-
? RealComponent.render.displayName || RealComponent.render.name
29-
: 'Unknown');
37+
RealComponent.displayName ??
38+
RealComponent.name ??
39+
// $FlowFixMe[incompatible-use] - Checking for `forwardRef` values.
40+
// $FlowFixMe[prop-missing]
41+
(RealComponent.render == null ? 'Unknown' : RealComponent.render.displayName ?? RealComponent.render.name);
3042

3143
const nameWithoutPrefix = name.replace(/^(RCT|RK)/, '');
3244

3345
const Component = class extends SuperClass {
34-
static displayName = 'Component';
46+
static displayName: ?string = 'Component';
3547

36-
render() {
37-
const props = Object.assign({}, RealComponent.defaultProps);
48+
render(): React.Node {
49+
// $FlowIgnore[prop-missing]
50+
const props = {...RealComponent.defaultProps};
3851

3952
if (this.props) {
4053
Object.keys(this.props).forEach(prop => {
@@ -49,7 +62,8 @@ module.exports = (moduleName, instanceMethods, isESModule) => {
4962
});
5063
}
5164

52-
return React.createElement(nameWithoutPrefix, props, this.props.children);
65+
// $FlowIgnore[not-a-function]
66+
return createElement(nameWithoutPrefix, props, this.props.children);
5367
}
5468
};
5569

@@ -62,13 +76,16 @@ module.exports = (moduleName, instanceMethods, isESModule) => {
6276

6377
Component.displayName = nameWithoutPrefix;
6478

79+
// $FlowIgnore[not-an-object]
6580
Object.keys(RealComponent).forEach(classStatic => {
6681
Component[classStatic] = RealComponent[classStatic];
6782
});
6883

6984
if (instanceMethods != null) {
85+
// $FlowIgnore[unsafe-object-assign]
7086
Object.assign(Component.prototype, instanceMethods);
7187
}
7288

89+
// $FlowIgnore[incompatible-return]
7390
return Component;
7491
};

packages/react-native/jest/mockModal.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow strict-local
7+
* @flow strict
88
* @format
99
*/
1010

11-
/* eslint-env jest */
11+
import * as React from 'react';
1212

13-
'use strict';
14-
15-
const React = require('react');
16-
17-
function mockModal(BaseComponent: $FlowFixMe) {
18-
class ModalMock extends BaseComponent {
19-
render(): React.MixedElement | null {
13+
export default function mockModal(
14+
BaseComponent: React.ComponentType<{children?: React.Node}>,
15+
): React.ComponentType<{...React.ElementConfig<typeof BaseComponent>, visible?: ?boolean}> {
16+
// $FlowIgnore[incompatible-use]
17+
return class ModalMock extends BaseComponent {
18+
render(): React.Node {
2019
if (this.props.visible === false) {
2120
return null;
2221
}
@@ -26,7 +25,4 @@ function mockModal(BaseComponent: $FlowFixMe) {
2625
);
2726
}
2827
}
29-
return ModalMock;
3028
}
31-
32-
module.exports = (mockModal: $FlowFixMe);

packages/react-native/jest/mockNativeComponent.js

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,38 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @flow strict
78
* @format
89
*/
910

10-
'use strict';
11+
import type {HostInstance} from '../src/private/types/HostInstance';
1112

12-
const React = require('react');
13-
const {createElement} = require('react');
13+
import * as React from 'react';
14+
import {createElement} from 'react';
1415

1516
let nativeTag = 1;
1617

17-
export default viewName => {
18-
const Component = class extends React.Component {
19-
_nativeTag = nativeTag++;
18+
type MockNativeComponent<TProps: $ReadOnly<{children?: React.Node}>> =
19+
component(ref?: ?React.RefSetter<HostInstance>, ...props: TProps);
2020

21-
render() {
21+
export default function mockNativeComponent<
22+
TProps: $ReadOnly<{children?: React.Node}>,
23+
>(viewName: string): MockNativeComponent<TProps> {
24+
const Component = class extends React.Component<TProps> {
25+
_nativeTag: number = nativeTag++;
26+
27+
render(): React.Node {
28+
// $FlowIgnore[not-a-function]
2229
return createElement(viewName, this.props, this.props.children);
2330
}
2431

2532
// The methods that exist on host components
26-
blur = jest.fn();
27-
focus = jest.fn();
28-
measure = jest.fn();
29-
measureInWindow = jest.fn();
30-
measureLayout = jest.fn();
31-
setNativeProps = jest.fn();
33+
blur: () => void = jest.fn();
34+
focus: () => void = jest.fn();
35+
measure: () => void = jest.fn();
36+
measureInWindow: () => void = jest.fn();
37+
measureLayout: () => void = jest.fn();
38+
setNativeProps: () => void = jest.fn();
3239
};
3340

3441
if (viewName === 'RCTView') {
@@ -38,4 +45,4 @@ export default viewName => {
3845
}
3946

4047
return Component;
41-
};
48+
}

packages/react-native/jest/mockScrollView.js

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,20 @@
88
* @format
99
*/
1010

11-
/* eslint-env jest */
11+
import type {ScrollViewNativeProps} from "../Libraries/Components/ScrollView/ScrollViewNativeComponentType";
1212

13-
'use strict';
13+
import View from '../Libraries/Components/View/View';
14+
import requireNativeComponent from '../Libraries/ReactNative/requireNativeComponent';
15+
import * as React from 'react';
1416

15-
const View = require('../Libraries/Components/View/View').default;
16-
const requireNativeComponent =
17-
require('../Libraries/ReactNative/requireNativeComponent').default;
18-
const React = require('react');
19-
const RCTScrollView: $FlowFixMe = requireNativeComponent('RCTScrollView');
17+
const RCTScrollView = requireNativeComponent<ScrollViewNativeProps>('RCTScrollView');
2018

21-
function mockScrollView(BaseComponent: $FlowFixMe) {
22-
class ScrollViewMock extends BaseComponent {
23-
render(): React.MixedElement {
19+
export default function mockScrollView(
20+
BaseComponent: React.ComponentType<{children?: React.Node}>,
21+
): React.ComponentType<{...React.ElementConfig<typeof BaseComponent>, refreshControl?: ?React.MixedElement}> {
22+
// $FlowIgnore[incompatible-use]
23+
return class ScrollViewMock extends BaseComponent {
24+
render(): React.Node {
2425
return (
2526
<RCTScrollView {...this.props}>
2627
{this.props.refreshControl}
@@ -29,7 +30,4 @@ function mockScrollView(BaseComponent: $FlowFixMe) {
2930
);
3031
}
3132
}
32-
return ScrollViewMock;
3333
}
34-
35-
module.exports = (mockScrollView: $FlowFixMe);

packages/react-native/jest/react-native-env.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @noflow
78
* @format
89
*/
910

packages/react-native/jest/renderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow
7+
* @flow strict
88
* @format
99
*/
1010

packages/react-native/jest/resolver.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7+
* @noflow
78
* @format
89
*/
910

0 commit comments

Comments
 (0)