|
| 1 | +const mockGetBulkPlaylists = jest.fn() |
| 2 | +const mockGetPlaylist = jest.fn() |
| 3 | +const mockGetBulkTracks = jest.fn() |
| 4 | + |
| 5 | +jest.mock('@audius/sdk', () => ({ |
| 6 | + sdk: () => ({ |
| 7 | + playlists: { |
| 8 | + getBulkPlaylists: (...args) => mockGetBulkPlaylists(...args), |
| 9 | + getPlaylist: (...args) => mockGetPlaylist(...args) |
| 10 | + }, |
| 11 | + tracks: { |
| 12 | + getBulkTracks: (...args) => mockGetBulkTracks(...args) |
| 13 | + }, |
| 14 | + events: {} |
| 15 | + }) |
| 16 | +})) |
| 17 | + |
| 18 | +// Avoid loading amplitude-js (and its browser globals) at import time. |
| 19 | +jest.mock('../analytics/analytics', () => ({ |
| 20 | + recordListen: jest.fn() |
| 21 | +})) |
| 22 | + |
| 23 | +const { getCollectionByPermalink } = require('./BedtimeClient') |
| 24 | + |
| 25 | +describe('getCollectionByPermalink', () => { |
| 26 | + beforeEach(() => { |
| 27 | + mockGetBulkPlaylists.mockReset() |
| 28 | + mockGetPlaylist.mockReset() |
| 29 | + }) |
| 30 | + |
| 31 | + it('re-fetches the full collection by id so the track list is hydrated', async () => { |
| 32 | + // The bulk/permalink endpoint resolves metadata but returns an empty |
| 33 | + // `tracks` array (this is what produced the header-only embed bug). |
| 34 | + mockGetBulkPlaylists.mockResolvedValue({ |
| 35 | + data: [{ id: 'vjgoJWp', playlistName: 'Halleluyah', tracks: [] }] |
| 36 | + }) |
| 37 | + // Fetching by (hash) id hydrates the full track list. |
| 38 | + mockGetPlaylist.mockResolvedValue({ |
| 39 | + data: [ |
| 40 | + { |
| 41 | + id: 'vjgoJWp', |
| 42 | + playlistName: 'Halleluyah', |
| 43 | + tracks: [{ id: 't1' }, { id: 't2' }] |
| 44 | + } |
| 45 | + ] |
| 46 | + }) |
| 47 | + |
| 48 | + const collection = await getCollectionByPermalink( |
| 49 | + 'AcidRayn', |
| 50 | + 'halleluyah', |
| 51 | + 'playlist' |
| 52 | + ) |
| 53 | + |
| 54 | + expect(mockGetBulkPlaylists).toHaveBeenCalledWith({ |
| 55 | + permalink: ['/AcidRayn/playlist/halleluyah'] |
| 56 | + }) |
| 57 | + // Must follow up with a by-id fetch using the resolved hash id. |
| 58 | + expect(mockGetPlaylist).toHaveBeenCalledWith({ playlistId: 'vjgoJWp' }) |
| 59 | + expect(collection.tracks).toHaveLength(2) |
| 60 | + }) |
| 61 | + |
| 62 | + it('builds the album permalink with the album path segment', async () => { |
| 63 | + mockGetBulkPlaylists.mockResolvedValue({ |
| 64 | + data: [{ id: 'vjgoJWp', tracks: [] }] |
| 65 | + }) |
| 66 | + mockGetPlaylist.mockResolvedValue({ |
| 67 | + data: [{ id: 'vjgoJWp', tracks: [{ id: 't1' }] }] |
| 68 | + }) |
| 69 | + |
| 70 | + await getCollectionByPermalink('AcidRayn', 'iterations', 'album') |
| 71 | + |
| 72 | + expect(mockGetBulkPlaylists).toHaveBeenCalledWith({ |
| 73 | + permalink: ['/AcidRayn/album/iterations'] |
| 74 | + }) |
| 75 | + }) |
| 76 | + |
| 77 | + it('returns null without a follow-up fetch when the permalink does not resolve', async () => { |
| 78 | + mockGetBulkPlaylists.mockResolvedValue({ data: [] }) |
| 79 | + |
| 80 | + const collection = await getCollectionByPermalink( |
| 81 | + 'nobody', |
| 82 | + 'nope', |
| 83 | + 'playlist' |
| 84 | + ) |
| 85 | + |
| 86 | + expect(collection).toBeNull() |
| 87 | + expect(mockGetPlaylist).not.toHaveBeenCalled() |
| 88 | + }) |
| 89 | +}) |
0 commit comments