Add some (WIP) tests
This commit is contained in:
parent
b64281b1ac
commit
86e323fbca
15 changed files with 417 additions and 384 deletions
|
@ -55,7 +55,7 @@ export default function HomePage({
|
|||
}}
|
||||
icon={<Settings />}
|
||||
>
|
||||
Go to Settings
|
||||
Go to Settings
|
||||
</Item>
|
||||
</Command.Group>
|
||||
</>
|
||||
|
|
|
@ -7,7 +7,9 @@ import { File } from "react-feather"
|
|||
import { fetchWithUser } from "src/app/lib/fetch-with-user"
|
||||
import Item from "../item"
|
||||
|
||||
export default function PostsPage({ setOpen }: {
|
||||
export default function PostsPage({
|
||||
setOpen
|
||||
}: {
|
||||
setOpen: (open: boolean) => void
|
||||
}) {
|
||||
const { session } = useSessionSWR()
|
||||
|
@ -32,7 +34,14 @@ export default function PostsPage({ setOpen }: {
|
|||
return (
|
||||
<>
|
||||
{isLoading && (
|
||||
<div style={{ display: "flex", justifyContent: "center", alignItems: "center", height: 100 }}>
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
justifyContent: "center",
|
||||
alignItems: "center",
|
||||
height: 100
|
||||
}}
|
||||
>
|
||||
<Spinner />
|
||||
</div>
|
||||
)}
|
||||
|
|
27
src/lib/__tests__/byte-to-mb.test.ts
Normal file
27
src/lib/__tests__/byte-to-mb.test.ts
Normal file
|
@ -0,0 +1,27 @@
|
|||
import byteToMB from "@lib/byte-to-mb"
|
||||
|
||||
describe("byteToMB", () => {
|
||||
it("converts 0 bytes to 0 MB", () => {
|
||||
expect(byteToMB(0)).toEqual(0)
|
||||
})
|
||||
|
||||
it("converts 1024 bytes to 0.001 MB", () => {
|
||||
expect(byteToMB(1024)).toBeCloseTo(0.001)
|
||||
})
|
||||
|
||||
it("converts 1048576 bytes to 1 MB", () => {
|
||||
expect(byteToMB(1048576)).toEqual(1)
|
||||
})
|
||||
|
||||
it("converts 3145728 bytes to 3 MB", () => {
|
||||
expect(byteToMB(3145728)).toEqual(3)
|
||||
})
|
||||
|
||||
it("returns NaN when given a negative number", () => {
|
||||
expect(byteToMB(-1)).toBeNaN()
|
||||
})
|
||||
|
||||
it("returns NaN when given a non-numeric value", () => {
|
||||
expect(byteToMB("test" as any)).toBeNaN()
|
||||
})
|
||||
})
|
|
@ -1,4 +1,9 @@
|
|||
const byteToMB = (bytes: number) =>
|
||||
Math.round((bytes / 1024 / 1024) * 100) / 100
|
||||
const byteToMB = (bytes: number) => {
|
||||
if (bytes < 0) {
|
||||
return NaN
|
||||
}
|
||||
|
||||
return Math.round((bytes / 1024 / 1024) * 100) / 100
|
||||
}
|
||||
|
||||
export default byteToMB
|
||||
|
|
54
src/lib/server/__tests__/verify-api-user.test.ts
Normal file
54
src/lib/server/__tests__/verify-api-user.test.ts
Normal file
|
@ -0,0 +1,54 @@
|
|||
import { verifyApiUser } from "../verify-api-user"
|
||||
import { prismaMock } from "src/test/prisma.mock"
|
||||
import "src/test/react.mock"
|
||||
import { User } from "@prisma/client"
|
||||
|
||||
describe("verifyApiUser", () => {
|
||||
const mockReq = {} as any
|
||||
const mockRes = {} as any
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks()
|
||||
})
|
||||
|
||||
it("returns null if there is no userId param or auth token", async () => {
|
||||
mockReq.query = {}
|
||||
mockReq.headers = {}
|
||||
const result = await verifyApiUser(mockReq, mockRes)
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it("returns the user id if there is a userId param and it matches the authenticated user id", async () => {
|
||||
mockReq.query = { userId: "123" }
|
||||
const mockUser = { id: "123" }
|
||||
const mockGetCurrentUser = prismaMock.user.findUnique.mockResolvedValue(
|
||||
mockUser as User
|
||||
)
|
||||
const result = await verifyApiUser(mockReq, mockRes)
|
||||
expect(mockGetCurrentUser).toHaveBeenCalled()
|
||||
expect(result).toEqual("123")
|
||||
})
|
||||
|
||||
it("returns null if there is a userId param but it doesn't match the authenticated user id", async () => {
|
||||
mockReq.query = { userId: "123" }
|
||||
const mockUser = { id: "456" }
|
||||
const mockGetCurrentUser = jest.fn().mockResolvedValue(mockUser)
|
||||
const result = await verifyApiUser(mockReq, mockRes)
|
||||
expect(mockGetCurrentUser).toHaveBeenCalled()
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it("returns the user id if there is an auth token and it is valid", async () => {
|
||||
mockReq.query = {}
|
||||
mockReq.headers.authorization = "Bearer mytoken"
|
||||
const mockUser = { userId: "123", expiresAt: new Date(Date.now() + 10000) }
|
||||
const mockFindUnique = jest.fn().mockResolvedValue(mockUser)
|
||||
const mockPrisma = { apiToken: { findUnique: mockFindUnique } } as any
|
||||
const result = await verifyApiUser(mockReq, mockRes)
|
||||
expect(mockFindUnique).toHaveBeenCalledWith({
|
||||
where: { token: "mytoken" },
|
||||
select: { userId: true, expiresAt: true }
|
||||
})
|
||||
expect(result).toEqual("123")
|
||||
})
|
||||
})
|
|
@ -3,7 +3,7 @@ import { parseQueryParam } from "@lib/server/parse-query-param"
|
|||
import { getCurrentUser } from "@lib/server/session"
|
||||
import { NextApiRequest, NextApiResponse } from "next"
|
||||
import { prisma } from "src/lib/server/prisma"
|
||||
import { deleteUser } from "../user/[id]"
|
||||
import { deleteUser } from "../user/[userId]"
|
||||
|
||||
const actions = [
|
||||
"user",
|
||||
|
|
29
src/test/.env.test
Normal file
29
src/test/.env.test
Normal file
|
@ -0,0 +1,29 @@
|
|||
DATABASE_URL=postgresql://user:password@localhost:5432/dbname
|
||||
|
||||
# Optional if you use Vercel (defaults to VERCEL_URL).
|
||||
# Necessary in development unless you use the vercel CLI (`vc dev`)
|
||||
DRIFT_URL=http://localhost:3000
|
||||
|
||||
# Optional: The first user becomes an admin. Defaults to false
|
||||
ENABLE_ADMIN=false
|
||||
|
||||
# Required: Next auth secret is a required valid JWT secret. You can generate one with `openssl rand -hex 32`
|
||||
NEXTAUTH_SECRET=7f8b8b5c5e5f5e5f5e5f5e5f5e5f5e5f5e5f5e5f5e5f5e5f5e5f5e5f5e5f5f5
|
||||
|
||||
# Required: but unnecessary if you use a supported host like Vercel
|
||||
NEXTAUTH_URL=http://localhost:3000
|
||||
|
||||
# Optional: for locking your instance
|
||||
REGISTRATION_PASSWORD=
|
||||
|
||||
# Optional: for if you want GitHub oauth. Currently incompatible with the registration password
|
||||
GITHUB_CLIENT_ID=
|
||||
GITHUB_CLIENT_SECRET=
|
||||
|
||||
# Optional: if you want to support credential auth (username/password, supports registration password)
|
||||
# Defaults to true
|
||||
CREDENTIAL_AUTH=true
|
||||
|
||||
# Optional:
|
||||
WELCOME_CONTENT=
|
||||
WELCOME_TITLE=
|
16
src/test/prisma.mock.ts
Normal file
16
src/test/prisma.mock.ts
Normal file
|
@ -0,0 +1,16 @@
|
|||
import { PrismaClient } from "@prisma/client"
|
||||
|
||||
import { mockDeep, mockReset, DeepMockProxy } from "jest-mock-extended"
|
||||
|
||||
import { prisma } from "@lib/server/prisma"
|
||||
|
||||
jest.mock("@lib/server/prisma", () => ({
|
||||
__esModule: true,
|
||||
default: mockDeep<PrismaClient>()
|
||||
}))
|
||||
|
||||
beforeEach(() => {
|
||||
mockReset(prismaMock)
|
||||
})
|
||||
|
||||
export const prismaMock = prisma as unknown as DeepMockProxy<PrismaClient>
|
12
src/test/react.mock.ts
Normal file
12
src/test/react.mock.ts
Normal file
|
@ -0,0 +1,12 @@
|
|||
jest.mock("react", () => {
|
||||
const ActualReact = jest.requireActual("react")
|
||||
|
||||
return {
|
||||
...ActualReact,
|
||||
cache: jest.fn((fn) => {
|
||||
return fn()
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
export {}
|
4
src/test/setup-tests.ts
Normal file
4
src/test/setup-tests.ts
Normal file
|
@ -0,0 +1,4 @@
|
|||
import * as dotenv from "dotenv"
|
||||
import * as path from "path"
|
||||
|
||||
dotenv.config({ path: path.resolve(__dirname, "./.env.test") })
|
Loading…
Add table
Add a link
Reference in a new issue