chore: add tests for tabs

This commit is contained in:
Michal Osadnik
2019-08-26 13:07:12 +01:00
parent dee25057e8
commit 2adccdef1d
2 changed files with 151 additions and 1 deletions

View File

@@ -14,7 +14,7 @@
"type": "git",
"url": "git+https://github.com/satya164/react-navigation.git"
},
"author": "Satyajit Sahoo <satyajit.happy@gmail.com> (https://github.com/satya164/)",
"author": "Satyajit Sahoo <satyajit.happy@gmail.com> (https://github.com/satya164/), Michał Osadnik <micosa97@gmail.com> (https://github.com/osdnk/)",
"scripts": {
"lint": "eslint --ext '.js,.ts,.tsx' .",
"typescript": "tsc --noEmit",

View File

@@ -0,0 +1,150 @@
import { BaseActions } from '@react-navigation/core';
import { TabRouter } from '../src';
jest.mock('shortid', () => () => 'test');
it('handles navigate action', () => {
const router = TabRouter({});
expect(
router.getStateForAction(
{
stale: false,
key: 'root',
index: 1,
routeNames: ['baz', 'bar'],
routeKeyHistory: [],
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
},
BaseActions.navigate('baz', { answer: 42 })
)
).toEqual({
stale: false,
key: 'root',
index: 0,
routeNames: ['baz', 'bar'],
routeKeyHistory: ['bar'],
routes: [
{ key: 'baz', name: 'baz', params: { answer: 42 } },
{ key: 'bar', name: 'bar' },
],
});
});
it('handles jump to action', () => {
const router = TabRouter({});
expect(
router.getStateForAction(
{
stale: false,
key: 'root',
index: 0,
routeNames: ['baz', 'bar'],
routeKeyHistory: [],
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
},
{ type: 'JUMP_TO', payload: { name: 'bar' } }
)
).toEqual({
stale: false,
key: 'root',
index: 1,
routeNames: ['baz', 'bar'],
routeKeyHistory: ['baz'],
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
});
});
it('handles history back action', () => {
const router = TabRouter({});
expect(
router.getStateForAction(
{
stale: false,
key: 'root',
index: 0,
routeNames: ['baz', 'bar'],
routeKeyHistory: ['bar'],
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
},
{ type: 'GO_BACK' }
)
).toEqual({
stale: false,
key: 'root',
index: 1,
routeNames: ['baz', 'bar'],
routeKeyHistory: [],
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
});
});
it('handles order back action', () => {
const router = TabRouter({ backBehavior: 'order' });
expect(
router.getStateForAction(
{
stale: false,
key: 'root',
index: 1,
routeNames: ['baz', 'bar'],
routeKeyHistory: [],
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
},
{ type: 'GO_BACK' }
)
).toEqual({
stale: false,
key: 'root',
index: 0,
routeNames: ['baz', 'bar'],
routeKeyHistory: [],
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
});
});
it('handles order back action', () => {
const router = TabRouter({ backBehavior: 'initialRoute' });
expect(
router.getStateForAction(
{
stale: false,
key: 'root',
index: 1,
routeNames: ['baz', 'bar'],
routeKeyHistory: [],
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
},
{ type: 'GO_BACK' }
)
).toEqual({
stale: false,
key: 'root',
index: 0,
routeNames: ['baz', 'bar'],
routeKeyHistory: [],
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
});
});
it('handles none back action', () => {
const router = TabRouter({ backBehavior: 'none' });
expect(
router.getStateForAction(
{
stale: false,
key: 'root',
index: 0,
routeNames: ['baz', 'bar'],
routeKeyHistory: [],
routes: [{ key: 'baz', name: 'baz' }, { key: 'bar', name: 'bar' }],
},
{ type: 'GO_BACK' }
)
).toEqual(null);
});