Skip to content

Commit

Permalink
Add Flowtests for HostComponent
Browse files Browse the repository at this point in the history
Summary:
`HostComponent` is built specifically to differentiate from `ReactNative.NativeComponent`. These tests should ensure that is the case, and help it stay that way.

I also expect these tests to be duplicated to DefinitelyTyped to help the team working on the TypeScript types ensure they have things modeled correctly.

Reviewed By: cpojer

Differential Revision: D17580120

fbshipit-source-id: c14eb18507cbee1b308beeff0092607e18706171
  • Loading branch information
TheSavior authored and facebook-github-bot committed Sep 26, 2019
1 parent bcc58bc commit 79e08f3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
52 changes: 52 additions & 0 deletions Libraries/Renderer/__flowtests__/ReactNativeTypes-flowtest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* 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.
*
* @flow
* @format
*/

'use strict';

import * as React from 'react';
import type {
HostComponent,
ReactNativeComponentClass_ForTestsOnly,
} from '../shims/ReactNativeTypes';

function takesHostComponentInstance(
instance: React$ElementRef<HostComponent<mixed>> | null,
): void {}

const MyHostComponent = (('Host': any): HostComponent<mixed>);

<MyHostComponent
ref={hostComponentRef => {
takesHostComponentInstance(hostComponentRef);

if (hostComponentRef == null) {
return;
}

hostComponentRef.measureLayout(hostComponentRef, () => {});
}}
/>;

declare var NativeComponent: ReactNativeComponentClass_ForTestsOnly<{}>;
class MyNativeComponent extends NativeComponent {}

<MyNativeComponent
ref={nativeComponentRef => {
// $FlowExpectedError - NativeComponent cannot be passed as HostComponent.
takesHostComponentInstance(nativeComponentRef);

if (nativeComponentRef == null) {
return;
}

// $FlowExpectedError - NativeComponent cannot be passed as HostComponent.
nativeComponentRef.measureLayout(nativeComponentRef, () => {});
}}
/>;
5 changes: 5 additions & 0 deletions Libraries/Renderer/shims/ReactNativeTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ class ReactNativeComponent<Props> extends React.Component<Props> {
setNativeProps(nativeProps: Object): void {}
}

// This type is only used for FlowTests. It shouldn't be imported directly
export type ReactNativeComponentClass_ForTestsOnly<Props> = Class<
ReactNativeComponent<Props>,
>;

/**
* This type keeps HostComponent and NativeMethodsMixin in sync.
* It can also provide types for ReactNative applications that use NMM or refs.
Expand Down

0 comments on commit 79e08f3

Please sign in to comment.