Skip to content
This repository was archived by the owner on Jul 30, 2020. It is now read-only.

Commit c91ee14

Browse files
author
Brandon Carroll
committed
working state
1 parent 0836360 commit c91ee14

File tree

9 files changed

+161
-0
lines changed

9 files changed

+161
-0
lines changed

src/preset/core-components.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const CoreComponents = [
2+
'ActivityIndicator',
3+
'FAKE',
4+
'Button',
5+
'DrawerLayoutAndroid',
6+
'Image',
7+
'Modal',
8+
'Picker',
9+
'RefreshControl',
10+
'ScrollView',
11+
'Switch',
12+
'Text',
13+
'TextInput',
14+
'TouchableHighlight',
15+
'TouchableNativeFeedback',
16+
'TouchableOpacity',
17+
'TouchableWithoutFeedback',
18+
'View',
19+
];
20+
21+
function setCoreComponents(components) {
22+
return [...CoreComponents, ...components];
23+
}
24+
25+
function getCoreComponents() {
26+
return CoreComponents;
27+
}
28+
29+
export { getCoreComponents, setCoreComponents };

src/preset/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { mockComponent } from './mock-component';
2+
import { getCoreComponents, setCoreComponents } from './core-components';
3+
4+
module.exports = {
5+
CoreComponents: getCoreComponents(),
6+
dangerouslyMockComponent: ({ __mock }) => mockComponent({ name: __mock.name, path: __mock.path }),
7+
dangerouslySetCoreComponents: ({ __components }) => setCoreComponents(__components),
8+
};

src/preset/mock-component.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'use strict';
2+
3+
import React from 'react';
4+
5+
import { getCoreComponents } from './core-components';
6+
7+
function mockComponent(component, path = component) {
8+
const RealComponent = jest.requireActual(path);
9+
const { mockNativeMethods } = jest.requireActual('./mock-native-methods');
10+
11+
const displayName =
12+
RealComponent.displayName ||
13+
RealComponent.name ||
14+
(RealComponent.render // handle React.forwardRef
15+
? RealComponent.render.displayName || RealComponent.render.name
16+
: 'Unknown');
17+
18+
const SuperClass = typeof RealComponent === 'function' ? RealComponent : React.Component;
19+
20+
class Component extends SuperClass {
21+
static displayName = displayName;
22+
23+
render() {
24+
const props = Object.assign({}, RealComponent.defaultProps);
25+
26+
if (this.props) {
27+
Object.keys(this.props).forEach(prop => {
28+
// We can't just assign props on top of defaultProps
29+
// because React treats undefined as special and different from null.
30+
// If a prop is specified but set to undefined it is ignored and the
31+
// default prop is used instead. If it is set to null, then the
32+
// null value overwrites the default value.
33+
if (this.props[prop] !== undefined) {
34+
props[prop] = this.props[prop];
35+
}
36+
});
37+
}
38+
39+
return React.createElement(displayName, props, this.props.children);
40+
}
41+
}
42+
43+
Object.keys(RealComponent).forEach(classStatic => {
44+
Component[classStatic] = RealComponent[classStatic];
45+
});
46+
47+
Object.assign(Component.prototype, mockNativeMethods);
48+
49+
return Component;
50+
}
51+
52+
getCoreComponents().forEach(component => {
53+
try {
54+
jest.doMock(component, () => mockComponent(component));
55+
} catch (e) {
56+
//
57+
}
58+
});
59+
60+
export { mockComponent };

src/preset/mock-native-methods.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
const mockNativeMethods = {
4+
measure: jest.fn(),
5+
measureInWindow: jest.fn(),
6+
measureLayout: jest.fn(),
7+
setNativeProps: jest.fn(),
8+
focus: jest.fn(),
9+
blur: jest.fn(),
10+
};
11+
12+
export { mockNativeMethods };

src/preset/native-modules.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const NativeModules = require('NativeModules');
2+
3+
jest.doMock('NativeModules', () => ({
4+
...NativeModules,
5+
NativeAnimatedModule: {
6+
addAnimatedEventToView: jest.fn(),
7+
createAnimatedNode: jest.fn(),
8+
connectAnimatedNodes: jest.fn(),
9+
connectAnimatedNodeToView: jest.fn(),
10+
},
11+
}));

src/preset/new-mock-component.js

Whitespace-only changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
jest.doMock('requireNativeComponent', () => {
2+
const React = require('react');
3+
4+
return componentName =>
5+
class extends React.Component {
6+
static displayName = componentName;
7+
8+
static _nativeTag = 1;
9+
10+
static viewConfig = { validAttributes: { style: {} } };
11+
12+
render() {
13+
const { children, ...props } = this.props;
14+
15+
return React.createElement(componentName, props, children);
16+
}
17+
};
18+
});

src/preset/setup.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Un-mock the things that we'll be mocking
2+
require('./unmock');
3+
4+
// Mock native modules
5+
require('./native-modules');
6+
7+
// Mock requireNativeComponent
8+
require('./require-native-component');
9+
10+
// Mock lean core components
11+
require('./mock-component');

src/preset/unmock.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { getCoreComponents } from './core-components';
2+
3+
getCoreComponents().forEach(component => {
4+
try {
5+
// try to un-mock the component
6+
jest.unmock(component);
7+
} catch (e) {
8+
// do nothing if we can't
9+
}
10+
});
11+
12+
jest.dontMock('requireNativeComponent');

0 commit comments

Comments
 (0)