|
| 1 | +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | +import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'; |
| 3 | +import { JoinPage } from '../pages'; |
| 4 | +import wrapper from './helpers/wrapper'; |
| 5 | +import supabase from '@/supabase'; |
| 6 | +import '@testing-library/jest-dom'; |
| 7 | + |
| 8 | +vi.mock('@/supabase', () => ({ |
| 9 | + default: { |
| 10 | + auth: { |
| 11 | + signUp: vi.fn(), |
| 12 | + }, |
| 13 | + }, |
| 14 | +})); |
| 15 | + |
| 16 | +describe('Join test', () => { |
| 17 | + beforeEach(() => { |
| 18 | + render(<JoinPage />, { |
| 19 | + wrapper, |
| 20 | + }); |
| 21 | + window.HTMLDialogElement.prototype.showModal = vi.fn(); |
| 22 | + window.HTMLDialogElement.prototype.close = vi.fn(); |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + vi.resetAllMocks(); |
| 27 | + cleanup(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('이름이 유효하지 않다면 에러 텍스트를 보여줄 수 있어야 한다.', async () => { |
| 31 | + const nameInput = screen.getByLabelText(/join-name/i) as HTMLInputElement; |
| 32 | + fireEvent.change(nameInput, { |
| 33 | + target: { |
| 34 | + value: '1', |
| 35 | + }, |
| 36 | + }); |
| 37 | + await waitFor(() => {}); |
| 38 | + const errorNameText = screen.getByText(/이름을 입력해/); |
| 39 | + expect(errorNameText).toBeInTheDocument(); |
| 40 | + }); |
| 41 | + |
| 42 | + it('닉네임이 유효하지 않다면 에러 텍스트를 보여줄 수 있어야 한다.', async () => { |
| 43 | + const nickNameInput = screen.getByLabelText(/nickName/i) as HTMLInputElement; |
| 44 | + fireEvent.change(nickNameInput, { |
| 45 | + target: { |
| 46 | + value: '★☆★파이리☆★☆', |
| 47 | + }, |
| 48 | + }); |
| 49 | + await waitFor(() => {}); |
| 50 | + const errorNameText = screen.getByText(/닉네임은 2~12자 이내/); |
| 51 | + expect(errorNameText).toBeInTheDocument(); |
| 52 | + }); |
| 53 | + |
| 54 | + it('휴대폰 번호가 유효하지 않다면 에러 텍스트를 보여줄 수 있어야 한다.', async () => { |
| 55 | + const phoneInput = screen.getByLabelText(/phone/i) as HTMLInputElement; |
| 56 | + fireEvent.change(phoneInput, { |
| 57 | + target: { |
| 58 | + value: '010111', |
| 59 | + }, |
| 60 | + }); |
| 61 | + await waitFor(() => {}); |
| 62 | + const errorNameText = screen.getByText(/11자리를/); |
| 63 | + expect(errorNameText).toBeInTheDocument(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('이메일이 유효하지 않다면 에러 텍스트를 보여줄 수 있어야 한다.', async () => { |
| 67 | + const emailInput = screen.getByLabelText(/email/i) as HTMLInputElement; |
| 68 | + fireEvent.change(emailInput, { |
| 69 | + target: { |
| 70 | + value: 'asdfgh.com', |
| 71 | + }, |
| 72 | + }); |
| 73 | + await waitFor(() => {}); |
| 74 | + const errorNameText = screen.getByText(/올바른 이메일을/); |
| 75 | + expect(errorNameText).toBeInTheDocument(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('비밀번호가 유효하지 않다면 에러 텍스트를 보여줄 수 있어야 한다.', async () => { |
| 79 | + const passwordInput = screen.getByLabelText(/password/i) as HTMLInputElement; |
| 80 | + fireEvent.change(passwordInput, { |
| 81 | + target: { |
| 82 | + value: '123', |
| 83 | + }, |
| 84 | + }); |
| 85 | + await waitFor(() => {}); |
| 86 | + const errorNameText = screen.getByText(/비밀번호는 6자리 이상/); |
| 87 | + expect(errorNameText).toBeInTheDocument(); |
| 88 | + }); |
| 89 | + |
| 90 | + it('비밀번호 value와 비밀번호 확인 value가 일치하지 않다면 에러 텍스트를 보여줄 수 있어야 한다.', async () => { |
| 91 | + const passwordInput = screen.getByLabelText(/password/i) as HTMLInputElement; |
| 92 | + const pwCheckInput = screen.getByLabelText(/pwCheck/i) as HTMLInputElement; |
| 93 | + fireEvent.change(passwordInput, { |
| 94 | + target: { |
| 95 | + value: '123456', |
| 96 | + }, |
| 97 | + }); |
| 98 | + fireEvent.change(pwCheckInput, { |
| 99 | + target: { |
| 100 | + value: '123123', |
| 101 | + }, |
| 102 | + }); |
| 103 | + await waitFor(() => {}); |
| 104 | + expect(screen.getByText(/비밀번호가 일치하지/)).toBeInTheDocument(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('이용약관 및 개인정보 관련 체크를 하지 않으면 에러 텍스트를 보여줄 수 있어야 한다.', async () => { |
| 108 | + const useTermsCheckInput = screen.getByLabelText(/useTermsCheck/i) as HTMLInputElement; |
| 109 | + const privacyTermsCheckInput = screen.getByLabelText(/privacyTermsCheck/i) as HTMLInputElement; |
| 110 | + fireEvent.change(useTermsCheckInput, { |
| 111 | + target: { |
| 112 | + checked: false, |
| 113 | + }, |
| 114 | + }); |
| 115 | + fireEvent.change(privacyTermsCheckInput, { |
| 116 | + target: { |
| 117 | + checked: false, |
| 118 | + }, |
| 119 | + }); |
| 120 | + await waitFor(() => {}); |
| 121 | + expect(screen.getByText(/이용약관에 동의해/)).toBeInTheDocument(); |
| 122 | + expect(screen.getByText(/개인정보 수집, 이용에 동의해/)).toBeInTheDocument(); |
| 123 | + }); |
| 124 | + |
| 125 | + it('회원가입 입력정보가 유효하면 회원가입 버튼을 통해 회원가입을 할 수 있어야 한다.', async () => { |
| 126 | + const nameInput = screen.getByLabelText(/join-name/i) as HTMLInputElement; |
| 127 | + const nickNameInput = screen.getByLabelText(/nickName/i) as HTMLInputElement; |
| 128 | + const phoneInput = screen.getByLabelText(/phone/i) as HTMLInputElement; |
| 129 | + const emailInput = screen.getByLabelText(/email/i) as HTMLInputElement; |
| 130 | + const passwordInput = screen.getByLabelText(/password/i) as HTMLInputElement; |
| 131 | + const pwCheckInput = screen.getByLabelText(/pwCheck/i) as HTMLInputElement; |
| 132 | + const useTermsCheckInput = screen.getByLabelText(/useTermsCheck/i) as HTMLInputElement; |
| 133 | + const privacyTermsCheckInput = screen.getByLabelText(/privacyTermsCheck/i) as HTMLInputElement; |
| 134 | + |
| 135 | + fireEvent.change(nameInput, { |
| 136 | + target: { |
| 137 | + value: '방시혁', |
| 138 | + }, |
| 139 | + }); |
| 140 | + fireEvent.change(nickNameInput, { |
| 141 | + target: { |
| 142 | + value: 'BangTS', |
| 143 | + }, |
| 144 | + }); |
| 145 | + fireEvent.change(phoneInput, { |
| 146 | + target: { |
| 147 | + value: '010-1234-1234', |
| 148 | + }, |
| 149 | + }); |
| 150 | + fireEvent.change(emailInput, { |
| 151 | + target: { |
| 152 | + |
| 153 | + }, |
| 154 | + }); |
| 155 | + fireEvent.change(passwordInput, { |
| 156 | + target: { |
| 157 | + value: '111111', |
| 158 | + }, |
| 159 | + }); |
| 160 | + fireEvent.change(pwCheckInput, { |
| 161 | + target: { |
| 162 | + value: '111111', |
| 163 | + }, |
| 164 | + }); |
| 165 | + fireEvent.click(useTermsCheckInput, { |
| 166 | + target: { |
| 167 | + checked: true, |
| 168 | + }, |
| 169 | + }); |
| 170 | + fireEvent.click(privacyTermsCheckInput, { |
| 171 | + target: { |
| 172 | + checked: true, |
| 173 | + }, |
| 174 | + }); |
| 175 | + await waitFor(() => {}); |
| 176 | + const submitButton = screen.getByText('회원가입 하기') as HTMLButtonElement; |
| 177 | + fireEvent.click(submitButton); |
| 178 | + await waitFor(() => {}); |
| 179 | + expect(supabase.auth.signUp).toBeCalled(); |
| 180 | + }); |
| 181 | + |
| 182 | + it('회원가입 입력정보가 유효하지 않으면 회원가입 버튼을 통해 회원가입을 할 수 없어야 한다.', async () => { |
| 183 | + const nameInput = screen.getByLabelText(/join-name/i) as HTMLInputElement; |
| 184 | + const nickNameInput = screen.getByLabelText(/nickName/i) as HTMLInputElement; |
| 185 | + const phoneInput = screen.getByLabelText(/phone/i) as HTMLInputElement; |
| 186 | + const emailInput = screen.getByLabelText(/email/i) as HTMLInputElement; |
| 187 | + const passwordInput = screen.getByLabelText(/password/i) as HTMLInputElement; |
| 188 | + const pwCheckInput = screen.getByLabelText(/pwCheck/i) as HTMLInputElement; |
| 189 | + const useTermsCheckInput = screen.getByLabelText(/useTermsCheck/i) as HTMLInputElement; |
| 190 | + const privacyTermsCheckInput = screen.getByLabelText(/privacyTermsCheck/i) as HTMLInputElement; |
| 191 | + |
| 192 | + fireEvent.change(nameInput, { |
| 193 | + target: { |
| 194 | + value: '방시혁', |
| 195 | + }, |
| 196 | + }); |
| 197 | + fireEvent.change(nickNameInput, { |
| 198 | + target: { |
| 199 | + value: 'BangTS', |
| 200 | + }, |
| 201 | + }); |
| 202 | + fireEvent.change(phoneInput, { |
| 203 | + target: { |
| 204 | + value: '010-1234-1234', |
| 205 | + }, |
| 206 | + }); |
| 207 | + fireEvent.change(emailInput, { |
| 208 | + target: { |
| 209 | + |
| 210 | + }, |
| 211 | + }); |
| 212 | + fireEvent.change(passwordInput, { |
| 213 | + target: { |
| 214 | + value: '123456', |
| 215 | + }, |
| 216 | + }); |
| 217 | + fireEvent.change(pwCheckInput, { |
| 218 | + target: { |
| 219 | + value: '123456', |
| 220 | + }, |
| 221 | + }); |
| 222 | + fireEvent.change(useTermsCheckInput, { |
| 223 | + target: { |
| 224 | + checked: true, |
| 225 | + }, |
| 226 | + }); |
| 227 | + fireEvent.change(privacyTermsCheckInput, { |
| 228 | + target: { |
| 229 | + checked: true, |
| 230 | + }, |
| 231 | + }); |
| 232 | + await waitFor(() => {}); |
| 233 | + const submitButton = screen.getByText('회원가입 하기') as HTMLButtonElement; |
| 234 | + fireEvent.click(submitButton); |
| 235 | + await waitFor(() => expect(supabase.auth.signUp).toBeCalledTimes(0)); |
| 236 | + }); |
| 237 | +}); |
0 commit comments