Skip to content

Commit 6c35552

Browse files
committed
will add unit tests for dockerComposeReducer.test.js and add REFRESH_HOST_DATA test to ListReducer.test file
1 parent 8ec98c9 commit 6c35552

File tree

2 files changed

+134
-77
lines changed

2 files changed

+134
-77
lines changed

__tests__/ListReducer.test.js

Lines changed: 84 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,134 @@
1-
import containerListReducer from '../src/redux/reducers/containerListReducer'; // import containerList reducer
2-
import imageListReducer from '../src/redux/reducers/imageListReducer'; // import imageListReducer reducer
3-
import {describe, beforeEach, expect, test} from '@jest/globals';
1+
import containerListReducer from '../src/redux/reducers/containerListReducer' // import containerList reducer
2+
import imageListReducer from '../src/redux/reducers/imageListReducer' // import imageListReducer reducer
3+
import { describe, beforeEach, expect, test } from '@jest/globals'
44

55
describe('Dockeeter reducer', () => {
6-
let state;
6+
let state
77

88
beforeEach(() => {
99
state = {
1010
imagesList: [],
1111
runningList: [],
12-
stoppedList: []
13-
};
14-
});
12+
stoppedList: [],
13+
hostStats: {},
14+
}
15+
})
1516

1617
describe('Action Types', () => {
1718
test('Should return initial state if type is invalid', () => {
18-
expect(containerListReducer(state, { type: 'FakeActionType' })).toBe(state);
19-
});
20-
});
19+
expect(containerListReducer(state, { type: 'FakeActionType' })).toBe(
20+
state,
21+
)
22+
})
23+
})
24+
// REFRESH_HOST_DATA
25+
describe('REFRESH_HOST_DATA', () => {
26+
test('Should refresh host data', () => {
27+
expect(state.hostStats).toEqual({})
28+
let action = {
29+
type: 'REFRESH_HOST_DATA',
30+
payload: { cpuPerc: 24.45, memPerc: 95.08 },
31+
}
32+
})
33+
})
2134

2235
describe('REFRESH_RUNNING_CONTAINERS', () => {
2336
test('Should return a different state with each reducer invocation', () => {
24-
expect(state.runningList.length).toEqual(0);
37+
expect(state.runningList.length).toEqual(0)
2538
let action = {
2639
type: 'REFRESH_RUNNING_CONTAINERS',
27-
payload: [{ cid: '123' }, { cid: '456' }]
28-
};
29-
let newState = containerListReducer(state, action);
30-
expect(newState.runningList.length).toEqual(2);
40+
payload: [{ cid: '123' }, { cid: '456' }],
41+
}
42+
let newState = containerListReducer(state, action)
43+
expect(newState.runningList.length).toEqual(2)
3144

3245
action = {
3346
type: 'REFRESH_RUNNING_CONTAINERS',
34-
payload: [{ cid: '789' }]
35-
};
36-
newState = containerListReducer(state, action);
37-
expect(newState.runningList.length).toEqual(1);
38-
expect(newState.runningList[0].cid).toEqual(
39-
'789'
40-
);
41-
});
42-
});
47+
payload: [{ cid: '789' }],
48+
}
49+
newState = containerListReducer(state, action)
50+
expect(newState.runningList.length).toEqual(1)
51+
expect(newState.runningList[0].cid).toEqual('789')
52+
})
53+
})
4354

4455
describe('REFRESH_STOPPED_CONTAINERS', () => {
4556
test('should overwrite the stoppedList array in the state to update it', () => {
46-
expect(state.stoppedList.length).toEqual(0);
57+
expect(state.stoppedList.length).toEqual(0)
4758
let action = {
4859
type: 'REFRESH_STOPPED_CONTAINERS',
49-
payload: [{ cid: '123' }, { cid: '456' }]
50-
};
51-
let newState = containerListReducer(state, action);
52-
expect(newState.stoppedList.length).toEqual(2);
60+
payload: [{ cid: '123' }, { cid: '456' }],
61+
}
62+
let newState = containerListReducer(state, action)
63+
expect(newState.stoppedList.length).toEqual(2)
5364

5465
action = {
5566
type: 'REFRESH_STOPPED_CONTAINERS',
56-
payload: [{ cid: '789' }]
57-
};
58-
newState = containerListReducer(state, action);
59-
expect(newState.stoppedList.length).toEqual(1);
60-
expect(newState.stoppedList[0].cid).toEqual(
61-
'789'
62-
);
63-
});
64-
});
67+
payload: [{ cid: '789' }],
68+
}
69+
newState = containerListReducer(state, action)
70+
expect(newState.stoppedList.length).toEqual(1)
71+
expect(newState.stoppedList[0].cid).toEqual('789')
72+
})
73+
})
6574

6675
describe('REFRESH_IMAGES', () => {
6776
test('should overwrite the imagesList array in the state to update it', () => {
68-
expect(state.imagesList.length).toEqual(0);
77+
expect(state.imagesList.length).toEqual(0)
6978
let action = {
7079
type: 'REFRESH_IMAGES',
71-
payload: [{ imgid: '123' }, { imgid: '456' }]
72-
};
73-
expect(imageListReducer(state, action).imagesList.length).toEqual(2);
74-
action = { type: 'REFRESH_IMAGES', payload: [{ imgid: '789' }] };
75-
expect(imageListReducer(state, action).imagesList.length).toEqual(1);
76-
expect(imageListReducer(state, action).imagesList[0].imgid).toEqual('789');
77-
});
78-
});
80+
payload: [{ imgid: '123' }, { imgid: '456' }],
81+
}
82+
expect(imageListReducer(state, action).imagesList.length).toEqual(2)
83+
action = { type: 'REFRESH_IMAGES', payload: [{ imgid: '789' }] }
84+
expect(imageListReducer(state, action).imagesList.length).toEqual(1)
85+
expect(imageListReducer(state, action).imagesList[0].imgid).toEqual('789')
86+
})
87+
})
7988

