aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/__tests__/components/ValueEditor/ValueEditorSpec.js
blob: 9b2796cba5960c7e02aa99951ab38555efdfda74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import React from 'react'
import renderer from 'react-test-renderer'
import TestUtils from 'react-dom/test-utils'
import ValueEditor from '../../../components/ValueEditor/ValueEditor'
import { Key } from '../../../utils'

describe('ValueEditor Component', () => {

    let mockFn = jest.fn()
    it ('should render correctly', () => {
        let valueEditor = renderer.create(
            <ValueEditor content="foo" onDone={mockFn}/>
        ),
            tree = valueEditor.toJSON()
        expect(tree).toMatchSnapshot()
    })

    let valueEditor = TestUtils.renderIntoDocument(
        <ValueEditor content="<script>foo</script>" onDone={mockFn}/>
    )
    it('should handle this.blur', () => {
        valueEditor.input.blur = jest.fn()
        valueEditor.blur()
        expect(valueEditor.input.blur).toHaveBeenCalled()
    })

    it('should handle reset', () => {
        valueEditor.reset()
        expect(valueEditor.input.innerHTML).toEqual(
            _.escape("<script>foo</script>")
        )
    })

    it('should handle paste', () => {
        let mockEvent = {
            preventDefault: jest.fn(),
            clipboardData: { getData: (t) => "foo content"}
        }
        document.execCommand = jest.fn()
        valueEditor.onPaste(mockEvent)
        expect(document.execCommand).toBeCalledWith('insertHTML', false, "foo content")
    })

    it('should handle mouseDown', () => {
        window.addEventListener = jest.fn()
        valueEditor.onMouseDown({})
        expect(valueEditor._mouseDown).toBeTruthy()
        expect(window.addEventListener).toBeCalledWith('mouseup', valueEditor.onMouseUp)
    })

    it('should handle mouseUp', () => {
        window.removeEventListener = jest.fn()
        valueEditor.onMouseUp()
        expect(window.removeEventListener).toBeCalledWith('mouseup', valueEditor.onMouseUp)
    })

    it('should handle focus', () => {
        let mockEvent = { clientX: 1, clientY: 2 },
            mockSelection = {
                rangeCount: 1,
                getRangeAt: jest.fn( (index) => {return { selectNodeContents: jest.fn() }}),
                removeAllRanges: jest.fn(),
                addRange: jest.fn()
            },
            clearState = (v) => {
                v._mouseDown = false
                v._ignore_events = false
                v.state.editable = false
            }
        window.getSelection = () => mockSelection

        // return undefined when mouse down
        valueEditor.onMouseDown()
        expect(valueEditor.onFocus(mockEvent)).toEqual(undefined)
        valueEditor.onMouseUp()

        // sel.rangeCount > 0
        valueEditor.onFocus(mockEvent)
        expect(mockSelection.getRangeAt).toBeCalledWith(0)
        expect(valueEditor.state.editable).toBeTruthy()
        expect(mockSelection.removeAllRanges).toBeCalled()
        expect(mockSelection.addRange).toBeCalled()
        clearState(valueEditor)

        // document.caretPositionFromPoint
        mockSelection.rangeCount = 0
        let mockRange = { setStart: jest.fn(), selectNodeContents: jest.fn() }

        document.caretPositionFromPoint = jest.fn((x, y) => {
            return { offsetNode: 0, offset: x + y}
        })
        document.createRange = jest.fn(() => mockRange)
        valueEditor.onFocus(mockEvent)
        expect(mockRange.setStart).toBeCalledWith(0, 3)
        clearState(valueEditor)
        document.caretPositionFromPoint = null

        //document.caretRangeFromPoint
        document.caretRangeFromPoint = jest.fn(() => mockRange)
        valueEditor.onFocus(mockEvent)
        expect(document.caretRangeFromPoint).toBeCalledWith(1, 2)
        clearState(valueEditor)
        document.caretRangeFromPoint = null

        //else
        valueEditor.onFocus(mockEvent)
        expect(mockRange.selectNodeContents).toBeCalledWith(valueEditor.input)
        clearState(valueEditor)
    })

    it('should handle click', () => {
        valueEditor.onMouseUp = jest.fn()
        valueEditor.onFocus = jest.fn()
        valueEditor.onClick('foo')
        expect(valueEditor.onMouseUp).toBeCalled()
        expect(valueEditor.onFocus).toBeCalledWith('foo')
    })

    it('should handle blur', () => {
        // return undefined
        valueEditor._ignore_events = true
        expect(valueEditor.onBlur({})).toEqual(undefined)
        // else
        valueEditor._ignore_events = false
        valueEditor.onBlur({})
        expect(valueEditor.state.editable).toBeFalsy()
        expect(valueEditor.props.onDone).toBeCalledWith(valueEditor.input.textContent)
    })

    it('should handle key down', () => {
        let mockKeyEvent = (keyCode, shiftKey=false) => {
            return {
                keyCode: keyCode,
                shiftKey: shiftKey,
                stopPropagation: jest.fn(),
                preventDefault: jest.fn()
            }
        }
        valueEditor.reset = jest.fn()
        valueEditor.blur = jest.fn()
        valueEditor.onKeyDown(mockKeyEvent(Key.ESC))
        expect(valueEditor.reset).toBeCalled()
        expect(valueEditor.blur).toBeCalled()
        valueEditor.blur.mockReset()

        valueEditor.onKeyDown(mockKeyEvent(Key.ENTER))
        expect(valueEditor.blur).toBeCalled()

        valueEditor.onKeyDown(mockKeyEvent(Key.SPACE))
    })

    it('should handle input', () => {
        valueEditor.onInput()
    })
})