Skip to content

Commit

Permalink
refactor: 정의된 상수 사용 및 함수 네이밍 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Largopie committed Jul 25, 2024
1 parent 6d573a8 commit af51744
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { renderHook } from '@testing-library/react';
import { act } from 'react';

import { INITIAL_END_TIME, INITIAL_START_TIME } from './constants';
import useTimeRangeDropdown from './useTimeRangeDropdown';

describe('useTimeRangeDropdown', () => {
it('초기 시작 시간(startTime)은 0:00, 끝 시간(emdTime)은 23:00으로 설정된다.', () => {
it(`초기 시작 시간(startTime)은 ${INITIAL_START_TIME}, 끝 시간(emdTime)은 ${INITIAL_END_TIME}으로 설정된다.`, () => {
const { result } = renderHook(() => useTimeRangeDropdown());

expect(result.current.startTime).toBe('0:00');
expect(result.current.endTime).toBe('23:00');
expect(result.current.startTime).toBe(INITIAL_START_TIME);
expect(result.current.endTime).toBe(INITIAL_END_TIME);
});

it('선택한 시작 시간(startTime)이 끝 시간(endTime)보다 느리다면 선택한 시간값으로 변경되지 않는다.', () => {
Expand Down
10 changes: 4 additions & 6 deletions frontend/src/hooks/useTimeRangeDropdown/useTimeRangeDropdown.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import { useState } from 'react';

import { compareTimes } from './useTimeRangeDropdown.utils';

const INITIAL_START_TIME = '0:00';
const INITIAL_END_TIME = '23:00';
import { INITIAL_END_TIME, INITIAL_START_TIME } from './constants';
import { isTimeSelectable } from './useTimeRangeDropdown.utils';

export default function useTimeRangeDropdown() {
const [startTime, setStartTime] = useState(INITIAL_START_TIME);
const [endTime, setEndTime] = useState(INITIAL_END_TIME);

// 시작 시간이 끝 시간보다 빠르다면 startTime이 변경되지 않도록 설정
const handleStartTimeChange = (time: string) => {
if (!compareTimes(time, endTime)) return;
if (!isTimeSelectable(time, endTime)) return;

setStartTime(time);
};

// 시작 시간이 끝 시간보다 빠르다면 startTime이 변경되지 않도록 설정
const handleEndTimeChange = (time: string) => {
if (!compareTimes(startTime, time)) return;
if (!isTimeSelectable(startTime, time)) return;

setEndTime(time);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import type { Option } from '@components/_common/Dropdown';

const MINIMUM_TIME = 0;
const MAXIMUM_TIME = 23;

import { MAXIMUM_TIME, MINIMUM_TIME } from './constants';

// label에 보여줄 이름을 변환해주는 함수
function formatHours(hour: number) {
Expand Down Expand Up @@ -41,12 +39,12 @@ export function generateEndTimeOptions(startTime: string) {
}

// 만약 시작 시간보다 끝 시간이 빠르다면 false를 반환하는 함수(@낙타)
export function compareTimes(startTime: string, endTime: string) {
export function isTimeSelectable(startTime: string, endTime: string) {
const [startHours, startMinutes] = startTime.split(':');
const [endHours, endMinutes] = endTime.split(':');

if (endHours < startHours) return false;
else if (endHours === startHours && endMinutes < startMinutes) return false;
if (endHours === startHours && endMinutes < startMinutes) return false;

return true;
}

0 comments on commit af51744

Please sign in to comment.