[英]React Native how to mock PixelRatio on 1 specific test
I am having trouble testing my snapshot in react-native, to be more specific it's the PixelRatio I am having troubles with. 我无法在react-native中测试快照,更具体地说,这是我遇到麻烦的PixelRatio。
Code speaks more than words - I've simplified the code and removed all the noise: 代码胜于雄辩-我简化了代码并消除了所有干扰:
The component: 组件:
const Slide = (props) => (
<Image
source={props.source}
style={PixelRatio.get() === 2 ?
{ backgroundColor: 'red' } :
{ backgroundCoor: 'green' }}
/>
);
The Snapshot test : 快照测试 :
import { Platform } from 'react-native';
describe('When loading the Slide component', () => {
it('should render correctly on ios', () => {
Platform.OS = 'ios';
const tree = renderer.create(
<Provider store={store}>
<Slide />
</Provider>,
).toJSON();
expect(tree).toMatchSnapshot();
});
describe('on devices with a pixelRatio of 2', () => {
it('it should render correctly', () => {
jest.mock('PixelRatio', () => ({
get: () => 2,
roundToNearestPixel: jest.fn(),
}));
const tree = renderer.create(
<Provider store={store}>
<Slide />
</Provider>,
).toJSON();
expect(tree).toMatchSnapshot();
});
});
});
But this doesn't work and after some digging I found a bug on github that was already resolved - apparently you need to use beforeEach . 但这是行不通的,经过一番挖掘后,我发现github上的一个错误已解决-显然您需要使用beforeEach 。 But that seems also not to be working or I am doing it wrong?
但这似乎也不起作用,或者我做错了吗?
The Snapshot test with the suggested solution of github : 带有github建议解决方案的Snapshot测试 :
import { Platform } from 'react-native';
describe('When loading the Slide component', () => {
it('should render correctly on ios', () => {
Platform.OS = 'ios';
const tree = renderer.create(
<Provider store={store}>
<Slide />
</Provider>,
).toJSON();
expect(tree).toMatchSnapshot();
});
describe('on devices with a pixelRatio of 2', () => {
it('it should render correctly', () => {
beforeEach(() => {
jest.mock(pxlr, () => ({
get: () => 2,
roundToNearestPixel: jest.fn(),
}));
const pxlr = require('react-native').PixelRatio;
}
const tree = renderer.create(
<Provider store={store}>
<Slide />
</Provider>,
).toJSON();
expect(tree).toMatchSnapshot();
});
});
});
When you write jest.mock('my-module', () => {...})
you tell jest
to mock module with the name of 'my-module'
. 当您编写
jest.mock('my-module', () => {...})
您告诉jest
模拟名为'my-module'
。 And then when you will write const MyModule = require('my-module')
you'll get a mock. 然后,当您编写
const MyModule = require('my-module')
您将得到一个模拟。
So statement jest.mock('PixelRatio', () => {...})
would make sense if PixelRatio
was a module, but it isn't. 因此
jest.mock('PixelRatio', () => {...})
如果PixelRatio
是模块,则声明jest.mock('PixelRatio', () => {...})
很有意义,但事实并非如此。 PixelRatio
is a global JS variable (JS class
to be precise). PixelRatio
是一个全局JS变量( PixelRatio
是JS class
)。 You can mock its static methods as follows: 您可以如下模拟其静态方法:
1) Using jest.spyOn
method: 1)使用
jest.spyOn
方法:
const mockGet = jest.spyOn(PixelRatio, 'get')
.mockImplementation(() => customImplementation)
const mockRoundToNearestPixel = jest.spyOn(PixelRatio, 'roundToNearestPixel')
.mockImplementation(() => customImplementation)
2) Using jest.fn
method: 2)使用
jest.fn
方法:
PixelRatio.get = jest.fn(() => customImplementation)
PixelRatio.roundToNearestPixel = jest.fn(() => customImplementation)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.