'Typescript Unit Testing using jest enzym
I am implementing unit testing in typescript class file which is used in react js project but but when checking coverage it's just 16 %.is it possible to implement UT for normal ts class file and how?
i implemented as below:
` it('should call CameraOn', async () => {
spy = jest.spyOn(document, 'getElementById')
mockElement = document.createElement('div')
spy.mockReturnValue(mockElement)
vidContainer = document
.createElement('div')
.setAttribute('id', 'video-container')
const Mock = jest.fn(() => ({
cameraOn: jest.fn()
}))
const mock = new Mock()
try {
const deviceList: MediaDeviceInfo[] =
await deviceController.listVideoInputDevices()
//Choose video device
await deviceController.chooseVideoInputDevice(deviceList[0].deviceId)
deviceController.startVideoPreviewForVideoInput(
vidElement as HTMLVideoElement
)
if (meetingSession) {
meetingSession.audioVideo.startLocalVideoTile()
}
localVideoElements[0] = vidElement as HTMLElement
const isVideoOn = true
let vidContainer = videoContainer();
vidContainer.innerHTML = ''
vidContainer.appendChild(localVideoElements[0])
store.dispatch(updateSession({}))
} catch (err) {
}
chimeAVFn.cameraOn()
expect(chimeAVFn.cameraOn).toBeDefined()
})`
ts class file as below:
import Chime from 'aws-sdk/clients/chime';
import {
VideoTileState,
ConsoleLogger,
DefaultDeviceController,
DefaultMeetingSession,
MeetingSessionConfiguration,
AudioVideoObserver,
MeetingSession,
ContentShareObserver,
DefaultModality,
MeetingSessionStatusCode,
DefaultActiveSpeakerPolicy,
LogLevel,
} from 'amazon-chime-sdk-js'
import {
PlatformType,
CommonAV,
ParticipantVideo,
CallState
} from './common-av';
import {
setScreenPresesnt,
updateSession,
setVideo,
setCallState
} from '../store/actions/user-activity/user-activity.action';
import store from '../store'
type ParticipantData = {
[key: string]: { videoElement: HTMLElement | undefined }
}
export default class ChimeAV implements CommonAV {
platform: PlatformType = 'AWS'
chime!: Chime;
localVideoElements: HTMLElement[]
participantVideoElements!: HTMLVideoElement[]
logger!: ConsoleLogger
deviceController!: DefaultDeviceController
configuration!: MeetingSessionConfiguration
isMicOn: boolean
isVideoOn: boolean
error: string[]
participantsData: ParticipantData
meetingSession!: MeetingSession
indexMap: { [key: number]: number }
videoElements: HTMLVideoElement[]
callState: CallState
userMap: {
[key: string]: { userId: string; userName: string; active: boolean }
}
userList: any
isScreenShareOn: boolean
callAuth: () => any
vidElement: HTMLVideoElement;
videoElement:()=>any;
videoContainer:()=>any;
constructor(callAuth: () => any,vidElement: HTMLVideoElement,videoElement:()=>any,videoContainer:()=>any) {
this.callAuth = callAuth
this.videoContainer = videoContainer;
this.vidElement = vidElement;
this.videoElement = videoElement;
this.error = []
this.localVideoElements = []
this.participantsData = {}
this.videoElements = []
this.indexMap = {}
const MAX_VIDEO_TILES = 16
this.isVideoOn = false
for (var i = 0; i < MAX_VIDEO_TILES; i++) {
this.videoElements.push(this.videoElement())
}
this.isMicOn = false
this.userMap = {}
this.userList = []
this.isScreenShareOn = false
this.callState = 'NOT STARTED'
}
auth = async (): Promise<MeetingSessionConfiguration | undefined> => {
// You need responses from server-side Chime API. See below for details.
try {
let { meetingResponse, attendeeResponse } = await this.callAuth()
let configuration = new MeetingSessionConfiguration(
meetingResponse,
attendeeResponse
)
return configuration
} catch (err) {}
}
joinMeeting = async () => {
this.callState = 'CONNECTING'
let configuration = await this.auth()
this.logger = new ConsoleLogger('MyLogger', LogLevel.OFF)
this.deviceController = new DefaultDeviceController(this.logger)
this.configuration = configuration as MeetingSessionConfiguration
const meetingSession = new DefaultMeetingSession(
this.configuration,
this.logger,
this.deviceController
)
this.meetingSession = meetingSession
/* HTMLAudioElement object e.g. document.getElementById('audio-element-id') */
const audioElement = document.getElementById(
'chime-audio'
) as HTMLAudioElement
if (!audioElement) {
}
meetingSession.audioVideo.bindAudioElement(audioElement)
this.muteSelf()
// Select camera, mic and speaker
const audioInputDevices =
await meetingSession.audioVideo.listAudioInputDevices()
const audioOutputDevices =
await meetingSession.audioVideo.listAudioOutputDevices()
/* An array item from meetingSession.audioVideo.listAudioInputDevices */
try {
const audioInputDeviceInfo = audioInputDevices[0]
await meetingSession.audioVideo.chooseAudioInputDevice(
audioInputDeviceInfo.deviceId
)
} catch (err) {}
/* An array item from meetingSession.audioVideo.listAudioOutputDevices */ try {
const audioOutputDeviceInfo = audioOutputDevices[0]
await meetingSession.audioVideo.chooseAudioOutputDevice(
audioOutputDeviceInfo.deviceId
)
} catch (err) {}
// The camera LED light will turn on indicating that it is now capturing.
}
cameraOn = async () => {
try {
const deviceList: MediaDeviceInfo[] =
await this.deviceController.listVideoInputDevices()
//Choose video device
await this.deviceController.chooseVideoInputDevice(deviceList[0].deviceId)
this.deviceController.startVideoPreviewForVideoInput(
this.vidElement as HTMLVideoElement
)
if (this.meetingSession) {
this.meetingSession.audioVideo.startLocalVideoTile()
}
} catch (err) {
}
}
}
help me with jest unit test implement i refered this official jest documentation https://jestjs.io/docs/es6-class-mocks
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
