aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/__tests__/components/Modal/OptionSpec.js
blob: a275aee61a897a9ee228d933e9880c1c9d74c16b (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
import React from 'react'
import renderer from 'react-test-renderer'
import { Options, ChoicesOption } from '../../../components/Modal/Option'

describe('BooleanOption Component', () => {
    let BooleanOption = Options['bool'],
        onChangeFn = jest.fn(),
        booleanOption = renderer.create(
            <BooleanOption value={true} onChange={onChangeFn}/>
        ),
        tree = booleanOption.toJSON()

    it('should render correctly', () => {
        expect(tree).toMatchSnapshot()
    })

    it('should handle onChange', () => {
        let input = tree.children[0].children[0],
            mockEvent = { target: { checked: true }}
        input.props.onChange(mockEvent)
        expect(onChangeFn).toBeCalledWith(mockEvent.target.checked)
    })
})

describe('StringOption Component', () => {
    let StringOption = Options['str'],
        onChangeFn = jest.fn(),
        stringOption = renderer.create(
            <StringOption value="foo" onChange={onChangeFn}/>
        ),
        tree = stringOption.toJSON()

    it('should render correctly', () => {
        expect(tree).toMatchSnapshot()
    })

    it('should handle onChange', () => {
        let mockEvent = { target: { value: 'bar' }}
        tree.props.onChange(mockEvent)
        expect(onChangeFn).toBeCalledWith(mockEvent.target.value)
    })

})

describe('NumberOption Component', () => {
    let NumberOption = Options['int'],
        onChangeFn = jest.fn(),
        numberOption = renderer.create(
            <NumberOption value={1} onChange={onChangeFn}/>
        ),
        tree = numberOption.toJSON()

    it('should render correctly', () => {
        expect(tree).toMatchSnapshot()
    })

    it('should handle onChange', () => {
        let mockEvent = {target: { value: '2'}}
        tree.props.onChange(mockEvent)
        expect(onChangeFn).toBeCalledWith(2)
    })
})

describe('ChoiceOption Component', () => {
    let onChangeFn = jest.fn(),
        choiceOption = renderer.create(
            <ChoicesOption value='a' choices={['a', 'b', 'c']} onChange={onChangeFn}/>
        ),
        tree = choiceOption.toJSON()

    it('should render correctly', () => {
        expect(tree).toMatchSnapshot()
    })

    it('should handle onChange', () => {
        let mockEvent = { target: {value: 'b'} }
        tree.props.onChange(mockEvent)
        expect(onChangeFn).toBeCalledWith(mockEvent.target.value)
    })
})

describe('StringOption Component', () => {
    let onChangeFn = jest.fn(),
        StringSequenceOption = Options['sequence of str'],
        stringSequenceOption = renderer.create(
            <StringSequenceOption value={['a', 'b']} onChange={onChangeFn}/>
        ),
        tree = stringSequenceOption.toJSON()

    it('should render correctly', () => {
        expect(tree).toMatchSnapshot()
    })

    it('should handle onChange', () => {
        let mockEvent = { target: {value: 'a\nb\nc\n'}}
        tree.props.onChange(mockEvent)
        expect(onChangeFn).toBeCalledWith(['a', 'b', 'c', ''])
    })
})