Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 9, 2019
Merged
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
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