Skip to content

Commit

Permalink
test: add test cases for getPageById
Browse files Browse the repository at this point in the history
  • Loading branch information
karrui committed Oct 11, 2024
1 parent 3ad59e9 commit 0f943ae
Showing 1 changed file with 103 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {

import type { Resource } from "../../database"
import { db } from "../../database"
import { getFullPageById, getSiteResourceById } from "../resource.service"
import {
getFullPageById,
getPageById,
getSiteResourceById,
} from "../resource.service"

describe("resource.service", () => {
describe("getSiteResourceById", () => {
Expand Down Expand Up @@ -183,4 +187,102 @@ describe("resource.service", () => {
expect(result).toBeUndefined()
})
})

describe("getPageById", () => {
it("should return the 'Page' resource with the given `id`", async () => {
// Arrange
const { site, page } = await setupPageResource({
resourceType: "Page",
})

// Act
const result = await getPageById(db, {
siteId: site.id,
resourceId: Number(page.id),
})

// Assert
expect(result).toMatchObject(page)
})

it("should return the 'RootPage' resource with the given `id`", async () => {
// Arrange
const { site, page: rootPage } = await setupPageResource({
resourceType: "RootPage",
})

// Act
const result = await getPageById(db, {
siteId: site.id,
resourceId: Number(rootPage.id),
})

// Assert
expect(result).toMatchObject(rootPage)
})

it("should return the 'CollectionPage' resource with the given `id`", async () => {
// Arrange
const { site, page: collectionPage } = await setupPageResource({
resourceType: "CollectionPage",
})

// Act
const result = await getPageById(db, {
siteId: site.id,
resourceId: Number(collectionPage.id),
})

// Assert
expect(result).toMatchObject(collectionPage)
})

it("should return undefined if resource type is not a supported type", async () => {
// Arrange
const { site, page } = await setupPageResource({
resourceType: "Folder",
})

// Act
const result = await getPageById(db, {
siteId: site.id,
resourceId: Number(page.id),
})

// Assert
expect(result).toBeUndefined()
})

it("should return undefined if no resource with the given `id` exists", async () => {
// Arrange
const { site } = await setupPageResource({
resourceType: "Page",
})

// Act
const result = await getPageById(db, {
siteId: site.id,
resourceId: 99999,
})

// Assert
expect(result).toBeUndefined()
})

it("should return undefined if the resource with the given `id` does not belong to the given `siteId`", async () => {
// Arrange
const { page } = await setupPageResource({
resourceType: "Page",
})

// Act
const result = await getPageById(db, {
siteId: 99999,
resourceId: Number(page.id),
})

// Assert
expect(result).toBeUndefined()
})
})
})

0 comments on commit 0f943ae

Please sign in to comment.