aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/__tests__/components/Header/FilterInputSpec.js
blob: 6b19770e6bf350311765f87eb7269a8ef0a7529e (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
import React from 'react'
import renderer from 'react-test-renderer'
import FilterInput from '../../../components/Header/FilterInput'
import FilterDocs from '../../../components/Header/FilterDocs'
import TestUtil from 'react-dom/test-utils'
import ReactDOM from 'react-dom'
import { Key } from '../../../utils'

describe('FilterDocs Component', () => {
    it('should render correctly', () => {
        let filterInput = renderer.create(<FilterInput type='foo' color='red' placeholder='bar'/>),
            tree = filterInput.toJSON()
        expect(tree).toMatchSnapshot()
    })

    let filterInput = TestUtil.renderIntoDocument(
        <FilterInput type='foo' color='red' placeholder='bar' value='' onChange={jest.fn()}/>)
    it('should handle componentWillReceiveProps', () => {
       filterInput.componentWillReceiveProps({value: 'foo'})
        expect(filterInput.state.value).toEqual('foo')
    })

    it('should handle isValid', () => {
        // valid
        expect(filterInput.isValid("~u foo")).toBeTruthy()
        expect(filterInput.isValid("~foo bar")).toBeFalsy()
    })

    it('should handle getDesc', () => {
        filterInput.state.value = ''
        expect(filterInput.getDesc().type).toEqual(FilterDocs)

        filterInput.state.value = '~u foo'
        expect(filterInput.getDesc()).toEqual('url matches /foo/i')

        filterInput.state.value = '~foo bar'
        expect(filterInput.getDesc()).toEqual('SyntaxError: Expected filter expression but \"~\" found.')
    })

    it('should handle change', () => {
        let mockEvent = { target: { value: '~a bar'} }
        filterInput.onChange(mockEvent)
        expect(filterInput.state.value).toEqual('~a bar')
        expect(filterInput.props.onChange).toBeCalledWith('~a bar')
    })

    it('should handle focus', () => {
        filterInput.onFocus()
        expect(filterInput.state.focus).toBeTruthy()
    })

    it('should handle blur', () => {
        filterInput.onBlur()
        expect(filterInput.state.focus).toBeFalsy()
    })

    it('should handle mouseEnter', () => {
        filterInput.onMouseEnter()
        expect(filterInput.state.mousefocus).toBeTruthy()
    })

    it('should handle mouseLeave', () => {
        filterInput.onMouseLeave()
        expect(filterInput.state.mousefocus).toBeFalsy()
    })

    let input = ReactDOM.findDOMNode(filterInput.refs.input)

    it('should handle keyDown', () => {
        input.blur = jest.fn()
        let mockEvent = {
            keyCode: Key.ESC,
            stopPropagation: jest.fn()
        }
        filterInput.onKeyDown(mockEvent)
        expect(input.blur).toBeCalled()
        expect(filterInput.state.mousefocus).toBeFalsy()
        expect(mockEvent.stopPropagation).toBeCalled()
    })

    it('should handle selectFilter', () => {
        input.focus = jest.fn()
        filterInput.selectFilter('bar')
        expect(filterInput.state.value).toEqual('bar')
        expect(input.focus).toBeCalled()
    })

    it('should handle select', () => {
        input.select = jest.fn()
        filterInput.select()
        expect(input.select).toBeCalled()
    })
})