Skip to content

Commit

Permalink
Add tests for functions related to "currentListen"
Browse files Browse the repository at this point in the history
  • Loading branch information
ishaanshah committed Apr 20, 2020
1 parent c2b3e26 commit 8631861
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
45 changes: 43 additions & 2 deletions listenbrainz/webserver/static/js/src/RecentListens.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,50 @@ describe("receiveNewListen", () => {});

describe("receiveNewPlayingNow", () => {});

describe("handleCurrentListenChange", () => {});
describe("handleCurrentListenChange", () => {
it("sets the state correctly", () => {
const wrapper = shallow<RecentListens>(<RecentListens {...props} />);
const instance = wrapper.instance();

const listen: Listen = {
listened_at: 0,
track_metadata: {
artist_name: "George Erza",
track_name: "Shotgun",
},
};
instance.handleCurrentListenChange(listen);

expect(wrapper.state().currentListen).toMatchObject(listen);
});
});

describe("isCurrentListen", () => {
it("returns true if currentListen and passed listen is same", () => {
const wrapper = shallow<RecentListens>(<RecentListens {...props} />);
const instance = wrapper.instance();

describe("isCurrentListen", () => {});
const listen: Listen = {
listened_at: 0,
track_metadata: {
artist_name: "Coldplay",
track_name: "Up & Up",
},
};
wrapper.setState({ currentListen: listen });

expect(instance.isCurrentListen(listen)).toBe(true);
});

it("returns false if currentListen is not set", () => {
const wrapper = shallow<RecentListens>(<RecentListens {...props} />);
const instance = wrapper.instance();

wrapper.setState({ currentListen: undefined });

expect(instance.isCurrentListen({} as Listen)).toBeFalsy();
});
});

describe("getRecentListensForFollowList", () => {});

Expand Down
5 changes: 2 additions & 3 deletions listenbrainz/webserver/static/js/src/RecentListens.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface RecentListensProps {
export interface RecentListensState {
alerts: Array<Alert>;
canPlayMusic?: boolean;
currentListen: Listen;
currentListen?: Listen;
direction: SpotifyPlayDirection;
followList: Array<string>;
listId?: number;
Expand All @@ -72,7 +72,6 @@ export default class RecentListens extends React.Component<
this.state = {
alerts: [],
listens: props.listens || [],
currentListen: {} as Listen,
mode: props.mode,
followList: props.followList || [],
playingNowByUser: {},
Expand Down Expand Up @@ -227,7 +226,7 @@ export default class RecentListens extends React.Component<

isCurrentListen = (listen: Listen): boolean => {
const { currentListen } = this.state;
return currentListen && _.isEqual(listen, currentListen);
return Boolean(currentListen && _.isEqual(listen, currentListen));
};

getRecentListensForFollowList = async () => {
Expand Down

0 comments on commit 8631861

Please sign in to comment.