8089
describe('REMOVE_CONTAINER', () => {
8190
test('should remove the specified container from the stoppedList array in the state', () => {
8291
const newState = {
83-
stoppedList: [{ ID: '123' }, { ID: '456' }]
84-
};
85-
const action = { type: 'REMOVE_CONTAINER', payload: '123' };
86-
const storedValue = containerListReducer(newState,action);
87-
expect(storedValue.stoppedList[0].ID).toEqual(
88-
'456'
89-
);
90-
});
91-
});
92+
stoppedList: [{ ID: '123' }, { ID: '456' }],
93+
}
94+
const action = { type: 'REMOVE_CONTAINER', payload: '123' }
95+
const storedValue = containerListReducer(newState, action)
96+
expect(storedValue.stoppedList[0].ID).toEqual('456')
97+
})
98+
})
9299

93100
describe('STOP_RUNNING_CONTAINER', () => {
94101
test('should remove a specified container from the runningList and add it to the stoppedList', () => {
95102
let newState = {
96103
runningList: [{ ID: '123' }, { ID: '456' }],
97-
stoppedList: []
98-
};
99-
const action = { type: 'STOP_RUNNING_CONTAINER', payload: '123' };
100-
newState = containerListReducer(newState, action);
101-
expect(newState.runningList[0].ID).toEqual('456');
102-
});
103-
});
104+
stoppedList: [],
105+
}
106+
const action = { type: 'STOP_RUNNING_CONTAINER', payload: '123' }
107+
newState = containerListReducer(newState, action)
108+
expect(newState.runningList[0].ID).toEqual('456')
109+
})
110+
})
104111

105112
describe('RUN_STOPPED_CONTAINER', () => {
106113
test('should remove a specified container from the stoppedList', () => {
107114
const newState = {
108115
runningList: [],
109-
stoppedList: [{ ID: '123' }, { ID: '456' }]
110-
};
111-
const action = { type: 'RUN_STOPPED_CONTAINER', payload: '123' };
116+
stoppedList: [{ ID: '123' }, { ID: '456' }],
117+
}
118+
const action = { type: 'RUN_STOPPED_CONTAINER', payload: '123' }
112119
expect(containerListReducer(newState, action).stoppedList[0].ID).toEqual(
113-
'456'
114-
);
115-
});
116-
});
120+
'456',
121+
)
122+
})
123+
})
117124

118125
describe('REMOVE_IMAGE', () => {
119126
test('should remove a specified image from the imagesList', () => {
120127
const newState = {
121-
imagesList: [{ id: '123' }, { id: '456' }]
122-
};
123-
const action = { type: 'REMOVE_IMAGE', payload: '123' };
124-
expect(imageListReducer(newState, action).imagesList[0].id).toEqual('456');
125-
});
126-
});
127-
});
128+
imagesList: [{ id: '123' }, { id: '456' }],
129+
}
130+
const action = { type: 'REMOVE_IMAGE', payload: '123' }
131+
expect(imageListReducer(newState, action).imagesList[0].id).toEqual('456')
132+
})
133+
})
134+
})
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import dockerComposeReducer from '../src/redux/reducers/dockerComposeReducer'
2+
import { describe, beforeEach, expect, test, jest } from '@jest/globals'
3+
import '@testing-library/jest-dom'
4+
5+
describe('Docker compose reducer', () => {
6+
let state
7+
8+
beforeEach(() => {
9+
state = {
10+
runningList: [],
11+
stoppedList: [],
12+
networkList: [],
13+
composeStack: [],
14+
hostStats: {},
15+
}
16+
})
17+
18+
describe('Action Types', () => {
19+
test('Should return initial state if type is invalid', () => {
20+
const nonExistentAction = 'FakeActionType'
21+
expect(dockerComposeReducer(state, { type: nonExistentAction })).toBe(
22+
state,
23+
)
24+
})
25+
})
26+
describe('GET_CONTAINER_STACKS', () => {
27+
test('Should return a different state with each reducer invocation', () => {
28+
expect(state.composeStack.length).toEqual(0)
29+
let action = {
30+
type: 'GET_CONTAINER_STACKS',
31+
payload: [
32+
{
33+
CreatedAt: '2020-01-0315:34:16.833926458+0000UTC',
34+
Driver: 'bridge',
35+
ID: 'dummyId',
36+
IPv6: 'false',
37+
Internal: 'false',
38+
Labels:
39+
'com.docker.compose.network=default,com.docker.compose.project=database,com.docker.compose.version=2.13.0',
40+
Name: 'database_default',
41+
Scope: 'local',
42+
},
43+
],
44+
}
45+
let newState = dockerComposeReducer(state, action)
46+
expect(newState.composeStack.length).toEqual(1)
47+
expect(newState.composeStack[0].ID).toEqual('dummyId')
48+
})
49+
})
50+
})

0 commit comments

Comments
 (0)