{"size": 3471, "ext": "js", "max_stars_repo_path": "src/bundles/notify.test.js", "max_stars_repo_name": "decooio/ipfs-webui", "max_stars_repo_stars_event_min_datetime": "2017-12-24T21:46:46.000Z", "max_stars_repo_stars_event_max_datetime": "2021-03-25T08:56:29.000Z", "max_issues_repo_path": "src/bundles/notify.test.js", "max_issues_repo_name": "decooio/ipfs-webui", "max_issues_repo_issues_event_min_datetime": "2017-12-21T21:34:00.000Z", "max_issues_repo_issues_event_max_datetime": "2021-03-25T00:52:19.000Z", "max_forks_repo_path": "src/bundles/notify.test.js", "max_forks_repo_name": "decooio/ipfs-webui", "max_forks_repo_forks_event_min_datetime": "2018-01-02T11:00:27.000Z", "max_forks_repo_forks_event_max_datetime": "2021-03-24T10:16:46.000Z"}
{"max_stars_count": 752, "max_issues_count": 802, "max_forks_count": 280, "avg_line_length": 33.6990291262, "max_line_length": 87, "alphanum_fraction": 0.703255546}
/* global it, expect */
import { composeBundlesRaw, createReactorBundle } from 'redux-bundler'
import notifyBundle from './notify'
import sleep from '../../test/helpers/sleep'

const appTimeBundle = {
  name: 'appTime',
  reducer: () => Date.now(),
  selectAppTime: state => state.appTime
}

const ipfsBundle = (provider = 'js-ipfs-api', opts) => {
  return Object.assign({}, {
    name: 'ipfs',
    selectIpfsProvider: () => provider
  }, opts)
}

it('should notify about api stats fetch errors', async () => {
  const store = composeBundlesRaw(
    appTimeBundle,
    ipfsBundle(),
    notifyBundle
  )()
  expect(store.selectNotify().show).toEqual(false)
  store.dispatch({ type: 'STATS_FETCH_FAILED', payload: { error: new Error('test') } })
  expect(store.selectNotify().show).toEqual(true)
  expect(store.selectNotify().error).toEqual(true)
  expect(store.selectNotifyI18nKey()).toEqual('ipfsApiRequestFailed')
})

it('should notify about window.ipfs fetch errors', async () => {
  const store = composeBundlesRaw(
    appTimeBundle,
    ipfsBundle('window.ipfs'),
    notifyBundle
  )()
  expect(store.selectNotify().show).toEqual(false)
  store.dispatch({ type: 'STATS_FETCH_FAILED' })
  expect(store.selectNotify().show).toEqual(true)
  expect(store.selectNotify().error).toEqual(true)
  expect(store.selectNotifyI18nKey()).toEqual('windowIpfsRequestFailed')
})

it('should notify about connection returning after a previous error', async () => {
  const store = composeBundlesRaw(
    appTimeBundle,
    ipfsBundle(),
    notifyBundle
  )()
  expect(store.selectNotify().show).toEqual(false)
  store.dispatch({ type: 'STATS_FETCH_FAILED', payload: { error: new Error('test') } })
  expect(store.selectNotify().show).toEqual(true)
  expect(store.selectNotify().error).toEqual(true)
  expect(store.selectNotifyI18nKey()).toEqual('ipfsApiRequestFailed')
  store.dispatch({ type: 'STATS_FETCH_FINISHED' })
})

it('should dismiss connection ok message after 3s', async () => {
  let fakeTime = Date.now()
  const store = composeBundlesRaw(
    {
      name: 'appTime',
      reducer: () => fakeTime,
      selectAppTime: state => fakeTime
    },
    ipfsBundle(),
    notifyBundle,
    createReactorBundle()
  )()
  expect(store.selectNotify().show).toEqual(false)
  store.dispatch({ type: 'STATS_FETCH_FAILED', payload: { error: new Error('test') } })
  expect(store.selectNotify().show).toEqual(true)
  expect(store.selectNotify().error).toEqual(true)
  expect(store.selectNotifyI18nKey()).toEqual('ipfsApiRequestFailed')

  store.dispatch({ type: 'STATS_FETCH_FINISHED' })
  expect(store.selectNotify().show).toEqual(true)
  expect(store.selectNotify().error).toEqual(false)
  expect(store.selectNotify().eventId).toEqual('STATS_FETCH_FINISHED')

  // add 3s to the app time clock
  fakeTime = Date.now() + 3001
  store.dispatch({ type: 'APP_IDLE' })
  // wait for reactor to kick in
  await sleep(1)
  expect(store.selectNotify().show).toEqual(false)
})

it('should notify about file errors', async () => {
  const store = composeBundlesRaw(
    appTimeBundle,
    ipfsBundle(),
    notifyBundle
  )()
  expect(store.selectNotify().show).toEqual(false)
  store.dispatch({ type: 'FILES_WRITE_FAILED', payload: { error: new Error('test') } })
  expect(store.selectNotify().show).toEqual(true)
  expect(store.selectNotify().error).toEqual(true)
  expect(store.selectNotifyI18nKey()).toEqual('filesEventFailed')
  store.dispatch({ type: 'FILES_WRITE_FINISHED' })
})
