Skip to content

Commit

Permalink
feat: add custom method toJSON for _LocalStream (#70)
Browse files Browse the repository at this point in the history
* feat: add custom toJSON for _LocalStream
---------

Co-authored-by: Anna Tsukanova <[email protected]>
  • Loading branch information
antsukanova and Anna Tsukanova authored Jan 29, 2024
1 parent daae765 commit 6ac5ba1
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/media/local-stream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}

Expand Down Expand Up @@ -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');
});
});
});
31 changes: 31 additions & 0 deletions src/media/local-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down

0 comments on commit 6ac5ba1

Please sign in to comment.