Skip to content

Commit

Permalink
Add a test to validate-collection-params
Browse files Browse the repository at this point in the history
Signed-off-by: Olga Bulat <obulat@gmail.com>
  • Loading branch information
obulat committed Nov 27, 2023
1 parent 4103681 commit f50e056
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions frontend/test/unit/specs/utils/validate-collection-params.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { createPinia, setActivePinia } from "~~/test/unit/test-utils/pinia"

import { validateCollectionParams } from "~/utils/validate-collection-params"
import { useProviderStore } from "~/stores/provider"
import { useFeatureFlagStore } from "~/stores/feature-flag"
import { AUDIO, IMAGE } from "~/constants/media"
import { warn } from "~/utils/console"

jest.mock("~/utils/console", () => ({
warn: jest.fn(),
}))

describe("validateCollectionParams", () => {
/** @type { import("pinia").Pinia } **/
let pinia

beforeEach(() => {
pinia = createPinia()
setActivePinia(pinia)
useProviderStore().isSourceNameValid = jest.fn(() => true)
useFeatureFlagStore().toggleFeature("additional_search_views", "on")
})

it("returns null if feature flag is off", () => {
useFeatureFlagStore().toggleFeature("additional_search_views", "off")

const result = validateCollectionParams({
firstParam: "tag",
mediaType: IMAGE,
params: { tag: "nature" },
$pinia: pinia,
})

expect(result).toBeNull()
})

it.each`
firstParam | mediaType | params | expected
${"tag"} | ${IMAGE} | ${{ tag: "nature" }} | ${{ collection: "tag", tag: "nature" }}
${"source"} | ${IMAGE} | ${{ source: "flickr", pathMatch: "/flickr" }} | ${{ collection: "source", source: "flickr" }}
${"source"} | ${AUDIO} | ${{ source: "freesound", pathMatch: "/freesound" }} | ${{ collection: "source", source: "freesound" }}
${"source"} | ${IMAGE} | ${{ source: "flickr", pathMatch: "/flickr/creator/creatorName" }} | ${{ collection: "creator", source: "flickr", creator: "creatorName" }}
${"source"} | ${IMAGE} | ${{ source: "flickr", pathMatch: "/flickr/creator/http%3A%2F%2FcreatorName.com%2Fme" }} | ${{ collection: "creator", source: "flickr", creator: "http%3A%2F%2FcreatorName.com%2Fme" }}
`(
"returns $expected for $firstParam and $mediaType",
({ firstParam, mediaType, params, expected }) => {
const result = validateCollectionParams({
firstParam,
mediaType,
params,
$pinia: pinia,
})

expect(result).toEqual(expected)
}
)
it.each`
firstParam | mediaType | params
${"source"} | ${IMAGE} | ${{ source: "flickr", pathMatch: "/invalidSourceName/creator/creatorName" }}
${"source"} | ${AUDIO} | ${{ source: "invalidSourceName", pathMatch: "/invalidSourceName" }}
`(
"returns `null` for invalid source name",
({ firstParam, mediaType, params }) => {
useProviderStore().isSourceNameValid = jest.fn(() => false)

const result = validateCollectionParams({
firstParam,
mediaType,
params,
$pinia: pinia,
})

expect(result).toBeNull()
expect(warn).toHaveBeenCalledWith(
'Invalid source name "invalidSourceName" for a creator collection page.'
)
}
)
})

0 comments on commit f50e056

Please sign in to comment.