|
1 | 1 | /* eslint-disable jest/no-conditional-expect */ |
| 2 | +import React, { useRef } from 'react'; |
2 | 3 | import TestRenderer from 'react-test-renderer'; |
| 4 | +import { fireEvent, render } from '@testing-library/react'; |
3 | 5 | import TextareaCodeEditor from '../'; |
4 | 6 |
|
5 | 7 | it('Should output a TextareaCodeEditor', async () => { |
6 | 8 | const component = TestRenderer.create(<TextareaCodeEditor />); |
7 | 9 | let tree = component.toJSON(); |
8 | 10 | if (tree && !Array.isArray(tree)) { |
9 | | - expect(tree.type).toEqual('button'); |
| 11 | + expect(tree.type).toEqual('div'); |
10 | 12 | expect(tree.props.disabled).toBeFalsy(); |
11 | | - expect(tree.props.className).toEqual('w-btn w-btn-default w-btn-primary'); |
| 13 | + expect(tree.props.className).toEqual('w-tc-editor '); |
| 14 | + expect(tree.props.style).toEqual({ |
| 15 | + position: 'relative', |
| 16 | + textAlign: 'left', |
| 17 | + boxSizing: 'border-box', |
| 18 | + padding: 0, |
| 19 | + overflow: 'hidden', |
| 20 | + }); |
| 21 | + expect(tree.children!.length).toEqual(2); |
| 22 | + tree.children!.forEach((child) => { |
| 23 | + if (typeof child === 'object') { |
| 24 | + expect(/^(div|textarea)$/.test(child.type || '')).toBeTruthy(); |
| 25 | + if (child.type === 'textarea') { |
| 26 | + expect(child.props.autoComplete).toEqual('off'); |
| 27 | + expect(child.props.autoCorrect).toEqual('off'); |
| 28 | + expect(child.props.spellCheck).toEqual('false'); |
| 29 | + expect(child.props.autoCapitalize).toEqual('off'); |
| 30 | + expect(child.props.className).toEqual('w-tc-editor-text'); |
| 31 | + } |
| 32 | + } |
| 33 | + }); |
| 34 | + } |
| 35 | +}); |
| 36 | + |
| 37 | +it('TextareaCodeEditor language="html"', async () => { |
| 38 | + const component = TestRenderer.create( |
| 39 | + <TextareaCodeEditor language="html" onChange={() => {}} value="<div>Hello World!</div>" />, |
| 40 | + ); |
| 41 | + let tree = component.toJSON(); |
| 42 | + if (tree && !Array.isArray(tree)) { |
| 43 | + expect(tree.type).toEqual('div'); |
| 44 | + expect(tree.props.disabled).toBeFalsy(); |
| 45 | + expect(tree.props.className).toEqual('w-tc-editor '); |
| 46 | + expect(tree.props.style).toEqual({ |
| 47 | + position: 'relative', |
| 48 | + textAlign: 'left', |
| 49 | + boxSizing: 'border-box', |
| 50 | + padding: 0, |
| 51 | + overflow: 'hidden', |
| 52 | + }); |
| 53 | + expect(tree.children!.length).toEqual(2); |
| 54 | + tree.children!.forEach((child) => { |
| 55 | + if (typeof child === 'object') { |
| 56 | + expect(/^(div|textarea)$/.test(child.type || '')).toBeTruthy(); |
| 57 | + if (child.type === 'textarea') { |
| 58 | + expect(child.props.autoComplete).toEqual('off'); |
| 59 | + expect(child.props.autoCorrect).toEqual('off'); |
| 60 | + expect(child.props.spellCheck).toEqual('false'); |
| 61 | + expect(child.props.autoCapitalize).toEqual('off'); |
| 62 | + expect(child.props.className).toEqual('w-tc-editor-text'); |
| 63 | + } |
| 64 | + if (child.type === 'div') { |
| 65 | + expect(child.props.style).toEqual({ |
| 66 | + paddingTop: 10, |
| 67 | + paddingRight: 10, |
| 68 | + paddingBottom: 10, |
| 69 | + paddingLeft: 10, |
| 70 | + }); |
| 71 | + expect(child.props.className).toEqual('w-tc-editor-preview language-html'); |
| 72 | + expect(typeof child.props.dangerouslySetInnerHTML).toEqual('object'); |
| 73 | + } |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | +}); |
| 78 | + |
| 79 | +it('TextareaCodeEditor onChange', async () => { |
| 80 | + const MyComponent = () => { |
| 81 | + const txtRef = useRef<HTMLTextAreaElement>(null); |
| 82 | + const [code] = React.useState(`function add(a, b) {\n return a + b;\n}`); |
| 83 | + return ( |
| 84 | + <TextareaCodeEditor |
| 85 | + language="js" |
| 86 | + value={code} |
| 87 | + ref={txtRef} |
| 88 | + onChange={(evn) => { |
| 89 | + expect(evn.target.value).toEqual('a'); |
| 90 | + }} |
| 91 | + /> |
| 92 | + ); |
| 93 | + }; |
| 94 | + |
| 95 | + const { |
| 96 | + container: { firstChild }, |
| 97 | + } = render(<MyComponent />); |
| 98 | + if (firstChild && firstChild.firstChild) { |
| 99 | + fireEvent.input(firstChild.firstChild, { target: { value: 'a' } }); |
12 | 100 | } |
13 | 101 | }); |
0 commit comments