-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1251 from nukeop/fix/881
Allow importing more than 1000 last.fm tracks
- Loading branch information
Showing
11 changed files
with
256 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 109 additions & 8 deletions
117
packages/app/app/containers/SettingsContainer/SettingsContainer.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,121 @@ | ||
import { buildStoreState } from '../../../test/storeBuilders'; | ||
import { mountedComponentFactory, setupI18Next } from '../../../test/testUtils'; | ||
/* eslint-disable @typescript-eslint/no-var-requires */ | ||
/* eslint-disable @typescript-eslint/ban-ts-comment */ | ||
import { waitFor } from '@testing-library/react';import fetchMock from 'fetch-mock'; | ||
import { store as electronStore } from '@nuclear/core'; | ||
|
||
import { buildElectronStoreState, buildStoreState } from '../../../test/storeBuilders'; | ||
import { AnyProps, mountedComponentFactory, setupI18Next } from '../../../test/testUtils'; | ||
import { range } from 'lodash'; | ||
|
||
jest.mock('../../globals', () => ({ | ||
lastfmApiKey: 'last_fm_key', | ||
lastfmApiSecret: 'last_fm_secret' | ||
})); | ||
|
||
describe('Settings view container', () => { | ||
beforeAll(() => { | ||
setupI18Next(); | ||
}); | ||
|
||
beforeEach(() => { | ||
fetchMock.reset(); | ||
electronStore.clear(); | ||
process.env.LAST_FM_API_KEY = 'last_fm_key'; | ||
process.env.LAST_FM_API_SECRET = 'last_fm_secret'; | ||
}); | ||
|
||
it('should render settings', () => { | ||
const { component } = mountComponent(); | ||
expect(component.asFragment()).toMatchSnapshot(); | ||
}); | ||
|
||
const mountComponent = mountedComponentFactory( | ||
['/settings'], | ||
buildStoreState() | ||
.withConnectivity() | ||
.build() | ||
); | ||
it('should import last.fm favorites (less than 1000)', async () => { | ||
const coreMock = require('@nuclear/core'); | ||
const LastFmApi = jest.requireActual('@nuclear/core/src/rest/Lastfm'); | ||
coreMock.rest.LastFmApi.mockImplementation( | ||
(key, secret) => | ||
new LastFmApi.default(key, secret) | ||
); | ||
const totalTracks = 3; | ||
const tracksFromLastfm = createTracksFromLastfm(totalTracks); | ||
|
||
withNumberOfTracks(totalTracks); | ||
withLovedTracks(3, 1, totalTracks, tracksFromLastfm); | ||
|
||
const { component, store } = mountComponent({ | ||
lastFm: { | ||
lastFmName: 'nuclear' | ||
} | ||
}); | ||
await waitFor(() => component.getByText('Import').click()); | ||
const state = store.getState(); | ||
|
||
expect(state.favorites.tracks).toEqual(tracksFromLastfm); | ||
expect(electronStore.get('favorites')).toEqual(expect.objectContaining({ | ||
tracks: tracksFromLastfm | ||
})); | ||
}); | ||
|
||
it('should import last.fm favorites (more than 1000)', async () => { | ||
const coreMock = require('@nuclear/core'); | ||
const LastFmApi = jest.requireActual('@nuclear/core/src/rest/Lastfm'); | ||
coreMock.rest.LastFmApi.mockImplementation( | ||
(key, secret) => | ||
new LastFmApi.default(key, secret) | ||
); | ||
const totalTracks = 4500; | ||
const tracksFromLastfm = createTracksFromLastfm(totalTracks); | ||
|
||
withNumberOfTracks(totalTracks); | ||
withLovedTracks(1000, 1, totalTracks, tracksFromLastfm.slice(0, 1000)); | ||
withLovedTracks(1000, 2, totalTracks, tracksFromLastfm.slice(1000, 2000)); | ||
withLovedTracks(1000, 3, totalTracks, tracksFromLastfm.slice(2000, 3000)); | ||
withLovedTracks(1000, 4, totalTracks, tracksFromLastfm.slice(3000, 4000)); | ||
withLovedTracks(1000, 5, totalTracks, tracksFromLastfm.slice(4000, 4500)); | ||
|
||
const { component, store } = mountComponent({ | ||
lastFm: { | ||
lastFmName: 'nuclear' | ||
} | ||
}); | ||
await waitFor(() => component.getByText('Import').click()); | ||
const state = store.getState(); | ||
|
||
expect(state.favorites.tracks).toEqual(tracksFromLastfm); | ||
expect(electronStore.get('favorites')).toEqual(expect.objectContaining({ | ||
tracks: tracksFromLastfm | ||
})); | ||
}); | ||
|
||
const mountComponent = (electronStoreState?: AnyProps) => { | ||
// @ts-ignore | ||
electronStore.init({ | ||
...buildElectronStoreState(electronStoreState) | ||
}); | ||
|
||
return mountedComponentFactory( | ||
['/settings'], | ||
buildStoreState() | ||
.withConnectivity() | ||
.build() | ||
)(); | ||
}; | ||
|
||
const createTracksFromLastfm = (totalTracks: number) => range(totalTracks).map(num => ({ | ||
artist: `artist ${num}`, | ||
name: `track ${num}` | ||
})); | ||
|
||
const tracksApiResponse = (track: object[], totalTracks: number) => ({ | ||
lovedtracks: { | ||
track, | ||
'@attr': { | ||
total: totalTracks.toString() | ||
} | ||
} | ||
}); | ||
|
||
const withNumberOfTracks = (totalTracks: number) => fetchMock.post('https://ws.audioscrobbler.com/2.0/?method=user.getlovedtracks&user=nuclear&format=json&limit=1&page=1&api_key=last_fm_key', tracksApiResponse([], totalTracks)); | ||
|
||
const withLovedTracks = (limit: number, page: number, totalTracks: number, tracks: object[]) => fetchMock.post(`https://ws.audioscrobbler.com/2.0/?method=user.getlovedtracks&user=nuclear&format=json&limit=${limit}&page=${page}&api_key=last_fm_key`, tracksApiResponse(tracks, totalTracks)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
export const initialStoreState = () => ({ | ||
equalizer: { | ||
selected: 'Default' | ||
}, | ||
downloads: [], | ||
favorites: { | ||
albums: [], | ||
tracks: [] | ||
}, | ||
playlists: [] | ||
}); | ||
|
||
export type MockStore = ReturnType<typeof initialStoreState>; | ||
|
||
export const mockElectronStore = (mockStore: MockStore) => ({ | ||
init: (store: typeof mockStore) => mockStore = store, | ||
get: (key: string) => mockStore[key] || {}, | ||
set: (key: string, value: any) => { | ||
mockStore[key] = value; | ||
}, | ||
clear: () => mockStore = initialStoreState() | ||
}); |
Oops, something went wrong.