File size: 2,393 Bytes
bd98362
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import test from 'ava';
import * as I from 'immutable';
import * as td from 'testdouble';
import { ensureLoggedIn, login } from './routes';
import { push } from 'react-router-redux';
import * as AuthActionCreators from '../action-creators/auth';

test('"ensureLoggedIn" should redirect the user to "/", if the token does not exist', () => {
  const session = I.Map({});
  const dispatch = td.function();
  const store = {
    getState: () => ({ session }),
    dispatch,
  };
  ensureLoggedIn(store);
  td.verify(dispatch(push('/')));
});

test('"ensureLoggedIn" should do no redirects, if the token exists', (assert) => {
  const session = I.Map({ token: '123' });
  const dispatch = td.function();
  const store = {
    getState: () => ({ session }),
    dispatch,
  };
  ensureLoggedIn(store);
  assert.throws(() => td.verify(dispatch(td.matchers.anything())));
});

test('"login" should save user token and name, if they exist in the querystring', () => {
  const accessToken = '1234';
  const firstName = 'John';
  const lastName = 'Doe';
  const displayName = `${firstName} ${lastName}`;
  const state = {
    routing: {
      locationBeforeTransitions: {
        query: {
          access_token: accessToken,
          display_name: displayName,
        },
      },
    },
  };

  const store = {
    getState: () => state,
    dispatch: td.function(),
  };

  login(store);

  td.verify(store.dispatch(AuthActionCreators.loginUser({ firstName, lastName }, accessToken)));
});

test('"login" should redirect to "/", if token doesn\'t exist in querystring', () => {
  const firstName = 'John';
  const lastName = 'Doe';
  const displayName = `${firstName} ${lastName}`;
  const state = {
    routing: {
      locationBeforeTransitions: {
        query: {
          display_name: displayName,
        },
      },
    },
  };

  const store = {
    getState: () => state,
    dispatch: td.function(),
  };

  login(store);

  td.verify(store.dispatch(push('/')));
});

test('"login" should redirect to "/", if "displayName" doesn\'t exist in querystring', () => {
  const accessToken = '123';
  const state = {
    routing: {
      locationBeforeTransitions: {
        query: {
          access_token: accessToken,
        },
      },
    },
  };

  const store = {
    getState: () => state,
    dispatch: td.function(),
  };

  login(store);

  td.verify(store.dispatch(push('/')));
});