summaryrefslogtreecommitdiffhomepage
path: root/test/components/Settings.spec.js
blob: 63087c5088c5f730d1a78874a7b01e73cc376dba (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
156
157
158
159
160
161
162
163
164
165
166
// @flow

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

import type { AccountReduxState } from '../../app/redux/account/reducers';
import type { SettingsReduxState } from '../../app/redux/settings/reducers';
import type { SettingsProps } from '../../app/components/Settings';

describe('components/Settings', () => {
  const loggedOutAccountState: AccountReduxState = {
    accountToken: null,
    expiry: null,
    status: 'none',
    error: null
  };

  const loggedInAccountState: AccountReduxState = {
    accountToken: '1234',
    expiry: (new Date('2038-01-01')).toISOString(),
    status: 'ok',
    error: null
  };

  const unpaidAccountState: AccountReduxState = {
    accountToken: '1234',
    expiry: (new Date('2001-01-01')).toISOString(),
    status: 'ok',
    error: null
  };

  const settingsState: SettingsReduxState = {
    autoSecure: true,
    preferredServer: defaultServer
  };

  const makeProps = (anAccountState: AccountReduxState, aSettingsState: SettingsReduxState, mergeProps: $Shape<SettingsProps> = {}): SettingsProps => {
    const defaultProps: SettingsProps = {
      account: anAccountState,
      settings: aSettingsState,
      onQuit: () => {},
      onClose: () => {},
      onViewAccount: () => {},
      onExternalLink: (_type) => {},
      onUpdateSettings: (_update) => {}
    };
    return Object.assign({}, defaultProps, mergeProps);
  };

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

  it('should show quit button when logged out', () => {
    const props = makeProps(loggedOutAccountState, settingsState);
    ReactTestUtils.findRenderedDOMComponentWithClass(render(props), 'settings__quit');
  });

  it('should show quit button when logged in', () => {
    const props = makeProps(loggedInAccountState, settingsState);
    ReactTestUtils.findRenderedDOMComponentWithClass(render(props), 'settings__quit');
  });

  it('should show external links when logged out', () => {
    const props = makeProps(loggedOutAccountState, settingsState);
    ReactTestUtils.findRenderedDOMComponentWithClass(render(props), 'settings__external');
  });

  it('should show external links when logged in', () => {
    const props = makeProps(loggedInAccountState, settingsState);
    ReactTestUtils.findRenderedDOMComponentWithClass(render(props), 'settings__external');
  });

  it('should show account section when logged in', () => {
    const props = makeProps(loggedInAccountState, settingsState);
    ReactTestUtils.findRenderedDOMComponentWithClass(render(props), 'settings__account');
  });

  it('should hide account section when logged out', () => {
    const props = makeProps(loggedOutAccountState, settingsState);
    const elements = ReactTestUtils.scryRenderedDOMComponentsWithClass(render(props), 'settings__account');
    expect(elements).to.be.empty;
  });

  it('should hide account link when not logged in', () => {
    const props = makeProps(loggedOutAccountState, settingsState);
    const elements = ReactTestUtils.scryRenderedDOMComponentsWithClass(render(props), 'settings__view-account');
    expect(elements).to.be.empty;
  });

  it('should show out-of-time message for unpaid account', () => {
    const props = makeProps(unpaidAccountState, settingsState);
    const domNode = ReactTestUtils.findRenderedDOMComponentWithClass(render(props), 'settings__account-paid-until-label');
    expect(domNode.textContent).to.contain('OUT OF TIME');
  });

  it('should hide out-of-time message for paid account', () => {
    const props = makeProps(loggedInAccountState, settingsState);
    const domNode = ReactTestUtils.findRenderedDOMComponentWithClass(render(props), 'settings__account-paid-until-label');
    expect(domNode.textContent).to.not.contain('OUT OF TIME');
  });

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

  it('should call quit callback', (done) => {
    const props = makeProps(loggedOutAccountState, settingsState, {
      onQuit: () => done()
    });
    const domNode = ReactTestUtils.findRenderedDOMComponentWithClass(render(props), 'settings__quit');
    Simulate.click(domNode);
  });

  it('should call account callback', (done) => {
    const props = makeProps(loggedInAccountState, settingsState, {
      onViewAccount: () => done()
    });
    const domNode = ReactTestUtils.findRenderedDOMComponentWithClass(render(props), 'settings__view-account');
    Simulate.click(domNode);
  });

  it('should call update callback', (done) => {
    const props = makeProps(loggedInAccountState, settingsState, {
      onUpdateSettings: (update) => {
        try {
          expect(update).to.include({ autoSecure: false });
          done();
        } catch(e) {
          done(e);
        }
      }
    });
    const domNode = ReactTestUtils.findRenderedDOMComponentWithClass(render(props), 'settings__autosecure');

    // TODO(Andrej): Add click handler to Switch to avoid calling that horrible chain of events.
    Simulate.mouseDown(domNode);
    Simulate.mouseUp(domNode);
    Simulate.change(domNode, { target: { checked: false } });
  });

  it('should call external links callback', () => {
    let collectedExternalLinkTypes: Array<string> = [];
    const props = makeProps(loggedOutAccountState, settingsState, {
      onExternalLink: (type) => {
        collectedExternalLinkTypes.push(type);
      }
    });
    const container = ReactTestUtils.findRenderedDOMComponentWithClass(render(props), 'settings__external');
    Array.from(container.childNodes)
      .filter((elm: HTMLElement) => elm.classList.contains('settings__cell'))
      .forEach((elm) => Simulate.click(elm));

    expect(collectedExternalLinkTypes).to.include.ordered.members(['faq', 'guides', 'supportEmail']);
  });

});