[React Native] Add tests to paper renderer for measure, measureLayout (#15323)

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

* Update tests

* Shouldn't have removed UIManager import
This commit is contained in:
Eli White
2019-04-09 14:49:07 -07:00
committed by GitHub
parent aece8119cf
commit c7a959982b
2 changed files with 122 additions and 41 deletions
+34 -1
View File
@@ -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(),
};
@@ -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},
@@ -269,7 +351,7 @@ describe('ReactNative', () => {
});
[View, Subclass, CreateClass].forEach(Component => {
UIManager.measureLayout.mockReset();
UIManager.measureLayout.mockClear();
let viewRef;
let otherRef;
@@ -291,7 +373,6 @@ describe('ReactNative', () => {
);
expect(UIManager.measureLayout).not.toBeCalled();
const successCallback = jest.fn();
const failureCallback = jest.fn();
viewRef.measureLayout(
@@ -299,25 +380,9 @@ describe('ReactNative', () => {
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);
});
});
@@ -328,7 +393,7 @@ describe('ReactNative', () => {
}));
[View].forEach(Component => {
UIManager.measureLayout.mockReset();
UIManager.measureLayout.mockClear();
let viewRef;
let otherRef;
@@ -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);
});
});