Skip to content

Commit d21b608

Browse files
committed
テストコード実装
1 parent d9a4048 commit d21b608

8 files changed

Lines changed: 53 additions & 9 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7+
"build": "tsc -p tsconfig.build.json",
78
"test": "jest",
89
"format": "prettier --write \"src/**/*.{js,jsx,ts,tsx,css,scss,html}\"",
910
"prepare": "husky install",

src/util/isValidEmail.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import isEmail from 'validator/lib/isEmail';
22

3+
/**
4+
* メールアドレスの検証
5+
* @param mail
6+
*/
37
export default function isValidEmail(mail: string) {
48
// 150文字制限
59
if (mail.length > 150) {

src/util/isValidUsername.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* ユーザー名の検証
3+
* @param username
4+
*/
5+
export default function isValidUsername(username: string) {
6+
const pattern = /^[A-Za-z0-9_]{3,15}$/;
7+
return pattern.test(username);
8+
}

test/index.test.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

test/util/isValidUsername.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import isValidUsername from '@/util/isValidUsername';
2+
3+
describe('isValidUsername', () => {
4+
it('有効なユーザー名の場合はtrueを返す', () => {
5+
expect(isValidUsername('cuculus123')).toBe(true);
6+
expect(isValidUsername('cuculus_name')).toBe(true);
7+
expect(isValidUsername('Cuculus1')).toBe(true);
8+
});
9+
10+
it('3文字未満のユーザー名の場合はfalseを返す', () => {
11+
expect(isValidUsername('us')).toBe(false);
12+
});
13+
14+
it('15文字を超えるユーザー名の場合はfalseを返す', () => {
15+
expect(isValidUsername('thisIsAReallyLongUsername')).toBe(false);
16+
});
17+
18+
it('無効な文字を含むユーザー名の場合はfalseを返す', () => {
19+
expect(isValidUsername('cuculus@123')).toBe(false);
20+
expect(isValidUsername('cuculus-name')).toBe(false);
21+
expect(isValidUsername('cuculus!')).toBe(false);
22+
expect(isValidUsername('cuculus.me')).toBe(false);
23+
});
24+
});

tsconfig.build.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"exclude": [
4+
"node_modules",
5+
"test"
6+
]
7+
}

tsconfig.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
]
2323
}
2424
},
25-
"exclude": ["node_modules", "test"],
26-
25+
"exclude": [
26+
"node_modules"
27+
]
2728
}

tsconfig.test.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"exclude": [
4+
"node_modules"
5+
]
6+
}

0 commit comments

Comments
 (0)