summaryrefslogtreecommitdiffhomepage
path: root/test/components/Settings.spec.js
blob: a9302a9ea7fe93682f219d30be50212ed1124453 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// @flow

import * as React from 'react';
import { shallow } from 'enzyme';
import Settings from '../../app/components/Settings';
import { CloseBarItem } from '../../app/components/NavigationBar';

type SettingsProps = React.ElementProps<typeof Settings>;

describe('components/Settings', () => {
  const defaultProps: SettingsProps = {
    loginState: 'none',
    accountExpiry: null,
    appVersion: '',
    onQuit: () => {},
    onClose: () => {},
    onViewAccount: () => {},
    onViewSupport: () => {},
    onViewAdvancedSettings: () => {},
    onViewPreferences: () => {},
    onExternalLink: (_type) => {},
    updateAccountExpiry: () => Promise.resolve(),
  };

  it('should show quit button when logged out', () => {
    const props = {
      ...defaultProps,
      loginState: 'none',
      accountExpiry: null,
    };
    const component = getComponent(shallow(<Settings {...props} />), 'settings__quit');
    expect(component).to.have.length(1);
  });

  it('should show quit button when logged in', () => {
    const props = {
      ...defaultProps,
      loginState: 'ok',
      accountExpiry: new Date('2038-01-01').toISOString(),
    };
    const component = getComponent(shallow(<Settings {...props} />), 'settings__quit');
    expect(component).to.have.length(1);
  });

  it('should show external links when logged out', () => {
    const props = {
      ...defaultProps,
      loginState: 'none',
      accountExpiry: null,
    };
    const component = getComponent(shallow(<Settings {...props} />), 'settings__external_link');
    expect(component.length).to.be.above(0);
  });

  it('should show external links when logged in', () => {
    const props = {
      ...defaultProps,
      loginState: 'ok',
      accountExpiry: new Date('2038-01-01').toISOString(),
    };
    const component = getComponent(shallow(<Settings {...props} />), 'settings__external_link');
    expect(component.length).to.be.above(0);
  });

  it('should show account section when logged in', () => {
    const props = {
      ...defaultProps,
      loginState: 'ok',
      accountExpiry: new Date('2038-01-01').toISOString(),
    };
    const component = getComponent(shallow(<Settings {...props} />), 'settings__account');
    expect(component).to.have.length(1);
  });

  it('should hide account section when logged out', () => {
    const props = {
      ...defaultProps,
      loginState: 'none',
      accountExpiry: null,
    };
    const elements = getComponent(shallow(<Settings {...props} />), 'settings__account');
    expect(elements).to.have.length(0);
  });

  it('should hide account link when not logged in', () => {
    const props = {
      ...defaultProps,
      loginState: 'none',
      accountExpiry: null,
    };
    const elements = getComponent(shallow(<Settings {...props} />), 'settings__view_account');
    expect(elements).to.have.length(0);
  });

  it('should show out-of-time message for unpaid account', () => {
    const props = {
      ...defaultProps,
      loginState: 'ok',
      accountExpiry: new Date('2001-01-01').toISOString(),
    };
    const component = getComponent(
      shallow(<Settings {...props} />),
      'settings__account_paid_until_subtext',
    );
    expect(component.children().text()).to.equal('OUT OF TIME');
  });

  it('should hide out-of-time message for paid account', () => {
    const props = {
      ...defaultProps,
      loginState: 'ok',
      accountExpiry: new Date('2038-01-01').toISOString(),
    };
    const component = getComponent(
      shallow(<Settings {...props} />),
      'settings__account_paid_until_subtext',
    );
    expect(component.children().text()).not.to.equal('OUT OF TIME');
  });

  it('should call close callback', (done) => {
    const props = {
      ...defaultProps,
      loginState: 'none',
      accountExpiry: null,
      onClose: () => done(),
    };
    const component = shallow(<Settings {...props} />)
      .find(CloseBarItem)
      .dive();
    component.simulate('press');
  });

  it('should call quit callback', (done) => {
    const props = {
      ...defaultProps,
      loginState: 'none',
      accountExpiry: null,
      onQuit: () => done(),
    };
    const component = getComponent(shallow(<Settings {...props} />), 'settings__quit');
    component.simulate('press');
  });

  it('should call account callback', (done) => {
    const props = {
      ...defaultProps,
      loginState: 'ok',
      accountExpiry: new Date('2038-01-01').toISOString(),
      onViewAccount: () => done(),
    };
    const component = getComponent(
      shallow(<Settings {...props} />),
      'settings__account_paid_until_button',
    );
    component.simulate('press');
  });

  it('should call advanced settings callback', (done) => {
    const props = {
      ...defaultProps,
      loginState: 'ok',
      accountExpiry: new Date('2038-01-01').toISOString(),
      onViewAdvancedSettings: () => done(),
    };
    const component = getComponent(shallow(<Settings {...props} />), 'settings__advanced');
    component.simulate('press');
  });

  it('should call preferences callback', (done) => {
    const props = {
      ...defaultProps,
      loginState: 'ok',
      accountExpiry: new Date('2038-01-01').toISOString(),
      onViewPreferences: () => done(),
    };
    const component = getComponent(shallow(<Settings {...props} />), 'settings__preferences');
    component.simulate('press');
  });

  it('should call support callback', (done) => {
    const props = {
      ...defaultProps,
      loginState: 'ok',
      accountExpiry: new Date('2038-01-01').toISOString(),
      onViewSupport: () => done(),
    };
    const component = getComponent(shallow(<Settings {...props} />), 'settings__view_support');
    component.simulate('press');
  });

  it('should call external links callback', () => {
    const collectedExternalLinkTypes: Array<string> = [];
    const props = {
      ...defaultProps,
      loginState: 'none',
      accountExpiry: null,
      onExternalLink: (type) => {
        collectedExternalLinkTypes.push(type);
      },
    };
    const container = getComponent(shallow(<Settings {...props} />), 'settings__external_link');
    container
      .find({ testName: 'settings__external_link' })
      .forEach((element) => element.simulate('press'));

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

function getComponent(container, testName) {
  return container.findWhere((n) => n.prop('testName') === testName);
}