Skip to content

Commit

Permalink
feat(test): Postのバリデーションテストの実装
Browse files Browse the repository at this point in the history
  • Loading branch information
laminne committed Sep 9, 2023
1 parent bb46f48 commit 17ba73b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/domain/post.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, expect, it } from "vitest";
import { Post, PostArgs } from "./post.js";
import { Snowflake } from "../helpers/id_generator.js";
import { Media } from "./media.js";

describe("Post", () => {
const defaultPostArgs: PostArgs = {
id: "10101450391945216" as Snowflake,
authorID: "10101451896913920" as Snowflake,
text: "Hello, world!",
visibility: 0,
createdAt: new Date(),
attachments: [],
reactions: [],
};

it("添付ファイルは16個以上添付できない", () => {
expect(() => {
new Post({
...defaultPostArgs,
attachments: new Array(17).fill({} as Media),
});
}).toThrowError(
new Error(
"failed to create post: The number of attachments must be less than 16.",
),
);
});

it("ファイルを添付できる", () => {
expect(() => {
new Post({
...defaultPostArgs,
attachments: new Array(10).fill({} as Media),
});
}).toBeTruthy();
});
});
19 changes: 19 additions & 0 deletions src/domain/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ import { Snowflake } from "../helpers/id_generator.js";
import { User } from "./user.js";
import { Media } from "./media.js";

export interface PostArgs {
id: Snowflake;
authorID: Snowflake;
text: string;
visibility: number;
createdAt: Date;
attachments: Array<Media>;
reactions: Array<PostReactionEvent>;
}

export class Post {
get visibility(): number {
return this._visibility;
Expand Down Expand Up @@ -54,6 +64,7 @@ export class Post {
attachments: Array<Media>;
reactions: Array<PostReactionEvent>;
}) {
this.validate(args);
this._id = args.id;
this._authorID = args.authorID;

Expand All @@ -70,6 +81,14 @@ export class Post {
}
return text;
}

private validate(args: PostArgs) {
if (args.attachments.length > 16) {
throw new Error(
"failed to create post: The number of attachments must be less than 16.",
);
}
}
}

export class PostReactionEvent {
Expand Down

0 comments on commit 17ba73b

Please sign in to comment.