Reorganize window

This commit is contained in:
Jerko Steiner 2017-06-17 23:14:26 -04:00
parent 368fa5102b
commit edda50b287
22 changed files with 107 additions and 82 deletions

View File

@ -1 +0,0 @@
export default 'call1234'

View File

@ -1 +0,0 @@
module.exports = []

View File

@ -0,0 +1,23 @@
import Promise from 'bluebird'
export const createObjectURL = object => 'blob://' + String(object)
class MediaStream {}
export function getUserMedia () {
return !getUserMedia.shouldFail
? Promise.resolve(getUserMedia.stream)
: Promise.reject(new Error('test'))
}
getUserMedia.fail = shouldFail => getUserMedia.shouldFail = shouldFail
getUserMedia.stream = new MediaStream()
export const navigator = window.navigator
export const play = jest.fn()
export const valueOf = jest.fn()
export const callId = 'call1234'
export const iceServers = []

View File

@ -1,6 +1,4 @@
jest.mock('../actions/CallActions.js')
jest.mock('../callId.js')
jest.mock('../iceServers.js')
jest.mock('../socket.js')
import * as constants from '../constants.js'

View File

@ -1,8 +1,12 @@
import Promise from 'bluebird'
import createObjectURL from '../createObjectURL.js'
import getUserMedia from '../getUserMedia.js'
import navigator from '../navigator.js'
import { play } from '../video.js'
import {
createObjectURL,
getUserMedia,
navigator,
play,
valueOf
} from '../window.js'
describe('window', () => {
@ -58,8 +62,7 @@ describe('window', () => {
window.document.body.appendChild(v2)
v1.play = jest.fn()
v2.play = jest.fn()
})
})
afterEach(() => {
window.document.body.removeChild(v1)
window.document.body.removeChild(v2)
@ -97,4 +100,27 @@ describe('window', () => {
})
describe('valueOf', () => {
let input
beforeEach(() => {
input = window.document.createElement('input')
input.setAttribute('id', 'my-main-id')
input.value = 'test'
window.document.body.appendChild(input)
})
afterEach(() => {
window.document.body.removeChild(input)
})
it('should return value of input', () => {
expect(valueOf('my-main-id')).toEqual('test')
})
it('does not fail when not found', () => {
expect(valueOf('my-main-id2')).toEqual(null)
})
})
})

View File

