|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +import { fireEvent, render } from '@testing-library/react'; |
| 4 | + |
| 5 | +import TagsForm from './TagsForm'; |
| 6 | + |
| 7 | +describe('TagsForm', () => { |
| 8 | + const handleChange = jest.fn(); |
| 9 | + |
| 10 | + beforeEach(() => { |
| 11 | + handleChange.mockClear(); |
| 12 | + }); |
| 13 | + |
| 14 | + const renderTagsTagsForm = (tags) => render(( |
| 15 | + <TagsForm |
| 16 | + tags={tags} |
| 17 | + onChange={handleChange} |
| 18 | + /> |
| 19 | + )); |
| 20 | + |
| 21 | + describe('render Tag Form Container contents text', () => { |
| 22 | + it('renders tag form text', () => { |
| 23 | + const { getByPlaceholderText, container } = renderTagsTagsForm([]); |
| 24 | + |
| 25 | + expect(getByPlaceholderText('태그를 입력하세요')).not.toBeNull(); |
| 26 | + expect(container).toHaveTextContent('태그'); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('Check input tag validate', () => { |
| 31 | + context('with tag input value', () => { |
| 32 | + const tags = ['JavaScript', 'React']; |
| 33 | + it('listens write field change events', () => { |
| 34 | + const { getByPlaceholderText } = renderTagsTagsForm(['CSS']); |
| 35 | + |
| 36 | + const input = getByPlaceholderText('태그를 입력하세요'); |
| 37 | + |
| 38 | + tags.forEach((tag) => { |
| 39 | + fireEvent.change(input, { target: { value: tag } }); |
| 40 | + |
| 41 | + fireEvent.keyPress(input, { key: 'Enter', code: 13, charCode: 13 }); |
| 42 | + |
| 43 | + expect(input).toHaveValue(''); |
| 44 | + }); |
| 45 | + expect(handleChange).toBeCalledTimes(2); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + context('without tag input value', () => { |
| 50 | + const tags = []; |
| 51 | + it("doesn't listens write field change events", () => { |
| 52 | + const { getByPlaceholderText } = renderTagsTagsForm(tags); |
| 53 | + |
| 54 | + const input = getByPlaceholderText('태그를 입력하세요'); |
| 55 | + |
| 56 | + fireEvent.change(input, { target: { value: '' } }); |
| 57 | + |
| 58 | + fireEvent.keyPress(input, { key: 'Enter', code: 13, charCode: 13 }); |
| 59 | + |
| 60 | + expect(handleChange).not.toBeCalled(); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('It is not executed because the current tag value is included.', () => { |
| 65 | + const tags = ['JavaScript', 'React']; |
| 66 | + it('listens write field change events', () => { |
| 67 | + const { getByPlaceholderText } = renderTagsTagsForm(tags); |
| 68 | + |
| 69 | + const input = getByPlaceholderText('태그를 입력하세요'); |
| 70 | + |
| 71 | + tags.forEach((tag) => { |
| 72 | + fireEvent.change(input, { target: { value: tag } }); |
| 73 | + |
| 74 | + fireEvent.keyPress(input, { key: 'Enter', code: 13, charCode: 13 }); |
| 75 | + |
| 76 | + expect(input).toHaveValue(''); |
| 77 | + |
| 78 | + expect(handleChange).not.toBeCalled(); |
| 79 | + }); |
| 80 | + }); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + context('with tags', () => { |
| 85 | + const tags = ['JavaScript', 'React']; |
| 86 | + |
| 87 | + it('renders Tags are below the input window', () => { |
| 88 | + const { container } = renderTagsTagsForm(tags); |
| 89 | + |
| 90 | + expect(container).toHaveTextContent('#JavaScript'); |
| 91 | + expect(container).toHaveTextContent('#React'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('Click event remove tag', () => { |
| 95 | + const { getByText, container } = renderTagsTagsForm(tags); |
| 96 | + |
| 97 | + tags.forEach((tag) => { |
| 98 | + fireEvent.click(getByText(`#${tag}`)); |
| 99 | + |
| 100 | + expect(container).not.toHaveTextContent(`#${tag}`); |
| 101 | + }); |
| 102 | + |
| 103 | + expect(handleChange).toBeCalledTimes(2); |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + context('without tags', () => { |
| 108 | + const tags = []; |
| 109 | + |
| 110 | + it('renders Tags are below the input window', () => { |
| 111 | + const { container } = renderTagsTagsForm(tags); |
| 112 | + |
| 113 | + expect(container).not.toHaveTextContent('#JavaScript'); |
| 114 | + expect(container).not.toHaveTextContent('#React'); |
| 115 | + }); |
| 116 | + }); |
| 117 | +}); |
0 commit comments