我正在尝试测试组件的样式属性,该样式属性会根据称为isIphoneX
的函数的返回而变化。因此,我编写了2个测试用例,在其中对每个函数的返回值的模拟方式不同。因此,我希望组件每次都具有不同的样式道具。
第一个测试通过(因为isIphoneX被评估为false),但是第二个测试则保持80,而不是高度110。
请注意,我已经尝试使用:
jest.clearAllMocks(),jest.resetAllMocks(),jest.resetModules(),位于beforeEach / afterEach的内部,外部和内部。
我认为问题与在模拟isIphoneX之前运行的CameraOverlay组件有关。
// CameraOverlay.js
import { isIphoneX } from 'utils/device';
const styles = StyleSheet.create({
capturePanel: {
height: isIphoneX() ? 110 : 80,
},
});
const CameraOverlay = () => (
<View style={styles.capturePanel} testID="capturePanel">
Some dummy text
</View>
);
export default CameraOverlay;
// CameraOverlay.test.js
import { shallow } from 'enzyme';
import CameraOverlay from 'components/CameraOverlay';
import { isIphoneX } from 'utils/device';
jest.mock('utils/device');
describe('<View style={styles.capturePanel} testID="capturePanel">', () => {
let wrapper;
beforeEach(() => {
wrapper = shallow(<CameraOverlay {...defaultProps} />);
});
it('renders correct style height for standard devices', () => {
isIphoneX.mockReturnValue(false);
expect(
wrapper.find({ testID: 'capturePanel' }).prop('style').height,
).toEqual(80);
});
it('renders correct style height for iPhone X (and newer) devices', () => {
isIphoneX.mockReturnValue(true);
expect(
wrapper.find({ testID: 'capturePanel' }).prop('style').height,
).toEqual(110);
});
});