Fix media.test.ts

This commit is contained in:
Jerko Steiner 2020-03-10 13:38:27 +01:00
parent 80ac818616
commit 0ae475f898
5 changed files with 25 additions and 16 deletions

View File

@ -6,6 +6,10 @@ const Peer = jest.fn().mockImplementation(() => {
(peer as any).destroy = jest.fn();
(peer as any).signal = jest.fn();
(peer as any).send = jest.fn();
(peer as any).addTrack = jest.fn();
(peer as any).removeTrack = jest.fn();
(peer as any).addStream = jest.fn();
(peer as any).removeStream = jest.fn();
(Peer as any).instances.push(peer)
return peer
});

View File

@ -1,3 +1,5 @@
jest.mock('simple-peer')
import * as MediaActions from '../actions/MediaActions'
import { MEDIA_ENUMERATE, MEDIA_VIDEO_CONSTRAINT_SET, MEDIA_AUDIO_CONSTRAINT_SET, MEDIA_STREAM, ME, PEERS_DESTROY, PEER_ADD, STREAM_TYPE_CAMERA, STREAM_TYPE_DESKTOP } from '../constants'
import { createStore, Store } from '../store'
@ -89,7 +91,12 @@ describe('media', () => {
})
describe(MEDIA_STREAM, () => {
const stream: MediaStream = {} as MediaStream
const track: MediaStreamTrack = {} as unknown as MediaStreamTrack
const stream: MediaStream = {
getTracks() {
return [track]
},
} as MediaStream
describe('using navigator.mediaDevices.getUserMedia', () => {
beforeEach(() => {
@ -120,11 +127,7 @@ describe('media', () => {
describe('reducers/peers', () => {
const peer1 = new SimplePeer()
peer1.addStream = jest.fn()
peer1.removeStream = jest.fn()
const peer2 = new SimplePeer()
peer2.addStream = jest.fn()
peer2.removeStream = jest.fn()
const peers = [peer1, peer2]
beforeEach(() => {
@ -153,19 +156,19 @@ describe('media', () => {
})
})
it('replaces local camera stream on all peers', async () => {
it('adds local camera stream to all peers', async () => {
await dispatch()
peers.forEach(peer => {
expect((peer.addStream as jest.Mock).mock.calls)
.toEqual([[ stream ]])
expect((peer.removeStream as jest.Mock).mock.calls).toEqual([])
expect((peer.addTrack as jest.Mock).mock.calls)
.toEqual([[ track, stream ]])
expect((peer.removeTrack as any).mock.calls).toEqual([])
})
await dispatch()
peers.forEach(peer => {
expect((peer.addStream as jest.Mock).mock.calls)
.toEqual([[ stream ], [ stream ]])
expect((peer.removeStream as jest.Mock).mock.calls)
.toEqual([[ stream ]])
expect((peer.addTrack as jest.Mock).mock.calls)
.toEqual([[ track, stream ], [ track, stream ]])
expect((peer.removeTrack as jest.Mock).mock.calls)
.toEqual([[ track, stream ]])
})
})
})

View File

@ -22,7 +22,6 @@ function handleRemoveStream(
const stream = action.payload.stream
if (action.payload.userId === constants.ME) {
forEach(state, peer => {
console.log('removing track from peer')
stream.getTracks().forEach(track => {
peer.removeTrack(track, stream)
})

View File

@ -65,4 +65,8 @@ describe('reducers/alerts', () => {
})
})
describe('removeStreamTrack', () => {
})
})

View File

@ -43,7 +43,6 @@ function addStream (
}
if (userStreams.streams.map(s => s.stream).indexOf(stream) >= 0) {
console.log('stream already found')
return state
}
@ -80,7 +79,7 @@ function removeStream (
}
return !found
})
if (userStreams.streams.length > 0) {
if (streams.length > 0) {
return {
...state,
[userId]: {