Skip to content

Commit

Permalink
Add OPEN_SCREEN action for Reactotron dispatches
Browse files Browse the repository at this point in the history
  • Loading branch information
derekgreenberg committed Dec 2, 2016
1 parent 129c455 commit 2ea4865
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
10 changes: 10 additions & 0 deletions ignite-base/App/Redux/OpenScreenRedux.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { createActions } from 'reduxsauce'

/* ------------- Types and Action Creators ------------- */

const { Types, Creators } = createActions({
openScreen: ['screen', 'options']
})

export const OpenScreenTypes = Types
export default Creators
12 changes: 12 additions & 0 deletions ignite-base/App/Sagas/OpenScreenSagas.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { call } from 'redux-saga/effects'
import { Actions as NavigationActions, ActionConst } from 'react-native-router-flux'
import { merge } from 'ramda'

// Process OPEN_SCREEN actions, intended for use with Reactotron to open a screen via a direct dispatch
export function * openScreen (action) {
const {screen, options} = action
// Always reset the nav stack when opening a screen via Reactotron
// You can override the RESET type in the options passed to the OPEN_SCREEN dispatch
const mergedOptions = merge({type: ActionConst.RESET}, options)
yield call(NavigationActions[screen], mergedOptions)
}
3 changes: 3 additions & 0 deletions ignite-base/App/Sagas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import DebugSettings from '../Config/DebugSettings'
import { StartupTypes } from '../Redux/StartupRedux'
import { TemperatureTypes } from '../Redux/TemperatureRedux'
import { LoginTypes } from '../Redux/LoginRedux'
import { OpenScreenTypes } from '../Redux/OpenScreenRedux'

/* ------------- Sagas ------------- */

import { startup } from './StartupSagas'
import { login } from './LoginSagas'
import { getTemperature } from './TemperatureSagas'
import { openScreen } from './OpenScreenSagas'

/* ------------- API ------------- */

Expand All @@ -28,6 +30,7 @@ export default function * root () {
// some sagas only receive an action
takeLatest(StartupTypes.STARTUP, startup),
takeLatest(LoginTypes.LOGIN_REQUEST, login),
takeLatest(OpenScreenTypes.OPEN_SCREEN, openScreen),

// some sagas receive extra parameters in addition to an action
takeLatest(TemperatureTypes.TEMPERATURE_REQUEST, getTemperature, api)
Expand Down
21 changes: 21 additions & 0 deletions ignite-base/Tests/Sagas/OpenScreenSagaTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import test from 'ava'
import { call } from 'redux-saga/effects'
import { openScreen } from '../../App/Sagas/OpenScreenSagas'
import { Actions as NavigationActions, ActionConst } from 'react-native-router-flux'

const stepper = (fn) => (mock) => fn.next(mock).value

// Using 'myScreen' in the mocks of the arguments passed to the action for these tests is important.
// The react-native-router-flux component is mocked in App/Tests/Setup.js to use 'myScreen'

test('the right default options are passed to the navigation action', (t) => {
const mock = {screen: 'myScreen'}
const step = stepper(openScreen(mock))
t.deepEqual(step(), call(NavigationActions['myScreen'], {type: ActionConst.RESET}))
})

test('the right merged options are passed to the navigation action', (t) => {
const mock = {screen: 'myScreen', options: {type: 'replace', foo: 'bar'}}
const step = stepper(openScreen(mock))
t.deepEqual(step(), call(NavigationActions['myScreen'], {type: 'replace', foo: 'bar'}))
})
1 change: 1 addition & 0 deletions ignite-base/Tests/Setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mockery.registerMock('reactotron-redux', {})
mockery.registerMock('reactotron-apisauce', {})
mockery.registerMock('react-native-animatable', {View: 'Animatable.View'})
mockery.registerMock('react-native-vector-icons/Ionicons', {})
mockery.registerMock('react-native-router-flux', {Actions: {'myScreen': () => {}}, ActionConst: {RESET: 'reset'}})

// Mock i18n as it uses react native stuff
// This mock returns the interpolated text from the english.json file in App/I18n
Expand Down

0 comments on commit 2ea4865

Please sign in to comment.