Skip to content

Commit

Permalink
Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JackNeto committed Aug 11, 2019
1 parent 14ffce7 commit ff9754b
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 17 deletions.
27 changes: 13 additions & 14 deletions src/core/Budget/BudgetChart.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ const BudgetChart = () => {
}))

// Used categories except Income
const categorizedTransactions = transactions.list.filter(transaction => transaction.categoryId !== undefined)
const usedCategories = [
...new Set(
Object.values(budget.rules)
.map(rules => budget.categoriesById[rules.categoryId])
categorizedTransactions
.map(transaction => budget.categoriesById[transaction.categoryId])
)
].filter(category => !budget.categoriesById[category.parentId].isIncome)

Expand All @@ -42,20 +43,18 @@ const BudgetChart = () => {
const [opacity, setOpacity] = useState(initialState)

const byMonth = {}
transactions.list.forEach((transaction) => {
categorizedTransactions.forEach((transaction) => {
const dateKey = startOfMonth(transaction.createdAt).getTime()
if (transaction.categoryId !== undefined) {
const category = budget.categoriesById[transaction.categoryId]
const group = budget.categoriesById[category.parentId]
if (!group.isIncome) {
if (byMonth[dateKey] === undefined) {
byMonth[dateKey] = usedCategories.reduce((res, cat) => ({ ...res, [cat.name]: 0 }), {})
}
if (byMonth[dateKey][category.name] === undefined) {
byMonth[dateKey][category.name] = 0
}
byMonth[dateKey][category.name] += Math.abs(transaction.amount)
const category = budget.categoriesById[transaction.categoryId]
const group = budget.categoriesById[category.parentId]
if (!group.isIncome) {
if (byMonth[dateKey] === undefined) {
byMonth[dateKey] = usedCategories.reduce((res, cat) => ({ ...res, [cat.name]: 0 }), {})
}
if (byMonth[dateKey][category.name] === undefined) {
byMonth[dateKey][category.name] = 0
}
byMonth[dateKey][category.name] += Math.abs(transaction.amount)
}
})
const data = Object.keys(byMonth).sort((a, b) => a - b).map(date => ({
Expand Down
25 changes: 25 additions & 0 deletions src/core/Budget/__tests__/BudgetChart.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react'
import { mount } from 'enzyme'
import { Provider } from 'react-redux'
import configureMockStore from 'redux-mock-store'
import BudgetChart from '../BudgetChart'
import { initialState as transactionsInitialState } from '../../../store/transactions/reducer'
import { initialState as settingsInitialState } from '../../../store/settings/reducer'
import { initialState as budgetInitialState } from '../../../store/budget/reducer'

describe('BudgetChart', () => {
it('matches snapshot with no used categories', () => {
const mockStore = configureMockStore()
const store = mockStore({
transactions: transactionsInitialState,
settings: settingsInitialState,
budget: budgetInitialState
})
const wrapper = mount((
<Provider store={store}>
<BudgetChart />
</Provider>
))
expect(wrapper.debug()).toMatchSnapshot()
})
})
16 changes: 16 additions & 0 deletions src/core/Budget/__tests__/__snapshots__/BudgetChart.test.jsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`BudgetChart matches snapshot with no used categories 1`] = `
"<Provider store={{...}}>
<BudgetChart>
<ResponsiveContainer width=\\"100%\\" height=\\"100%\\" debounce={0}>
<div id={[undefined]} className=\\"recharts-responsive-container\\" style={{...}}>
<LineChart data={{...}} margin={{...}} layout=\\"horizontal\\" stackOffset=\\"none\\" barCategoryGap=\\"10%\\" barGap={4} reverseStackOrder={false} width={0} height={0} />
<ResizeDetector handleWidth={true} handleHeight={true} onResize={[Function]} skipOnMount={false} refreshRate={1000} refreshMode={[undefined]} resizableElementId=\\"\\">
<div style={{...}} />
</ResizeDetector>
</div>
</ResponsiveContainer>
</BudgetChart>
</Provider>"
`;
17 changes: 17 additions & 0 deletions src/core/Budget/__tests__/__snapshots__/index.test.jsx.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`BudgetIndex matches snapshot with no accounts 1`] = `
"<Provider store={{...}}>
<Connect(WithStyles(BudgetIndexComponent))>
<WithStyles(BudgetIndexComponent) budget={{...}} dispatch={[Function: dispatch]}>
<BudgetIndexComponent classes={{...}} budget={{...}} dispatch={[Function: dispatch]}>
<WithStyles(ForwardRef(Container)) className=\\"BudgetIndexComponent-root-1\\">
<ForwardRef(Container) classes={{...}} className=\\"BudgetIndexComponent-root-1\\">
<div className=\\"MuiContainer-root BudgetIndexComponent-root-1 MuiContainer-maxWidthLg\\" />
</ForwardRef(Container)>
</WithStyles(ForwardRef(Container))>
</BudgetIndexComponent>
</WithStyles(BudgetIndexComponent)>
</Connect(WithStyles(BudgetIndexComponent))>
</Provider>"
`;
21 changes: 21 additions & 0 deletions src/core/Budget/__tests__/index.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react'
import { mount } from 'enzyme'
import { Provider } from 'react-redux'
import configureMockStore from 'redux-mock-store'
import BudgetIndex from '..'
import { initialState as budgetInitialState } from '../../../store/budget/reducer'

describe('BudgetIndex', () => {
it('matches snapshot with no accounts', () => {
const mockStore = configureMockStore()
const store = mockStore({
budget: budgetInitialState
})
const wrapper = mount((
<Provider store={store}>
<BudgetIndex />
</Provider>
))
expect(wrapper.debug()).toMatchSnapshot()
})
})
6 changes: 3 additions & 3 deletions src/core/Budget/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const mapStateToProps = state => ({
budget: state.budget
})

export const DashboardComponent = ({
export const BudgetIndexComponent = ({
classes,
budget
}) => {
Expand All @@ -48,12 +48,12 @@ export const DashboardComponent = ({
)
}

DashboardComponent.propTypes = {
BudgetIndexComponent.propTypes = {
classes: PropTypes.object.isRequired,
budget: PropTypes.object.isRequired
}

export default compose(
connect(mapStateToProps),
withStyles(styles)
)(DashboardComponent)
)(BudgetIndexComponent)

0 comments on commit ff9754b

Please sign in to comment.