summaryrefslogtreecommitdiffhomepage
path: root/test/components/AccountInput.spec.js
blob: 0a6e15b12e849830006f12bee80077edb956ba0d (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
import { expect } from 'chai';
import { KeyType, createKeyEvent } from '../mocks/dom';

import React from 'react';
import ReactTestUtils, { Simulate } from 'react-dom/test-utils';
import AccountInput from '../../app/components/AccountInput';

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

  it('should call onEnter', (done) => {
    const onEnter = () => {
      done();
    };

    const component = ReactTestUtils.renderIntoDocument(
      <AccountInput onEnter={ onEnter } />
    );

    Simulate.keyUp(component._ref, createKeyEvent(KeyType.Enter));
  });

  it('should call onChange', (done) => {
    const onChange = (val) => {
      expect(val).to.be.equal('1');
      done();
    };

    const component = ReactTestUtils.renderIntoDocument(
      <AccountInput onChange={ onChange } />
    );

    Simulate.keyDown(component._ref, createKeyEvent(KeyType._1));
  });

  it('should format input properly', () => {
    const cases = [
      '1111111111111',
      '1111 1111 1111',
      '1111 1111 111',
      '1111 1111 11',
      '1111 1111 1',
      '1111 1111',
      '1111 111',
      '1111 11',
      '1111 1',
      '1111',
      '111',
      '11',
      '1',
      ''
    ];

    for(const val of cases) {
      const component = ReactTestUtils.renderIntoDocument(
        <AccountInput value={ val } />
      );
      expect(component._ref.value).to.be.equal(val);
    }
  });

  it('should remove last character', (done) => {
    const onChange = (val) => {
      expect(val).to.be.equal('123');
      done();
    };

    const component = ReactTestUtils.renderIntoDocument(
      <AccountInput value="1234" onChange={ onChange } />
    );

    Simulate.keyDown(component._ref, createKeyEvent(KeyType.Backspace));
  });

  it('should remove first character', (done) => {
    const onChange = (val) => {
      expect(val).to.be.equal('234');
      done();
    };

    const component = ReactTestUtils.renderIntoDocument(
      <AccountInput value="1234" onChange={ onChange } />
    );

    component.setState({ selectionRange: [1, 1] }, () => {
      Simulate.keyDown(component._ref, createKeyEvent(KeyType.Backspace));
    });
  });

  it('should remove all characters', (done) => {
    const onChange = (val) => {
      expect(val).to.be.empty;
      done();
    };

    const component = ReactTestUtils.renderIntoDocument(
      <AccountInput value="12345678" onChange={ onChange } />
    );

    component.setState({ selectionRange: [0, 8] }, () => {
      Simulate.keyDown(component._ref, createKeyEvent(KeyType.Backspace));
    });
  });

  it('should remove selection', (done) => {
    const onChange = (val) => {
      expect(val).to.be.equal('12349999');
      done();
    };

    const component = ReactTestUtils.renderIntoDocument(
      <AccountInput value="1234 5678 9999" onChange={ onChange } />
    );

    component.setState({ selectionRange: [4, 8] }, () => {
      Simulate.keyDown(component._ref, createKeyEvent(KeyType.Backspace));
    });
  });

  it('should replace selection', (done) => {
    const component = ReactTestUtils.renderIntoDocument(
      <AccountInput value="0000" />
    );

    component.setState({ selectionRange: [1, 3] }, () => {
      Simulate.keyDown(component._ref, createKeyEvent(KeyType._1));

      component.setState({}, () => {
        expect(component.state.value).to.be.equal('010');
        expect(component.state.selectionRange).to.deep.equal([2, 2]);
        done();
      });
    });
  });

  it('should keep selection in the back', (done) => {
    const component = ReactTestUtils.renderIntoDocument(
      <AccountInput />
    );

    for(let i = 0; i < 12; i++) {
      Simulate.keyDown(component._ref, createKeyEvent(KeyType._1));
    }

    component.setState({}, () => {
      expect(component.state.value).to.be.equal('111111111111');
      expect(component.state.selectionRange).to.deep.equal([12, 12]);
      done();
    });
  });

  it('should advance selection on insertion', (done) => {
    const component = ReactTestUtils.renderIntoDocument(
      <AccountInput value="0000" />
    );

    component.setState({ selectionRange: [1, 1]}, () => {
      Simulate.keyDown(component._ref, createKeyEvent(KeyType._1));

      component.setState({}, () => {
        expect(component.state.value).to.be.equal('01000');
        expect(component.state.selectionRange).to.deep.equal([2, 2]);
        done();
      });
    });
  });

  it('should not do anything when nothing to remove', (done) => {
    const component = ReactTestUtils.renderIntoDocument(
      <AccountInput value="0000" />
    );

    component.setState({ selectionRange: [0, 0] }, () => {
      Simulate.keyDown(component._ref, createKeyEvent(KeyType.Backspace));

      component.setState({}, () => {
        expect(component.state.value).to.be.equal('0000');
        expect(component.state.selectionRange).to.deep.equal([0, 0]);
        done();
      });
    });
  });

});