Skip to content

Commit

Permalink
Add more tests to increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
on3iro committed Jan 26, 2018
1 parent ede74b2 commit 37bc0a9
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 5 deletions.
88 changes: 88 additions & 0 deletions src/HOCS/_tests_/makeWithLoadingIndicator.test.jsx
@@ -0,0 +1,88 @@
import React from 'react'
import { mount } from 'enzyme'

import Cube from '../../components/Cube'
import SimpleCircle from '../../components/SimpleCircle'

import makeWithLoadingIndicator, { getSpinner } from '../makeWithLoadingIndicator'

const CustomComponent = () => <h1>CustomComponent</h1>

describe('getSpinner()', () => {
it('should render Cube', () => {
const result = getSpinner('Cube')

expect(result).toEqual(Cube)
})

it('should render SimpleCircle', () => {
const result = getSpinner('SimpleCircle')

expect(result).toEqual(SimpleCircle)
})

it('should render custom component', () => {
const result = getSpinner(CustomComponent)

expect(result).toEqual(CustomComponent)
})
}) // end getSpinner()

describe('makeWithLoadingIndicator', () => {
const WrappedComponent = (props) => (
<h1>WrappedComponent</h1>
)

it('should render wrapped component, if condition HOC returns false', () => {
const condition = () => false
const widthLoadingIndicator = makeWithLoadingIndicator({ condition })

const ComponentWithLoadingIndicator = widthLoadingIndicator(WrappedComponent)

const wrapper = mount(<ComponentWithLoadingIndicator />)
expect(wrapper.find(WrappedComponent).exists()).toBe(true)
expect(wrapper.find(SimpleCircle).exists()).toBe(false)
})

it('should render SimpleCircle by default if condition returns true', () => {
const condition = () => true
const widthLoadingIndicator = makeWithLoadingIndicator({ condition })

const ComponentWithLoadingIndicator = widthLoadingIndicator(WrappedComponent)

const wrapper = mount(<ComponentWithLoadingIndicator />)
expect(wrapper.find(SimpleCircle).exists()).toBe(true)
expect(wrapper.find(WrappedComponent).exists()).toBe(false)
})

it('should render CustomComponent if condition returns true and custom spinner was defined', () => {
const condition = () => true
const widthLoadingIndicator = makeWithLoadingIndicator({
condition,
spinnerType: CustomComponent
})

const ComponentWithLoadingIndicator = widthLoadingIndicator(WrappedComponent)

const wrapper = mount(<ComponentWithLoadingIndicator />)
expect(wrapper.find(SimpleCircle).exists()).toBe(false)
expect(wrapper.find(WrappedComponent).exists()).toBe(false)
expect(wrapper.find(CustomComponent).exists()).toBe(true)
})

it('should render custom Container if condition is true', () => {
const Container = () => <h1>Container</h1>
const condition = () => true

const widthLoadingIndicator = makeWithLoadingIndicator({
Container,
condition
})

const ComponentWithLoadingIndicator = widthLoadingIndicator(WrappedComponent)

const wrapper = mount(<ComponentWithLoadingIndicator />)
expect(wrapper.find(Container).exists()).toBe(true)
expect(wrapper.find(WrappedComponent).exists()).toBe(false)
})
}) // end makeWithLoadingIndicator
6 changes: 4 additions & 2 deletions src/HOCS/makeWithLoadingIndicator.jsx
Expand Up @@ -6,7 +6,7 @@ import { branch, renderComponent } from 'recompose'
import Cube from '../components/Cube'
import SimpleCircle from '../components/SimpleCircle'

const getSpinner = (spinnerType: string | React.Node): React.Node => {
export const getSpinner = (spinnerType: string | React.Node): React.Node => {
switch (spinnerType) {
case 'Cube':
return Cube
Expand All @@ -17,7 +17,7 @@ const getSpinner = (spinnerType: string | React.Node): React.Node => {
}
}

export const makeWithLoadingIndicator = ({
const makeWithLoadingIndicator = ({
Container = React.Fragment,
condition,
spinnerType = 'SimpleCircle',
Expand Down Expand Up @@ -46,3 +46,5 @@ export const makeWithLoadingIndicator = ({
})
)
}

export default makeWithLoadingIndicator
33 changes: 33 additions & 0 deletions src/_tests_/index.test.js
@@ -0,0 +1,33 @@
import HOCSpinners, {
Cube,
makeWithLoadingIndicator,
SimpleCircle
} from '../index'

describe('HOCSpinners default export', () => {
it('should contain makeWithLoadingIndicator', () => {
expect(HOCSpinners.makeWithLoadingIndicator).toBeDefined()
})

it('should contain Cube', () => {
expect(HOCSpinners.Cube).toBeDefined()
})

it('should contain SimpleCircle', () => {
expect(HOCSpinners.SimpleCircle).toBeDefined()
})
}) // end default export

describe('Named exports', () => {
it('should export makeWithLoadingIndicator', () => {
expect(makeWithLoadingIndicator).toBeDefined()
})

it('should export Cube', () => {
expect(Cube).toBeDefined()
})

it('should export SimpleCircle', () => {
expect(SimpleCircle).toBeDefined()
})
}) // end named exports
27 changes: 27 additions & 0 deletions src/components/SimpleCircle/_tests_/index.test.js
@@ -0,0 +1,27 @@
import React from 'react'
import { shallow } from 'enzyme'

import Wrapper from '../../Wrapper'
import SimpleCircle, { Circle } from '../index'

describe('SimpleCircle', () => {
it('should render Wrapper', () => {
const wrapper = shallow(<SimpleCircle />)

expect(wrapper.find(Wrapper).exists()).toBe(true)
})

it('should render Circle', () => {
const wrapper = shallow(<SimpleCircle />)

expect(wrapper.find(Circle).exists()).toBe(true)
})
})

describe('Circle', () => {
it('should render <div>', () => {
const wrapper = shallow(<Circle />)

expect(wrapper.find('div').exists()).toBe(true)
})
})
2 changes: 1 addition & 1 deletion src/components/SimpleCircle/index.jsx
Expand Up @@ -5,7 +5,7 @@ import { colors } from '../../config'

import Wrapper from '../Wrapper'

const Circle = styled.div`
export const Circle = styled.div`
@keyframes spin {
0% {
transform: rotate(0deg);
Expand Down
10 changes: 8 additions & 2 deletions src/index.js
@@ -1,10 +1,16 @@
import { makeWithLoadingIndicator } from './HOCS/makeWithLoadingIndicator'
import makeWithLoadingIndicator from './HOCS/makeWithLoadingIndicator'
import Cube from './components/Cube'
import SimpleCircle from './components/SimpleCircle'

// Components
export { Cube }
export { SimpleCircle }

// HOCs
export { makeWithLoadingIndicator }

export default {
makeWithLoadingIndicator,
Cube
Cube,
SimpleCircle
}

0 comments on commit 37bc0a9

Please sign in to comment.