Skip to content

Commit

Permalink
test(webrtc-core): shouldNThrow param for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fnowakow committed Feb 13, 2024
1 parent dad425e commit da4cf0a
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/peer-connection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ describe('PeerConnection', () => {

describe('createOffer', () => {
let mockPc: MockedObjectDeep<RTCPeerConnectionStub>;
let shouldCreateOfferThrow = false;
let createOfferSpy: jest.SpyInstance;
const callback = jest.fn();
let pc: PeerConnection;
Expand All @@ -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);
Expand All @@ -278,6 +279,7 @@ describe('PeerConnection', () => {

it('should emit event when createOffer called', async () => {
expect.hasAssertions();
shouldCreateOfferThrow = false;
const options: RTCOfferOptions = {
iceRestart: true,
};
Expand All @@ -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);
Expand All @@ -298,6 +301,7 @@ describe('PeerConnection', () => {

describe('setLocalDescription', () => {
let mockPc: MockedObjectDeep<RTCPeerConnectionStub>;
let shouldSetLocalDescriptionThrow = false;
let setLocalDescriptionSpy: jest.SpyInstance;
const callback = jest.fn();
let pc: PeerConnection;
Expand All @@ -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();
Expand All @@ -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' })
Expand All @@ -341,6 +348,7 @@ describe('PeerConnection', () => {

it('should emit event when setLocalDescription called', async () => {
expect.hasAssertions();
shouldSetLocalDescriptionThrow = false;
const options = {
sdp: 'blah',
};
Expand All @@ -351,6 +359,7 @@ describe('PeerConnection', () => {

it('should not emit event when setLocalDescription failed', async () => {
expect.hasAssertions();
shouldSetLocalDescriptionThrow = true;
const options = {
sdp: 'reject',
};
Expand All @@ -363,16 +372,17 @@ describe('PeerConnection', () => {

describe('setRemoteDescription', () => {
let mockPc: MockedObjectDeep<RTCPeerConnectionStub>;
let shouldSetRemoteDescriptionThrow = false;
let setRemoteDescriptionSpy: jest.SpyInstance;
const callback = jest.fn();
let pc: 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();
Expand All @@ -387,6 +397,7 @@ describe('PeerConnection', () => {

it('should emit event when setRemoteDescription called', async () => {
expect.hasAssertions();
shouldSetRemoteDescriptionThrow = false;
const options = {
sdp: 'blah',
};
Expand All @@ -397,6 +408,7 @@ describe('PeerConnection', () => {

it('should not emit event when setRemoteDescription failed', async () => {
expect.hasAssertions();
shouldSetRemoteDescriptionThrow = true;
const options = {
sdp: 'reject',
};
Expand Down

0 comments on commit da4cf0a

Please sign in to comment.