Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Replace use-fetch-state with simpler implementation (#1747)

Olga Bulat2ff021ffb33a
diff --git a/src/components/VAudioDetails/VRelatedAudio.vue b/src/components/VAudioDetails/VRelatedAudio.vue
index 0fd4c2600..a93a53f98 100644
--- a/src/components/VAudioDetails/VRelatedAudio.vue
+++ b/src/components/VAudioDetails/VRelatedAudio.vue
@@ -3,7 +3,10 @@
<h2 class="mb-6 text-2xl lg:text-3xl">
{{ $t('audio-details.related-audios') }}
</h2>
- <div v-if="!fetchState.isError" class="mb-12 flex flex-col gap-8 lg:gap-12">
+ <div
+ v-if="!fetchState.fetchingError"
+ class="mb-12 flex flex-col gap-8 lg:gap-12"
+ >
<VAudioTrack
v-for="audio in media"
:key="audio.id"
@@ -13,7 +16,7 @@
/>
<LoadingIcon v-show="fetchState.isFetching" />
</div>
- <p v-show="!!fetchState.isError">
+ <p v-show="!!fetchState.fetchingError">
{{ $t('media-details.related-error') }}
</p>
</aside>
diff --git a/src/components/VImageDetails/VRelatedImages.vue b/src/components/VImageDetails/VRelatedImages.vue
index 25807df7c..89fd9e87b 100644
--- a/src/components/VImageDetails/VRelatedImages.vue
+++ b/src/components/VImageDetails/VRelatedImages.vue
@@ -16,7 +16,7 @@
import { defineComponent, PropType } from '@nuxtjs/composition-api'
import type { ImageDetail } from '~/models/media'
-import type { FetchState } from '~/composables/use-fetch-state'
+import type { FetchState } from '~/models/fetch-state'
import VImageGrid from '~/components/VImageGrid/VImageGrid.vue'
import VLoadingIcon from '~/components/LoadingIcon.vue'
diff --git a/src/components/VImageGrid/VImageGrid.vue b/src/components/VImageGrid/VImageGrid.vue
index 3ea017eb3..8285c4710 100644
--- a/src/components/VImageGrid/VImageGrid.vue
+++ b/src/components/VImageGrid/VImageGrid.vue
@@ -27,7 +27,7 @@
*/
import { computed, defineComponent, PropType } from '@nuxtjs/composition-api'
-import type { FetchState } from '~/composables/use-fetch-state'
+import type { FetchState } from '~/models/fetch-state'
import type { ImageDetail } from '~/models/media'
import { defineEvent } from '~/types/emits'
diff --git a/src/components/VLoadMore.vue b/src/components/VLoadMore.vue
index 741ec6f8e..82a1953f2 100644
--- a/src/components/VLoadMore.vue
+++ b/src/components/VLoadMore.vue
@@ -32,7 +32,8 @@ export default defineComponent({
const canLoadMore = computed(
() =>
searchStore.searchTerm !== '' &&
- mediaStore.fetchState.canFetch &&
+ !mediaStore.fetchState.isFetching &&
+ !mediaStore.fetchState.fetchingError &&
mediaStore.resultCount > 0
)
const onLoadMore = async () => {
diff --git a/src/components/VSearchGrid.vue b/src/components/VSearchGrid.vue
index ee18147a4..b3270231d 100644
--- a/src/components/VSearchGrid.vue
+++ b/src/components/VSearchGrid.vue
@@ -41,7 +41,7 @@ import { ALL_MEDIA, IMAGE, SearchType, mediaTypes } from '~/constants/media'
import { NO_RESULT } from '~/constants/errors'
import { defineEvent } from '~/types/emits'
import type { ApiQueryParams } from '~/utils/search-query-transform'
-import type { FetchState } from '~/composables/use-fetch-state'
+import type { FetchState } from '~/models/fetch-state'
import VMetaSearchForm from '~/components/VMetaSearch/VMetaSearchForm.vue'
import VErrorSection from '~/components/VErrorSection/VErrorSection.vue'
diff --git a/src/composables/use-fetch-state.ts b/src/composables/use-fetch-state.ts
deleted file mode 100644
index 5063705d3..000000000
--- a/src/composables/use-fetch-state.ts
+++ /dev/null
@@ -1,168 +0,0 @@
-import { computed, reactive, Ref, ref, watch } from '@nuxtjs/composition-api'
-
-export interface FetchState {
- isFetching: boolean
- fetchingError: null | string
- canFetch?: boolean
- hasStarted?: boolean
- isFinished?: boolean
-}
-export const initialFetchState: FetchState = {
- hasStarted: false,
- isFetching: false,
- canFetch: true,
- isFinished: false,
- fetchingError: null,
-} as const
-
-/* Constants */
-
-/**
- * Mutually exclusive request statuses:
- * - `idle`: the current request has never been sent yet, for example,
- * on the first app load, or after a change in search filters or search term.
- * - `fetching`: the request was sent, but no response was received yet.
- * - `success`: a successful response was received for the current request.
- * - `error`: an error response was received.
- */
-const statuses = Object.freeze({
- IDLE: 'idle',
- FETCHING: 'fetching',
- SUCCESS: 'success',
- ERROR: 'error',
-} as const)
-
-type Status = typeof statuses[keyof typeof statuses]
-/**
- * When one of these statuses is received, the fetchError is set to `null` in a watcher.
- */
-const nonErrorStatuses: Status[] = [
- statuses.IDLE,
- statuses.FETCHING,
- statuses.SUCCESS,
-]
-/**
- * With these statuses, it is possible to send the same request for a new page.
- * This can help debounce requests and prevent racing states.
- */
-const canFetchStatuses: Status[] = [statuses.IDLE, statuses.SUCCESS]
-
-/* Composable */
-export const useFetchState = (state: FetchState = initialFetchState) => {
- const initialState: {
- status: Status
- fetchError: null | string
- isFinished: boolean
- } = {
- status: statuses.IDLE,
- fetchError: null,
- isFinished: false,
- }
- if (state) {
- if (state.isFetching) {
- initialState.status = statuses.FETCHING
- } else if (state.fetchingError) {
- initialState.status = statuses.ERROR
- initialState.fetchError = state.fetchingError
- } else if (state.hasStarted) {
- initialState.status = statuses.SUCCESS
- }
- if (state.isFinished) {
- initialState.isFinished = true
- }
- }
-
- const fetchStatus: Ref<Status> = ref(initialState.status)
- const fetchError: Ref<string | null> = ref(initialState.fetchError)
- const isFinished: Ref<boolean> = ref(initialState.isFinished)
-
- watch(fetchStatus, () => {
- if (nonErrorStatuses.includes(fetchStatus.value)) {
- fetchError.value = null
- }
- })
- const reset = () => {
- fetchStatus.value = statuses.IDLE
- fetchError.value = null
- isFinished.value = false
- }
- const startFetching = () => {
- fetchStatus.value = statuses.FETCHING
- }
- /**
- * Called when any response is received. For an error response,
- * pass the errorMessage string as a parameter.
- * @param errorMessage - the message to show for response error.
- */
- const endFetching = (errorMessage?: string) => {
- if (errorMessage) {
- fetchStatus.value = statuses.ERROR
- fetchError.value = errorMessage
- isFinished.value = true
- } else {
- fetchStatus.value = statuses.SUCCESS
- }
- }
- /**
- * Used for paginated requests, `isFinished` means there are no more pages left.
- * It does not change the fetchStatus, which should be handled separately.
- */
- const setFinished = () => {
- isFinished.value = true
- }
-
- /**
- * Whether the current request has been sent. This can be useful for displaying
- * initial search page with a blank search term, such as for a provider search page.
- */
- const hasStarted = computed(() => fetchStatus.value !== statuses.IDLE)
- const isFetching = computed(() => fetchStatus.value === statuses.FETCHING)
- /**
- * Whether a new request for the same parameters with a new page can be sent.
- * Use this to ensure that prevent racing requests.
- */
- const canFetch = computed(
- () => canFetchStatuses.includes(fetchStatus.value) && !isFinished.value
- )
- const fetchingError = computed(() => fetchError.value)
-
- const fetchState: FetchState = reactive({
- hasStarted,
- isFetching,
- canFetch,
- isFinished,
- fetchingError,
- })
-
- return {
- fetchState,
-
- startFetching,
- endFetching,
- setFinished,
- reset,
- }
-}
-
-export const updateFetchState = (
- initial: FetchState,
- action: 'end' | 'finish' | 'start' | 'reset',
- option?: string
-) => {
- const fetchState = useFetchState(initial)
- switch (action) {
- case 'end':
- fetchState.endFetching(option)
- break
- case 'start':
- fetchState.startFetching()
- break
- case 'finish':
- fetchState.setFinished()
- break
- case 'reset':
- fetchState.reset()
- break
- }
- return fetchState.fetchState
-}
diff --git a/src/models/fetch-state.ts b/src/models/fetch-state.ts
new file mode 100644
index 000000000..3c655c6b1
--- /dev/null
+++ b/src/models/fetch-state.ts
@@ -0,0 +1,6 @@
+export interface FetchState {
+ isFetching: boolean
+ fetchingError: null | string
+ hasStarted?: boolean
+ isFinished?: boolean
+}
diff --git a/src/pages/search/search-page.types.ts b/src/pages/search/search-page.types.ts
index d68aa6f80..7545e187d 100644
--- a/src/pages/search/search-page.types.ts
+++ b/src/pages/search/search-page.types.ts
@@ -1,6 +1,6 @@
import type { SupportedMediaType } from '~/constants/media'
import type { Media } from '~/models/media'
-import type { FetchState } from '~/composables/use-fetch-state'
+import type { FetchState } from '~/models/fetch-state'
import type { ExtractPropTypes, PropType } from '@nuxtjs/composition-api'
diff --git a/src/stores/media/index.ts b/src/stores/media/index.ts
index ed33f8804..73823b416 100644
--- a/src/stores/media/index.ts
+++ b/src/stores/media/index.ts
@@ -6,11 +6,7 @@ import { warn } from '~/utils/console'
import { hash, rand as prng } from '~/utils/prng'
import prepareSearchQueryParams from '~/utils/prepare-search-query-params'
import type { DetailFromMediaType, Media } from '~/models/media'
-import {
- FetchState,
- initialFetchState,
- updateFetchState,
-} from '~/composables/use-fetch-state'
+import type { FetchState } from '~/models/fetch-state'
import {
ALL_MEDIA,
AUDIO,
@@ -56,8 +52,18 @@ export const useMediaStore = defineStore('media', {
[IMAGE]: { ...initialResults },
},
mediaFetchState: {
- [AUDIO]: { ...initialFetchState },
- [IMAGE]: { ...initialFetchState },
+ [AUDIO]: {
+ isFetching: false,
+ hasStarted: false,
+ isFinished: false,
+ fetchingError: null,
+ },
+ [IMAGE]: {
+ isFetching: false,
+ hasStarted: false,
+ isFinished: false,
+ fetchingError: null,
+ },
},
}),
@@ -140,7 +146,6 @@ export const useMediaStore = defineStore('media', {
return {
isFetching: atLeastOne('isFetching'),
fetchingError: findFirstError(),
- canFetch: atLeastOne('canFetch'),
hasStarted: atLeastOne('hasStarted'),
isFinished: supportedMediaTypes.every(
(type) => this.mediaFetchState[type].isFinished
@@ -149,7 +154,12 @@ export const useMediaStore = defineStore('media', {
} else if (isSearchTypeSupported(this._searchType)) {
return this.mediaFetchState[this._searchType]
} else {
- return initialFetchState
+ return {
+ isFetching: false,
+ fetchingError: null,
+ hasStarted: false,
+ isFinished: false,
+ }
}
},
@@ -221,16 +231,64 @@ export const useMediaStore = defineStore('media', {
},
actions: {
+ _startFetching(mediaType: SupportedMediaType) {
+ this.mediaFetchState[mediaType].isFetching = true
+ this.mediaFetchState[mediaType].hasStarted = true
+ this.mediaFetchState[mediaType].isFinished = false
+ this.mediaFetchState[mediaType].fetchingError = null
+ },
+ /**
+ * Called when the request is finished, regardless of whether it was successful or not.
+ * @param mediaType - The media type for which the request was made.
+ * @param error - The string representation of the error, if any.
+ */
+ _endFetching(mediaType: SupportedMediaType, error?: string) {
+ this.mediaFetchState[mediaType].fetchingError = error || null
+ this.mediaFetchState[mediaType].hasStarted = true
+ this.mediaFetchState[mediaType].isFetching = false
+
+ if (error) {
+ this.mediaFetchState[mediaType].isFinished = true
+ }
+ },
+ /**
+ * This is called when there are no more results available in the API for specific query.
+ * @param mediaType - The media type for which the request was made.
+ */
+ _finishFetchingForQuery(mediaType: SupportedMediaType) {
+ this.mediaFetchState[mediaType].isFinished = true
+ this.mediaFetchState[mediaType].hasStarted = true
+ this.mediaFetchState[mediaType].isFetching = false
+ },
+
+ _resetFetchState() {
+ for (const mediaType of supportedMediaTypes) {
+ this.mediaFetchState[mediaType].isFetching = false
+ this.mediaFetchState[mediaType].hasStarted = false
+ this.mediaFetchState[mediaType].isFinished = false
+ this.mediaFetchState[mediaType].fetchingError = null
+ }
+ },
+
_updateFetchState(
mediaType: SupportedMediaType,
action: 'reset' | 'start' | 'end' | 'finish',
option?: string
) {
- this.mediaFetchState[mediaType] = updateFetchState(
- this.mediaFetchState[mediaType],
- action,
- option
- )
+ switch (action) {
+ case 'reset':
+ this._resetFetchState()
+ break
+ case 'start':
+ this._startFetching(mediaType)
+ break
+ case 'end':
+ this._endFetching(mediaType, option)
+ break
+ case 'finish':
+ this._finishFetchingForQuery(mediaType)
+ break
+ }
},
setMedia<T extends SupportedMediaType>(params: {
@@ -282,12 +340,6 @@ export const useMediaStore = defineStore('media', {
this.results[mediaType].pageCount = 0
},
- resetFetchState() {
- for (const mediaType of supportedMediaTypes) {
- this._updateFetchState(mediaType, 'reset')
- }
- },
-
/**
* Calls `fetchSingleMediaType` for selected media type(s). Can be called by changing the search query
* (search term or filter item), or by clicking 'Load more' button.
@@ -297,13 +349,17 @@ export const useMediaStore = defineStore('media', {
async fetchMedia(payload: { shouldPersistMedia?: boolean } = {}) {
const mediaType = this._searchType
if (!payload.shouldPersistMedia) {
- this.resetFetchState()
+ this._resetFetchState()
}
const mediaToFetch = (
(mediaType !== ALL_MEDIA
? [mediaType]
: [IMAGE, AUDIO]) as SupportedMediaType[]
- ).filter((type) => this.mediaFetchState[type].canFetch)
+ ).filter(
+ (type) =>
+ !this.mediaFetchState[type].fetchingError &&
+ !this.mediaFetchState[type].isFetching
+ )
await Promise.all(
mediaToFetch.map((type) =>
this.fetchSingleMediaType({
diff --git a/src/stores/media/related-media.ts b/src/stores/media/related-media.ts
index cad2c1874..ba8e06183 100644
--- a/src/stores/media/related-media.ts
+++ b/src/stores/media/related-media.ts
@@ -1,10 +1,6 @@
import { defineStore } from 'pinia'
-import {
- FetchState,
- initialFetchState,
- updateFetchState,
-} from '~/composables/use-fetch-state'
+import type { FetchState } from '~/models/fetch-state'
import { initServices } from '~/stores/media/services'
import type { Media } from '~/models/media'
import type { SupportedMediaType } from '~/constants/media'
@@ -18,7 +14,7 @@ interface RelatedMediaState {
export const useRelatedMediaStore = defineStore('related-media', {
state: (): RelatedMediaState => ({
mainMediaId: null,
- fetchState: { ...initialFetchState },
+ fetchState: { isFetching: false, hasStarted: false, fetchingError: null },
media: [],
}),
@@ -30,9 +26,26 @@ export const useRelatedMediaStore = defineStore('related-media', {
},
actions: {
+ _endFetching(error?: string) {
+ this.fetchState.fetchingError = error || null
+ this.fetchState.hasStarted = true
+ this.fetchState.isFetching = false
+ },
+ _startFetching() {
+ this.fetchState.isFetching = true
+ this.fetchState.hasStarted = true
+ this.fetchState.fetchingError = null
+ },
+ _resetFetching() {
+ this.fetchState.isFetching = false
+ this.fetchState.hasStarted = false
+ this.fetchState.fetchingError = null
+ },
+
async fetchMedia(mediaType: SupportedMediaType, id: string) {
+ this._resetFetching()
this.mainMediaId = id
- this.fetchState = updateFetchState(this.fetchState, 'start')
+ this._startFetching()
this.media = []
try {
const accessToken = this.$nuxt.$openverseApiToken
@@ -40,13 +53,9 @@ export const useRelatedMediaStore = defineStore('related-media', {
this.media = (
await service.getRelatedMedia<typeof mediaType>(id)
).results
- this.fetchState = updateFetchState(this.fetchState, 'end')
+ this._endFetching()
} catch (error) {
- this.fetchState = updateFetchState(
- this.fetchState,
- 'end',
- `Could not fetch related ${mediaType} for id ${id}`
- )
+ this._endFetching(`Could not fetch related ${mediaType} for id ${id}`)
throw new Error(`Could not fetch related ${mediaType} for id ${id}`)
}
},
diff --git a/src/stores/media/single-result.ts b/src/stores/media/single-result.ts
index 35ab7c355..6ed444897 100644
--- a/src/stores/media/single-result.ts
+++ b/src/stores/media/single-result.ts
@@ -4,11 +4,7 @@ import axios from 'axios'
import type { AudioDetail, ImageDetail, Media } from '~/models/media'
import type { SupportedMediaType } from '~/constants/media'
-import {
- FetchState,
- initialFetchState,
- updateFetchState,
-} from '~/composables/use-fetch-state'
+import type { FetchState } from '~/models/fetch-state'
import { initServices } from '~/stores/media/services'
import { useMediaStore } from '~/stores/media/index'
import { useRelatedMediaStore } from '~/stores/media/related-media'
@@ -35,12 +31,22 @@ export const useSingleResultStore = defineStore('single-result', {
state: (): MediaItemState => ({
mediaItem: null,
mediaType: null,
- fetchState: initialFetchState,
+ fetchState: { isFetching: false, hasStarted: false, fetchingError: null },
}),
actions: {
- _updateFetchState(action: 'end' | 'start', option?: string) {
- this.fetchState = updateFetchState(this.fetchState, action, option)
+ _endFetching(error?: string) {
+ this.fetchState.isFetching = false
+ this.fetchState.fetchingError = error || null
+ },
+ _startFetching() {
+ this.fetchState.isFetching = true
+ this.fetchState.hasStarted = true
+ this.fetchState.fetchingError = null
+ },
+
+ _updateFetchState(action: 'start' | 'end', option?: string) {
+ action === 'start' ? this._startFetching() : this._endFetching(option)
},
_addProviderName(mediaItem: Media) {
diff --git a/src/stores/provider.ts b/src/stores/provider.ts
index 8a14770cb..b4934a639 100644
--- a/src/stores/provider.ts
+++ b/src/stores/provider.ts
@@ -13,11 +13,7 @@ import {
import { warn } from '~/utils/console'
import { initProviderServices } from '~/data/media-provider-service'
import type { MediaProvider } from '~/models/media-provider'
-import {
- FetchState,
- initialFetchState,
- updateFetchState,
-} from '~/composables/use-fetch-state'
+import type { FetchState } from '~/models/fetch-state'
export interface ProviderState {
providers: {
@@ -55,27 +51,39 @@ export const useProviderStore = defineStore('provider', {
[IMAGE]: [],
},
fetchState: {
- [AUDIO]: { ...initialFetchState },
- [IMAGE]: { ...initialFetchState },
+ [AUDIO]: { isFetching: false, hasStarted: false, fetchingError: null },
+ [IMAGE]: { isFetching: false, hasStarted: false, fetchingError: null },
},
}),
actions: {
- async getProviders() {
- await this.fetchMediaProviders()
- return this.providers
+ _endFetching(mediaType: SupportedMediaType, error?: string) {
+ this.fetchState[mediaType].fetchingError = error || null
+ if (error) {
+ this.fetchState[mediaType].isFinished = true
+ this.fetchState[mediaType].hasStarted = true
+ } else {
+ this.fetchState[mediaType].hasStarted = true
+ }
+ this.fetchState[mediaType].isFetching = false
+ },
+ _startFetching(mediaType: SupportedMediaType) {
+ this.fetchState[mediaType].isFetching = true
+ this.fetchState[mediaType].hasStarted = true
},
_updateFetchState(
mediaType: SupportedMediaType,
- action: 'end' | 'start',
+ action: 'start' | 'end',
option?: string
) {
- this.fetchState[mediaType] = updateFetchState(
- this.fetchState[mediaType],
- action,
- option
- )
+ action === 'start'
+ ? this._startFetching(mediaType)
+ : this._endFetching(mediaType, option)
+ },
+ async getProviders() {
+ await this.fetchMediaProviders()
+ return this.providers
},
/**
diff --git a/test/unit/specs/composables/use-fetch-state.spec.ts b/test/unit/specs/composables/use-fetch-state.spec.ts
deleted file mode 100644
index 810b3f167..000000000
--- a/test/unit/specs/composables/use-fetch-state.spec.ts
+++ /dev/null
@@ -1,61 +0,0 @@
-import { useFetchState } from '~/composables/use-fetch-state'
-
-describe('useFetchState', () => {
- it('should use the default values', () => {
- const fetchState = useFetchState()
- expect(fetchState.fetchState).toEqual({
- isFetching: false,
- isFinished: false,
- fetchingError: null,
- canFetch: true,
- hasStarted: false,
- })
- })
- it('should start fetching', () => {
- const fetchState = useFetchState()
- fetchState.startFetching()
-
- expect(fetchState.fetchState).toEqual({
- isFetching: true,
- isFinished: false,
- fetchingError: null,
- canFetch: false,
- hasStarted: true,
- })
- })
- it('should end fetching with success', () => {
- const fetchState = useFetchState()
- fetchState.endFetching()
- expect(fetchState.fetchState).toEqual({
- isFetching: false,
- isFinished: false,
- fetchingError: null,
- canFetch: true,
- hasStarted: true,
- })
- })
- it('should end fetching with error', () => {
- const fetchState = useFetchState()
- fetchState.endFetching('Server Error')
- expect(fetchState.fetchState).toEqual({
- isFetching: false,
- isFinished: true,
- fetchingError: 'Server Error',
- canFetch: false,
- hasStarted: true,
- })
- })
- it('setFinished should set isFinished to true, and isFetching to false', () => {
- const fetchState = useFetchState()
- fetchState.startFetching()
- fetchState.endFetching()
- fetchState.setFinished()
- expect(fetchState.fetchState).toEqual({
- isFetching: false,
- isFinished: true,
- fetchingError: null,
- canFetch: false,
- hasStarted: true,
- })
- })
-})
diff --git a/test/unit/specs/stores/media-store.spec.js b/test/unit/specs/stores/media-store.spec.js
index 2ec202e0b..d2b3e5a79 100644
--- a/test/unit/specs/stores/media-store.spec.js
+++ b/test/unit/specs/stores/media-store.spec.js
@@ -5,7 +5,6 @@ import { deepClone } from '~/utils/clone'
import { initialResults, useMediaStore } from '~/stores/media'
import { useSearchStore } from '~/stores/search'
import { ALL_MEDIA, AUDIO, IMAGE, supportedMediaTypes } from '~/constants/media'
-import { initialFetchState } from '~/composables/use-fetch-state'
jest.mock('axios', () => ({
...jest.requireActual('axios'),
@@ -78,8 +77,18 @@ describe('Media Store', () => {
audio: { ...initialResults },
})
expect(mediaStore.mediaFetchState).toEqual({
- audio: initialFetchState,
- image: initialFetchState,
+ audio: {
+ fetchingError: null,
+ hasStarted: false,
+ isFetching: false,
+ isFinished: false,
+ },
+ image: {
+ fetchingError: null,
+ hasStarted: false,
+ isFetching: false,
+ isFinished: false,
+ },
})
})
})
@@ -167,9 +176,9 @@ describe('Media Store', () => {
it.each`
searchType | fetchState
- ${ALL_MEDIA} | ${{ canFetch: false, fetchingError: 'Error', hasStarted: true, isFetching: true, isFinished: false }}
- ${AUDIO} | ${{ canFetch: false, fetchingError: 'Error', hasStarted: true, isFetching: false, isFinished: true }}
- ${IMAGE} | ${{ canFetch: false, fetchingError: null, hasStarted: true, isFetching: true, isFinished: false }}
+ ${ALL_MEDIA} | ${{ fetchingError: 'Error', hasStarted: true, isFetching: true, isFinished: false }}
+ ${AUDIO} | ${{ fetchingError: 'Error', hasStarted: true, isFetching: false, isFinished: true }}
+ ${IMAGE} | ${{ fetchingError: null, hasStarted: true, isFetching: true, isFinished: false }}
`(
'fetchState for $searchType returns $fetchState',
({ searchType, fetchState }) => {
@@ -317,7 +326,6 @@ describe('Media Store', () => {
expect(mediaStore.results[mediaType]).toEqual(initialResults)
expect(mediaStore.fetchState).toEqual({
isFetching: false,
- canFetch: true,
hasStarted: true,
isFinished: false,
fetchingError: `No ${mediaType} found for this query`,
diff --git a/test/unit/specs/stores/provider.spec.js b/test/unit/specs/stores/provider.spec.js
index 663757bf0..f5ee3ae3c 100644
--- a/test/unit/specs/stores/provider.spec.js
+++ b/test/unit/specs/stores/provider.spec.js
@@ -5,7 +5,6 @@ import { AUDIO, IMAGE, supportedMediaTypes } from '~/constants/media'
import { useSearchStore } from '~/stores/search'
import { useProviderStore } from '~/stores/provider'
import { initProviderServices } from '~/data/media-provider-service'
-import { initialFetchState } from '~/composables/use-fetch-state'
jest.mock('axios', () => ({
...jest.requireActual('axios'),
@@ -77,8 +76,8 @@ describe('Provider Store', () => {
[IMAGE]: [],
})
expect(providerStore.fetchState).toEqual({
- [AUDIO]: initialFetchState,
- [IMAGE]: initialFetchState,
+ [AUDIO]: { hasStarted: false, isFetching: false, fetchingError: null },
+ [IMAGE]: { hasStarted: false, isFetching: false, fetchingError: null },
})
})
@@ -100,8 +99,9 @@ describe('Provider Store', () => {
it('fetchMediaProviders on success', async () => {
await providerStore.fetchMediaProviders()
expect(providerStore.fetchState[IMAGE]).toEqual({
- ...initialFetchState,
+ fetchingError: null,
hasStarted: true,
+ isFetching: false,
})
expect(providerStore.providers[IMAGE]).toEqual(mockData)
})
diff --git a/test/unit/specs/stores/single-result-store.spec.js b/test/unit/specs/stores/single-result-store.spec.js
index 2047e19b8..1ef7d6a23 100644
--- a/test/unit/specs/stores/single-result-store.spec.js
+++ b/test/unit/specs/stores/single-result-store.spec.js
@@ -1,6 +1,5 @@
import { createPinia, setActivePinia } from '~~/test/unit/test-utils/pinia'
-import { initialFetchState } from '~/composables/use-fetch-state'
import { AUDIO, IMAGE, supportedMediaTypes } from '~/constants/media'
import { useMediaStore } from '~/stores/media'
import { useSingleResultStore } from '~/stores/media/single-result'
@@ -44,7 +43,11 @@ describe('Media Item Store', () => {
it('sets default state', () => {
setActivePinia(createPinia())
const singleResultStore = useSingleResultStore()
- expect(singleResultStore.fetchState).toEqual(initialFetchState)
+ expect(singleResultStore.fetchState).toEqual({
+ hasStarted: false,
+ isFetching: false,
+ fetchingError: null,
+ })
expect(singleResultStore.mediaItem).toEqual(null)
expect(singleResultStore.mediaType).toEqual(null)
})