Skip to content

Commit c6b7ec9

Browse files
authored
test: add tests (#15)
* test: add item domain test * test: add item repository test * chore: implement item repository mock * test: add item usecase test * chore: add test task * chore: update deno.lock * ci: add test step
1 parent 6a7fb2d commit c6b7ec9

File tree

7 files changed

+569
-6
lines changed

7 files changed

+569
-6
lines changed

.github/workflows/ci.yml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1-
name: CI
1+
name: ci
22
on: [push, pull_request]
33

44
jobs:
55
ci:
6-
name: CI
6+
name: ci
77
runs-on: ubuntu-latest
88
steps:
99
- name: Checkout
1010
uses: actions/checkout@v4
1111

12-
- name: Setup Deno Environment
12+
- name: Set up Deno environment
1313
uses: denoland/setup-deno@v1
1414
with:
1515
deno-version: latest
1616

17-
- name: Format Check
17+
- name: Check format
1818
run: deno fmt --check
1919

20-
- name: Lint Check
20+
- name: Check lint
2121
run: deno lint
22+
23+
- name: Run tests
24+
run: deno task test

deno.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"tasks": {
33
"start": "deno run --env --unstable-kv --allow-net --allow-env --allow-read src/main.ts",
4-
"dev": "deno run --watch --env --unstable-kv --allow-net --allow-env --allow-read src/main.ts"
4+
"dev": "deno run --watch --env --unstable-kv --allow-net --allow-env --allow-read src/main.ts",
5+
"test": "deno test --unstable-kv"
56
},
67
"imports": {
78
"@hono/zod-validator": "npm:@hono/zod-validator@^0.2.2",

deno.lock

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/domain/item/impl.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { assertEquals } from "jsr:@std/assert";
2+
3+
import { Item } from "./impl.ts";
4+
5+
import type { ItemFields } from "./impl.ts";
6+
7+
Deno.test("getURL - 適切なURLを取得できる", async (t) => {
8+
const url = "https://example.com";
9+
const obj: ItemFields = {
10+
param: "com",
11+
description: "example.com",
12+
url,
13+
count: 1,
14+
unavailable: false,
15+
};
16+
17+
await t.step("有効なitemのURLを取得できる", () => {
18+
const item = new Item({ ...obj });
19+
assertEquals(item.url, url);
20+
});
21+
22+
await t.step("無効なitemのURLはnullである", () => {
23+
const item = new Item({ ...obj, unavailable: true });
24+
assertEquals(item.url, null);
25+
});
26+
});

src/repository/item/impl.mock.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { Item } from "../../domain/item/impl.ts";
2+
3+
import type { ItemFields } from "../../domain/item/impl.ts";
4+
import type { ItemRepositoryInterface } from "./interface.ts";
5+
6+
type MockItems = {
7+
[host: string]: Item[];
8+
};
9+
10+
export class MockItemRepository implements ItemRepositoryInterface {
11+
constructor(private items: MockItems) {}
12+
13+
findAllItems(host: string): Promise<Item[]> {
14+
return Promise.resolve(this.items[host] ?? []);
15+
}
16+
17+
findItem(host: string, param: string): Promise<Item | null> {
18+
const item = this.items[host].find((item) => item.param === param);
19+
return Promise.resolve(item ?? null);
20+
}
21+
22+
upsertItem(host: string, fields: ItemFields): Promise<Item> {
23+
const item = new Item(fields);
24+
this.items[host].push(item);
25+
return Promise.resolve(item);
26+
}
27+
28+
updateItem(
29+
host: string,
30+
item: Item,
31+
fields: Partial<Omit<ItemFields, "param">>,
32+
): Promise<Item> {
33+
this.items[host] = this.items[host].filter((i) => i.param !== item.param);
34+
35+
const newItem = new Item({ ...item.getFields(), ...fields });
36+
this.items[host].push(newItem);
37+
return Promise.resolve(newItem);
38+
}
39+
40+
deleteItem(host: string, item: Item): Promise<void> {
41+
this.items[host] = this.items[host].filter((i) => i.param !== item.param);
42+
return Promise.resolve();
43+
}
44+
}

0 commit comments

Comments
 (0)