Skip to content

Commit

Permalink
Skip missing album data for podcasts [Fixes watsonbox#105]
Browse files Browse the repository at this point in the history
  • Loading branch information
watsonbox committed Jul 15, 2022
1 parent 31362d7 commit ccccd35
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
45 changes: 44 additions & 1 deletion src/components/PlaylistTable.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import JSZip from "jszip"
import PlaylistTable from "./PlaylistTable"

import "../icons"
import { handlerCalled, handlers, nullTrackHandlers, localTrackHandlers, duplicateTrackHandlers } from "../mocks/handlers"
import { handlerCalled, handlers, nullAlbumHandlers, nullTrackHandlers, localTrackHandlers, duplicateTrackHandlers } from "../mocks/handlers"

const server = setupServer(...handlers)

Expand Down Expand Up @@ -228,6 +228,49 @@ describe("single playlist exporting", () => {
)
})

test("tracks without album data omit it", async () => {
server.use(...nullAlbumHandlers)

const saveAsMock = jest.spyOn(FileSaver, "saveAs")
saveAsMock.mockImplementation(jest.fn())

render(<PlaylistTable accessToken="TEST_ACCESS_TOKEN" config={{ includeAlbumData: true }} />);

expect(await screen.findByText(/Export All/)).toBeInTheDocument()

const linkElement = screen.getAllByText("Export")[0]

expect(linkElement).toBeInTheDocument()

userEvent.click(linkElement)

await waitFor(() => {
expect(handlerCalled.mock.calls).toEqual([ // Ensure API call order and no duplicates
[ 'https://api.spotify.com/v1/me' ],
[ 'https://api.spotify.com/v1/users/watsonbox/playlists?offset=0&limit=20' ],
[ 'https://api.spotify.com/v1/users/watsonbox/tracks' ],
[ 'https://api.spotify.com/v1/me/tracks?offset=0&limit=20' ],
[ 'https://api.spotify.com/v1/albums?ids=4iwv7b8gDPKztLkKCbWyhi' ]
])
})

await waitFor(() => {
expect(saveAsMock).toHaveBeenCalledTimes(1)
})

expect(saveAsMock).toHaveBeenCalledWith(
{
content: [
`${baseTrackHeaders},"Album Genres","Label","Copyrights"\n` +
`${baseTrackDataCrying},"","",""\n`
],
options: { type: 'text/csv;charset=utf-8' }
},
'liked.csv',
true
)
})

test("playlist with null track skips null track", async () => {
server.use(...nullTrackHandlers)

Expand Down
8 changes: 4 additions & 4 deletions src/components/data/TracksAlbumData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class TracksAlbumData extends TracksData {
const albumDataById = new Map<string, string[]>(
albumResponses.flatMap((response) => response.data.albums.map((album: any) => {
return [
album.id,
album == null ? "" : album.id,
[
album.genres.join(", "),
album.label,
album.copyrights.map((c: any) => `${c.type} ${c.text}`).join(", ")
album == null ? "" : album.genres.join(", "),
album == null ? "" : album.label,
album == null ? "" : album.copyrights.map((c: any) => `${c.type} ${c.text}`).join(", ")
]
]
}))
Expand Down
16 changes: 16 additions & 0 deletions src/mocks/handlers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,22 @@ export const handlers = [
})
]

export const nullAlbumHandlers = [
rest.get('https://api.spotify.com/v1/albums?ids=4iwv7b8gDPKztLkKCbWyhi', (req, res, ctx) => {
handlerCalled(req.url.toString())

if (req.headers.get("Authorization") !== "Bearer TEST_ACCESS_TOKEN") {
return res(ctx.status(401), ctx.json({ message: 'Not authorized' }))
}

return res(ctx.json(
{
"albums" : [ null ]
}
))
})
]

export const nullTrackHandlers = [
rest.get('https://api.spotify.com/v1/playlists/4XOGDpHMrVoH33uJEwHWU5/tracks?offset=0&limit=10', (req, res, ctx) => {
handlerCalled(req.url.toString())
Expand Down

0 comments on commit ccccd35

Please sign in to comment.