Skip to content

Commit

Permalink
chore: fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Nov 27, 2022
1 parent cf6687b commit 32ba253
Show file tree
Hide file tree
Showing 24 changed files with 359 additions and 139 deletions.
5 changes: 3 additions & 2 deletions example/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ export default function App() {
const [theme, setTheme] = React.useState(DefaultTheme);

const [isReady, setIsReady] = React.useState(Platform.OS === 'web');
const [initialState, setInitialState] =
React.useState<InitialState | undefined>();
const [initialState, setInitialState] = React.useState<
InitialState | undefined
>();

React.useEffect(() => {
const restoreState = async () => {
Expand Down
218 changes: 212 additions & 6 deletions jest/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,223 @@
import 'react-native-gesture-handler/jestSetup';

jest.mock('react-native-reanimated', () => {
const Reanimated = require('react-native-reanimated/mock');
const React = require('react');
const {
View,
Text,
Image,
Animated,
Platform,
processColor,
} = require('react-native');

// The mock for `call` immediately calls the callback which is incorrect
// So we override it with a no-op
Reanimated.default.call = () => {};
function NOOP() {
// noop
}

return Reanimated;
function simulateCallbackFactory(...params) {
return (callback) => {
callback &&
setTimeout(() => {
// user defined callback
callback(...params);
}, 0);
};
}

class Code extends React.Component {
render() {
return null;
}
}

const getValue = (node) => {
if (typeof node === 'number') {
return node;
}
return (node && node[' __value']) || 0;
};

class AnimatedValue {
constructor(val) {
this[' __value'] = val;
}

setValue(val) {
this[' __value'] = val;
}

interpolate() {
return this;
}
}

const Reanimated = {
SpringUtils: {
makeDefaultConfig: NOOP,
makeConfigFromBouncinessAndSpeed: NOOP,
makeConfigFromOrigamiTensionAndFriction: NOOP,
},

View,
Text,
Image,
ScrollView: Animated.ScrollView,
Code,

Clock: NOOP,
Node: NOOP,
Value: AnimatedValue,

EasingNode: {
linear: NOOP,
ease: NOOP,
quad: NOOP,
cubic: NOOP,
poly: () => NOOP,
sin: NOOP,
circle: NOOP,
exp: NOOP,
elastic: () => NOOP,
back: () => NOOP,
bounce: () => NOOP,
bezier: () => NOOP,
in: () => NOOP,
out: () => NOOP,
inOut: () => NOOP,
},

Extrapolate: {
EXTEND: 'extend',
CLAMP: 'clamp',
IDENTITY: 'identity',
},

processColor,

add: (...vals) =>
new AnimatedValue(
vals.map((v) => getValue(v)).reduce((acc, v) => acc + v)
),
sub: (...vals) =>
new AnimatedValue(
vals.map((v) => getValue(v)).reduce((acc, v) => acc - v)
),
divide: (...vals) =>
new AnimatedValue(
vals.map((v) => getValue(v)).reduce((acc, v) => acc / v)
),
multiply: (...vals) =>
new AnimatedValue(
vals.map((v) => getValue(v)).reduce((acc, v) => acc * v)
),
pow: (...vals) =>
new AnimatedValue(
vals.map((v) => getValue(v)).reduce((acc, v) => acc ** v)
),
modulo: (a, b) => new AnimatedValue(getValue(a) % getValue(b)),
sqrt: (a) => new AnimatedValue(Math.sqrt(getValue(a))),
log: (a) => new AnimatedValue(Math.log(getValue(a))),
sin: (a) => new AnimatedValue(Math.sin(getValue(a))),
cos: (a) => new AnimatedValue(Math.cos(getValue(a))),
tan: (a) => new AnimatedValue(Math.tan(getValue(a))),
acos: (a) => new AnimatedValue(Math.acos(getValue(a))),
asin: (a) => new AnimatedValue(Math.asin(getValue(a))),
atan: (a) => new AnimatedValue(Math.atan(getValue(a))),
exp: (a) => new AnimatedValue(Math.exp(getValue(a))),
round: (a) => new AnimatedValue(Math.round(getValue(a))),
floor: (a) => new AnimatedValue(Math.floor(getValue(a))),
ceil: (a) => new AnimatedValue(Math.ceil(getValue(a))),
lessThan: (a, b) => new AnimatedValue(getValue(a) < getValue(b)),
eq: (a, b) => new AnimatedValue(getValue(a) === getValue(b)),
greaterThan: (a, b) => new AnimatedValue(getValue(a) > getValue(b)),
lessOrEq: (a, b) => new AnimatedValue(getValue(a) <= getValue(b)),
greaterOrEq: (a, b) => new AnimatedValue(getValue(a) >= getValue(b)),
neq: (a, b) => new AnimatedValue(getValue(a) !== getValue(b)),
and: (a, b) => new AnimatedValue(getValue(a) && getValue(b)),
or: (a, b) => new AnimatedValue(getValue(a) || getValue(b)),
defined: (a) =>
new AnimatedValue(getValue(a) !== null && getValue(a) !== undefined),
not: (a) => new AnimatedValue(!getValue(a)),
set: (a, b) => {
a.setValue(getValue(b));
return a;
},
concat: (a, b) => `${a}${b}`,
cond: (a, b, c) => {
if (getValue(a)) {
return b;
} else {
return c;
}
},
block: (a) => a[a.length - 1],
call: (a, b) => () => b(a.map(getValue)),
debug: NOOP,
onChange: NOOP,
startClock: NOOP,
stopClock: NOOP,
clockRunning: NOOP,
event: NOOP,
abs: (a) => Math.abs(getValue(a)),
acc: NOOP,
color: (r, g, b, a = 1) => {
const color =
16777216 * Math.round(getValue(a) * 255) +
65536 * getValue(r) +
256 * getValue(g) +
getValue(b);
if (Platform.OS === 'android') {
// on Android color is represented as signed 32 bit int
if (color < (1 << 31) >>> 0) {
return new AnimatedValue(color);
}
return new AnimatedValue(color - 2 ** 32);
}
return new AnimatedValue(color);
},
diff: NOOP,
diffClamp: NOOP,
interpolateNode: NOOP,
interpolateColors: NOOP,
max: (a, b) => Math.max(getValue(a), getValue(b)),
min: (a, b) => Math.min(getValue(a), getValue(b)),

decay: () => ({
start: simulateCallbackFactory({ finished: true }),
stop: simulateCallbackFactory({ finished: true }),
}),
timing: () => ({
start: simulateCallbackFactory({ finished: true }),
stop: simulateCallbackFactory({ finished: true }),
}),
spring: () => ({
start: simulateCallbackFactory({ finished: true }),
stop: simulateCallbackFactory({ finished: true }),
}),

proc: (cb) => cb,

useCode: NOOP,
useValue: (a) => new AnimatedValue(a),
createAnimatedComponent: (Component) => Component,
addWhitelistedUIProps: NOOP,
addWhitelistedNativeProps: NOOP,
};

return {
__esModule: true,

...Reanimated,

default: {
...Reanimated,
},
};
});

// Silence the warning: Animated: `useNativeDriver` is not supported because the native animated module is missing
jest.mock('react-native/Libraries/Animated/src/NativeAnimatedHelper');
jest.mock('react-native/Libraries/Animated/NativeAnimatedHelper');

const error = console.error;

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@
"setupFiles": [
"<rootDir>/jest/setup.js"
],
"transformIgnorePatterns": [
"node_modules/(?!(@react-native|react-native|react-native-iphone-x-helper)/)"
],
"moduleNameMapper": {
"@react-navigation/([^/]+)": "<rootDir>/packages/$1/src"
},
Expand Down Expand Up @@ -102,7 +105,5 @@
}
}
}
},
"name": "react-navigation",
"version": "1.0.0"
}
}
5 changes: 3 additions & 2 deletions packages/core/src/CurrentRenderContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import * as React from 'react';
* Context which holds the values for the current navigation tree.
* Intended for use in SSR. This is not safe to use on the client.
*/
const CurrentRenderContext =
React.createContext<{ options?: object } | undefined>(undefined);
const CurrentRenderContext = React.createContext<
{ options?: object } | undefined
>(undefined);

