summaryrefslogtreecommitdiffhomepage
path: root/test/components/SelectLocation.spec.js
blob: 4e2265c6dda9e20e35c7312133f7efa7baf380f3 (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
// @flow

import { expect } from 'chai';
import * as React from 'react';
import { shallow } from 'enzyme';
import SelectLocation from '../../app/components/SelectLocation';

import type { SettingsReduxState } from '../../app/redux/settings/reducers';
import type { SelectLocationProps } from '../../app/components/SelectLocation';

describe('components/SelectLocation', () => {
  const state: SettingsReduxState = {
    relaySettings: {
      normal: {
        location: 'any',
        protocol: 'any',
        port: 'any',
      }
    },
    relayLocations: [{
      name: 'Sweden',
      code: 'se',
      hasActiveRelays: true,
      cities: [{
        name: 'Malmö',
        code: 'mma',
        latitude: 0,
        longitude: 0,
        hasActiveRelays: true,
      }, {
        name: 'Stockholm',
        code: 'sto',
        latitude: 0,
        longitude: 0,
        hasActiveRelays: true,
      }],
    }],
    allowLan: false,
  };

  const makeProps = (state: SettingsReduxState, mergeProps: $Shape<SelectLocationProps>): SelectLocationProps => {
    const defaultProps: SelectLocationProps = {
      settings: state,
      onClose: () => {},
      onSelect: (_server) => {}
    };
    return Object.assign({}, defaultProps, mergeProps);
  };

  const render = (props: SelectLocationProps) => {
    return shallow(
      <SelectLocation { ...props } />
    );
  };

  it('should call close callback', (done) => {
    const props = makeProps(state, {
      onClose: () => done()
    });
    const node = getComponent(render(props), 'close');
    click(node);
  });

  it('should call select callback for country', (done) => {
    const props = makeProps(state, {
      onSelect: (location) => {
        try {
          expect(location).to.deep.equal({
            country: 'se'
          });
          done();
        } catch(e) {
          done(e);
        }
      }
    });
    const elements = getComponent(render(props), 'country');
    expect(elements).to.have.length(1);
    click(elements.at(0));
  });

  it('should call select callback for city', (done) => {
    const props = makeProps(state, {
      onSelect: (location) => {
        try {
          expect(location).to.deep.equal({
            city: ['se', 'mma']
          });
          done();
        } catch(e) {
          done(e);
        }
      }
    });
    const elements = getComponent(render(props), 'city');
    expect(elements).to.have.length(2);
    click(elements.at(0));
  });

});

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

function click(component) {
  component.prop('onPress')();
}