Skip to content

Commit

Permalink
Add coverage ignore and formatTitle utility function***
Browse files Browse the repository at this point in the history
  • Loading branch information
yasuaki640 committed Feb 25, 2024
1 parent b75a88b commit a6dfe1c
Show file tree
Hide file tree
Showing 7 changed files with 518 additions and 110 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ wrangler.toml
yarn.lock
pnpm-lock.yaml

.idea
.idea

coverage
551 changes: 455 additions & 96 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@biomejs/biome": "^1.5.3",
"@cloudflare/workers-types": "^4.20240129.0",
"@types/uuid": "^9.0.8",
"@vitest/coverage-v8": "^1.3.1",
"better-sqlite3": "^9.3.0",
"drizzle-kit": "^0.20.13",
"typescript": "^5.3.3",
Expand Down
44 changes: 44 additions & 0 deletions src/utils/format-tilte.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, expect, it } from "vitest";
import { Rooms } from "../schema";
import { formatTitle } from "./format-tilte";

describe("formatTitle", () => {
it("should return the room ID if roomTitle is falsy", () => {
const room: typeof Rooms.$inferSelect = {
roomId: "test-room-id",
roomTitle: null,
roomCreated: "2021-01-01T00:00:00Z",
roomUpdated: "2021-01-01T00:00:00Z",
};

const result = formatTitle(room);

expect(result).toBe("test-room-id");
});

it("should truncate the roomTitle if it exceeds the specified length", () => {
const room: typeof Rooms.$inferSelect = {
roomId: "test-room-id",
roomTitle: "This is a very long room title",
roomCreated: "2021-01-01T00:00:00Z",
roomUpdated: "2021-01-01T00:00:00Z",
};

const result = formatTitle(room, 10);

expect(result).toBe("This is a ...");
});

it("should return the roomTitle as is if it does not exceed the specified length", () => {
const room: typeof Rooms.$inferSelect = {
roomId: "test-room-id",
roomTitle: "Short title",
roomCreated: "2021-01-01T00:00:00Z",
roomUpdated: "2021-01-01T00:00:00Z",
};

const result = formatTitle(room, 20);

expect(result).toBe("Short title");
});
});
10 changes: 10 additions & 0 deletions src/utils/format-tilte.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Rooms } from "../schema";

export const formatTitle = (room: typeof Rooms.$inferSelect, len = 20) => {
if (!room.roomTitle) {
return room.roomId;
}
return room.roomTitle.length > len
? `${room.roomTitle.slice(0, len)}...`
: room.roomTitle;
};
7 changes: 4 additions & 3 deletions src/views/Room.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { FC } from "hono/dist/types/jsx";
import { Messages, Rooms } from "../schema";
import { formatTitle } from "../utils/format-tilte";

type Props = {
room: typeof Rooms.$inferSelect;
Expand All @@ -8,19 +9,19 @@ type Props = {

export const Room: FC<{ props: Props }> = ({ props }) => (
<>
<h1>{props.room.roomId}</h1>
<h1>{formatTitle(props.room)}</h1>
<a href={"/chats"}>Back</a>
<table>
<thead>
<tr>
<th>{props.room.roomTitle ? "Title" : "ID"}</th>
<th>ID</th>
<th>Created</th>
<th>Updated</th>
</tr>
</thead>
<tbody>
<tr>
<td>{props.room.roomTitle ?? props.room.roomId}</td>
<td>{props.room.roomId}</td>
<td>{props.room.roomCreated}</td>
<td>{props.room.roomUpdated}</td>
</tr>
Expand Down
11 changes: 1 addition & 10 deletions src/views/RoomList.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import { FC } from "hono/dist/types/jsx";
import { Rooms } from "../schema";
import { formatTitle } from "../utils/format-tilte";

type Props = {
rooms: (typeof Rooms.$inferSelect)[];
};

const TITLE_LENGTH = 20;
const formatTitle = (room: typeof Rooms.$inferSelect) => {
if (!room.roomTitle) {
return room.roomId;
}
return room.roomTitle.length > TITLE_LENGTH
? `${room.roomTitle.slice(0, TITLE_LENGTH)}...`
: room.roomTitle;
};

export const RoomList: FC<{ props: Props }> = ({ props }) => (
<>
<h1>Chat Rooms.</h1>
Expand Down

0 comments on commit a6dfe1c

Please sign in to comment.