@ -3,9 +3,8 @@ import * as SocketActions from './SocketActions.js'
import * as StreamActions from './StreamActions.js'
import * as constants from '../constants.js'
import Promise from 'bluebird'
import callId from '../callId.js'
import getUserMedia from '../window/getUserMedia.js'
import socket from '../socket.js'
import { callId, getUserMedia } from '../window.js'
export const init = () => dispatch => {
return dispatch({

View File

@ -4,8 +4,7 @@ import * as constants from '../constants.js'
import Peer from 'simple-peer'
import _ from 'underscore'
import _debug from 'debug'
import iceServers from '../iceServers.js'
import { play } from '../window/video.js'
import { play, iceServers } from '../window.js'
const debug = _debug('peercalls')

View File

@ -1,17 +1,14 @@
jest.mock('../../callId.js')
jest.mock('../../iceServers.js')
jest.mock('../../socket.js')
jest.mock('../../window/getUserMedia.js')
jest.mock('../../window.js')
jest.mock('../../store.js')
jest.mock('../SocketActions.js')
import * as CallActions from '../CallActions.js'
import * as SocketActions from '../SocketActions.js'
import * as constants from '../../constants.js'
import * as getUserMediaMock from '../../window/getUserMedia.js'
import callId from '../../callId.js'
import socket from '../../socket.js'
import store from '../../store.js'
import { callId, getUserMedia } from '../../window.js'
jest.useFakeTimers()
@ -19,7 +16,7 @@ describe('reducers/alerts', () => {
beforeEach(() => {
store.clearActions()
getUserMediaMock.fail(false)
getUserMedia.fail(false)
SocketActions.handshake.mockReturnValue(jest.fn())
})
@ -47,7 +44,7 @@ describe('reducers/alerts', () => {
expect(SocketActions.handshake.mock.calls).toEqual([[{
socket,
roomName: callId,
stream: getUserMediaMock.stream
stream: getUserMedia.stream
}]])
})
.then(done)
@ -79,7 +76,7 @@ describe('reducers/alerts', () => {
})
it('dispatches alert when failed to get media stream', done => {
getUserMediaMock.fail(true)
getUserMedia.fail(true)
const promise = store.dispatch(CallActions.init())
socket.emit('connect')
promise

View File

@ -1,13 +1,11 @@
jest.mock('../../window/video.js')
jest.mock('../../callId.js')
jest.mock('../../iceServers.js')
jest.mock('../../window.js')
jest.mock('simple-peer')
import * as PeerActions from '../PeerActions.js'
import Peer from 'simple-peer'
import { EventEmitter } from 'events'
import { createStore } from '../../store.js'
import { play } from '../../window/video.js'
import { play } from '../../window.js'
describe('PeerActions', () => {
function createSocket () {

View File

@ -1,7 +1,5 @@
jest.mock('simple-peer')
jest.mock('../../callId.js')
jest.mock('../../iceServers.js')
jest.mock('../../window/createObjectURL.js')
jest.mock('../../window.js')
import * as SocketActions from '../SocketActions.js'
import * as constants from '../../constants.js'

View File

@ -1 +0,0 @@
export default window.document.getElementById('callId').value

View File

@ -1,6 +1,3 @@
jest.mock('../../callId.js')
jest.mock('../../iceServers.js')
import Input from '../Input.js'
import React from 'react'
import ReactDOM from 'react-dom'

View File

@ -1,3 +0,0 @@
export default JSON.parse(
window.document.getElementById('iceServers').value
)

View File

@ -1,6 +1,4 @@
jest.mock('../../callId.js')
jest.mock('../../iceServers.js')
jest.mock('../../window/createObjectURL.js')
jest.mock('../../window.js')
import * as StreamActions from '../../actions/StreamActions.js'
import { applyMiddleware, createStore } from 'redux'

View File

@ -1,6 +1,6 @@
import * as constants from '../constants.js'
import createObjectURL from '../window/createObjectURL'
import Immutable from 'seamless-immutable'
import { createObjectURL } from '../window.js'
const defaultState = Immutable({})

40
src/client/window.js Normal file
View File

@ -0,0 +1,40 @@
import Promise from 'bluebird'
import _debug from 'debug'
const debug = _debug('peercalls')
export function getUserMedia (constraints) {
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
return navigator.mediaDevices.getUserMedia(constraints)
}
return new Promise((resolve, reject) => {
const getMedia = navigator.getUserMedia || navigator.webkitGetUserMedia
if (!getMedia) reject(new Error('Browser unsupported'))
getMedia.call(navigator, constraints, resolve, reject)
})
}
export const createObjectURL = object => window.URL.createObjectURL(object)
export const navigator = window.navigator
export function play () {
let videos = window.document.querySelectorAll('video')
Array.prototype.forEach.call(videos, (video, index) => {
debug('playing video: %s', index)
try {
video.play()
} catch (e) {
debug('error playing video: %s', e.name)
}
})
}
export const valueOf = id => {
const el = window.document.getElementById(id)
return el && el.value
}
export const callId = valueOf('callId')
export const iceServers = JSON.parse(valueOf('iceServers'))

View File

@ -1 +0,0 @@
export default object => 'blob://' + String(object)

View File

@ -1,12 +0,0 @@
import Promise from 'bluebird'
class MediaStream {}
let shouldFail
export const fail = _fail => shouldFail = !!_fail
export const stream = new MediaStream()
export default function getUserMedia () {
return !shouldFail
? Promise.resolve(stream)
: Promise.reject(new Error('test'))
}

View File

@ -1 +0,0 @@
export default object => window.URL.createObjectURL(object)

View File

@ -1,14 +0,0 @@
import Promise from 'bluebird'
import navigator from './navigator.js'
export default function getUserMedia (constraints) {
if (navigator.mediaDevices && navigator.mediaDevices.getUserMedia) {
return navigator.mediaDevices.getUserMedia(constraints)
}
return new Promise((resolve, reject) => {
const getMedia = navigator.getUserMedia || navigator.webkitGetUserMedia
if (!getMedia) reject(new Error('Browser unsupported'))
getMedia.call(navigator, constraints, resolve, reject)
})
}

View File

@ -1 +0,0 @@
export default window.navigator

View File

@ -1,13 +0,0 @@
const debug = require('debug')('peercalls')
export function play () {
let videos = window.document.querySelectorAll('video')
Array.prototype.forEach.call(videos, (video, index) => {
debug('playing video: %s', index)
try {
video.play()
} catch (e) {
debug('error playing video: %s', e.name)
}
})
}