Skip to content

Commit

Permalink
test(frontend): add tests for playback controller playback functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedevnull committed May 15, 2022
1 parent 5f621bf commit 1dfc881
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
71 changes: 71 additions & 0 deletions frontend/src/upnpapi/playback_control.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,75 @@ describe("PlaybackControl", () => {
});
});
});

describe("playback controls if player present", () => {
let control: PlaybackControl;
const fakePlayerId = "1234";
beforeEach(() => {
control = new PlaybackControl(eventBus);
eventBus.triggerStateChange("connected");
control.selectedPlayerId = fakePlayerId;
});

it("stops playback and starts playing new queue items", async () => {
await control.playItemsImmediatly(["a", "b", "c"]);
expect(api.stop).toHaveBeenCalledWith(fakePlayerId);
expect(api.setPlaybackQueue).toHaveBeenCalledWith(fakePlayerId, [
"a",
"b",
"c",
]);
expect(api.play).toHaveBeenCalledWith(fakePlayerId);
});

it("changes the volume", async () => {
control.setVolume(23);
expect(api.setVolume).toHaveBeenCalledWith(fakePlayerId, 23);
});

it("toggles play/pause", async () => {
control.playbackInfo.transport = "STOPPED";
expect(control.playbackInfo.transport).toBe("STOPPED");

await control.playPause();
expect(api.play).toHaveBeenCalledWith(fakePlayerId);
expect(control.playbackInfo.transport).toBe("PLAYING");

await control.playPause();
expect(api.stop).toHaveBeenCalledWith(fakePlayerId);
expect(control.playbackInfo.transport).toBe("STOPPED");
});
});

describe("playback controls without player present", () => {
let control: PlaybackControl;
const fakePlayerId = "is-not-present";
beforeEach(() => {
control = new PlaybackControl(eventBus);
eventBus.triggerStateChange("connected");
control.selectedPlayerId = fakePlayerId;
});

it("stops playback and starts playing new queue items", async () => {
await control.playItemsImmediatly(["a", "b", "c"]);
expect(api.stop).not.toHaveBeenCalled();
expect(api.setPlaybackQueue).not.toHaveBeenCalled();

expect(api.play).not.toHaveBeenCalled();
});

it("changes the volume", async () => {
control.setVolume(23);
expect(api.setVolume).not.toHaveBeenCalled();
});

it("toggles play/pause", async () => {
control.playbackInfo.transport = "STOPPED";
expect(control.playbackInfo.transport).toBe("STOPPED");

await control.playPause();
expect(api.play).not.toHaveBeenCalled();
expect(api.stop).not.toHaveBeenCalled();
});
});
});
6 changes: 3 additions & 3 deletions frontend/src/upnpapi/playback_control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export default class PlaybackControl extends EventEmitter<PlaybackControlEvent>
}

async playItemsImmediatly(itemIds: string[]) {
if (this._selectedPlayerId === undefined) {
if (!this.isPlayerPresent || this._selectedPlayerId === undefined) {
return;
}
await api.stop(this._selectedPlayerId);
Expand All @@ -60,7 +60,7 @@ export default class PlaybackControl extends EventEmitter<PlaybackControlEvent>
}

async playPause() {
if (this._selectedPlayerId === undefined) {
if (!this.isPlayerPresent || this._selectedPlayerId === undefined) {
return;
}
if (this._playbackInfo.transport === "PLAYING") {
Expand All @@ -75,7 +75,7 @@ export default class PlaybackControl extends EventEmitter<PlaybackControlEvent>
}

setVolume(volume: number) {
if (this._selectedPlayerId === undefined) {
if (!this.isPlayerPresent || this._selectedPlayerId === undefined) {
return;
}
if (this._playbackInfo.volumePercent != volume) {
Expand Down

0 comments on commit 1dfc881

Please sign in to comment.