summaryrefslogtreecommitdiffhomepage
path: root/test/components/Connect.spec.js
blob: 59079082c591e4d757b967f6602d01cab58ed009 (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
// @flow

import { expect } from 'chai';
import React from 'react';
import { shallow } from 'enzyme';

import Connect from '../../app/components/Connect';

import type { ConnectProps } from '../../app/components/Connect';

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

  it('shows unsecured hints when disconnected', () => {
    const component = renderWithProps({
      connection: {
        ...defaultProps.connection,
        status: 'disconnected',
      }
    });

    const header = getComponent(component, 'header');
    const securityMessage = getComponent(component, 'networkSecurityMessage');
    const connectButton = getComponent(component, 'secureConnection');
    expect(header.prop('style')).to.equal('error');
    expect(securityMessage.html()).to.contain('UNSECURED');
    expect(connectButton.html()).to.contain('Secure my connection');
  });

  it('shows secured hints when connected', () => {
    const component = renderWithProps({
      connection: {
        ...defaultProps.connection,
        status: 'connected',
      }
    });

    const header = getComponent(component, 'header');
    const securityMessage = getComponent(component, 'networkSecurityMessage');
    const disconnectButton = getComponent(component, 'disconnect');
    expect(header.prop('style')).to.equal('success');
    expect(securityMessage.html()).to.contain('SECURE ');
    expect(disconnectButton.html()).to.contain('Disconnect');
  });

  it('shows the connection location when connecting', () => {
    const component = renderWithProps({
      connection: {
        ...defaultProps.connection,
        status: 'connecting',
        country: 'Norway',
        city: 'Oslo',
      }
    });
    const countryAndCity = getComponent(component, 'location');
    const ipAddr = getComponent(component, 'ipAddress');

    expect(countryAndCity.html()).to.contain('Norway');
    expect(countryAndCity.html()).not.to.contain('Oslo');
    expect(ipAddr.length).to.equal(0);
  });

  it('shows the connection location when connected', () => {
    const component = renderWithProps({
      connection: {
        ...defaultProps.connection,
        status: 'connected',
        country: 'Norway',
        city: 'Oslo',
        ip: '4.3.2.1',
      }
    });
    const countryAndCity = getComponent(component, 'location');
    const ipAddr = getComponent(component, 'ipAddress');

    expect(countryAndCity.html()).to.contain('Norway');
    expect(countryAndCity.html()).to.contain('Oslo');
    expect(ipAddr.html()).to.contain('4.3.2.1');
  });

  it('shows the connection location when disconnected', () => {
    const component = renderWithProps({
      connection: {
        ...defaultProps.connection,
        status: 'disconnected',
        country: 'Norway',
        city: 'Oslo',
        ip: '4.3.2.1',
      }
    });
    const countryAndCity = getComponent(component, 'location');
    const ipAddr = getComponent(component, 'ipAddress');

    expect(countryAndCity.html()).to.contain('Norway');
    expect(countryAndCity.html()).to.not.contain('Oslo');
    expect(ipAddr.html()).to.contain('4.3.2.1');
  });

  it('invokes the onConnect prop', (done) => {
    const component = renderWithProps({
      onConnect: () => done(),
      connection: {
        ...defaultProps.connection,
        status: 'disconnected',
      }
    });
    const connectButton = getComponent(component, 'secureConnection');

    connectButton.prop('onPress')();
  });
});

const defaultProps: ConnectProps = {
  onSettings: () => {},
  onSelectLocation: () => {},
  onConnect: () => {},
  onCopyIP: () => {},
  onDisconnect: () => {},
  onExternalLink: () => {},
  accountExpiry: '',
  selectedRelayName: '',
  connection: {
    status: 'disconnected',
    isOnline: true,
    ip: null,
    latitude: null,
    longitude: null,
    country: null,
    city: null,
  },
};

function renderWithProps(customProps: $Shape<ConnectProps>) {
  const props = { ...defaultProps, ...customProps };
  return shallow( <Connect { ...props } /> );
}

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