-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from Selleo/jw/fe-test-setup
feat: fe test setup
- Loading branch information
Showing
34 changed files
with
2,725 additions
and
174 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
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
3 changes: 3 additions & 0 deletions
3
examples/common_nestjs_remix/apps/api/src/common/decorators/staging.decorator.ts
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,3 @@ | ||
import { SetMetadata } from "@nestjs/common"; | ||
|
||
export const OnlyStaging = () => SetMetadata("onlyStaging", true); |
File renamed without changes.
File renamed without changes.
18 changes: 18 additions & 0 deletions
18
examples/common_nestjs_remix/apps/api/src/common/guards/staging.guard.ts
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,18 @@ | ||
import { Injectable, CanActivate, ExecutionContext } from "@nestjs/common"; | ||
import { Reflector } from "@nestjs/core"; | ||
|
||
@Injectable() | ||
export class StagingGuard implements CanActivate { | ||
constructor(private reflector: Reflector) {} | ||
|
||
canActivate(context: ExecutionContext): boolean { | ||
const onlyStaging = this.reflector.get<boolean>( | ||
"onlyStaging", | ||
context.getHandler(), | ||
); | ||
if (!onlyStaging) { | ||
return true; | ||
} | ||
return process.env.NODE_ENV === "staging"; | ||
} | ||
} |
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,112 @@ | ||
import postgres from "postgres"; | ||
import { drizzle } from "drizzle-orm/postgres-js"; | ||
import { users, credentials } from "./storage/schema"; | ||
import { DatabasePg } from "./common"; | ||
import { faker } from "@faker-js/faker"; | ||
import * as dotenv from "dotenv"; | ||
import hashPassword from "./common/helpers/hashPassword"; | ||
|
||
dotenv.config({ path: "./.env" }); | ||
|
||
if (!("DATABASE_URL" in process.env)) | ||
throw new Error("DATABASE_URL not found on .env"); | ||
|
||
async function seed() { | ||
const connectionString = process.env.DATABASE_URL!; | ||
const testUserEmail = "[email protected]"; | ||
const adminPassword = "password"; | ||
|
||
const sql = postgres(connectionString); | ||
const db = drizzle(sql) as DatabasePg; | ||
|
||
try { | ||
const adminUserData = { | ||
id: faker.string.uuid(), | ||
email: testUserEmail, | ||
createdAt: new Date().toISOString(), | ||
updatedAt: new Date().toISOString(), | ||
}; | ||
|
||
const [insertedAdminUser] = await db | ||
.insert(users) | ||
.values(adminUserData) | ||
.returning(); | ||
|
||
const adminCredentialData = { | ||
id: faker.string.uuid(), | ||
userId: insertedAdminUser.id, | ||
password: await hashPassword(adminPassword), | ||
createdAt: new Date().toISOString(), | ||
updatedAt: new Date().toISOString(), | ||
}; | ||
|
||
const [insertedAdminCredential] = await db | ||
.insert(credentials) | ||
.values(adminCredentialData) | ||
.returning(); | ||
|
||
console.log("Created admin user:", { | ||
...insertedAdminUser, | ||
credentials: { | ||
...insertedAdminCredential, | ||
password: adminPassword, | ||
}, | ||
}); | ||
|
||
const usersWithCredentials = await Promise.all( | ||
Array.from({ length: 5 }, async () => { | ||
const userData = { | ||
id: faker.string.uuid(), | ||
email: faker.internet.email(), | ||
createdAt: new Date().toISOString(), | ||
updatedAt: new Date().toISOString(), | ||
}; | ||
|
||
const [insertedUser] = await db | ||
.insert(users) | ||
.values(userData) | ||
.returning(); | ||
|
||
const password = faker.internet.password(); | ||
const credentialData = { | ||
id: faker.string.uuid(), | ||
userId: insertedUser.id, | ||
password: await hashPassword(password), | ||
createdAt: new Date().toISOString(), | ||
updatedAt: new Date().toISOString(), | ||
}; | ||
|
||
const [insertedCredential] = await db | ||
.insert(credentials) | ||
.values(credentialData) | ||
.returning(); | ||
|
||
return { | ||
...insertedUser, | ||
credentials: { | ||
...insertedCredential, | ||
password: password, | ||
}, | ||
}; | ||
}), | ||
); | ||
|
||
console.log("Created users with credentials:", usersWithCredentials); | ||
console.log("Seeding completed successfully"); | ||
} catch (error) { | ||
console.error("Seeding failed:", error); | ||
} finally { | ||
await sql.end(); | ||
} | ||
} | ||
|
||
if (require.main === module) { | ||
seed() | ||
.then(() => process.exit(0)) | ||
.catch((error) => { | ||
console.error("An error occurred:", error); | ||
process.exit(1); | ||
}); | ||
} | ||
|
||
export default seed; |
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
22 changes: 22 additions & 0 deletions
22
examples/common_nestjs_remix/apps/api/src/test-config/api/test-config.controller.ts
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,22 @@ | ||
import { Controller, Post } from "@nestjs/common"; | ||
import { TestConfigService } from "../test-config.service"; | ||
import { OnlyStaging } from "src/common/decorators/staging.decorator"; | ||
import { Public } from "src/common/decorators/public.decorator"; | ||
|
||
@Controller("test-config") | ||
export class TestConfigController { | ||
constructor(private testConfigService: TestConfigService) {} | ||
|
||
@Public() | ||
@Post("setup") | ||
@OnlyStaging() | ||
async setup(): Promise<void> { | ||
return this.testConfigService.setup(); | ||
} | ||
|
||
@Post("teardown") | ||
@OnlyStaging() | ||
async teardown(): Promise<void> { | ||
return this.testConfigService.teardown(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
examples/common_nestjs_remix/apps/api/src/test-config/test-config.module.ts
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,11 @@ | ||
import { Module } from "@nestjs/common"; | ||
import { TestConfigController } from "./api/test-config.controller"; | ||
import { TestConfigService } from "./test-config.service"; | ||
|
||
@Module({ | ||
imports: [], | ||
controllers: [TestConfigController], | ||
providers: [TestConfigService], | ||
exports: [], | ||
}) | ||
export class TestConfigModule {} |
14 changes: 14 additions & 0 deletions
14
examples/common_nestjs_remix/apps/api/src/test-config/test-config.service.ts
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,14 @@ | ||
import { Injectable, NotImplementedException } from "@nestjs/common"; | ||
|
||
@Injectable() | ||
export class TestConfigService { | ||
constructor() {} | ||
|
||
public async setup() { | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public async teardown() { | ||
throw new NotImplementedException(); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -3,3 +3,7 @@ node_modules | |
/.cache | ||
/build | ||
.env | ||
/test-results/ | ||
/playwright-report/ | ||
/blob-report/ | ||
/playwright/.cache/ |
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
30 changes: 30 additions & 0 deletions
30
...es/common_nestjs_remix/apps/web/app/components/ThemeToggle/__tests__/ThemeToggle.test.tsx
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,30 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { screen, fireEvent } from "@testing-library/react"; | ||
|
||
import ThemeToggle from "../ThemeToggle"; | ||
import { renderWith } from "../../../utils/testUtils"; | ||
|
||
describe("ThemeToggle", () => { | ||
it("renders without crashing", () => { | ||
renderWith({ | ||
withTheme: true, | ||
}).render(<ThemeToggle />); | ||
|
||
expect(screen.getByRole("button")).toBeInTheDocument(); | ||
}); | ||
|
||
it("toggles theme", async () => { | ||
renderWith({ | ||
withTheme: true, | ||
}).render(<ThemeToggle />); | ||
|
||
const button = screen.getByRole("button"); | ||
|
||
expect(button).toBeInTheDocument(); | ||
|
||
fireEvent.click(button); | ||
expect(await screen.findByLabelText(/dark/)).toBeInTheDocument(); | ||
fireEvent.click(button); | ||
expect(await screen.findByLabelText(/light/)).toBeInTheDocument(); | ||
}); | ||
}); |
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 @@ | ||
/// <reference types="vitest/globals" /> |
46 changes: 46 additions & 0 deletions
46
examples/common_nestjs_remix/apps/web/app/modules/Auth/Login.page.test.tsx
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,46 @@ | ||
import { createRemixStub } from "@remix-run/testing"; | ||
import { screen, waitFor } from "@testing-library/react"; | ||
import { userEvent } from "@testing-library/user-event"; | ||
import { describe, expect, it, vi } from "vitest"; | ||
import { | ||
mockRemixReact, | ||
mockedUseNavigate, | ||
} from "~/utils/mocks/remix-run-mock"; | ||
import { renderWith } from "~/utils/testUtils"; | ||
import LoginPage from "./Login.page"; | ||
|
||
vi.mock("../../../api/api-client"); | ||
|
||
mockRemixReact(); | ||
|
||
describe("Login page", () => { | ||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
}); | ||
|
||
const RemixStub = createRemixStub([ | ||
{ | ||
path: "/", | ||
Component: LoginPage, | ||
}, | ||
]); | ||
|
||
it("renders without crashing", () => { | ||
renderWith({ withQuery: true }).render(<RemixStub />); | ||
|
||
expect(screen.getByRole("heading", { name: "Login" })).toBeInTheDocument(); | ||
}); | ||
|
||
it("submits the form with valid data", async () => { | ||
renderWith({ withQuery: true }).render(<RemixStub />); | ||
|
||
const user = userEvent.setup(); | ||
await user.type(screen.getByLabelText("Email"), "[email protected]"); | ||
await user.type(screen.getByLabelText("Password"), "password123"); | ||
await user.click(screen.getByRole("button", { name: "Login" })); | ||
|
||
await waitFor(() => { | ||
expect(mockedUseNavigate).toHaveBeenCalledWith("/dashboard"); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.