summaryrefslogtreecommitdiffhomepage
path: root/test/login.spec.js
blob: 3d7a1ed0d15f9814d7447e5ff054ac756e1cfb9c (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
// @flow

import { expect } from 'chai';
import { setupBackendAndStore, setupBackendAndMockStore, checkNextTick, getLocation, failFast, check } from './helpers/ipc-helpers';
import { IpcChain } from './helpers/IpcChain';
import accountActions from '../app/redux/account/actions';

describe('Logging in', () => {

  it('should validate the account number and then set it in the backend', (done) => {
    const { store, mockIpc, backend } = setupBackendAndStore();

    const chain = new IpcChain(mockIpc, done);
    chain.addRequiredStep('getAccountData')
      .withInputValidation((an) => {
        expect(an).to.equal('123');
      })
      .done();

    chain.addRequiredStep('setAccount')
      .withInputValidation((an) => {
        expect(an).to.equal('123');
      })
      .done();

    const action: any = accountActions.login(backend, '123');
    store.dispatch(action);
  });

  it('should put the account data in the state', (done) => {
    const { store, backend, mockIpc } = setupBackendAndStore();
    mockIpc.getAccountData = () => new Promise(r => r({
      paid_until: '2001-01-01T00:00:00',
    }));

    const action: any = accountActions.login(backend, '123');
    store.dispatch(action);

    checkNextTick( () => {
      const state = store.getState().account;
      expect(state.status).to.equal('ok');
      expect(state.accountNumber).to.equal('123');
      expect(state.paidUntil).to.equal('2001-01-01T00:00:00');
    }, done);
  });

  it('should indicate failure for non-existing accounts', (done) => {
    const { store, mockIpc, backend } = setupBackendAndStore();

    mockIpc.getAccountData = (_num) => new Promise((_,reject) => {
      reject('NO SUCH ACCOUNT');
    });


    const action: any = accountActions.login(backend, '123');
    store.dispatch(action);


    checkNextTick(() => {
      const state = store.getState().account;
      expect(state.status).to.equal('failed');
      expect(state.error).to.not.be.null;
    }, done);
  });

  it('should redirect to /connect after 1s after successful login', (done) => {
    const { store, backend } = setupBackendAndMockStore();

    const action: any = accountActions.login(backend, '123');
    store.dispatch(action);


    setTimeout(() => {

      failFast(() => {
        expect(getLocation(store)).not.to.equal('/connect');
      }, done);

    }, 100);


    setTimeout(() => {

      check(() => {
        expect(getLocation(store)).to.equal('/connect');
      }, done);

    }, 1100);
  });
});