-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #130 from DDD-Community/refactor/home-page
[REFACTOR] 홈, 보드 생성 페이지 리팩토링 및 테스트 코드 추가
- Loading branch information
Showing
15 changed files
with
436 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { act, renderHook } from '@testing-library/react' | ||
import { useBoardName } from '@/hooks/useBoardName' | ||
|
||
describe('useBoardName()', () => { | ||
it('description', () => { | ||
// Given | ||
const { result } = renderHook(() => useBoardName()) | ||
|
||
// When | ||
act(() => { | ||
result.current.setBoardName('board name') | ||
}) | ||
|
||
// Then | ||
expect(result.current.description).toEqual('10/15자') | ||
}) | ||
|
||
describe('errorMessage', () => { | ||
it('should return error message on empty string', () => { | ||
// Given | ||
const { result } = renderHook(() => useBoardName()) | ||
|
||
// When | ||
act(() => { | ||
result.current.setBoardName('') | ||
}) | ||
|
||
// Then | ||
expect(result.current.errorMessage).toEqual( | ||
'최소 한글자 이상 입력해주세요', | ||
) | ||
}) | ||
|
||
it('should return error message on value longer than max length', () => { | ||
// Given | ||
const { result } = renderHook(() => useBoardName()) | ||
|
||
// When | ||
act(() => { | ||
result.current.setBoardName('abcdefghijklmnop') | ||
}) | ||
|
||
// Then | ||
expect(result.current.errorMessage).toEqual('15자 이내로 입력 가능해요') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
import { act, renderHook } from '@testing-library/react' | ||
import { useInputValidation, Validation } from '@/hooks/useInputValidation' | ||
|
||
describe('useInputValidation()', () => { | ||
describe('value', () => { | ||
it('should return initialValue as value', () => { | ||
// Given | ||
const initialValue = 'initialValue' | ||
|
||
// When | ||
const { result } = renderHook(() => useInputValidation(initialValue)) | ||
|
||
// Then | ||
expect(result.current.value).toEqual(initialValue) | ||
}) | ||
|
||
it('should set value to the new value when setValue is called', () => { | ||
// Given | ||
const initialValue = 'initialValue' | ||
const { result } = renderHook(() => useInputValidation(initialValue)) | ||
|
||
// When | ||
const newValue = 'newValue' | ||
act(() => { | ||
result.current.setValue(newValue) | ||
}) | ||
|
||
// Then | ||
expect(result.current.value).toEqual(newValue) | ||
}) | ||
}) | ||
describe('isDirty', () => { | ||
it('should set isDirty to false before the value is changed', () => { | ||
// Given | ||
const initialValue = 'initialValue' | ||
|
||
// When | ||
const { result } = renderHook(() => useInputValidation(initialValue)) | ||
|
||
// Then | ||
expect(result.current.isDirty).toBeFalsy() | ||
}) | ||
|
||
it('should set isDirty to true when the value is changed', () => { | ||
// Given | ||
const initialValue = 'initialValue' | ||
const { result } = renderHook(() => useInputValidation(initialValue)) | ||
|
||
// When | ||
const newValue = 'newValue' | ||
act(() => { | ||
result.current.setValue(newValue) | ||
}) | ||
|
||
// Then | ||
expect(result.current.isDirty).toBeTruthy() | ||
}) | ||
|
||
it('should set isDirty to false when the value is not changed', () => { | ||
// Given | ||
const initialValue = 'initialValue' | ||
const { result } = renderHook(() => useInputValidation(initialValue)) | ||
|
||
// When | ||
const newValue = 'initialValue' | ||
act(() => { | ||
result.current.setValue(newValue) | ||
}) | ||
|
||
// Then | ||
expect(result.current.isDirty).toBeFalsy() | ||
}) | ||
}) | ||
|
||
describe('isInvalid', () => { | ||
it('should set isInvalid to true when a validation fails', () => { | ||
// Given | ||
const initialValue = 'initialValue' | ||
const validations: Validation<string>[] = [ | ||
{ | ||
validator: () => false, | ||
errorMessage: 'error message1', | ||
}, | ||
{ | ||
validator: () => true, | ||
errorMessage: 'error message2', | ||
}, | ||
] | ||
|
||
// When | ||
const { result } = renderHook(() => | ||
useInputValidation(initialValue, validations), | ||
) | ||
|
||
// Then | ||
expect(result.current.isInvalid).toBeTruthy() | ||
}) | ||
|
||
it('should set isInvalid to false when all validations pass', () => { | ||
// Given | ||
const initialValue = 'initialValue' | ||
const validations: Validation<string>[] = [ | ||
{ | ||
validator: () => true, | ||
errorMessage: 'error message1', | ||
}, | ||
{ | ||
validator: () => true, | ||
errorMessage: 'error message2', | ||
}, | ||
{ | ||
validator: () => true, | ||
errorMessage: 'error message3', | ||
}, | ||
] | ||
|
||
// When | ||
const { result } = renderHook(() => | ||
useInputValidation(initialValue, validations), | ||
) | ||
|
||
// Then | ||
expect(result.current.isInvalid).toBeFalsy() | ||
}) | ||
}) | ||
|
||
describe('errorMessage', () => { | ||
it('should set errorMessage to the first validation that fails ', () => { | ||
// Given | ||
const initialValue = 'initialValue' | ||
const validations: Validation<string>[] = [ | ||
{ | ||
validator: () => false, | ||
errorMessage: 'error message1', | ||
}, | ||
{ | ||
validator: () => false, | ||
errorMessage: 'error message2', | ||
}, | ||
{ | ||
validator: () => false, | ||
errorMessage: 'error message3', | ||
}, | ||
] | ||
|
||
// When | ||
const { result } = renderHook(() => | ||
useInputValidation(initialValue, validations), | ||
) | ||
|
||
// Then | ||
expect(result.current.errorMessage).toEqual('error message1') | ||
}) | ||
|
||
it('should set errorMessage to empty string when all validations pass', () => { | ||
// Given | ||
const initialValue = 'initialValue' | ||
const validations: Validation<string>[] = [ | ||
{ | ||
validator: () => true, | ||
errorMessage: 'error message1', | ||
}, | ||
{ | ||
validator: () => true, | ||
errorMessage: 'error message2', | ||
}, | ||
{ | ||
validator: () => true, | ||
errorMessage: 'error message3', | ||
}, | ||
] | ||
|
||
// When | ||
const { result } = renderHook(() => | ||
useInputValidation(initialValue, validations), | ||
) | ||
|
||
// Then | ||
expect(result.current.errorMessage).toEqual('') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { copyToClipboard } from '@/lib/utils' | ||
|
||
describe('lib/utils/clipboard', () => { | ||
describe('copyToClipboard()', () => { | ||
beforeEach(() => { | ||
Object.assign(navigator, { | ||
clipboard: { | ||
writeText: jest.fn().mockImplementation(() => Promise.resolve()), | ||
}, | ||
}) | ||
}) | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks() | ||
}) | ||
|
||
it('Should Copy target string to clipboard', async () => { | ||
// Given | ||
const target = 'copy target string' | ||
|
||
// When | ||
await copyToClipboard(target) | ||
|
||
// Then | ||
expect(navigator.clipboard.writeText).toHaveBeenCalledWith(target) | ||
}) | ||
|
||
it('Should return resolved promise object when it is done', () => { | ||
// Given | ||
const target = 'copy target string' | ||
|
||
// When | ||
const result = copyToClipboard(target) | ||
|
||
// Then | ||
expect(result).toEqual(Promise.resolve()) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React from 'react' | ||
import Modal from '@/components/Modal' | ||
import TwoPolaroidsIcon from 'public/icons/twopolaroids.svg' | ||
|
||
interface LinkCopiedModalProps { | ||
isOpen: boolean | ||
onClose: () => void | ||
} | ||
|
||
const LinkCopiedModal = ({ | ||
isOpen, | ||
onClose: handleCloseModal, | ||
}: LinkCopiedModalProps) => { | ||
return ( | ||
<Modal isOpen={isOpen} onClose={handleCloseModal}> | ||
<Modal.CenterModal icon={<TwoPolaroidsIcon />}> | ||
<Modal.Title>링크가 복사되었습니다!</Modal.Title> | ||
<Modal.Content>{'POLABO를\n 지인들에게도 알려주세요!'}</Modal.Content> | ||
<Modal.CenterConfirm confirmText="확인" /> | ||
</Modal.CenterModal> | ||
</Modal> | ||
) | ||
} | ||
|
||
export default LinkCopiedModal |
Oops, something went wrong.