mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-19 15:23:36 +00:00
refactor(ui): point lint at useDirectDataProvider and simplify uploadImage args (#6482)
This commit is contained in:
parent
1f0238ce4b
commit
2ae6defbc4
@ -53,11 +53,11 @@
|
||||
{
|
||||
"group": ["**/dataProvider"],
|
||||
"importNames": ["httpClient"],
|
||||
"message": "Do not call httpClient directly. Add a method to wrapperDataProvider and call it through useDataProvider(), or add the Subsonic call to src/subsonic."
|
||||
"message": "Do not call httpClient directly. Add a method to wrapperDataProvider and call it through useDirectDataProvider(), or add the Subsonic call to src/subsonic."
|
||||
},
|
||||
{
|
||||
"group": ["**/dataProvider/httpClient"],
|
||||
"message": "Do not call httpClient directly. Add a method to wrapperDataProvider and call it through useDataProvider(), or add the Subsonic call to src/subsonic."
|
||||
"message": "Do not call httpClient directly. Add a method to wrapperDataProvider and call it through useDirectDataProvider(), or add the Subsonic call to src/subsonic."
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@ -64,7 +64,7 @@ export const ImageUploadOverlay = ({
|
||||
if (!file || !entityId) return
|
||||
|
||||
try {
|
||||
await dataProvider.uploadImage(entityType, { id: entityId, file })
|
||||
await dataProvider.uploadImage(entityType, entityId, file)
|
||||
notify(`message.coverUploaded`, 'success')
|
||||
if (onImageChange) onImageChange()
|
||||
refresh()
|
||||
|
||||
@ -46,8 +46,8 @@ describe('useDirectDataProvider', () => {
|
||||
const provider = { uploadImage: vi.fn().mockRejectedValue(error) }
|
||||
const { result } = renderWithOptimistic(provider, false)
|
||||
|
||||
await expect(
|
||||
result.current.uploadImage('playlist', { id: 'pl-1' }),
|
||||
).rejects.toBe(error)
|
||||
await expect(result.current.uploadImage('playlist', 'pl-1')).rejects.toBe(
|
||||
error,
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
@ -239,8 +239,7 @@ const wrapperDataProvider = {
|
||||
httpClient(`${REST_URL}/metadata/${REFRESH_KIND[resource]}/${id}/refresh`, {
|
||||
method: 'POST',
|
||||
}).then(() => ({ data: { id } })),
|
||||
// useDataProvider forwards only two arguments when the first is a string, so id and file share one.
|
||||
uploadImage: (resource, { id, file }) => {
|
||||
uploadImage: (resource, id, file) => {
|
||||
const formData = new FormData()
|
||||
formData.append('image', file)
|
||||
return fetchData(`${REST_URL}/${resource}/${id}/image`, {
|
||||
|
||||
@ -140,23 +140,13 @@ describe('wrapperDataProvider', () => {
|
||||
it('posts the file as multipart form data under the image field', async () => {
|
||||
const file = new File(['img'], 'cover.png', { type: 'image/png' })
|
||||
|
||||
await wrapperDataProvider.uploadImage('artist', { id: 'ar-1', file })
|
||||
await wrapperDataProvider.uploadImage('artist', 'ar-1', file)
|
||||
|
||||
const [url, options] = mockHttpClient.mock.calls[0]
|
||||
expect(url).toBe('/api/artist/ar-1/image')
|
||||
expect(options.method).toBe('POST')
|
||||
expect(options.body).toBeInstanceOf(FormData)
|
||||
expect(options.body.get('image')).toBe(file)
|
||||
// An empty Headers keeps httpClient from forcing a JSON content type over the multipart boundary
|
||||
expect([...options.headers.keys()]).toEqual([])
|
||||
})
|
||||
|
||||
it('deletes the uploaded image', async () => {
|
||||
await wrapperDataProvider.deleteImage('playlist', 'pl-1')
|
||||
|
||||
expect(mockHttpClient).toHaveBeenCalledWith('/api/playlist/pl-1/image', {
|
||||
method: 'DELETE',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@ -184,32 +174,6 @@ describe('wrapperDataProvider', () => {
|
||||
})
|
||||
|
||||
describe('scrobbler links', () => {
|
||||
it('reads the Last.fm link status', async () => {
|
||||
mockHttpClient.mockResolvedValue({ json: { status: true, apiKey: 'k' } })
|
||||
|
||||
const result = await wrapperDataProvider.getLastfmLink()
|
||||
|
||||
expect(mockHttpClient).toHaveBeenCalledWith('/api/lastfm/link')
|
||||
expect(result).toEqual({ data: { status: true, apiKey: 'k' } })
|
||||
})
|
||||
|
||||
it('unlinks Last.fm', async () => {
|
||||
await wrapperDataProvider.unlinkLastfm()
|
||||
|
||||
expect(mockHttpClient).toHaveBeenCalledWith('/api/lastfm/link', {
|
||||
method: 'DELETE',
|
||||
})
|
||||
})
|
||||
|
||||
it('reads the ListenBrainz link status', async () => {
|
||||
mockHttpClient.mockResolvedValue({ json: { status: false } })
|
||||
|
||||
const result = await wrapperDataProvider.getListenBrainzLink()
|
||||
|
||||
expect(mockHttpClient).toHaveBeenCalledWith('/api/listenbrainz/link')
|
||||
expect(result).toEqual({ data: { status: false } })
|
||||
})
|
||||
|
||||
it('links ListenBrainz with the given token', async () => {
|
||||
mockHttpClient.mockResolvedValue({ json: { user: 'sam' } })
|
||||
|
||||
@ -221,23 +185,5 @@ describe('wrapperDataProvider', () => {
|
||||
})
|
||||
expect(result).toEqual({ data: { user: 'sam' } })
|
||||
})
|
||||
|
||||
it('unlinks ListenBrainz', async () => {
|
||||
await wrapperDataProvider.unlinkListenBrainz()
|
||||
|
||||
expect(mockHttpClient).toHaveBeenCalledWith('/api/listenbrainz/link', {
|
||||
method: 'DELETE',
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('rescanPlugins', () => {
|
||||
it('posts to the plugin rescan endpoint', async () => {
|
||||
await wrapperDataProvider.rescanPlugins()
|
||||
|
||||
expect(mockHttpClient).toHaveBeenCalledWith('/api/plugin/rescan', {
|
||||
method: 'POST',
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user