export default CurrentRenderContext;
15 changes: 7 additions & 8 deletions packages/core/src/EnsureSingleNavigator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ type Props = {

const MULTIPLE_NAVIGATOR_ERROR = `Another navigator is already registered for this container. You likely have multiple navigators under a single "NavigationContainer" or "Screen". Make sure each navigator is under a separate "Screen" container. See https://reactnavigation.org/docs/nesting-navigators for a guide on nesting.`;

export const SingleNavigatorContext =
React.createContext<
| {
register(key: string): void;
unregister(key: string): void;
}
| undefined
>(undefined);
export const SingleNavigatorContext = React.createContext<
| {
register(key: string): void;
unregister(key: string): void;
}
| undefined
>(undefined);

/**
* Component which ensures that there's only one navigator nested under it.
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/NavigationContainerRefContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import type { NavigationContainerRef } from './types';
/**
* Context which holds the route prop for a screen.
*/
const NavigationContainerRefContext =
React.createContext<NavigationContainerRef<ParamListBase> | undefined>(
undefined
);
const NavigationContainerRefContext = React.createContext<
NavigationContainerRef<ParamListBase> | undefined
>(undefined);

export default NavigationContainerRefContext;
5 changes: 3 additions & 2 deletions packages/core/src/NavigationContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import type { NavigationProp } from './types';
/**
* Context which holds the navigation prop for a screen.
*/
const NavigationContext =
React.createContext<NavigationProp<ParamListBase> | undefined>(undefined);
const NavigationContext = React.createContext<
NavigationProp<ParamListBase> | undefined
>(undefined);

export default NavigationContext;
5 changes: 3 additions & 2 deletions packages/core/src/NavigationHelpersContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import type { NavigationHelpers } from './types';
* Context which holds the navigation helpers of the parent navigator.
* Navigators should use this context in their view component.
*/
const NavigationHelpersContext =
React.createContext<NavigationHelpers<ParamListBase> | undefined>(undefined);
const NavigationHelpersContext = React.createContext<
NavigationHelpers<ParamListBase> | undefined
>(undefined);

export default NavigationHelpersContext;
5 changes: 3 additions & 2 deletions packages/core/src/NavigationRouteContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import * as React from 'react';
/**
* Context which holds the route prop for a screen.
*/
const NavigationRouteContext =
React.createContext<Route<string> | undefined>(undefined);
const NavigationRouteContext = React.createContext<Route<string> | undefined>(
undefined
);

export default NavigationRouteContext;
7 changes: 3 additions & 4 deletions packages/core/src/UnhandledActionContext.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import type { NavigationAction } from '@react-navigation/routers';
import * as React from 'react';

const UnhandledActionContext =
React.createContext<((action: NavigationAction) => void) | undefined>(
undefined
);
const UnhandledActionContext = React.createContext<
((action: NavigationAction) => void) | undefined
>(undefined);

export default UnhandledActionContext;
Loading

0 comments on commit 32ba253

Please sign in to comment.