Skip to content

Commit

Permalink
[React Native] Add tests to paper renderer for measure, measureLayout (
Browse files Browse the repository at this point in the history
…#15323)

* [React Native] Add tests to paper renderer for measure, measureLayout

* Update tests

* Shouldn't have removed UIManager import
  • Loading branch information
elicwhite authored Apr 9, 2019
1 parent aece811 commit c7a9599
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 41 deletions.
35 changes: 34 additions & 1 deletion packages/react-native-renderer/src/__mocks__/UIManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,40 @@ const RCTUIManager = {
views.get(parentTag).children.forEach(tag => removeChild(parentTag, tag));
}),
replaceExistingNonRootView: jest.fn(),
measureLayout: jest.fn(),
measure: jest.fn(function measure(tag, callback) {
invariant(
typeof tag === 'number',
'Expected tag to be a number, was passed %s',
tag,
);
callback(10, 10, 100, 100, 0, 0);
}),
measureInWindow: jest.fn(function measureInWindow(tag, callback) {
invariant(
typeof tag === 'number',
'Expected tag to be a number, was passed %s',
tag,
);
callback(10, 10, 100, 100);
}),
measureLayout: jest.fn(function measureLayout(
tag,
relativeTag,
fail,
success,
) {
invariant(
typeof tag === 'number',
'Expected tag to be a number, was passed %s',
tag,
);
invariant(
typeof relativeTag === 'number',
'Expected relativeTag to be a number, was passed %s',
relativeTag,
);
success(1, 1, 100, 100);
}),
__takeSnapshot: jest.fn(),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,88 @@ describe('ReactNative', () => {
});
});

it('should call UIManager.measure on ref.measure', () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTView',
}));

class Subclass extends ReactNative.NativeComponent {
render() {
return <View>{this.props.children}</View>;
}
}

const CreateClass = createReactClass({
mixins: [NativeMethodsMixin],
render() {
return <View>{this.props.children}</View>;
},
});

[View, Subclass, CreateClass].forEach(Component => {
UIManager.measure.mockClear();

let viewRef;
ReactNative.render(
<Component
ref={ref => {
viewRef = ref;
}}
/>,
11,
);

expect(UIManager.measure).not.toBeCalled();
const successCallback = jest.fn();
viewRef.measure(successCallback);
expect(UIManager.measure).toHaveBeenCalledTimes(1);
expect(successCallback).toHaveBeenCalledTimes(1);
expect(successCallback).toHaveBeenCalledWith(10, 10, 100, 100, 0, 0);
});
});

it('should call UIManager.measureInWindow on ref.measureInWindow', () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
uiViewClassName: 'RCTView',
}));

class Subclass extends ReactNative.NativeComponent {
render() {
return <View>{this.props.children}</View>;
}
}

const CreateClass = createReactClass({
mixins: [NativeMethodsMixin],
render() {
return <View>{this.props.children}</View>;
},
});

[View, Subclass, CreateClass].forEach(Component => {
UIManager.measureInWindow.mockClear();

let viewRef;
ReactNative.render(
<Component
ref={ref => {
viewRef = ref;
}}
/>,
11,
);

expect(UIManager.measureInWindow).not.toBeCalled();
const successCallback = jest.fn();
viewRef.measureInWindow(successCallback);
expect(UIManager.measureInWindow).toHaveBeenCalledTimes(1);
expect(successCallback).toHaveBeenCalledTimes(1);
expect(successCallback).toHaveBeenCalledWith(10, 10, 100, 100);
});
});

it('should support reactTag in ref.measureLayout', () => {
const View = createReactNativeComponentClass('RCTView', () => ({
validAttributes: {foo: true},
Expand All @@ -269,7 +351,7 @@ describe('ReactNative', () => {
});

[View, Subclass, CreateClass].forEach(Component => {
UIManager.measureLayout.mockReset();
UIManager.measureLayout.mockClear();

let viewRef;
let otherRef;
Expand All @@ -291,33 +373,16 @@ describe('ReactNative', () => {
);

expect(UIManager.measureLayout).not.toBeCalled();

const successCallback = jest.fn();
const failureCallback = jest.fn();
viewRef.measureLayout(
ReactNative.findNodeHandle(otherRef),
successCallback,
failureCallback,
);

expect(UIManager.measureLayout).toHaveBeenCalledTimes(1);
expect(UIManager.measureLayout).toHaveBeenCalledWith(
expect.any(Number),
expect.any(Number),
expect.any(Function),
expect.any(Function),
);

const args = UIManager.measureLayout.mock.calls[0];
expect(args[0]).not.toEqual(args[1]);
expect(successCallback).not.toBeCalled();
expect(failureCallback).not.toBeCalled();
args[2]('fail');
expect(failureCallback).toBeCalledWith('fail');

expect(successCallback).not.toBeCalled();
args[3]('success');
expect(successCallback).toBeCalledWith('success');
expect(successCallback).toHaveBeenCalledTimes(1);
expect(successCallback).toHaveBeenCalledWith(1, 1, 100, 100);
});
});

Expand All @@ -328,7 +393,7 @@ describe('ReactNative', () => {
}));

[View].forEach(Component => {
UIManager.measureLayout.mockReset();
UIManager.measureLayout.mockClear();

let viewRef;
let otherRef;
Expand All @@ -350,29 +415,12 @@ describe('ReactNative', () => {
);

expect(UIManager.measureLayout).not.toBeCalled();

const successCallback = jest.fn();
const failureCallback = jest.fn();
viewRef.measureLayout(otherRef, successCallback, failureCallback);

expect(UIManager.measureLayout).toHaveBeenCalledTimes(1);
expect(UIManager.measureLayout).toHaveBeenCalledWith(
expect.any(Number),
expect.any(Number),
expect.any(Function),
expect.any(Function),
);

const args = UIManager.measureLayout.mock.calls[0];
expect(args[0]).not.toEqual(args[1]);
expect(successCallback).not.toBeCalled();
expect(failureCallback).not.toBeCalled();
args[2]('fail');
expect(failureCallback).toBeCalledWith('fail');

expect(successCallback).not.toBeCalled();
args[3]('success');
expect(successCallback).toBeCalledWith('success');
expect(successCallback).toHaveBeenCalledTimes(1);
expect(successCallback).toHaveBeenCalledWith(1, 1, 100, 100);
});
});

Expand Down

0 comments on commit c7a9599

Please sign in to comment.