Skip to content

Commit

Permalink
Merge pull request #124 from canyener/private-firebase-data
Browse files Browse the repository at this point in the history
Private firebase data
  • Loading branch information
canyener authored May 3, 2020
2 parents 62c352b + 0cb21a8 commit 87ab4ba
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
20 changes: 12 additions & 8 deletions src/actions/expenses.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export const addExpense = (expense) => ({
})

export const startAddExpense = (expenseData = {}) => {
return (dispatch) => {
return (dispatch, getState) => {
const uid = getState().auth.uid
const {
description = '',
note = '',
Expand All @@ -17,7 +18,7 @@ export const startAddExpense = (expenseData = {}) => {

const expense = { description, note, amount, createdAt }

return database.ref('expenses').push(expense).then((ref) => {
return database.ref(`users/${uid}/expenses`).push(expense).then((ref) => {
dispatch(addExpense({
id: ref.key,
...expense
Expand All @@ -33,8 +34,9 @@ export const removeExpense = ({ id } = {}) => ({
})

export const startRemoveExpense = ({ id } = {}) => {
return (dispatch) => {
return database.ref(`expenses/${id}`).remove().then(() => {
return (dispatch, getState) => {
const uid = getState().auth.uid
return database.ref(`users/${uid}/expenses/${id}`).remove().then(() => {
dispatch(removeExpense({ id }))
})
}
Expand All @@ -48,8 +50,9 @@ export const editExpense = (id, updates) => ({
})

export const startEditExpense = (id, updates) => {
return (dispatch) => {
return database.ref(`expenses/${id}`).update(updates).then(() => {
return (dispatch, getState) => {
const uid = getState().auth.uid
return database.ref(`/users/${uid}/expenses/${id}`).update(updates).then(() => {
dispatch(editExpense(id, updates))
})
}
Expand All @@ -62,8 +65,9 @@ export const setExpenses = (expenses) => ({
})

export const startSetExpenses = () => {
return (dispatch) => {
return database.ref('expenses').once('value').then(snapshot => {
return (dispatch, getState) => {
const uid = getState().auth.uid
return database.ref(`users/${uid}/expenses`).once('value').then(snapshot => {
const expenses = []

snapshot.forEach(childSnapshot => {
Expand Down
22 changes: 12 additions & 10 deletions src/tests/actions/expenses.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import expenses from '../fixtures/expenses'

import database from '../../firebase/firebase'

const uid = 'thisismytestuid'
const defaultAuthState = { auth: { uid }}
const createMockStore = configureMockStore([thunk])

beforeEach((done) => {
Expand All @@ -24,7 +26,7 @@ beforeEach((done) => {
expensesData[id] = { description, note, amount, createdAt }
})

database.ref('expenses').set(expensesData).then(() => done())
database.ref(`users/${uid}/expenses`).set(expensesData).then(() => done())
})

afterAll(() => {
Expand All @@ -42,7 +44,7 @@ describe('expenses actions', () => {
})

test('Should remove expense from firebase', (done) => {
const store = createMockStore({})
const store = createMockStore(defaultAuthState)
const id = expenses[2].id

store.dispatch(startRemoveExpense({ id })).then(() => {
Expand All @@ -52,7 +54,7 @@ describe('expenses actions', () => {
id
})

return database.ref(`expenses/${id}`).once('value')
return database.ref(`users/${uid}/expenses/${id}`).once('value')
}).then(snapshot => {
expect(snapshot.val()).toBeFalsy()
done()
Expand All @@ -74,7 +76,7 @@ describe('expenses actions', () => {
})

test('Should edit expense from firebase', (done) => {
const store = createMockStore({})
const store = createMockStore(defaultAuthState)

const id = expenses[0].id
const updates = {
Expand All @@ -93,7 +95,7 @@ describe('expenses actions', () => {
updates
})

return database.ref(`expenses/${id}`).once('value')
return database.ref(`users/${uid}/expenses/${id}`).once('value')
}).then(snapshot => {
expect(snapshot.val()).toEqual(updates)
done()
Expand All @@ -112,7 +114,7 @@ describe('expenses actions', () => {
})

test('Should add expense to database and store', (done) => {
const store = createMockStore({})
const store = createMockStore(defaultAuthState)

const expenseData = {
description: 'Test desc',
Expand All @@ -132,15 +134,15 @@ describe('expenses actions', () => {
}
})

return database.ref(`expenses/${actions[0].expense.id}`).once('value')
return database.ref(`/users/${uid}/expenses/${actions[0].expense.id}`).once('value')
}).then((snapshot) => {
expect(snapshot.val()).toEqual(expenseData)
done()
})
})

test('Should add expense with default values to database and store', (done) => {
const store = createMockStore({})
const store = createMockStore(defaultAuthState)

const expectedExpense = {
description: '',
Expand All @@ -160,7 +162,7 @@ describe('expenses actions', () => {
}
})

return database.ref(`expenses/${actions[0].expense.id}`).once('value')
return database.ref(`/users/${uid}/expenses/${actions[0].expense.id}`).once('value')
}).then((snapshot) => {
expect(snapshot.val()).toEqual(expectedExpense)
done()
Expand All @@ -178,7 +180,7 @@ describe('expenses actions', () => {
})

test('Should fetch the expenses from firebase', (done) => {
const store = createMockStore({})
const store = createMockStore(defaultAuthState)

store.dispatch(startSetExpenses()).then(() => {
const actions = store.getActions()
Expand Down

0 comments on commit 87ab4ba

Please sign in to comment.