Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
omer88 committed Aug 15, 2017
1 parent b173dd8 commit cfd9a02
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 10 deletions.
66 changes: 57 additions & 9 deletions __tests__/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('firebase actions', () => {
).toEqual(expectedAction)
})

test('FIREBASE_UPDATE_REQUESTED', () => {
test(types.firebase.FIREBASE_UPDATE_REQUESTED, () => {
const uid = '1'
const expectedAction = {
type: types.firebase.FIREBASE_UPDATE_REQUESTED,
Expand All @@ -98,7 +98,7 @@ describe('firebase actions', () => {
).toEqual(expectedAction)
})

test('FIREBASE_UPDATE_FULFILLED', () => {
test(types.firebase.FIREBASE_UPDATE_FULFILLED, () => {
const error = 'error'
const expectedAction = {
type: types.firebase.FIREBASE_UPDATE_FULFILLED,
Expand All @@ -111,7 +111,7 @@ describe('firebase actions', () => {
)
})

test('FIREBASE_UPDATE_REJECTED', () => {
test(types.firebase.FIREBASE_UPDATE_REJECTED, () => {
const error = new Error('Error!')
const expectedAction = {
type: types.firebase.FIREBASE_UPDATE_REJECTED,
Expand All @@ -124,7 +124,7 @@ describe('firebase actions', () => {
).toEqual(expectedAction)
})

test('FIREBASE_REMOVE_REQUESTED', () => {
test(types.firebase.FIREBASE_REMOVE_REQUESTED, () => {
const uid = '1'
const expectedAction = {
type: types.firebase.FIREBASE_REMOVE_REQUESTED,
Expand All @@ -136,7 +136,7 @@ describe('firebase actions', () => {
).toEqual(expectedAction)
})

test('FIREBASE_REMOVE_FULFILLED', () => {
test(types.firebase.FIREBASE_REMOVE_FULFILLED, () => {
const error = 'error'
const expectedAction = {
type: types.firebase.FIREBASE_REMOVE_FULFILLED,
Expand All @@ -149,7 +149,7 @@ describe('firebase actions', () => {
)
})

test('FIREBASE_REMOVE_REJECTED', () => {
test(types.firebase.FIREBASE_REMOVE_REJECTED, () => {
const error = new Error('Error!')
const expectedAction = {
type: types.firebase.FIREBASE_REMOVE_REJECTED,
Expand All @@ -162,7 +162,7 @@ describe('firebase actions', () => {
).toEqual(expectedAction)
})

test('FIREBASE_LISTEN_REMOVED', () => {
test(types.firebase.FIREBASE_LISTEN_REMOVED, () => {
const expectedAction = {
type: types.firebase.FIREBASE_LISTEN_REMOVED,
payload: { clearItems: true },
Expand All @@ -174,7 +174,7 @@ describe('firebase actions', () => {
)
})

test('FIREBASE_REMOVE_LISTENER_REQUESTED', () => {
test(types.firebase.FIREBASE_REMOVE_LISTENER_REQUESTED, () => {
const expectedAction = {
type: types.firebase.FIREBASE_REMOVE_LISTENER_REQUESTED,
payload: { clearItems: true },
Expand All @@ -186,7 +186,7 @@ describe('firebase actions', () => {
).toEqual(expectedAction)
})

test('FIREBASE_REMOVE_ALL_LISTENERS_REQUESTED', () => {
test(types.firebase.FIREBASE_REMOVE_ALL_LISTENERS_REQUESTED, () => {
const expectedAction = {
type: types.firebase.FIREBASE_REMOVE_ALL_LISTENERS_REQUESTED,
payload: { clearItems: true },
Expand Down Expand Up @@ -217,4 +217,52 @@ describe('firebase actions', () => {
}
expect(actions.listenToUserContacts(uid)).toEqual(expectedAction)
})

test('removeMessagesListenerRequested', () => {
const expectedAction = {
type: types.firebase.FIREBASE_REMOVE_LISTENER_REQUESTED,
payload: { clearItems: false },
meta: { type: metaTypes.messages },
}
expect(actions.removeMessagesListenerRequested()).toEqual(expectedAction)
})

test('removeUserContactsListenerRequested', () => {
const expectedAction = {
type: types.firebase.FIREBASE_REMOVE_LISTENER_REQUESTED,
payload: { clearItems: false },
meta: { type: metaTypes.userContacts },
}
expect(actions.removeUserContactsListenerRequested()).toEqual(
expectedAction
)
})

test('updateUserContactsRequested', () => {
const uid = '1'
const contactId = '123'
const name = 'John Doe'
const phone = '123456789'
const expectedAction = {
type: types.firebase.FIREBASE_UPDATE_REQUESTED,
payload: { uid, contactId, name, phone },
meta: { type: metaTypes.userContacts },
}
expect(
actions.updateUserContactsRequested(uid, contactId, name, phone)
).toEqual(expectedAction)
})

test('removeUserContactsRequested', () => {
const uid = '1'
const contactId = '123'
const expectedAction = {
type: types.firebase.FIREBASE_REMOVE_REQUESTED,
payload: { uid, contactId },
meta: { type: metaTypes.userContacts },
}
expect(actions.removeUserContactsRequested(uid, contactId)).toEqual(
expectedAction
)
})
})
2 changes: 1 addition & 1 deletion __tests__/sagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ describe('database saga', () => {
test('getUpdateOfferingUpdates', () => {
const uid = '1'
const contactId = '123'
const name = 'John Due'
const name = 'John Doe'
const phone = '123456789'

const updates = {
Expand Down
14 changes: 14 additions & 0 deletions patterns/update_action_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
test('updateUserContactsRequested', () => {
const uid = '1'
const contactId = '123'
const name = 'John Doe'
const phone = '123456789'
const expectedAction = {
type: types.firebase.FIREBASE_UPDATE_REQUESTED,
payload: { uid, contactId, name, phone },
meta: { type: metaTypes.userContacts },
}
expect(
actions.updateUserContactsRequested(uid, contactId, name, phone)
).toEqual(expectedAction)
})
63 changes: 63 additions & 0 deletions patterns/update_sagas_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
test(`watchUpdateRequested ${metaTypes.userContacts}`, () => {
const generator = sagas.watchUpdateRequested()
const updates = { updates: { a: '1', b: '2' } }
const action = actions.updateUserContactsRequested()
const selector = sagas.getUserContactsUpdates

expect(generator.next().value).toEqual(
take(types.firebase.FIREBASE_UPDATE_REQUESTED)
)
expect(generator.next(action).value).toEqual(call(selector, action.payload))
expect(generator.next(updates).value).toEqual(
fork(sagas.updateItems, updates, action.meta.type)
)
})

test('watchUpdateRequested unknownType', () => {
const generator = sagas.watchUpdateRequested()

//test non function case
expect(generator.next().value).toEqual(
take(types.firebase.FIREBASE_UPDATE_REQUESTED)
)
expect(generator.next({ meta: { type: 'unknownType' } }).value).toEqual(
take(types.firebase.FIREBASE_UPDATE_REQUESTED)
)
})

test('updateItems - regular stream - success and failure', () => {
const updates = { x: true }
const metaType = 'someType'
const ref = firebase.database().ref()
const generator = cloneableGenerator(sagas.updateItems)(updates, metaType)
expect(generator.next().value).toEqual(call([ref, ref.update], updates))

const successGenerator = generator.clone()
expect(successGenerator.next().value).toEqual(
put(actions.firebaseUpdateFulfilled(metaType))
)
expect(successGenerator.next().done).toEqual(true)

const failGenerator = generator.clone()
const error = new Error('An error occured')
expect(failGenerator.throw(error).value).toEqual(
put(actions.firebaseUpdateRejected(error, metaType))
)
expect(failGenerator.next().done).toEqual(true)
})

test('getUpdateOfferingUpdates', () => {
const uid = '1'
const contactId = '123'
const name = 'John Doe'
const phone = '123456789'

const updates = {
[`users/${uid}/contacts/${contactId}/name`]: name,
[`users/${uid}/contacts/${contactId}/phone`]: phone,
}

expect(sagas.getUserContactsUpdates({ uid, contactId, name, phone })).toEqual(
updates
)
})

0 comments on commit cfd9a02

Please sign in to comment.