Skip to content

Commit

Permalink
feat(webrtc-core): create answer on success event
Browse files Browse the repository at this point in the history
  • Loading branch information
fnowakow committed Feb 14, 2024
1 parent 8751e4f commit 7ee2292
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"webex",
"webrtc",
"createofferonsuccess",
"createansweronsuccess",
"setlocaldescriptiononsuccess",
"setremotedescriptiononsuccess"
],
Expand Down
3 changes: 3 additions & 0 deletions src/mocks/rtc-peer-connection-stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
* This stub exists to act as a scaffold for creating a mock.
*/
class RTCPeerConnectionStub {
createAnswer(options?: RTCAnswerOptions): Promise<RTCSessionDescriptionInit> {
return new Promise(() => {});
}
createOffer(options?: RTCOfferOptions): Promise<RTCSessionDescriptionInit> {
return new Promise(() => {});
}
Expand Down
44 changes: 44 additions & 0 deletions src/peer-connection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,50 @@ describe('PeerConnection', () => {
connectionStateHandlerListener(ConnectionState.Connecting);
});
});
describe('createAnswer', () => {
let mockPc: MockedObjectDeep<RTCPeerConnectionStub>;
let createAnswerSpy: jest.SpyInstance;
const callback = jest.fn();
let pc: PeerConnection;
const mockedReturnedAnswer: RTCSessionDescriptionInit = {
sdp: 'blah',
type: 'offer',
};

beforeEach(() => {
jest.clearAllMocks();
mockPc = mocked(new RTCPeerConnectionStub(), true);
mockPc.createAnswer.mockImplementation(() => {
return Promise.resolve(mockedReturnedAnswer);
});
mockCreateRTCPeerConnection.mockReturnValueOnce(mockPc as unknown as RTCPeerConnection);
pc = new PeerConnection();
createAnswerSpy = jest.spyOn(pc, 'createAnswer');
pc.on(PeerConnection.Events.CreateAnswerOnSuccess, callback);
});

it('should emit event when createAnswer called', async () => {
expect.hasAssertions();
const options: RTCAnswerOptions = {
iceRestart: true,
};
const answer = await pc.createAnswer(options);
expect(answer).toStrictEqual(mockedReturnedAnswer);
expect(createAnswerSpy).toHaveBeenCalledWith(options);
expect(callback).toHaveBeenCalledWith(mockedReturnedAnswer);
});

it('should not emit event when createAnswer failed', async () => {
expect.hasAssertions();
mockPc.createAnswer.mockImplementation(() => {
return Promise.reject(new Error());
});
const answerPromise = pc.createAnswer(null as unknown as RTCOfferOptions);
await expect(answerPromise).rejects.toThrow(Error);
expect(createAnswerSpy).toHaveBeenCalledWith(null);
expect(callback).toHaveBeenCalledTimes(0);
});
});

describe('createOffer', () => {
let mockPc: MockedObjectDeep<RTCPeerConnectionStub>;
Expand Down
7 changes: 6 additions & 1 deletion src/peer-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ enum PeerConnectionEvents {
IceGatheringStateChange = 'icegatheringstatechange',
ConnectionStateChange = 'connectionstatechange',
CreateOfferOnSuccess = 'createofferonsuccess',
CreateAnswerOnSuccess = 'createansweronsuccess',
SetLocalDescriptionOnSuccess = 'setlocaldescriptiononsuccess',
SetRemoteDescriptionOnSuccess = 'setremotedescriptiononsuccess',
}
Expand All @@ -37,6 +38,7 @@ interface PeerConnectionEventHandlers extends EventMap {
[PeerConnectionEvents.IceGatheringStateChange]: (ev: IceGatheringStateChangeEvent) => void;
[PeerConnectionEvents.ConnectionStateChange]: (state: ConnectionState) => void;
[PeerConnectionEvents.CreateOfferOnSuccess]: (offer: RTCSessionDescriptionInit) => void;
[PeerConnectionEvents.CreateAnswerOnSuccess]: (answer: RTCSessionDescriptionInit) => void;
[PeerConnectionEvents.SetLocalDescriptionOnSuccess]: (
description: RTCSessionDescription | RTCSessionDescriptionInit
) => void;
Expand Down Expand Up @@ -183,7 +185,10 @@ class PeerConnection extends EventEmitter<PeerConnectionEventHandlers> {
* other peer.
*/
async createAnswer(options?: RTCAnswerOptions): Promise<RTCSessionDescriptionInit> {
return this.pc.createAnswer(options);
return this.pc.createAnswer(options).then((answer) => {
this.emit(PeerConnection.Events.CreateAnswerOnSuccess, answer);
return answer;
});
}

/**
Expand Down

0 comments on commit 7ee2292

Please sign in to comment.