From da4cf0ab566fad6fc814e00ecb4ef744d7e6458e Mon Sep 17 00:00:00 2001 From: Filip Nowakowski Date: Tue, 13 Feb 2024 16:47:49 +0100 Subject: [PATCH] test(webrtc-core): shouldNThrow param for tests --- src/peer-connection.spec.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/peer-connection.spec.ts b/src/peer-connection.spec.ts index 64f1937..2703037 100644 --- a/src/peer-connection.spec.ts +++ b/src/peer-connection.spec.ts @@ -250,6 +250,7 @@ describe('PeerConnection', () => { describe('createOffer', () => { let mockPc: MockedObjectDeep; + let shouldCreateOfferThrow = false; let createOfferSpy: jest.SpyInstance; const callback = jest.fn(); let pc: PeerConnection; @@ -261,9 +262,9 @@ describe('PeerConnection', () => { beforeEach(() => { jest.clearAllMocks(); mockPc = mocked(new RTCPeerConnectionStub(), true); - mockPc.createOffer.mockImplementation((description) => { + mockPc.createOffer.mockImplementation(() => { return new Promise((resolve, reject) => { - if (!description) { + if (shouldCreateOfferThrow) { reject(new Error()); } else { resolve(mockedReturnedOffer); @@ -278,6 +279,7 @@ describe('PeerConnection', () => { it('should emit event when createOffer called', async () => { expect.hasAssertions(); + shouldCreateOfferThrow = false; const options: RTCOfferOptions = { iceRestart: true, }; @@ -289,6 +291,7 @@ describe('PeerConnection', () => { it('should not emit event when createOffer failed', async () => { expect.hasAssertions(); + shouldCreateOfferThrow = true; const offerPromise = pc.createOffer(null as unknown as RTCOfferOptions); await expect(offerPromise).rejects.toThrow(Error); expect(createOfferSpy).toHaveBeenCalledWith(null); @@ -298,6 +301,7 @@ describe('PeerConnection', () => { describe('setLocalDescription', () => { let mockPc: MockedObjectDeep; + let shouldSetLocalDescriptionThrow = false; let setLocalDescriptionSpy: jest.SpyInstance; const callback = jest.fn(); let pc: PeerConnection; @@ -306,9 +310,9 @@ describe('PeerConnection', () => { jest.clearAllMocks(); mockPc = mocked(new RTCPeerConnectionStub(), true); mockCreateRTCPeerConnection.mockReturnValueOnce(mockPc as unknown as RTCPeerConnection); - mockPc.setLocalDescription.mockImplementation((description) => { + mockPc.setLocalDescription.mockImplementation(() => { return new Promise((resolve, reject) => { - if (description?.sdp === 'reject') { + if (shouldSetLocalDescriptionThrow) { reject(new Error()); } else { resolve(); @@ -322,17 +326,20 @@ describe('PeerConnection', () => { it('sets the local description with an SDP offer', async () => { expect.hasAssertions(); + shouldSetLocalDescriptionThrow = false; const description = { type: 'offer', sdp: 'fake sdp' } as RTCSessionDescriptionInit; pc.setLocalDescription(description); expect(setLocalDescriptionSpy).toHaveBeenCalledWith(description); }); it('sets the local description with no SDP offer', async () => { expect.hasAssertions(); + shouldSetLocalDescriptionThrow = false; pc.setLocalDescription(); expect(setLocalDescriptionSpy).toHaveBeenCalledWith(undefined); }); it('throws an error when the SDP has an invalid media line on Firefox', async () => { expect.hasAssertions(); + shouldSetLocalDescriptionThrow = false; jest.spyOn(BrowserInfo, 'isFirefox').mockReturnValue(true); await expect( pc.setLocalDescription({ type: 'offer', sdp: 'm=video 9 UDP/TLS/RTP' }) @@ -341,6 +348,7 @@ describe('PeerConnection', () => { it('should emit event when setLocalDescription called', async () => { expect.hasAssertions(); + shouldSetLocalDescriptionThrow = false; const options = { sdp: 'blah', }; @@ -351,6 +359,7 @@ describe('PeerConnection', () => { it('should not emit event when setLocalDescription failed', async () => { expect.hasAssertions(); + shouldSetLocalDescriptionThrow = true; const options = { sdp: 'reject', }; @@ -363,6 +372,7 @@ describe('PeerConnection', () => { describe('setRemoteDescription', () => { let mockPc: MockedObjectDeep; + let shouldSetRemoteDescriptionThrow = false; let setRemoteDescriptionSpy: jest.SpyInstance; const callback = jest.fn(); let pc: PeerConnection; @@ -370,9 +380,9 @@ describe('PeerConnection', () => { beforeEach(() => { jest.clearAllMocks(); mockPc = mocked(new RTCPeerConnectionStub(), true); - mockPc.setRemoteDescription.mockImplementation((description) => { + mockPc.setRemoteDescription.mockImplementation(() => { return new Promise((resolve, reject) => { - if (description?.sdp === 'reject') { + if (shouldSetRemoteDescriptionThrow) { reject(new Error()); } else { resolve(); @@ -387,6 +397,7 @@ describe('PeerConnection', () => { it('should emit event when setRemoteDescription called', async () => { expect.hasAssertions(); + shouldSetRemoteDescriptionThrow = false; const options = { sdp: 'blah', }; @@ -397,6 +408,7 @@ describe('PeerConnection', () => { it('should not emit event when setRemoteDescription failed', async () => { expect.hasAssertions(); + shouldSetRemoteDescriptionThrow = true; const options = { sdp: 'reject', };