All files / spec loading_bar.js

100% Statements 104/104
100% Branches 0/0
100% Functions 26/26
100% Lines 104/104
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 1631x 1x 1x 1x 1x 1x             1x   1x 1x 1x 1x   1x     1x 1x   1x 1x 1x 1x     1x 1x   1x 1x 1x     1x 1x 1x   1x 1x 1x 1x       1x 1x   1x 3x   1x 3x     1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x       1x 1x 1x   1x 3x       3x   1x 3x 3x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x       1x 1x   1x 3x   1x 3x     1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x       1x 1x 1x 1x 1x 1x 1x 1x 1x          
import React from 'react'
import { shallow } from 'enzyme'
import expect, { spyOn, restoreSpies } from 'expect'
import expectJSX from 'expect-jsx'
import lolex from 'lolex'
import {
  LoadingBar,
  UPDATE_TIME,
  MAX_PROGRESS,
  PROGRESS_INCREASE,
} from '../src/loading_bar'
 
expect.extend(expectJSX)
 
describe('LoadingBar', () => {
  describe('#render', () => {
    it('renders without problems', () => {
      const wrapper = shallow(<LoadingBar />)
 
      expect(wrapper.node.type).toEqual('div')
    })
 
    it('renders by default hidden 3px height red element', () => {
      const wrapper = shallow(<LoadingBar />)
 
      const resultStyle = wrapper.node.props.style
      expect(resultStyle.display).toEqual('none')
      expect(resultStyle.backgroundColor).toEqual('red')
      expect(resultStyle.height).toEqual('3px')
    })
 
    it('renders an element with passed color and height', () => {
      const wrapper = shallow(<LoadingBar color="blue" height="5px" />)
 
      const resultStyle = wrapper.node.props.style
      expect(resultStyle.backgroundColor).toEqual('blue')
      expect(resultStyle.height).toEqual('5px')
    })
 
    it('renders not hidden 3px height red element', () => {
      const wrapper = shallow(<LoadingBar loading={1} />)
      wrapper.setState({ percent: 10 })
 
      const resultStyle = wrapper.node.props.style
      expect(resultStyle.display).toEqual('block')
      expect(resultStyle.backgroundColor).toEqual('red')
      expect(resultStyle.height).toEqual('3px')
    })
  })
 
  describe('#componentWillMount and #componentWillReceiveProps', () => {
    let spyLaunch
 
    beforeEach(() => {
      spyLaunch = spyOn(LoadingBar.prototype, 'launch')
    })
    afterEach(() => {
      restoreSpies()
    })
 
    it('launches on component mount', () => {
      shallow(<LoadingBar />)
      expect(spyLaunch).toHaveBeenCalled()
      expect(spyLaunch.calls.length).toEqual(1)
    })
 
    it('launches on component mount and then on loading increase', () => {
      const wrapper = shallow(<LoadingBar />)
      wrapper.setProps({ loading: 1 })
      expect(spyLaunch).toHaveBeenCalled()
      expect(spyLaunch.calls.length).toEqual(2)
    })
 
    it('does not launch if loading count is not increased', () => {
      const wrapper = shallow(<LoadingBar />)
      wrapper.setProps({ loading: 0 })
      expect(spyLaunch).toHaveBeenCalled()
      expect(spyLaunch.calls.length).toEqual(1)
    })
  })
 
  describe('#launch', () => {
    let clock
    let spySimulateProgress
 
    beforeEach(() => {
      spySimulateProgress = spyOn(
        LoadingBar.prototype,
        'simulateProgress'
      )
      clock = lolex.install()
    })
    afterEach(() => {
      restoreSpies()
      clock.uninstall()
    })
 
    it('schedules simulateProgress on UPDATE_TIME', () => {
      shallow(<LoadingBar />)
      clock.tick(UPDATE_TIME)
      expect(spySimulateProgress).toHaveBeenCalled()
      expect(spySimulateProgress.calls.length).toEqual(1)
    })
 
    it('does not schedule simulateProgress before UPDATE_TIME', () => {
      shallow(<LoadingBar />)
      clock.tick(UPDATE_TIME - 1)
      expect(spySimulateProgress).toNotHaveBeenCalled()
      expect(spySimulateProgress.calls.length).toEqual(0)
    })
 
    it('schedules simulateProgress twice after UPDATE_TIME * 2', () => {
      shallow(<LoadingBar />)
      clock.tick(UPDATE_TIME * 2)
      expect(spySimulateProgress).toHaveBeenCalled()
      expect(spySimulateProgress.calls.length).toEqual(2)
    })
  })
 
  describe('#simulateProgress', () => {
    let clock
 
    beforeEach(() => {
      clock = lolex.install()
    })
    afterEach(() => {
      clock.uninstall()
    })
 
    it('clears interval if loading becomes 0', () => {
      const wrapper = shallow(<LoadingBar />)
      wrapper.setProps({ loading: 1 })
      clock.tick(UPDATE_TIME)
      expect(wrapper.state().interval).toExist()
      wrapper.setProps({ loading: 0 })
      clock.tick(UPDATE_TIME)
      expect(wrapper.state().interval).toNotExist()
    })
 
    describe('if percent is less than MAX_PROGRESS', () => {
      it('increases percent', () => {
        const wrapper = shallow(<LoadingBar />)
        expect(wrapper.state().percent).toBe(0)
        wrapper.setProps({ loading: 1 })
        clock.tick(UPDATE_TIME)
        expect(wrapper.state().percent).toBe(PROGRESS_INCREASE)
      })
    })
 
    describe('if percent is MAX_PROGRESS', () => {
      it('does not increase percent further', () => {
        const wrapper = shallow(<LoadingBar />)
        expect(wrapper.state().percent).toBe(0)
        wrapper.setProps({ loading: 1 })
        clock.tick(UPDATE_TIME * (MAX_PROGRESS / PROGRESS_INCREASE))
        expect(wrapper.state().percent).toBe(MAX_PROGRESS)
        clock.tick(UPDATE_TIME)
        expect(wrapper.state().percent).toBe(MAX_PROGRESS)
      })
    })
  })
})