Skip to content

Commit

Permalink
[CityOfZion#1318] Actual 2/2: Added camera availability unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ranbena committed Nov 28, 2018
1 parent 4ffa776 commit 6ad3fa3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
42 changes: 40 additions & 2 deletions __tests__/components/Login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import configureStore from 'redux-mock-store'
import { Provider } from 'react-redux'
import { MemoryRouter } from 'react-router-dom'
import { shallow, mount } from 'enzyme'
import { waitForElement } from '../testHelpers'


import Login from '../../app/containers/LoginPrivateKey/LoginPrivateKey'
import LoginWithHOC from '../../app/containers/LoginPrivateKey/'

const setup = (
shallowRender = true,
withHOC = false,
state = {
account: {
loggedIn: true,
Expand All @@ -16,15 +20,16 @@ const setup = (
}
) => {
const store = configureStore()(state)
const Component = withHOC ? LoginWithHOC : Login

let wrapper
if (shallowRender) {
wrapper = shallow(<Login store={store} />)
wrapper = shallow(<Component store={store} />)
} else {
wrapper = mount(
<Provider store={store}>
<MemoryRouter>
<Login store={store} />
<Component store={store} />
</MemoryRouter>
</Provider>
)
Expand Down Expand Up @@ -55,6 +60,39 @@ describe('Login', () => {
expect(wrapper.find('input').get(0).props.type).toEqual('text')
})

test('qr scan button should be active according to camera availability', async () => {
// mount Login with HOC
const { wrapper } = setup(false, true)

// mock 1 available camera
global.navigator.mediaDevices = {
enumerateDevices: jest.fn().mockResolvedValue([{ kind: 'videoinput' }])
}

// wait until the enabled button is found in dom
const enabledButton = await waitForElement(
wrapper,
'#scan-private-key-qr-button[disabled=false]'
)

// assert that the enabled button exists
expect(enabledButton).toBeDefined()

// mock 0 available cameras
global.navigator.mediaDevices = {
enumerateDevices: jest.fn().mockResolvedValue([])
}

// wait until the disabled button is found in dom
const disabledButton = await waitForElement(
wrapper,
'#scan-private-key-qr-button[disabled=true]'
)

// assert that the disabled button exists
expect(disabledButton).toBeDefined()
})

// test('private key field input onChange dispatches LOGIN action', () => {
// const { store, wrapper } = setup();
// let preventDefault = jest.fn();
Expand Down
16 changes: 16 additions & 0 deletions __tests__/testHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react'
import thunk from 'redux-thunk'
import configureStore from 'redux-mock-store'
import { Provider } from 'react-redux'
import poll from '../app/util/poll'

export const createStore = configureStore([thunk])

Expand All @@ -27,3 +28,18 @@ export const mockPromiseRejected = (message = 'test error') =>
reject(new Error(message))
})
})

// polls for an enzyme element existence for 2s
export const waitForElement = async (
wrapper,
selector,
config = { attempts: 10, frequency: 200 }
) => {
const findElement = async () => {
const element = wrapper.update().find(selector)
return element.length
? Promise.resolve(element.get(0))
: Promise.reject('Element not found')
}
return poll(findElement, config)
}

0 comments on commit 6ad3fa3

Please sign in to comment.