概要
そのまま使えるJestのフォーマット判定のテストコードです。
describe('Common', () => {
describe('フォーマット判定', () => {
test('電話番号', () => {
expect("012-3456-7890").toMatch(/^[0-9]{2,4}-[0-9]{2,4}-[0-9]{3,4}$/);
});
test('郵便番号', () => {
expect("012-3456").toMatch(/^[0-9]{3}-[0-9]{4}$/);
});
test('メールアドレス', () => {
expect("example@example.com").toMatch(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/);
});
test('日付フォーマット', () => {
expect("2017-01-01").toMatch(/^(\d{4})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/);
});
test('時刻フォーマット', () => {
expect("23:59:59").toMatch(/^(?:(2[0-3])|([0-1][0-9])):([0-5][0-9]):([0-5][0-9])/);
});
test('桁数カンマ区切り', () => {
expect("1,000,000").toMatch(/\b\d{1,3}(,\d{3})*\b/);
});
test('URL', () => {
expect("http://exapmle.com").toMatch(/^https?(:\/\/[-_.!~*\'()a-zA-Z0-9;\/?:\@&=+\$,%#]+)$/);
});
});
});