Skip to content
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
1 change: 1 addition & 0 deletions __mocks__/fileMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'test-file-stub';
1 change: 1 addition & 0 deletions __mocks__/styleMock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {};
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ module.exports = {
statements: 100,
},
},
moduleNameMapper: {
'\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': '<rootDir>/__mocks__/fileMock.js',
'\\.(css|less)$': '<rootDir>/__mocks__/styleMock.js',
},
Comment on lines +15 to +18
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 컴포넌트 안에서 CSS파일을 불러와 import 시킬 때 테스트가 깨지게 됩니다..
    그래서 jest 문서에 정적파일처리하는 내용이 있길래 문서와 같이 시도해봤지만, 해결되지 않았습니다.
  • 또한, 아니면 node_modules안에서 불러와서 그런가 생각해서 빼낸 후 다시 시도해보았지만, 역시 같은 이유로 테스트가 통과하지 못하였습니다.
  • 그래서 test가 관여하지 않는 index.jsximport를 시켜놓은 상태입니다.
import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css';

error

Copy link

@hannut91 hannut91 Nov 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

자바스크립트는 css 파일이나 image 파일들을 import할 수 없습니다. 그래서 webpack은 css-loader나 file-loader등을 사용해서 자바스크립트가 해당 리소스를 불러올 수 있도록 해줘야 합니다. 하지만 테스트 코드에서는 webpack으로 빌드한게 아니기 때문에 올바르게 동작하지 않습니다. 따라서 실제 코드에서는 동작하지만 테스트 코드에서는 필요없는 리소스들은 mocking해야 합니다.

jest.config.js에 다음과 같이 코드를 추가해서 리소스 파일을 불러올 때 마다 내가 지정한 가짜 코드를 불러오도록 할 수 있습니다.

module.exports = {
  moduleNameMapper: {
    '\\.(css|less)$': '<rootDir>/__mocks__/fileMock.js'
  },
};

<rootDir>/__mocks__/fileMock.js

module.exports = {};

See also

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

윤석님이 답해주신 해답을 어제도 문서를 보며 적용해보았지만 해결되지 않았습니다.
오늘 다시 해본 결과 잘 됩니다! 감사합니다. 🙏
아마 해결되지 않았던 이유는 test를 껐다가 재실행을 안해서 적용이 안되서 그런거 같습니다.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

다행이네요 👍

};
134 changes: 132 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@
"@emotion/styled": "^11.0.0",
"@reduxjs/toolkit": "^1.4.0",
"axios": "^0.21.0",
"draft-js": "^0.11.7",
"draftjs-to-html": "^0.9.1",
"moment": "^2.29.1",
"moment-timezone": "^0.5.32",
"qs": "^6.9.4",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-draft-wysiwyg": "^1.14.5",
"react-moment": "^1.0.0",
"react-redux": "^7.2.2",
"react-router-dom": "^5.2.0",
Expand All @@ -58,6 +61,7 @@
"file-loader": "^6.2.0",
"given2": "^2.1.7",
"html-webpack-plugin": "^4.5.0",
"identity-obj-proxy": "^3.0.0",
"jest": "^26.6.3",
"jest-plugin-context": "^2.9.0",
"json-server": "^0.16.3",
Expand Down
45 changes: 45 additions & 0 deletions src/components/write/WriteEditor.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import React, { useState } from 'react';

import styled from '@emotion/styled';

import { EditorState, convertToRaw } from 'draft-js';
import draftToHtml from 'draftjs-to-html';
import { Editor } from 'react-draft-wysiwyg';

import 'react-draft-wysiwyg/dist/react-draft-wysiwyg.css';

const WriteEditorWrapper = styled.div``;

const WriteEditor = ({ onChange }) => {
const [editorState, setEditorState] = useState(EditorState.createEmpty());

const handleChangeEditor = (state) => {
setEditorState(state);
onChange({
Comment on lines +17 to +18
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • coverage 만족못함..

name: 'contents',
value: draftToHtml(convertToRaw(state.getCurrentContent())),
});
};

return (
<WriteEditorWrapper>
<Editor
editorState={editorState}
onEditorStateChange={handleChangeEditor}
ariaLabel="contents"
placeholder="내용을 작성해주세요."
localization={{
locale: 'ko',
}}
toolbar={{
list: { inDropdown: true },
textAlign: { inDropdown: true },
link: { inDropdown: true },
history: { inDropdown: true },
}}
/>
</WriteEditorWrapper>
);
};

export default WriteEditor;
20 changes: 20 additions & 0 deletions src/components/write/WriteEditor.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react';

import { render } from '@testing-library/react';

import WriteEditor from './WriteEditor';

describe('WriteEditor', () => {
const handleChange = jest.fn();
const renderWriteButtons = () => render((
<WriteEditor
onChange={handleChange}
/>
));

it('render Write Editor', () => {
const { container } = renderWriteButtons();

expect(container).toHaveTextContent('내용을 작성해주세요.');
});
});
13 changes: 1 addition & 12 deletions src/components/write/WriteForm.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const WriteFormWrapper = styled.div``;

const WriteForm = ({ onChange, fields }) => {
const {
title, applyEndDate, personnel, contents,
title, applyEndDate, personnel,
} = fields;

const handleChange = (e) => {
Expand Down Expand Up @@ -46,17 +46,6 @@ const WriteForm = ({ onChange, fields }) => {
id="participants-number"
/>
</div>
<div>
{/* TODO: 추후 draft.js를 사용하여 변경 예정 */}
<textarea
rows="10"
cols="100"
name="contents"
value={contents}
onChange={handleChange}
placeholder="내용"
/>
</div>
</WriteFormWrapper>
);
};
Expand Down
3 changes: 1 addition & 2 deletions src/components/write/WriteForm.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ describe('WriteForm', () => {
const { getByLabelText, getByPlaceholderText } = renderWriteForm(WRITE_FORM);

const {
title, applyEndDate, personnel, contents,
title, applyEndDate, personnel,
} = WRITE_FORM;

expect(getByPlaceholderText('제목을 입력하세요')).toHaveValue(title);
expect(getByLabelText('모집 마감 날짜')).toHaveValue(applyEndDate);
expect(getByLabelText('참여 인원 수')).toHaveValue(personnel);
expect(getByPlaceholderText('내용')).toHaveTextContent(contents);
});

describe('listens change event', () => {
Expand Down
Loading