-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
add verification to cmsClient and add tests to it
Showing
5 changed files
with
137 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { describe, expect, it, vi } from "vitest"; | ||
import { | ||
createClient, | ||
getNavigationLinks, | ||
NavigationLink, | ||
navigationLinkSchema, | ||
} from "./cmsClient"; | ||
import { generateMock } from "@anatine/zod-mock"; | ||
import { z } from "zod"; | ||
import { TranslationKey } from "../hooks/useTranslate"; | ||
|
||
describe("cmsClient", async () => { | ||
it("returns valid data from the CMS", async () => { | ||
const mockData = generateMock(z.array(navigationLinkSchema)); | ||
const client = createClient("http://mockUrl"); | ||
const mockRequest = vi.spyOn(client, "request").mockResolvedValue(mockData); | ||
mockRequest.mockResolvedValue(mockData); | ||
|
||
const data = await getNavigationLinks(client); | ||
|
||
expect(mockRequest).toHaveBeenCalled(); | ||
expect(data).toEqual(mockData); | ||
}); | ||
|
||
it("fails on invalid label key from the CMS", async () => { | ||
const mockData: NavigationLink[] = [ | ||
{ | ||
label_key: "invalid" as TranslationKey, | ||
url: "page", | ||
category: "GENERAL", | ||
}, | ||
]; | ||
const client = createClient("http://mockUrl"); | ||
const mockRequest = vi.spyOn(client, "request").mockResolvedValue(mockData); | ||
mockRequest.mockResolvedValue(mockData); | ||
|
||
expect(getNavigationLinks(client)).rejects.toThrow(); | ||
}); | ||
|
||
it("fails on invalid category from the CMS", async () => { | ||
const mockData: NavigationLink[] = [ | ||
{ | ||
label_key: "general:nation", | ||
url: "page", | ||
category: "invalid" as "GENERAL", | ||
}, | ||
]; | ||
const client = createClient("http://mockUrl"); | ||
const mockRequest = vi.spyOn(client, "request").mockResolvedValue(mockData); | ||
mockRequest.mockResolvedValue(mockData); | ||
|
||
expect(getNavigationLinks(client)).rejects.toThrow(); | ||
}); | ||
}); |
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,32 +1,27 @@ | ||
import { TranslationKey } from "@/hooks/useTranslate"; | ||
import { createDirectus, rest } from "@directus/sdk"; | ||
import { translationKeySchema } from "../hooks/useTranslate"; | ||
import { createDirectus, readItems, rest } from "@directus/sdk"; | ||
import { z } from "zod"; | ||
|
||
type Schema = { | ||
NavigationLink: NavigationLink[]; | ||
Translation: Translation[]; | ||
}; | ||
|
||
export type NavigationLink = { | ||
label_key: TranslationKey; | ||
url: string; | ||
category: "GENERAL" | "FOR_MEMBERS"; | ||
}; | ||
export const navigationLinkSchema = z.object({ | ||
label_key: translationKeySchema, | ||
url: z.string(), | ||
category: z.enum(["GENERAL", "FOR_MEMBERS"]), | ||
}); | ||
|
||
/* | ||
* You shouldnt use this type, use the better typed one in useTranslate.tsx instead | ||
*/ | ||
export type Translation = { | ||
key: string; | ||
fi: string; | ||
en: string; | ||
sv: string; | ||
}; | ||
export type NavigationLink = z.infer<typeof navigationLinkSchema>; | ||
|
||
type CmsClient = ReturnType<typeof createClient>; | ||
|
||
const createClient = () => { | ||
if (process.env.DIRECTUS_URL === undefined) { | ||
throw Error("Environment variable DIRECTUS_URL not defined"); | ||
} | ||
return createDirectus<Schema>(process.env.DIRECTUS_URL).with(rest()); | ||
export const createClient = (url: string) => { | ||
return createDirectus<Schema>(url).with(rest()); | ||
}; | ||
|
||
export default createClient; | ||
export const getNavigationLinks = async (client: CmsClient) => { | ||
const links = await client.request(readItems("NavigationLink")); | ||
|
||
return links.map((link) => navigationLinkSchema.parse(link)); | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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