Skip to content
This repository was archived by the owner on Dec 3, 2022. It is now read-only.

Commit 3b9b4f5

Browse files
committed
not working jest setup
1 parent 0712009 commit 3b9b4f5

File tree

10 files changed

+225
-89
lines changed

10 files changed

+225
-89
lines changed

dist/Hooks.d.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
export declare function useNavigation(): {};
2-
export declare function useNavigationParam(paramName: any): any;
3-
export declare function useNavigationState(): any;
4-
export declare function useNavigationKey(): any;
5-
export declare function useNavigationEvents(handleEvt: any): void;
1+
import { NavigationScreenProp, NavigationRoute, NavigationParams, NavigationEventCallback } from 'react-navigation-types-only';
2+
export declare function useNavigation<S>(): NavigationScreenProp<S & NavigationRoute>;
3+
export declare function useNavigationParam<T extends keyof NavigationParams>(paramName: T): NavigationParams[T];
4+
export declare function useNavigationState(): (import("react-navigation-types-only").NavigationLeafRoute<NavigationParams> & {
5+
params?: NavigationParams | undefined;
6+
}) | (import("react-navigation-types-only").NavigationLeafRoute<NavigationParams> & import("react-navigation-types-only").NavigationState & {
7+
params?: NavigationParams | undefined;
8+
});
9+
export declare function useNavigationKey(): string;
10+
export declare function useNavigationEvents(handleEvt: NavigationEventCallback): void;
611
export declare function useFocusState(): {
712
isFocused: boolean;
813
isBlurring: boolean;

dist/Hooks.js

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
11
"use strict";
2-
var __assign = (this && this.__assign) || function () {
3-
__assign = Object.assign || function(t) {
4-
for (var s, i = 1, n = arguments.length; i < n; i++) {
5-
s = arguments[i];
6-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7-
t[p] = s[p];
8-
}
9-
return t;
10-
};
11-
return __assign.apply(this, arguments);
12-
};
132
Object.defineProperty(exports, "__esModule", { value: true });
3+
var tslib_1 = require("tslib");
144
var react_1 = require("react");
155
var core_1 = require("@react-navigation/core");
166
function useNavigation() {
@@ -58,13 +48,11 @@ var emptyFocusState = {
5848
isBlurred: false,
5949
isFocusing: false,
6050
};
61-
var didFocusState = __assign({}, emptyFocusState, { isFocused: true });
62-
var willBlurState = __assign({}, emptyFocusState, { isBlurring: true });
63-
var didBlurState = __assign({}, emptyFocusState, { isBlurred: true });
64-
var willFocusState = __assign({}, emptyFocusState, { isFocusing: true });
65-
var getInitialFocusState = function (isFocused) {
66-
return isFocused ? didFocusState : didBlurState;
67-
};
51+
var didFocusState = tslib_1.__assign({}, emptyFocusState, { isFocused: true });
52+
var willBlurState = tslib_1.__assign({}, emptyFocusState, { isBlurring: true });
53+
var didBlurState = tslib_1.__assign({}, emptyFocusState, { isBlurred: true });
54+
var willFocusState = tslib_1.__assign({}, emptyFocusState, { isFocusing: true });
55+
var getInitialFocusState = function (isFocused) { return isFocused ? didFocusState : didBlurState; };
6856
function focusStateOfEvent(eventName) {
6957
switch (eventName) {
7058
case 'didFocus':

package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,13 @@
3030
"url": "https://github.com/react-navigation/react-navigation-hooks/issues"
3131
},
3232
"homepage": "https://github.com/react-navigation/react-navigation-hooks#readme",
33-
"dependencies": {},
33+
"dependencies": {
34+
"@babel/core": "^7.1.6",
35+
"babel-jest": "^23.6.0"
36+
},
3437
"devDependencies": {
35-
"@react-navigation/core": "^3.0.0",
38+
"@react-navigation/core": "bySabi/react-navigation-core#wip",
39+
"@react-navigation/native": "^3.0.0",
3640
"@types/jest": "^23.3.9",
3741
"@types/react": "^16.7.7",
3842
"@types/react-native": "^0.57.14",
@@ -44,8 +48,8 @@
4448
"prettier": "^1.8.2",
4549
"pretty-quick": "^1.8.0",
4650
"react": "^16.7.0-alpha.2",
47-
"react-dom": "^16.7.0-alpha.2",
4851
"react-native": "^0.57.7",
52+
"react-navigation-types-only": "bySabi/react-navigation#patch-1",
4953
"react-test-renderer": "^16.7.0-alpha.2",
5054
"release-it": "^7.6.1",
5155
"ts-jest": "^23.10.5",
@@ -57,8 +61,9 @@
5761
},
5862
"peerDependencies": {
5963
"@react-navigation/core": "^3.0.0",
64+
"@react-navigation/native": "^3.0.0",
6065
"react": "^16.7.0-alpha.2",
61-
"react-dom": "^16.7.0-alpha.2"
66+
"react-native": "*"
6267
},
6368
"lint-staged": {
6469
"{src}/**/*.ts*": [

src/Hooks.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
import { useState, useContext, useEffect } from 'react';
22
import { NavigationContext } from '@react-navigation/core';
3+
// TODO: Remove "react-navigation-types-only" when https://github.com/react-navigation/react-navigation/pull/5276
4+
// get merged
5+
import {
6+
NavigationScreenProp,
7+
NavigationRoute,
8+
NavigationParams,
9+
NavigationEventCallback,
10+
NavigationEventPayload,
11+
EventType,
12+
} from 'react-navigation-types-only';
313

4-
export function useNavigation() {
5-
return useContext(NavigationContext);
14+
export function useNavigation<S>(): NavigationScreenProp<S & NavigationRoute> {
15+
return useContext(NavigationContext as any);
616
}
717

8-
export function useNavigationParam(paramName) {
18+
export function useNavigationParam<T extends keyof NavigationParams>(
19+
paramName: T,
20+
) {
921
return useNavigation().getParam(paramName);
1022
}
1123

@@ -17,7 +29,7 @@ export function useNavigationKey() {
1729
return useNavigation().state.key;
1830
}
1931

20-
export function useNavigationEvents(handleEvt) {
32+
export function useNavigationEvents(handleEvt: NavigationEventCallback) {
2133
const navigation = useNavigation();
2234
useEffect(
2335
() => {
@@ -53,9 +65,8 @@ const didFocusState = { ...emptyFocusState, isFocused: true };
5365
const willBlurState = { ...emptyFocusState, isBlurring: true };
5466
const didBlurState = { ...emptyFocusState, isBlurred: true };
5567
const willFocusState = { ...emptyFocusState, isFocusing: true };
56-
const getInitialFocusState = isFocused =>
57-
isFocused ? didFocusState : didBlurState;
58-
function focusStateOfEvent(eventName) {
68+
const getInitialFocusState = (isFocused: boolean) => isFocused ? didFocusState : didBlurState;
69+
function focusStateOfEvent(eventName: EventType) {
5970
switch (eventName) {
6071
case 'didFocus':
6172
return didFocusState;
@@ -74,7 +85,7 @@ export function useFocusState() {
7485
const navigation = useNavigation();
7586
const isFocused = navigation.isFocused();
7687
const [focusState, setFocusState] = useState(getInitialFocusState(isFocused));
77-
function handleEvt(e) {
88+
function handleEvt(e: NavigationEventPayload) {
7889
const newState = focusStateOfEvent(e.type);
7990
newState && setFocusState(newState);
8091
}

src/__tests__/Hooks-test.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/__tests__/Hooks-test.tsx

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import * as React from 'react';
2+
import { View, Button } from 'react-native';
3+
import * as renderer from 'react-test-renderer';
4+
// TODO: Remove "react-navigation-types-only" when https://github.com/react-navigation/react-navigation/pull/5276
5+
// get merged
6+
// import { NavigationScreenProp } from "react-navigation-types-only";
7+
import { createStackNavigator } from '@react-navigation/core';
8+
import { createAppContainer } from '@react-navigation/native';
9+
import {
10+
useNavigation
11+
} from '../../';
12+
13+
const HomeScreen = () => {
14+
const { navigate } = useNavigation();
15+
return <Button title="Go to Details" onPress={() => navigate('Details')} />;
16+
}
17+
18+
const DetailsScreen = () => <View />;
19+
20+
const AppNavigator = createStackNavigator(
21+
{
22+
Home: HomeScreen,
23+
Details: DetailsScreen,
24+
},
25+
{
26+
initialRouteName: 'Home',
27+
},
28+
);
29+
30+
const App = createAppContainer(AppNavigator);
31+
32+
describe('Navigating to a new screen', () => {
33+
it('renders successfully', () => {
34+
const rendered = renderer.create(<App />);
35+
let tree = rendered.toJSON();
36+
expect(tree).toMatchSnapshot();
37+
38+
// Manually trigger onPress
39+
tree!.props.onPress();
40+
tree = rendered.toJSON();
41+
42+
expect(rendered).toMatchSnapshot();
43+
});
44+
});

tsconfig-build.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
{
22
"extends": "./tsconfig",
3+
"compilerOptions": {
4+
"sourceMap": false
5+
},
36
"exclude": ["**/__tests__", "dist", ".jest", "node_modules"]
47
}

tsconfig.json

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,26 @@
22
"compilerOptions": {
33
"target": "es5",
44
"module": "commonjs",
5-
"lib": ["es2017", "es7", "es6", "dom"],
5+
"lib": ["es6"],
6+
"jsx": "react-native",
7+
"sourceMap": true,
8+
"moduleResolution": "node",
9+
"rootDir": "src",
10+
"forceConsistentCasingInFileNames": true,
11+
"noImplicitReturns": true,
12+
"noImplicitThis": true,
13+
"noImplicitAny": false,
14+
"strictNullChecks": true,
15+
"suppressImplicitAnyIndexErrors": true,
16+
"noUnusedLocals": true,
617
"declaration": true,
18+
"pretty": true,
19+
"noEmitHelpers": true,
20+
"importHelpers": true,
21+
"strict": false,
722
"outDir": "dist",
8-
"strict": true,
23+
"allowSyntheticDefaultImports": true,
924
"esModuleInterop": true
1025
},
11-
"include": ["src"],
12-
"exclude": [
13-
"dist",
14-
".jest",
15-
"node_modules"
16-
]
26+
"exclude": ["dist", ".jest", "node_modules"]
1727
}

tslint.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"object-literal-sort-keys": false,
1414
"arrow-parens": false,
1515
"no-console": [false],
16-
"no-unused-expression": true,
16+
"no-unused-expression": [true, "allow-fast-null-checks"],
1717
"comment-format": false,
1818
"semicolon": [false, "never", "ignore-interfaces"],
1919
"trailing-comma": [

0 commit comments

Comments
 (0)