Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] 기업 자동응답 Form 빈 문자열 체크 로직 변경 및 자동응답 추가 테스트 코드 작성 #51

Merged
merged 3 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/components/auto-reply-company/AutoReplyAddForm.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { fireEvent, screen, waitFor } from '@testing-library/react';

import { render } from '../../test-helper';

import AutoReplyAddForm from './AutoReplyAddForm';

const context = describe;

const store = {
addAutoReply: jest.fn(),
};

jest.mock('../../hooks/useAutoReplyFormStore', () => () => [{}, store]);

describe('<AutoReplyAddForm />', () => {
it('renders auto reply add form', () => {
render(<AutoReplyAddForm />);

screen.getByLabelText('질문');
screen.getByLabelText('답변');

screen.getByRole('button', { name: '저장하기' });
});

context('when submitting form', () => {
it('execute addAutoReply function in store', async () => {
render(<AutoReplyAddForm />);

fireEvent.submit(screen.getByTestId('auto-reply-add-form'));

await waitFor(() => {
expect(store.addAutoReply).toHaveBeenCalled();
});
});
});
});
4 changes: 2 additions & 2 deletions src/components/auto-reply-company/AutoReplyAddForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ export default function AutoReplyAddForm() {
}, [done]);

const onSubmit = (data: FormValues) => {
store.addAutoReply(data.question, data.answer.trim());
store.addAutoReply(data.question, data.answer);
};

return (
<div>
<form
onSubmit={handleSubmit(onSubmit)}
data-testid="auto-reply-form"
data-testid="auto-reply-add-form"
>
<Controller
control={control}
Expand Down
8 changes: 4 additions & 4 deletions src/components/auto-reply-company/AutoReplyEditForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ describe('<AutoReplyEditForm />', () => {
});

context('when submitting form', () => {
it('execute uploadLoginUser function in store', async () => {
it('execute modifyAutoReply function in store', async () => {
render(<AutoReplyEditForm
id={1}
question="question"
answer="answer"
id={autoReply.id}
question={autoReply.question}
answer={autoReply.answer}
/>);

fireEvent.submit(screen.getByTestId('auto-reply-edit-form'));
Expand Down
25 changes: 19 additions & 6 deletions src/stores/AutoReplyFormStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,21 @@ export default class AutoReplyFormStore {
answer: string,
) {
try {
if (!question) {
const trimQuestion = question.trim();
const trimAnswer = answer.trim();

if (!trimQuestion) {
throw Error('질문을 작성해주세요');
}

if (!answer) {
if (!trimAnswer) {
throw Error('답변을 작성해주세요');
}

await apiService.createAutoReply({ question, answer });
await apiService.createAutoReply({
question: trimQuestion,
answer: trimAnswer,
});

this.setDone();
} catch (e) {
Expand All @@ -73,15 +79,22 @@ export default class AutoReplyFormStore {
answer: string,
) {
try {
if (!question) {
const trimQuestion = question.trim();
const trimAnswer = answer.trim();

if (!trimQuestion) {
throw Error('질문을 작성해주세요');
}

if (!answer) {
if (!trimAnswer) {
throw Error('답변을 작성해주세요');
}

await apiService.modifyAutoReply({ id, question, answer });
await apiService.modifyAutoReply({
id,
question: trimQuestion,
answer: trimAnswer,
});

this.setDone();
} catch (e) {
Expand Down
Loading