aboutsummaryrefslogtreecommitdiffstats
path: root/web/src/js/__tests__/components/FlowView/MessagesSpec.js
blob: 02db77e8fbc979f84b245b736fe3fa02799d4777 (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
jest.mock('../../../components/ContentView')
import React from 'react'
import renderer from 'react-test-renderer'
import TestUtils from 'react-dom/test-utils'
import { Request, Response, ErrorView } from '../../../components/FlowView/Messages'
import { Provider } from 'react-redux'
import { TFlow, TStore } from '../../ducks/tutils'
import { updateEdit } from '../../../ducks/ui/flow'
import { parseUrl } from '../../../flow/utils'
import ContentView from '../../../components/ContentView'
import ContentViewOptions from '../../../components/ContentView/ContentViewOptions'
import Headers from '../../../components/FlowView/Headers'
import ValueEditor from '../../../components/ValueEditor/ValueEditor'

global.fetch = jest.fn()

let tflow = new TFlow(),
    store = TStore()
store.getState().ui.flow.modifiedFlow = false

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

    afterEach(() => {store.clearActions()})

    it('should render correctly', () => {
        let provider = renderer.create(
            <Provider store={store}>
                <Request/>
            </Provider>
            ),
            tree = provider.toJSON()
        expect(tree).toMatchSnapshot()
    })

    let provider = TestUtils.renderIntoDocument(
        <Provider store={store}>
            <Request/>
        </Provider>),
        valueEditors = TestUtils.scryRenderedComponentsWithType(provider, ValueEditor)

    it('should handle done on flow request method', () => {
        let valueEditor = valueEditors[0]
        valueEditor.props.onDone('foo')
        expect(store.getActions()).toEqual([updateEdit({ request: { method: 'foo' }})])
    })

    it('should handle done on flow request url', () => {
        let valueEditor = valueEditors[1],
            url = 'http://foo/bar'
        valueEditor.props.onDone(url)
        expect(store.getActions()).toEqual([updateEdit({ request: { path: '', ...parseUrl(url)}})])
    })

    it('should handle done on flow request http version', () => {
        let valueEditor = valueEditors[2]
        valueEditor.props.onDone('HTTP/9.9')
        expect(store.getActions()).toEqual([updateEdit({ request: { http_version: 'HTTP/9.9' }})])
    })

    it('should handle change on flow request header', () => {
        let headers = TestUtils.findRenderedComponentWithType(provider, Headers)
        headers.props.onChange('foo')
        expect(store.getActions()).toEqual([updateEdit({ request: { headers: 'foo' }})])
    })

    it('should handle change on flow request contentView', () => {
        let contentView = TestUtils.findRenderedComponentWithType(provider, ContentView)
        contentView.props.onContentChange('foo')
        expect(store.getActions()).toEqual([updateEdit({ request: { content: 'foo' }})])
    })

    it('should handle uploadContent on flow request ContentViewOptions', () => {
        let contentViewOptions = TestUtils.findRenderedComponentWithType(provider, ContentViewOptions)
        contentViewOptions.props.uploadContent('foo')
        expect(fetch).toBeCalled()
        fetch.mockClear()
    })
})

describe('Response Component', () => {
    afterEach(() => {store.clearActions()})

    it('should render correctly', () => {
        let provider = renderer.create(
            <Provider store={store}>
                <Response/>
            </Provider>
        ),
            tree = provider.toJSON()
        expect(tree).toMatchSnapshot()
    })

    let provider = TestUtils.renderIntoDocument(
        <Provider store={store}>
            <Response/>
        </Provider>),
        valueEditors = TestUtils.scryRenderedComponentsWithType(provider, ValueEditor)

    it('should handle done on flow response http version', () => {
        let valueEditor = valueEditors[0]
        valueEditor.props.onDone('HTTP/9.9')
        expect(store.getActions()).toEqual([updateEdit({ response: { http_version: 'HTTP/9.9' }})])
    })

    it('should handle done on flow response status code', () => {
        let valueEditor = valueEditors[1]
        valueEditor.props.onDone('404')
        expect(store.getActions()).toEqual([updateEdit({ response: { code: parseInt('404') }})])
    })

    it('should handle done on flow response reason', () => {
        let valueEdiotr = valueEditors[2]
        valueEdiotr.props.onDone('foo')
        expect(store.getActions()).toEqual([updateEdit( { response: { msg: 'foo' }})])
    })

    it('should handle change on flow response headers', () => {
        let headers = TestUtils.findRenderedComponentWithType(provider, Headers)
        headers.props.onChange('foo')
        expect(store.getActions()).toEqual([updateEdit( { response: { headers: 'foo' }})])
    })

    it('should handle change on flow response ContentView', () => {
        let contentView = TestUtils.findRenderedComponentWithType(provider, ContentView)
        contentView.props.onContentChange('foo')
        expect(store.getActions()).toEqual([updateEdit( { response: { content: 'foo' }})])
    })

    it('should handle updateContent on flow response ContentViewOptions', () => {
        let contentViewOptions = TestUtils.findRenderedComponentWithType(provider, ContentViewOptions)
        contentViewOptions.props.uploadContent('foo')
        expect(fetch).toBeCalled()
        fetch.mockClear()
    })
})

describe('Error Component', () => {
    it('should render correctly', () => {
        let errorView = renderer.create(<ErrorView flow={tflow}/>),
            tree = errorView.toJSON()
        expect(tree).toMatchSnapshot()
    })
})