Skip to content

Commit

Permalink
test(tests): fixed all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
seaerchin committed May 23, 2023
1 parent 004fad0 commit 3369dfb
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 87 deletions.
5 changes: 4 additions & 1 deletion src/integration/Sites.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { ResourcePageService } from "@root/services/fileServices/MdPageServices/
import { SubcollectionPageService } from "@root/services/fileServices/MdPageServices/SubcollectionPageService"
import { UnlinkedPageService } from "@root/services/fileServices/MdPageServices/UnlinkedPageService"
import { CollectionYmlService } from "@root/services/fileServices/YmlFileServices/CollectionYmlService"
import { ConfigService } from "@root/services/fileServices/YmlFileServices/ConfigService"
import { ConfigYmlService } from "@root/services/fileServices/YmlFileServices/ConfigYmlService"
import { FooterYmlService } from "@root/services/fileServices/YmlFileServices/FooterYmlService"
import IsomerAdminsService from "@root/services/identity/IsomerAdminsService"
Expand Down Expand Up @@ -89,14 +90,16 @@ const pageService = new PageService({
unlinkedPageService,
resourceRoomDirectoryService,
})
const configService = new ConfigService()
const reviewRequestService = new ReviewRequestService(
gitHubService,
User,
ReviewRequest,
Reviewer,
ReviewMeta,
ReviewRequestView,
pageService
pageService,
configService
)
const sitesService = new SitesService({
siteRepository: Site,
Expand Down
6 changes: 3 additions & 3 deletions src/routes/v2/authenticated/__tests__/Sites.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import express from "express"
import { ok, okAsync } from "neverthrow"
import { okAsync } from "neverthrow"
import request from "supertest"

import type { AuthorizationMiddleware } from "@middleware/authorization"
Expand Down Expand Up @@ -99,7 +99,7 @@ describe("Sites Router", () => {
describe("getStagingUrl", () => {
it("returns the site's staging URL", async () => {
const stagingUrl = "staging-url"
mockSitesService.getStagingUrl.mockResolvedValueOnce(ok(stagingUrl))
mockSitesService.getStagingUrl.mockReturnValueOnce(okAsync(stagingUrl))

const resp = await request(app)
.get(`/${mockSiteName}/stagingUrl`)
Expand Down Expand Up @@ -138,7 +138,7 @@ describe("Sites Router", () => {
stagingUrl: "staging-url",
siteUrl: "prod-url",
}
mockSitesService.getSiteInfo.mockResolvedValueOnce(ok(siteInfo))
mockSitesService.getSiteInfo.mockReturnValueOnce(okAsync(siteInfo))

const resp = await request(app).get(`/${mockSiteName}/info`).expect(200)

Expand Down
6 changes: 4 additions & 2 deletions src/routes/v2/authenticated/__tests__/review.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,12 @@ describe("Review Requests Router", () => {
// Arrange
const mockFilesChanged = ["file1", "file2"]
mockIdentityUsersService.getSiteMember.mockResolvedValueOnce("user")
mockReviewRequestService.compareDiff.mockResolvedValueOnce(
mockFilesChanged
mockReviewRequestService.compareDiff.mockReturnValueOnce(
okAsync(mockFilesChanged)
)
mockSitesService.getBySiteName.mockResolvedValueOnce(ok(true))
mockSitesService.getStagingUrl.mockReturnValueOnce(okAsync("staging-url"))
mockGithubService.getRepoInfo.mockResolvedValueOnce(true)

// Act
const response = await request(app).get("/mockSite/review/compare")
Expand Down
9 changes: 8 additions & 1 deletion src/routes/v2/authenticated/review.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import autoBind from "auto-bind"
import express from "express"
import _ from "lodash"
import { ResultAsync } from "neverthrow"

import logger from "@logger/logger"

Expand All @@ -15,6 +14,8 @@ import UserWithSiteSessionData from "@classes/UserWithSiteSessionData"

import { CollaboratorRoles, ReviewRequestStatus } from "@root/constants"
import { SiteMember, User } from "@root/database/models"
import MissingSiteError from "@root/errors/MissingSiteError"
import { NotFoundError } from "@root/errors/NotFoundError"
import { GitHubService } from "@root/services/db/GitHubService"
import CollaboratorsService from "@root/services/identity/CollaboratorsService"
import NotificationsService from "@root/services/identity/NotificationsService"
Expand Down Expand Up @@ -132,6 +133,12 @@ export class ReviewsRouter {
)
)
.map((items) => res.status(200).json({ items }))
.mapErr((err) => {
if (err instanceof MissingSiteError || err instanceof NotFoundError) {
return res.status(404).json({ message: err.message })
}
return res.status(500).json({ message: err.message })
})
}

createReviewRequest: RequestHandler<
Expand Down
10 changes: 8 additions & 2 deletions src/services/fileServices/MdPageServices/PageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,9 @@ export class PageService {
() => new NotFoundError()
).andThen<ResourceCategoryPageName, NotFoundError>(({ content }) => {
if (content.frontMatter.layout !== "post")
return errAsync(new NotFoundError())
return errAsync(
new NotFoundError("Please ensure that the post exists!")
)
return okAsync({
name: Brand.fromString(name),
resourceRoom: resourceRoomName.name,
Expand All @@ -274,7 +276,11 @@ export class PageService {
})
// NOTE: If we get an empty string as the `pageName`,
// we just treat the file as not being found
.mapErr(() => new NotFoundError())
.mapErr((e) => {
// NOTE: Done to preserve any existing error messages
if (e instanceof NotFoundError) return e
return new NotFoundError()
})

// NOTE: This is a safe wrapper over the js file for `getResourceRoomDirectoryName`
extractResourceRoomName = (
Expand Down
108 changes: 55 additions & 53 deletions src/services/fileServices/MdPageServices/__tests__/PageService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
UnlinkedPageName,
} from "@root/types/pages"
import { Brand } from "@root/types/util"
import { extractPathInfo } from "@root/utils/files"
import { ResourceRoomDirectoryService } from "@services/directoryServices/ResourceRoomDirectoryService"

import { CollectionPageService } from "../CollectionPageService"
Expand Down Expand Up @@ -88,7 +89,7 @@ describe("PageService", () => {

// Act
const actual = await pageService.parsePageName(
"index.md",
{ name: "index.md", path: err([]), __kind: "PathInfo" },
MOCK_USER_SESSION_DATA_ONE
)

Expand All @@ -105,7 +106,7 @@ describe("PageService", () => {

// Act
const actual = await pageService.parsePageName(
"pages/contact-us.md",
{ name: CONTACT_US_FILENAME, path: ok(["pages"]), __kind: "PathInfo" },
MOCK_USER_SESSION_DATA_ONE
)

Expand All @@ -115,14 +116,18 @@ describe("PageService", () => {

it('should parse "contact-us.md" into an error', async () => {
// Arrange
const expected = err(new NotFoundError())
const expected = err(
new NotFoundError(
"Error when parsing path: , please ensure that the file exists!"
)
)
mockResourceRoomDirectoryService.getResourceRoomDirectoryName.mockResolvedValueOnce(
{ resourceRoomName: MOCK_RESOURCE_ROOM_NAME }
)

// Act
const actual = await pageService.parsePageName(
CONTACT_US_FILENAME,
{ name: CONTACT_US_FILENAME, path: ok([]), __kind: "PathInfo" },
MOCK_USER_SESSION_DATA_ONE
)

Expand All @@ -139,7 +144,11 @@ describe("PageService", () => {

// Act
const actual = await pageService.parsePageName(
`pages/${MOCK_UNLINKED_PAGE_NAME}`,
{
name: `${MOCK_UNLINKED_PAGE_NAME}`,
path: ok(["pages"]),
__kind: "PathInfo",
},
MOCK_USER_SESSION_DATA_ONE
)

Expand Down Expand Up @@ -171,7 +180,15 @@ describe("PageService", () => {

// Act
const actual = await pageService.parsePageName(
`${MOCK_RESOURCE_ROOM_NAME}/${MOCK_RESOURCE_CATEGORY_NAME}/_posts/${MOCK_UNLINKED_PAGE_NAME}`,
{
name: `${MOCK_UNLINKED_PAGE_NAME}`,
path: ok([
MOCK_RESOURCE_ROOM_NAME,
MOCK_RESOURCE_CATEGORY_NAME,
"_posts",
]),
__kind: "PathInfo",
},
MOCK_USER_SESSION_DATA_ONE
)

Expand All @@ -182,15 +199,7 @@ describe("PageService", () => {

it("should return `NotFoundError` if the `layout` of the resource item is not `post`", async () => {
// Arrange
const MOCK_RESOURCE_ITEM = `${MOCK_RESOURCE_ROOM_NAME}/${MOCK_RESOURCE_CATEGORY_NAME}/_posts/${MOCK_UNLINKED_PAGE_NAME}`
const expected = err(
new NotFoundError(
`Error when parsing path: ${MOCK_RESOURCE_ITEM.split("/").slice(
0,
-1
)}, please ensure that the file exists!`
)
)
const expected = err(new NotFoundError())
mockResourceRoomDirectoryService.getResourceRoomDirectoryName.mockResolvedValueOnce(
{ resourceRoomName: MOCK_RESOURCE_ROOM_NAME }
)
Expand All @@ -207,7 +216,15 @@ describe("PageService", () => {

// Act
const actual = await pageService.parsePageName(
MOCK_RESOURCE_ITEM,
{
name: MOCK_UNLINKED_PAGE_NAME,
path: ok([
MOCK_RESOURCE_ROOM_NAME,
MOCK_RESOURCE_CATEGORY_NAME,
"_posts",
]),
__kind: "PathInfo",
},
MOCK_USER_SESSION_DATA_ONE
)

Expand All @@ -229,7 +246,11 @@ describe("PageService", () => {

// Act
const actual = await pageService.parsePageName(
`${MOCK_COLLECTION_NAME}/${MOCK_UNLINKED_PAGE_NAME}`,
{
name: MOCK_UNLINKED_PAGE_NAME,
path: ok([MOCK_COLLECTION_NAME]),
__kind: "PathInfo",
},
MOCK_USER_SESSION_DATA_ONE
)

Expand All @@ -252,7 +273,11 @@ describe("PageService", () => {

// Act
const actual = await pageService.parsePageName(
`${MOCK_COLLECTION_NAME}/${MOCK_SUBCOLLECTION_NAME}/${MOCK_UNLINKED_PAGE_NAME}`,
{
name: MOCK_UNLINKED_PAGE_NAME,
path: ok([MOCK_COLLECTION_NAME, MOCK_SUBCOLLECTION_NAME]),
__kind: "PathInfo",
},
MOCK_USER_SESSION_DATA_ONE
)

Expand All @@ -263,37 +288,15 @@ describe("PageService", () => {

it("should parse two level filepaths including 'index.md' to `NotFoundError`", async () => {
// Arrange
const expected = err(
new NotFoundError(
`Error when parsing path: , please ensure that the file exists!`
)
)
const expected = err(new NotFoundError())
mockResourceRoomDirectoryService.getResourceRoomDirectoryName.mockResolvedValueOnce(
{ resourceRoomName: MOCK_RESOURCE_ROOM_NAME }
)

// Act
const actual = await pageService.parsePageName(
// NOTE: Extra front slash
`/${HOMEPAGE_FILENAME}`,
MOCK_USER_SESSION_DATA_ONE
)

// Assert
expect(actual).toEqual(expected)
expect(mockResourcePageService.read).toBeCalled()
})

it("should parse empty strings into `EmptyStringError`", async () => {
// Arrange
const expected = err(new EmptyStringError())
mockResourceRoomDirectoryService.getResourceRoomDirectoryName.mockResolvedValueOnce(
{ resourceRoomName: MOCK_RESOURCE_ROOM_NAME }
)

// Act
const actual = await pageService.parsePageName(
"",
{ name: `/${HOMEPAGE_FILENAME}`, path: err([]), __kind: "PathInfo" },
MOCK_USER_SESSION_DATA_ONE
)

Expand All @@ -304,18 +307,14 @@ describe("PageService", () => {

it("should parse `/` into `NotFoundError`", async () => {
// Arrange
const expected = err(
new NotFoundError(
"Error when parsing path: , please ensure that the file exists!"
)
)
const expected = err(new NotFoundError())
mockResourceRoomDirectoryService.getResourceRoomDirectoryName.mockResolvedValueOnce(
{ resourceRoomName: MOCK_RESOURCE_ROOM_NAME }
)

// Act
const actual = await pageService.parsePageName(
"/",
{ name: "/", path: err([]), __kind: "PathInfo" },
MOCK_USER_SESSION_DATA_ONE
)

Expand All @@ -333,7 +332,7 @@ describe("PageService", () => {

// Act
const actual = await pageService.parsePageName(
"gibberish",
{ name: "gibberish", path: err([]), __kind: "PathInfo" },
MOCK_USER_SESSION_DATA_ONE
)

Expand Down Expand Up @@ -411,10 +410,11 @@ describe("PageService", () => {
const expected = ok({
name: MOCK_UNLINKED_PAGE_NAME,
path: ok([MOCK_RESOURCE_ROOM_NAME, MOCK_RESOURCE_CATEGORY_NAME]),
__kind: "PathInfo",
})

// Act
const actual = pageService.extractPathInfo(
const actual = extractPathInfo(
`${MOCK_RESOURCE_ROOM_NAME}/${MOCK_RESOURCE_CATEGORY_NAME}/${MOCK_UNLINKED_PAGE_NAME}`
)

Expand All @@ -427,10 +427,11 @@ describe("PageService", () => {
const expected = ok({
name: MOCK_UNLINKED_PAGE_NAME,
path: err([]),
__kind: "PathInfo",
})

// Act
const actual = pageService.extractPathInfo(`${MOCK_UNLINKED_PAGE_NAME}`)
const actual = extractPathInfo(`${MOCK_UNLINKED_PAGE_NAME}`)

// Assert
expect(actual).toStrictEqual(expected)
Expand All @@ -441,10 +442,11 @@ describe("PageService", () => {
const expected = ok({
name: "",
path: ok([MOCK_RESOURCE_ROOM_NAME]),
__kind: "PathInfo",
})

// Act
const actual = pageService.extractPathInfo(`${MOCK_RESOURCE_ROOM_NAME}/`)
const actual = extractPathInfo(`${MOCK_RESOURCE_ROOM_NAME}/`)

// Assert
expect(actual).toStrictEqual(expected)
Expand All @@ -455,7 +457,7 @@ describe("PageService", () => {
const expected = err(new EmptyStringError())

// Act
const actual = pageService.extractPathInfo("")
const actual = extractPathInfo("")

// Assert
expect(actual).toEqual(expected)
Expand Down
Loading

0 comments on commit 3369dfb

Please sign in to comment.