summaryrefslogtreecommitdiffhomepage
path: root/test/components/Support.spec.js
blob: 3251b08464d92d1c19040346d20b5ac18c363746 (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
// @flow

import { expect } from 'chai';
import sinon from 'sinon';
import React from 'react';
import ReactTestUtils, { Simulate } from 'react-dom/test-utils';
import Support from '../../app/components/Support';

import type { SupportProps } from '../../app/components/Support';

describe('components/Support', () => {

  const makeProps = (mergeProps: $Shape<SupportProps> = {}): SupportProps => {
    const defaultProps: SupportProps = {
      account: {
        accountToken: null,
        accountHistory: [],
        error: null,
        expiry: null,
        status: 'none',
      },
      onClose: () => {},
      onViewLog: (_path) => {},
      onCollectLog: () => Promise.resolve('/tmp/mullvad_problem_report.log'),
      onSend: (_report) => {}
    };
    return Object.assign({}, defaultProps, mergeProps);
  };

  const render = (props: SupportProps): Support => {
    return ReactTestUtils.renderIntoDocument(
      <Support { ...props } />
    );
  };

  it('should call close callback', (done) => {
    const props = makeProps({
      onClose: () => done()
    });
    const domNode = ReactTestUtils.findRenderedDOMComponentWithClass(render(props), 'support__close');
    Simulate.click(domNode);
  });

  it('should call view logs callback', (done) => {
    const props = makeProps({
      onViewLog: (_path) => done()
    });
    const domNode = ReactTestUtils.findRenderedDOMComponentWithClass(render(props), 'support__form-view-logs');
    Simulate.click(domNode);
  });

  it('should call send callback when description filled in', (done) => {
    const props = makeProps({
      onSend: (_report) => done()
    });

    const component = render(props);

    const descriptionField = ReactTestUtils.findRenderedDOMComponentWithClass(component, 'support__form-message');
    descriptionField.value = 'Lorem Ipsum';
    Simulate.change(descriptionField);

    const sendButton = ReactTestUtils.findRenderedDOMComponentWithClass(component, 'support__form-send');
    expect(sendButton.disabled).to.be.false;
    Simulate.click(sendButton);
  });

  it('should not call send callback when description is empty', () => {
    const component = render(makeProps());

    const descriptionField = ReactTestUtils.findRenderedDOMComponentWithClass(component, 'support__form-message');
    descriptionField.value = '';
    Simulate.change(descriptionField);

    const sendButton = ReactTestUtils.findRenderedDOMComponentWithClass(component, 'support__form-send');
    expect(sendButton.disabled).to.be.true;
  });

  it('should not collect report twice', (done) => {
    const collectCallback = sinon.spy(() => Promise.resolve('non-falsy'));
    const props = makeProps({
      onCollectLog: collectCallback
    });

    const component = render(props);
    const viewLogButton = ReactTestUtils.findRenderedDOMComponentWithClass(component, 'support__form-view-logs');
    Simulate.click(viewLogButton);

    setTimeout(() => {
      Simulate.click(viewLogButton);
    });

    setTimeout(() => {
      try {
        expect(collectCallback.callCount).to.equal(1);
        done();
      } catch (e) {
        done(e);
      }
    });
  });

  it('should collect report on submission', (done) => {
    const collectCallback = sinon.spy(() => Promise.resolve(''));
    const props = makeProps({
      onCollectLog: collectCallback,
      onSend: (_report) => {
        try {
          expect(collectCallback.calledOnce).to.be.true;
          done();
        } catch (e) {
          done(e);
        }
      }
    });

    const component = render(props);

    const descriptionField = ReactTestUtils.findRenderedDOMComponentWithClass(component, 'support__form-message');
    descriptionField.value = 'Lorem Ipsum';
    Simulate.change(descriptionField);

    const sendButton = ReactTestUtils.findRenderedDOMComponentWithClass(component, 'support__form-send');
    Simulate.click(sendButton);
  });

});