Completed
Pull Request — master (#162)
by Alejandro
09:21
created

test/servers/reducers/selectedServer.test.js   A

Complexity

Total Complexity 12
Complexity/F 1

Size

Lines of Code 56
Function Count 12

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 33
mnd 0
bc 0
fnc 12
dl 0
loc 56
rs 10
bpm 0
cpm 1
noi 2
c 0
b 0
f 0
1
import reducer, {
2
  selectServer,
3
  resetSelectedServer,
4
  RESET_SELECTED_SERVER,
5
  SELECT_SERVER,
6
} from '../../../src/servers/reducers/selectedServer';
7
import { RESET_SHORT_URL_PARAMS } from '../../../src/short-urls/reducers/shortUrlsListParams';
8
9
describe('selectedServerReducer', () => {
10
  describe('reducer', () => {
11
    it('returns default when action is RESET_SELECTED_SERVER', () =>
12
      expect(reducer(null, { type: RESET_SELECTED_SERVER })).toEqual(null));
13
14
    it('returns selected server when action is SELECT_SERVER', () => {
15
      const selectedServer = { id: 'abc123' };
16
17
      expect(reducer(null, { type: SELECT_SERVER, selectedServer })).toEqual(selectedServer);
18
    });
19
  });
20
21
  describe('resetSelectedServer', () => {
22
    it('returns proper action', () => {
23
      expect(resetSelectedServer()).toEqual({ type: RESET_SELECTED_SERVER });
24
    });
25
  });
26
27
  describe('selectServer', () => {
28
    const serverId = 'abc123';
29
    const selectedServer = {
30
      id: serverId,
31
    };
32
    const version = '1.19.0';
33
    const ServersServiceMock = {
34
      findServerById: jest.fn(() => selectedServer),
35
    };
36
    const apiClientMock = {
37
      health: jest.fn().mockResolvedValue({ version }),
38
    };
39
40
    afterEach(() => {
41
      ServersServiceMock.findServerById.mockClear();
42
    });
43
44
    it('dispatches proper actions', async () => {
45
      const dispatch = jest.fn();
46
      const expectedSelectedServer = {
47
        ...selectedServer,
48
        version,
49
      };
50
51
      await selectServer(ServersServiceMock, async () => apiClientMock)(serverId)(dispatch);
52
53
      expect(dispatch).toHaveBeenCalledTimes(2);
54
      expect(dispatch).toHaveBeenNthCalledWith(1, { type: RESET_SHORT_URL_PARAMS });
55
      expect(dispatch).toHaveBeenNthCalledWith(2, { type: SELECT_SERVER, selectedServer: expectedSelectedServer });
56
    });
57
58
    it('invokes dependencies', () => {
59
      selectServer(ServersServiceMock)(serverId)(() => {});
60
61
      expect(ServersServiceMock.findServerById).toHaveBeenCalledTimes(1);
62
    });
63
  });
64
});
65