Skip to content
This repository has been archived by the owner on Mar 9, 2021. It is now read-only.

Move from AVA to Jest #30

Merged
merged 2 commits into from
May 9, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion boilerplate.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async function install (context) {
.succeed()

// attempt to install React Native or die trying
const rnInstall = await reactNative.install({ name, skipJest: true })
const rnInstall = await reactNative.install({ name })
if (rnInstall.exitCode > 0) process.exit(rnInstall.exitCode)

// remove the __tests__ directory that come with React Native
Expand Down
29 changes: 14 additions & 15 deletions boilerplate/Tests/Components/AlertMessageTest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import test from 'ava'
import React from 'react'
import { Text } from 'react-native'
import AlertMessage from '../../App/Components/AlertMessage'
Expand All @@ -7,29 +6,29 @@ import { shallow } from 'enzyme'
// Basic wrapper
const wrapper = shallow(<AlertMessage title='howdy' />)

test('component exists', (t) => {
t.is(wrapper.length, 1) // exists
test('component exists', () => {
expect(wrapper.length).toBe(1) // exists
})

test('component structure', (t) => {
t.is(wrapper.name(), 'View')
t.is(wrapper.children().length, 1) // has 1 child
t.is(wrapper.children().first().name(), 'View') // that child is View
test('component structure', () => {
expect(wrapper.name()).toBe('View')
expect(wrapper.children().length).toBe(1) // has 1 child
expect(wrapper.children().first().name()).toBe('View') // that child is View

const subview = wrapper.children().first()
t.is(subview.children().length, 1)
expect(subview.children().length).toBe(1)
})

test('Has text and set properly', (t) => {
t.is(wrapper.containsMatchingElement(<Text>HOWDY</Text>), true)
test('Has text and set properly', () => {
expect(wrapper.containsMatchingElement(<Text allowFontScaling={false}>HOWDY</Text>)).toBe(true)
})

test('style props are passed to top view', (t) => {
const withStyle = shallow(<AlertMessage title='howdy' style={{color: 'red'}} />)
t.is(withStyle.props().style[1].color, 'red')
test('style props are passed to top view', () => {
const withStyle = shallow(<AlertMessage title='howdy' style={{backgroundColor: 'red'}} />)
expect(withStyle.props().style[1].backgroundColor).toBe('red')
})

test('show false', (t) => {
test('show false', () => {
const hidden = shallow(<AlertMessage title='howdy' show={false} />)
t.is(hidden.children().length, 0)
expect(hidden.children().length).toBe(0)
})
21 changes: 10 additions & 11 deletions boilerplate/Tests/Components/DrawerButtonTest.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
// https://github.com/airbnb/enzyme/blob/master/docs/api/shallow.md
import test from 'ava'
import React from 'react'
import DrawerButton from '../../App/Components/DrawerButton'
import { shallow } from 'enzyme'

const wrapper = shallow(<DrawerButton onPress={() => {}} text='hi' />)

test('component exists', (t) => {
t.is(wrapper.length, 1) // exists
test('component exists', () => {
expect(wrapper.length).toBe(1) // exists
})

test('component structure', (t) => {
t.is(wrapper.name(), 'TouchableOpacity') // the right root component
t.is(wrapper.children().length, 1) // has 1 child
t.is(wrapper.children().first().name(), 'Text') // that child is Text
test('component structure', () => {
expect(wrapper.name()).toBe('TouchableOpacity') // the right root component
expect(wrapper.children().length).toBe(1) // has 1 child
expect(wrapper.children().first().name()).toBe('Text') // that child is Text
})

test('onPress', (t) => {
test('onPress', () => {
let i = 0
const onPress = () => i++
const wrapperPress = shallow(<DrawerButton onPress={onPress} text='hi' />)

t.is(wrapperPress.prop('onPress'), onPress) // uses the right handler
t.is(i, 0)
expect(wrapperPress.prop('onPress')).toBe(onPress) // uses the right handler
expect(i).toBe(0)
wrapperPress.simulate('press')
t.is(i, 1)
expect(i).toBe(1)
})
21 changes: 10 additions & 11 deletions boilerplate/Tests/Components/FullButtonTest.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
// https://github.com/airbnb/enzyme/blob/master/docs/api/shallow.md
import test from 'ava'
import React from 'react'
import FullButton from '../../App/Components/FullButton'
import { shallow } from 'enzyme'

// Basic wrapper
const wrapper = shallow(<FullButton onPress={() => {}} text='hi' />)

test('component exists', (t) => {
t.is(wrapper.length, 1) // exists
test('component exists', () => {
expect(wrapper.length).toBe(1) // exists
})

test('component structure', (t) => {
t.is(wrapper.name(), 'TouchableOpacity') // the right root component
t.is(wrapper.children().length, 1) // has 1 child
t.is(wrapper.children().first().name(), 'Text') // that child is Text
test('component structure', () => {
expect(wrapper.name()).toBe('TouchableOpacity') // the right root component
expect(wrapper.children().length).toBe(1) // has 1 child
expect(wrapper.children().first().name()).toBe('Text') // that child is Text
})

test('onPress', (t) => {
test('onPress', () => {
let i = 0 // i guess i could have used sinon here too... less is more i guess
const onPress = () => i++
const wrapperPress = shallow(<FullButton onPress={onPress} text='hi' />)

t.is(wrapperPress.prop('onPress'), onPress) // uses the right handler
t.is(i, 0)
expect(wrapperPress.prop('onPress')).toBe(onPress) // uses the right handler
expect(i).toBe(0)
wrapperPress.simulate('press')
t.is(i, 1)
expect(i).toBe(1)
})
31 changes: 15 additions & 16 deletions boilerplate/Tests/Components/RoundedButtonTest.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
// https://github.com/airbnb/enzyme/blob/master/docs/api/shallow.md
import test from 'ava'
import React from 'react'
import RoundedButton from '../../App/Components/RoundedButton'
import { shallow } from 'enzyme'

// Basic wrapper
const wrapper = shallow(<RoundedButton onPress={() => {}} text='howdy' />)

test('component exists', (t) => {
t.is(wrapper.length, 1) // exists
test('component exists', () => {
expect(wrapper.length).toBe(1) // exists
})

test('component structure', (t) => {
t.is(wrapper.name(), 'TouchableOpacity') // the right root component
t.is(wrapper.children().length, 1) // has 1 child
t.is(wrapper.children().first().name(), 'Text') // that child is Text
test('component structure', () => {
expect(wrapper.name()).toBe('TouchableOpacity') // the right root component
expect(wrapper.children().length).toBe(1) // has 1 child
expect(wrapper.children().first().name()).toBe('Text') // that child is Text
})

test('the text is set properly - uppercase', (t) => {
t.is(wrapper.children().first().props().children, 'HOWDY')
test('the text is set properly - uppercase', () => {
expect(wrapper.children().first().props().children).toBe('HOWDY')
})

test('onPress', (t) => {
test('onPress', () => {
let i = 0 // i guess i could have used sinon here too... less is more i guess
const onPress = () => i++
const wrapperPress = shallow(<RoundedButton onPress={onPress} text='hi' />)

t.is(wrapperPress.prop('onPress'), onPress) // uses the right handler
t.is(i, 0)
expect(wrapperPress.prop('onPress')).toBe(onPress) // uses the right handler
expect(i).toBe(0)
wrapperPress.simulate('press')
t.is(i, 1)
expect(i).toBe(1)
})

test('renders children text when passed', (t) => {
test('renders children text when passed', () => {
const wrapperChild = shallow(<RoundedButton onPress={() => {}}>Howdy</RoundedButton>)
t.is(wrapperChild.children().length, 1) // has 1 child
t.is(wrapperChild.children().first().name(), 'Text') // that child is Text
expect(wrapperChild.children().length).toBe(1) // has 1 child
expect(wrapperChild.children().first().name()).toBe('Text') // that child is Text
})
25 changes: 12 additions & 13 deletions boilerplate/Tests/Redux/GithubReduxTest.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import test from 'ava'
import Actions, { reducer, INITIAL_STATE } from '../../App/Redux/GithubRedux'

test('request', (t) => {
test('request', () => {
const username = 'taco'
const state = reducer(INITIAL_STATE, Actions.userRequest(username))

t.true(state.fetching)
t.is(state.username, username)
t.is(null, state.avatar)
expect(state.fetching).toBe(true)
expect(state.username).toBe(username)
expect(state.avatar).toBeNull()
})

test('success', (t) => {
test('success', () => {
const avatar = 'http://placekitten.com/200/300'
const state = reducer(INITIAL_STATE, Actions.userSuccess(avatar))

t.false(state.fetching)
t.is(state.avatar, avatar)
t.is(null, state.error)
expect(state.fetching).toBe(false)
expect(state.avatar).toBe(avatar)
expect(state.error).toBeNull()
})

test('failure', (t) => {
test('failure', () => {
const state = reducer(INITIAL_STATE, Actions.userFailure())

t.false(state.fetching)
t.true(state.error)
t.is(null, state.avatar)
expect(state.fetching).toBe(false)
expect(state.error).toBe(true)
expect(state.avatar).toBeNull()
})
19 changes: 9 additions & 10 deletions boilerplate/Tests/Redux/LoginReduxTest.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
import test from 'ava'
import Actions, { reducer, INITIAL_STATE } from '../../App/Redux/LoginRedux'

test('attempt', (t) => {
test('attempt', () => {
const state = reducer(INITIAL_STATE, Actions.loginRequest('u', 'p'))

t.true(state.fetching)
expect(state.fetching).toBe(true)
})

test('success', (t) => {
test('success', () => {
const state = reducer(INITIAL_STATE, Actions.loginSuccess('hi'))

t.is(state.username, 'hi')
expect(state.username).toBe('hi')
})

test('failure', (t) => {
test('failure', () => {
const state = reducer(INITIAL_STATE, Actions.loginFailure(69))

t.false(state.fetching)
t.is(state.error, 69)
expect(state.fetching).toBe(false)
expect(state.error).toBe(69)
})

test('logout', (t) => {
test('logout', () => {
const loginState = reducer(INITIAL_STATE, Actions.loginSuccess('hi'))
const state = reducer(loginState, Actions.logout())

t.falsy(state.username)
expect(state.username).toBeFalsy()
})
13 changes: 6 additions & 7 deletions boilerplate/Tests/Sagas/GithubSagaTest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import test from 'ava'
import FixtureAPI from '../../App/Services/FixtureApi'
import { put, call } from 'redux-saga/effects'
import { getUserAvatar } from '../../App/Sagas/GithubSagas'
Expand All @@ -7,13 +6,13 @@ import { path } from 'ramda'

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

test('first calls API', (t) => {
test('first calls API', () => {
const step = stepper(getUserAvatar(FixtureAPI, {username: 'taco'}))
// first yield is API
t.deepEqual(step(), call(FixtureAPI.getUser, 'taco'))
expect(step()).toEqual(call(FixtureAPI.getUser, 'taco'))
})

test('success path', (t) => {
test('success path', () => {
const response = FixtureAPI.getUser('taco')
const step = stepper(getUserAvatar(FixtureAPI, {username: 'taco'}))
// first step API
Expand All @@ -23,14 +22,14 @@ test('success path', (t) => {
// Get the calculated temperature value from the fixture-based response
const firstUser = path(['data', 'items'], response)[0]
const avatar = firstUser.avatar_url
t.deepEqual(stepResponse, put(GithubActions.userSuccess(avatar)))
expect(stepResponse).toEqual(put(GithubActions.userSuccess(avatar)))
})

test('failure path', (t) => {
test('failure path', () => {
const response = {ok: false}
const step = stepper(getUserAvatar(FixtureAPI, {username: 'taco'}))
// first step API
step()
// Second step failed response
t.deepEqual(step(response), put(GithubActions.userFailure()))
expect(step(response)).toEqual(put(GithubActions.userFailure()))
})
9 changes: 4 additions & 5 deletions boilerplate/Tests/Sagas/LoginSagaTest.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import test from 'ava'
import { put } from 'redux-saga/effects'
import { login } from '../../App/Sagas/LoginSagas'
import LoginActions from '../../App/Redux/LoginRedux'

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

test('success', (t) => {
test('success', () => {
const mock = { username: 'a', password: 'b' }
const step = stepper(login(mock))

t.deepEqual(step(), put(LoginActions.loginSuccess(mock.username)))
expect(step()).toEqual(put(LoginActions.loginSuccess(mock.username)))
})

test('failure', (t) => {
test('failure', () => {
const mock = { username: '', password: '' }
const step = stepper(login(mock))

t.deepEqual(step(), put(LoginActions.loginFailure('WRONG')))
expect(step()).toEqual(put(LoginActions.loginFailure('WRONG')))
})
7 changes: 3 additions & 4 deletions boilerplate/Tests/Sagas/StartupSagaTest.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import test from 'ava'
import { select, put } from 'redux-saga/effects'
import { selectAvatar, startup } from '../../App/Sagas/StartupSagas'
import GithubActions from '../../App/Redux/GithubRedux'

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

test('watches for the right action', (t) => {
test('watches for the right action', () => {
const step = stepper(startup())
GithubActions.userRequest('GantMan')
t.deepEqual(step(), select(selectAvatar))
t.deepEqual(step(), put(GithubActions.userRequest('GantMan')))
expect(step()).toEqual(select(selectAvatar))
expect(step()).toEqual(put(GithubActions.userRequest('GantMan')))
})
5 changes: 2 additions & 3 deletions boilerplate/Tests/Services/FixtureAPITest.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import test from 'ava'
import API from '../../App/Services/Api'
import FixtureAPI from '../../App/Services/FixtureApi'
import R from 'ramda'

test('All fixtures map to actual API', (t) => {
test('All fixtures map to actual API', () => {
const fixtureKeys = R.keys(FixtureAPI).sort()
const apiKeys = R.keys(API.create())

const intersection = R.intersection(fixtureKeys, apiKeys).sort()

// There is no difference between the intersection and all fixtures
t.true(R.equals(fixtureKeys, intersection))
expect(R.equals(fixtureKeys, intersection)).toBe(true)
})
Loading