Skip to content

Commit

Permalink
feat: Re-fetch apps if app not loaded and drawer opens
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Nov 14, 2018
1 parent bea3ced commit dc0b9c4
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 19 deletions.
62 changes: 43 additions & 19 deletions src/components/Bar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import SearchBar from 'components/SearchBar'
import Claudy from 'components/Claudy'
import SupportModal from 'components/SupportModal'
import {
hasFetched,
getContent,
getCurrentApp,
fetchApps,
Expand All @@ -38,29 +39,47 @@ class Bar extends Component {
supportDisplayed: false,
searchBarEnabled: props.currentApp === 'Cozy Drive' && !props.isPublic
}
if (!props.isPublic) {
props.fetchContext()
props.fetchSettingsData(false)
props.fetchApps()
}
}

componentDidMount() {
// if tracking enabled, init the piwik tracker
if (shouldEnableTracking()) {
const trackerInstance = getTracker(
__PIWIK_TRACKER_URL__,
__PIWIK_SITEID__,
false,
false
)
configureTracker({
appDimensionId: __PIWIK_DIMENSION_ID_APP__,
app: 'Cozy Bar',
heartbeat: 0
})
this.setState({ usageTracker: trackerInstance })
this.initPiwikTracker()
}
this.fetchInitialData()
}

componentDidUpdate(prevProps, prevState) {
if (
!this.props.hasFetchedApps &&
this.state.drawerVisible &&
prevState.drawerVisible !== this.state.drawerVisible
) {
this.props.fetchApps()
}
}

initPiwikTracker() {
const trackerInstance = getTracker(
__PIWIK_TRACKER_URL__,
__PIWIK_SITEID__,
false,
false
)
configureTracker({
appDimensionId: __PIWIK_DIMENSION_ID_APP__,
app: 'Cozy Bar',
heartbeat: 0
})
this.setState({ usageTracker: trackerInstance })
}

fetchInitialData() {
if (this.props.isPublic) {
return
}
this.props.fetchContext()
this.props.fetchSettingsData(false)
this.props.fetchApps()
}

toggleDrawer = () => {
Expand Down Expand Up @@ -206,7 +225,8 @@ const mapStateToProps = state => ({
barRight: getContent(state, 'right'),
barCenter: getContent(state, 'center'),
currentApp: getCurrentApp(state),
claudyEnabled: shouldEnableClaudy(state)
claudyEnabled: shouldEnableClaudy(state),
hasFetchedApps: hasFetched(state)
})

const mapDispatchToProps = dispatch => ({
Expand All @@ -215,6 +235,10 @@ const mapDispatchToProps = dispatch => ({
fetchSettingsData: displayBusy => dispatch(fetchSettingsData(displayBusy))
})

export {
Bar
}

export default translate()(
connect(
mapStateToProps,
Expand Down
52 changes: 52 additions & 0 deletions test/components/Bar.spec.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import React from 'react'
import { Bar } from 'components/Bar'
import { shallow } from 'enzyme'

describe('Bar', () => {

let props
beforeEach(() => {
props = {
fetchContext: jest.fn().mockResolvedValue({}),
fetchApps: jest.fn().mockResolvedValue([]),
fetchSettingsData: jest.fn().mockResolvedValue({}),
t: x => x
}
})

it('should fetch data when mounted', () => {
const root = shallow(
<Bar { ...props} />
)
expect(props.fetchContext).toHaveBeenCalled()
expect(props.fetchApps).toHaveBeenCalled()
expect(props.fetchSettingsData).toHaveBeenCalled()
})

it('should not fetch data if public', () => {
const root = shallow(
<Bar { ...props} isPublic={true } />
)
expect(props.fetchContext).not.toHaveBeenCalled()
expect(props.fetchApps).not.toHaveBeenCalled()
expect(props.fetchSettingsData).not.toHaveBeenCalled()
})

it('should not fetch apps if hasFetchedApps is true', () => {
const root = shallow(
<Bar { ...props} hasFetchedApps={true } />
)
expect(props.fetchApps).toHaveBeenCalled()
})

it('should re-fetch apps if apps are not fetched and drawer opens', () => {
const root = shallow(
<Bar { ...props} />
)
expect(props.fetchApps).toHaveBeenCalledTimes(1)
root.setState({ drawerVisible: true })
expect(props.fetchApps).toHaveBeenCalledTimes(2)
root.setState({ drawerVisible: true, usageTracker: {} })
expect(props.fetchApps).toHaveBeenCalledTimes(2)
})
})

0 comments on commit dc0b9c4

Please sign in to comment.