From 6ac5ba1a4e156075717c6b31addaa4080e4d1999 Mon Sep 17 00:00:00 2001 From: Anna Tsukanova <38460776+antsukanova@users.noreply.github.com> Date: Mon, 29 Jan 2024 10:49:28 +0100 Subject: [PATCH] feat: add custom method toJSON for _LocalStream (#70) * feat: add custom toJSON for _LocalStream --------- Co-authored-by: Anna Tsukanova --- src/media/local-stream.spec.ts | 27 ++++++++++++++++++++++++++- src/media/local-stream.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/src/media/local-stream.spec.ts b/src/media/local-stream.spec.ts index 071b985..121cd24 100644 --- a/src/media/local-stream.spec.ts +++ b/src/media/local-stream.spec.ts @@ -4,7 +4,7 @@ import { LocalStream, LocalStreamEventNames, TrackEffect } from './local-stream' import { StreamEventNames } from './stream'; /** - * A dummy LocalStream implementation so we can instantiate it for testing. + * A dummy LocalStream implementation, so we can instantiate it for testing. */ class TestLocalStream extends LocalStream {} @@ -186,4 +186,29 @@ describe('LocalStream', () => { expect(emitSpy).toHaveBeenCalledTimes(0); }); }); + + describe('toJSON', () => { + it('should correctly serialize data', () => { + expect.assertions(1); + + const testLocalStream = new TestLocalStream(mockStream); + const jsonLocalStream = localStream.toJSON(); + const jsonTestLocalStream = testLocalStream.toJSON(); + + expect(JSON.stringify(jsonLocalStream)).toStrictEqual(JSON.stringify(jsonTestLocalStream)); + }); + + it('should return an object with inputStream, outputStream and effects properties', () => { + expect.assertions(6); + + const jsonLocalStream = localStream.toJSON(); + + expect(jsonLocalStream).toHaveProperty('muted'); + expect(jsonLocalStream).toHaveProperty('label'); + expect(jsonLocalStream).toHaveProperty('readyState'); + expect(jsonLocalStream).toHaveProperty('inputStream'); + expect(jsonLocalStream).toHaveProperty('outputStream'); + expect(jsonLocalStream).toHaveProperty('effects'); + }); + }); }); diff --git a/src/media/local-stream.ts b/src/media/local-stream.ts index 62d7526..a9b25fd 100644 --- a/src/media/local-stream.ts +++ b/src/media/local-stream.ts @@ -283,6 +283,37 @@ abstract class _LocalStream extends Stream { return this.effects; } + /** + * Method to serialize data about input, output streams + * and also effects from LocalStream. + * + * @returns - A JSON-compatible object representation with data from LocalStream. + */ + toJSON() { + return { + muted: this.muted, + label: this.label, + readyState: this.readyState, + inputStream: { + active: this.inputStream.active, + id: this.inputStream.id, + enabled: this.inputTrack.enabled, + muted: this.inputTrack.muted, + }, + outputStream: { + active: this.outputStream.active, + id: this.outputStream.id, + }, + effects: this.effects.map((effect) => { + return { + id: effect.id, + kind: effect.kind, + isEnabled: effect.isEnabled, + }; + }), + }; + } + /** * Cleanup the local effects. */