Skip to content

Commit

Permalink
Merge pull request #93 from saseungmin/refactor/typescript
Browse files Browse the repository at this point in the history
[WIP] refactor: wip 타입스트립트로 마이그레이션
  • Loading branch information
saseungmin committed Dec 28, 2023
2 parents 5b771c0 + 19899f5 commit 25f16d7
Show file tree
Hide file tree
Showing 128 changed files with 445 additions and 247 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
10 changes: 10 additions & 0 deletions @types/emotion.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import '@emotion/react';

import { lightTheme } from '../src/styles/theme';

declare module '@emotion/react' {
type CustomTheme = typeof lightTheme;

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface Theme extends CustomTheme {}
}
5 changes: 5 additions & 0 deletions @types/svg.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module '*.svg' {
const content: React.FunctionComponent<React.SVGAttributes<SVGElement>>;

export default content;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const authResultState = {
authSuccess: null,
};

export const userState = {
export const userState: { user: null | string; checkError: any } = {
user: null,
checkError: null,
};
Expand Down
File renamed without changes.
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"react-toggle": "^4.1.2",
"react-tooltip": "^4.2.13",
"react-use": "^17.1.1",
"recoil": "^0.3.1",
"recoil": "0.7.7",
"universal-cookie": "^4.0.4"
},
"devDependencies": {
Expand All @@ -57,7 +57,13 @@
"@svgr/webpack": "^5.5.0",
"@testing-library/jest-dom": "^5.11.9",
"@testing-library/react": "^11.2.3",
"@types/facepaint": "1.2.5",
"@types/jest": "^26.0.20",
"@types/jest-plugin-context": "2.9.7",
"@types/lodash": "4.14.202",
"@types/react-dom": "18.2.18",
"@types/react-responsive": "8.0.8",
"@types/react-toggle": "4.0.5",
"@typescript-eslint/eslint-plugin": "5.51.0",
"@typescript-eslint/parser": "5.51.0",
"babel-jest": "^26.6.3",
Expand Down
10 changes: 5 additions & 5 deletions src/App.test.jsx → src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import { todoResultState, userState, authState } from '../fixtures/recoil-atom-s
import App from './App';
import InjectTestingRecoilState from './components/common/InjectTestingRecoilState';

const mockGetApi = (response) => mockAxios.get.mockResolvedValueOnce(response);
const mockGetApi = (response: any) => (mockAxios.get as jest.Mock).mockResolvedValueOnce(response);

const mockPostApi = (response) => mockAxios.post.mockResolvedValueOnce(response);
const mockPostApi = (response: any) => (mockAxios.post as jest.Mock).mockResolvedValueOnce(response);

jest.mock('./services/storage');
describe('App', () => {
Expand Down Expand Up @@ -233,7 +233,7 @@ describe('App', () => {
response = renderApp({ user: mockUserState });
});

expect(response.container).toHaveTextContent('할 일2');
expect((response as any).container).toHaveTextContent('할 일2');
});

describe('when logged in have success status', () => {
Expand All @@ -242,7 +242,7 @@ describe('App', () => {
};

beforeEach(() => {
loadItem.mockImplementation(() => user);
(loadItem as jest.Mock).mockImplementation(() => user);
});

it('has the user session value, so the Sign out button is visible.', async () => {
Expand All @@ -253,7 +253,7 @@ describe('App', () => {
response = renderApp({ user: mockUserState });
});

expect(response.container).toHaveTextContent('Sign out');
expect((response as any).container).toHaveTextContent('Sign out');
});
});

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/__mocks__/axios.js → src/__mocks__/axios.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const mockAxios = jest.genMockFromModule('axios');

mockAxios.create.mockReturnThis();
(mockAxios as any).create.mockReturnThis();

export default mockAxios;
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';

import styled from '@emotion/styled';
import { RegisterOptions } from 'react-hook-form';
import palette from '../../styles/palette';

const AuthInputWrapper = styled.input`
Expand Down Expand Up @@ -48,7 +49,12 @@ const authFieldsProperty = {
},
};

function AuthInput({ inputRef, inputName }) {
type Props = {
inputRef: any;
inputName: 'userId' | 'password' | 'passwordConfirm';
};

function AuthInput({ inputRef, inputName }: Props) {
const { inputType, placeholder, autoComplete } = authFieldsProperty[inputName];

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import InjectTestingRecoilState from '../common/InjectTestingRecoilState';
import AuthModalForm from './AuthModalForm';

describe('AuthModalForm', () => {
const handleSubmit = jest.fn();

beforeEach(() => {
jest.clearAllMocks();
});
Expand All @@ -20,7 +22,7 @@ describe('AuthModalForm', () => {
<InjectTestingRecoilState
auth={given.auth}
/>
<AuthModalForm />
<AuthModalForm onSubmit={handleSubmit} />
</SnackbarProvider>
</RecoilRoot>
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { css } from '@emotion/react';

import { useForm } from 'react-hook-form';

import { AuthForm } from 'src/types/auth';
import mq from '../../styles/responsive';
import palette from '../../styles/palette';

Expand All @@ -17,7 +18,7 @@ import { authFormStatusAtom } from '../../recoil/auth';
import CloseIcon from '../../assets/icons/close.svg';
import AuthInput from './AuthInput';

const AuthModalFormWrapper = styled.div`
const AuthModalFormWrapper = styled.div<{ visible: boolean; }>`
top: 0;
left: 0;
position: fixed;
Expand Down Expand Up @@ -98,8 +99,12 @@ const SubmitButton = styled.button`
}
`;

function AuthModalForm({ onSubmit }) {
const { register, handleSubmit } = useForm();
type Props = {
onSubmit: (form: AuthForm) => void;
};

function AuthModalForm({ onSubmit }: Props) {
const { register, handleSubmit } = useForm<AuthForm>();

const { type, visible } = useRecoilValue(authFormStatusAtom);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ describe('AuthStatus', () => {
];

it('when sign up is successful, renders Sign in modal', async () => {
mockAxios.post.mockResolvedValueOnce({ data: { access_token: mockToken } });
mockAxios.get.mockRejectedValueOnce({ response: { status: 403 } });
(mockAxios.post as jest.Mock).mockResolvedValueOnce({ data: { access_token: mockToken } });
(mockAxios.get as jest.Mock).mockRejectedValueOnce({ response: { status: 403 } });

const { container } = renderAuthStatus();

Expand Down Expand Up @@ -94,8 +94,8 @@ describe('AuthStatus', () => {
];

it('when login is successful, renders success message', async () => {
mockAxios.post.mockResolvedValueOnce({ data: { access_token: mockToken } });
mockAxios.get.mockRejectedValueOnce({ response: { status: 403 } });
(mockAxios.post as jest.Mock).mockResolvedValueOnce({ data: { access_token: mockToken } });
(mockAxios.get as jest.Mock).mockRejectedValueOnce({ response: { status: 403 } });

const { container } = renderAuthStatus();

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ describe('LoginForm', () => {
},
};

mockAxios.post.mockRejectedValueOnce(mockData);
mockAxios.get.mockResolvedValueOnce(mockData);
(mockAxios.post as jest.Mock).mockRejectedValueOnce(mockData);
(mockAxios.get as jest.Mock).mockResolvedValueOnce(mockData);

const { container } = renderLoginForm();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import React, { useEffect } from 'react';

import { useResetRecoilState, useRecoilValue } from 'recoil';

import { useSnackbar } from 'notistack';
import { VariantType, useSnackbar } from 'notistack';

import { AuthForm } from 'src/types/auth';
import { isCheckValidate } from '../../utils/utils';
import { FORM_TYPE } from '../../utils/constants/constants';
import { EMPTY_AUTH_INPUT } from '../../utils/constants/messages';
Expand All @@ -27,11 +28,14 @@ function LoginForm() {

const resetAuthFormState = useResetRecoilState(authFormStatusAtom);

const snackbar = (variant) => (message) => enqueueSnackbar(message, { variant });
const snackbar = (
variant: VariantType,
) => (message: string) => enqueueSnackbar(message, { variant });

const errorSnackbar = snackbar('error');
const successSnackbar = snackbar('success');

const onSubmit = (formData) => {
const onSubmit = (formData: AuthForm) => {
if (!isCheckValidate(formData)) {
errorSnackbar(EMPTY_AUTH_INPUT);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ describe('RegisterForm', () => {
},
};

mockAxios.post.mockRejectedValueOnce(mockData);
(mockAxios.post as jest.Mock).mockRejectedValueOnce(mockData);

renderRegisterForm();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useSetRecoilState, useRecoilValue } from 'recoil';

import { useSnackbar } from 'notistack';

import { AuthForm } from 'src/types/auth';
import authResultAtom, { authFormStatusAtom, authWithRegister } from '../../recoil/auth';

import useAuthCallback from '../../hooks/useAuthCallback';
Expand All @@ -22,11 +23,11 @@ function RegisterForm() {
const setLoginVisible = useSetRecoilState(authFormStatusAtom);
const { authSuccess } = useRecoilValue(authResultAtom);

const errorSnackbar = (message) => enqueueSnackbar(message, {
const errorSnackbar = (message: string) => enqueueSnackbar(message, {
variant: 'error',
});

const onSubmit = (formData) => {
const onSubmit = (formData: AuthForm) => {
if (!isCheckValidate(formData)) {
errorSnackbar(EMPTY_AUTH_INPUT);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import AuthStatusSnackbar from './AuthStatusSnackbar';
import InjectTestingRecoilState from './InjectTestingRecoilState';

describe('AuthStatusSnackbar', () => {
const renderAuthStatusSnackbar = (state) => render((
const renderAuthStatusSnackbar = (state?: any) => render((
<RecoilRoot>
<InjectTestingRecoilState
user={given.user}
Expand All @@ -24,7 +24,7 @@ describe('AuthStatusSnackbar', () => {
</RecoilRoot>
));

const setAuthState = (state) => ({
const setAuthState = (state: any) => ({
...authResultState,
...state,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useRecoilState, useRecoilValue, useResetRecoilState } from 'recoil';

import { useSnackbar } from 'notistack';

import { AuthResultAtomType } from 'src/recoil/auth/atom';
import authResultAtom from '../../recoil/auth';
import userAtom from '../../recoil/user';

Expand All @@ -14,7 +15,7 @@ function AuthStatusSnackbar() {

const { enqueueSnackbar } = useSnackbar();

const resetState = (state) => setAuthState((prevState) => ({
const resetState = (state: Partial<AuthResultAtomType>) => setAuthState((prevState) => ({
...prevState,
...state,
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ function InjectTestingRecoilState({
const setThemeState = useSetRecoilState(themeModeAtom);

useEffect(() => {
setUserState(user);
setAuthState(auth);
setUserState(user as any);
setAuthState(auth as any);
setTodosState(todos);
setFilterState(filter);
setFilterState(filter as any);
setAuthResultState(authResult);
setLoadingState(isLoading);
setThemeState(theme);
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import TodoStatusSnackbar from './TodoStatusSnackbar';
import InjectTestingRecoilState from './InjectTestingRecoilState';

describe('TodoStatusSnackbar', () => {
const renderTodoStatusSnackbar = (state) => render((
const renderTodoStatusSnackbar = (state: any) => render((
<RecoilRoot>
<InjectTestingRecoilState
todos={state}
Expand All @@ -23,7 +23,7 @@ describe('TodoStatusSnackbar', () => {
</RecoilRoot>
));

const setTodoState = (state) => ({
const setTodoState = (state: any) => ({
...todoResultState,
...state,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { useRecoilState } from 'recoil';

import { useSnackbar } from 'notistack';

import todosResultAtom from '../../recoil/todos/atom';
import todosResultAtom, { TodosResultAtomType } from '../../recoil/todos/atom';

function TodoStatusSnackbar() {
const [{ todoError, todoSuccess }, setTodoState] = useRecoilState(todosResultAtom);

const { enqueueSnackbar } = useSnackbar();

const resetState = (state) => setTodoState((prevState) => ({
const resetState = (state: Partial<TodosResultAtomType>) => setTodoState((prevState) => ({
...prevState,
...state,
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import ToggleThemeButton from './ToggleThemeButton';
import InjectTestingRecoilState from './InjectTestingRecoilState';

describe('ToggleThemeButton', () => {
const renderToggleThemeButton = (theme) => render((
const renderToggleThemeButton = (theme: boolean) => render((
<RecoilRoot>
<InjectTestingRecoilState
theme={theme}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ const ThemeButtonWrapper = styled.div`
})};
display: flex;
justify-content: flex-end;
justify-content: flex-end;
`;

function ToggleThemeButton() {
const [theme, setTheme] = useRecoilState(themeWithChange);

const handleToggle = useCallback(() => setTheme(), [setTheme]);
const handleToggle = useCallback(() => setTheme(!theme), [theme]);

return (
<ThemeButtonWrapper>
Expand Down
11 changes: 0 additions & 11 deletions src/components/footer/ExternalLink.jsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { render } from '@testing-library/react';
import ExternalLink from './ExternalLink';

describe('Link', () => {
const renderLink = (url) => render((
const renderLink = (url: string) => render((
<ExternalLink
link={url}
>
Expand Down

0 comments on commit 25f16d7

Please sign in to comment.