Skip to content

Commit

Permalink
Hotfix for undefined in Nuxt provider data (#3185)
Browse files Browse the repository at this point in the history
* Fix undefined in provider data

* Update unit test

* Add unit test
  • Loading branch information
obulat authored Oct 26, 2023
1 parent 7cb0b90 commit 23d4536
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
17 changes: 15 additions & 2 deletions frontend/src/data/media-provider-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AUDIO, IMAGE } from "~/constants/media"
import type { AudioDetail, ImageDetail, Media } from "~/types/media"
import { ApiService, createApiService } from "~/data/api-service"
import type { MediaProvider } from "~/types/media-provider"
import { warn } from "~/utils/console"

export class MediaProviderService<T extends Media> {
private readonly apiService: ApiService
Expand All @@ -16,8 +17,20 @@ export class MediaProviderService<T extends Media> {
* Implements an endpoint to get audio provider statistics.
* SSR-called
*/
async getProviderStats(): Promise<{ data: MediaProvider[] }> {
return await this.apiService.get(this.mediaType, "stats")
async getProviderStats(): Promise<MediaProvider[]> {
const res = await this.apiService.get(this.mediaType, "stats")
if (res.data && Array.isArray(res.data)) {
return res.data
} else {
warn(
`Invalid response from provider stats endpoint: ${JSON.stringify(
res,
null,
2
)}`
)
return []
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/stores/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export const useProviderStore = defineStore("provider", {
this.$nuxt?.$config?.apiAccessToken
)
const res = await service.getProviderStats()
sortedProviders = sortProviders(res.data)
sortedProviders = sortProviders(res)
this._updateFetchState(mediaType, "end")
} catch (error: unknown) {
const errorData = parseFetchingError(error, mediaType, "provider")
Expand Down
28 changes: 28 additions & 0 deletions frontend/test/unit/specs/data/media-provider-service.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { initProviderServices } from "~/data/media-provider-service"
import { warn } from "~/utils/console"

jest.mock("~/utils/console", () => ({ warn: jest.fn(), log: jest.fn() }))
let mockReturn = {}
jest.mock("~/data/api-service", () => ({
createApiService: function () {
return {
get: async () => {
return mockReturn
},
}
},
}))

describe("Media Provider Service", () => {
afterEach(() => {
warn.mockClear()
})
it("No data in response", async () => {
const result = await initProviderServices.image().getProviderStats()
expect(result).toEqual([])
expect(warn).toHaveBeenCalledTimes(1)
expect(warn).toHaveBeenCalledWith(
"Invalid response from provider stats endpoint: {}"
)
})
})
2 changes: 1 addition & 1 deletion frontend/test/unit/specs/stores/provider.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const mockData = [
},
]

const mockImplementation = () => Promise.resolve({ data: [...mockData] })
const mockImplementation = () => Promise.resolve([...mockData])
const mock = jest.fn().mockImplementation(mockImplementation)
jest.mock("~/data/media-provider-service", () => ({
initProviderServices: {
Expand Down

0 comments on commit 23d4536

Please sign in to comment.