Skip to content

Commit 2d07e0b

Browse files
chrisr0892elsong86joeahn95dsterling7AidenCarere
committed
fixed ListReducer and NetworkReducer testing suites; added isMOdified state to handleDelete in jobConfigs.tsx
Co-authored-by: Ellis <[email protected]> Co-authored-by: Chris <[email protected]> Co-authored-by: Joseph <[email protected]> Co-authored-by: Dylan <[email protected]> Co-authored-by: Aiden <[email protected]>
1 parent 73b1474 commit 2d07e0b

File tree

5 files changed

+214
-500
lines changed

5 files changed

+214
-500
lines changed

__tests__/ListReducer.test.js

Lines changed: 112 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,141 @@
1-
import containerReducer from '../ui/src/reducers/containerReducer';
2-
import imageReducer from '../ui/src/reducers/imageReducer';
1+
import containerReducer, {
2+
fetchRunningContainers,
3+
fetchStoppedContainers,
4+
containerSlice, removeContainer, displayErrorModal
5+
} from '../ui/src/reducers/containerReducer';
6+
import imageReducer, { imageSlice, deleteImage, fetchImages} from '../ui/src/reducers/imageReducer';
37
import { describe, beforeEach, expect, test } from '@jest/globals';
8+
import { configureStore, createAsyncThunk } from '@reduxjs/toolkit';
9+
import { ddClientRequest } from '../ui/src/models/ddClientRequest';
410

5-
describe('Dockeeter reducer', () => {
6-
let state;
11+
// Creates a mock ddClient Request for Testing
12+
jest.mock('../ui/src/models/ddClientRequest', () => ({
13+
ddClientRequest: jest.fn(),
14+
}));
15+
16+
describe('Container reducer', () => {
17+
let initialState;
718

819
beforeEach(() => {
9-
state = {
20+
initialState = {
1021
imagesList: [],
1122
runningList: [],
1223
stoppedList: [],
13-
hostStats: {},
14-
}
24+
errorModalOn: false,
25+
};
1526
})
1627

1728
describe('Action Types', () => {
1829
test('Should return initial state if type is invalid', () => {
19-
expect(containerReducer(state, { type: 'FakeActionType' })).toBe(
20-
state,
21-
);
30+
expect(containerReducer(initialState, { type: 'FakeActionType' })).toBe(initialState);
2231
});
2332
});
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-
}
33+
34+
// Testing Asynchronous Redux Thunk calls
35+
describe('Container Thunks', () => {
36+
let store;
37+
38+
beforeEach(() => {
39+
store = configureStore({
40+
reducer: {
41+
containers: containerSlice.reducer,
42+
},
43+
});
44+
45+
// Reset mocks before each test
46+
ddClientRequest.mockReset();
3247
});
33-
});
34-
35-
describe('REFRESH_RUNNING_CONTAINERS', () => {
36-
test('Should return a different state with each reducer invocation', () => {
37-
expect(state.runningList.length).toEqual(0);
38-
let action = {
39-
type: 'REFRESH_RUNNING_CONTAINERS',
40-
payload: [{ cid: '123' }, { cid: '456' }],
41-
}
42-
let newState = containerReducer(state, action);
43-
expect(newState.runningList.length).toEqual(2);
44-
45-
action = {
46-
type: 'REFRESH_RUNNING_CONTAINERS',
47-
payload: [{ cid: '789' }],
48-
}
49-
newState = containerReducer(state, action);
50-
expect(newState.runningList.length).toEqual(1);
51-
expect(newState.runningList[0].cid).toEqual('789');
48+
49+
test('fetchRunningContainers should fetch and update running containers', async () => {
50+
const mockContainers = [
51+
{ ID: '123', name: 'container1' },
52+
{ ID: '456', name: 'container2' },
53+
];
54+
ddClientRequest.mockResolvedValue(mockContainers);
55+
56+
await store.dispatch(fetchRunningContainers());
57+
58+
const state = store.getState();
59+
expect(state.containers.runningList).toEqual(mockContainers);
5260
});
53-
});
5461

55-
describe('REFRESH_STOPPED_CONTAINERS', () => {
56-
test('should overwrite the stoppedList array in the state to update it', () => {
57-
expect(state.stoppedList.length).toEqual(0);
58-
let action = {
59-
type: 'REFRESH_STOPPED_CONTAINERS',
60-
payload: [{ cid: '123' }, { cid: '456' }],
61-
}
62-
let newState = containerReducer(state, action)
63-
expect(newState.stoppedList.length).toEqual(2);
64-
65-
action = {
66-
type: 'REFRESH_STOPPED_CONTAINERS',
67-
payload: [{ cid: '789' }],
68-
}
69-
newState = containerReducer(state, action)
70-
expect(newState.stoppedList.length).toEqual(1);
71-
expect(newState.stoppedList[0].cid).toEqual('789');
62+
test('fetchStoppedContainers should fetch and update stopped containers', async () => {
63+
const mockContainer = [{ ID: '789', name: 'container3' }];
64+
ddClientRequest.mockResolvedValue(mockContainer);
65+
66+
await store.dispatch(fetchStoppedContainers());
67+
68+
const state = store.getState();
69+
expect(state.containers.stoppedList).toEqual(mockContainer);
7270
});
7371
});
74-
75-
xdescribe('REFRESH_IMAGES', () => {
76-
test('should overwrite the imagesList array in the state to update it', () => {
77-
expect(state.imagesList.length).toEqual(0);
78-
let action = {
79-
type: 'REFRESH_IMAGES',
80-
payload: [{ imgid: '123' }, { imgid: '456' }],
81-
}
82-
expect(imageReducer(state, action).imagesList.length).toEqual(2);
83-
action = { type: 'REFRESH_IMAGES', payload: [{ imgid: '789' }] }
84-
expect(imageReducer(state, action).imagesList.length).toEqual(1);
85-
expect(imageReducer(state, action).imagesList[0].imgid).toEqual('789');
72+
// Synchronous Testing
73+
describe('containerSlice reducers', () => {
74+
75+
test('removeContainer should remove a container from stoppedList', () => {
76+
const startState = {
77+
...initialState,
78+
stoppedList: [
79+
{ ID: '123', name: 'container1' },
80+
{ ID: '456', name: 'container2' },
81+
],
82+
};
83+
const action = removeContainer('123');
84+
const newState = containerReducer(startState, action);
85+
expect(newState.stoppedList).toEqual([{ ID: '456', name: 'container2' }]);
8686
});
87-
});
8887

89-
xdescribe('REMOVE_CONTAINER', () => {
90-
test('should remove the specified container from the stoppedList array in the state', () => {
91-
const newState = {
92-
stoppedList: [{ ID: '123' }, { ID: '456' }],
93-
}
94-
const action = { type: 'REMOVE_CONTAINER', payload: '123' }
95-
const storedValue = containerReducer(newState, action);
96-
expect(storedValue.stoppedList[0].ID).toEqual('456');
97-
});
98-
});
99-
100-
xdescribe('STOP_RUNNING_CONTAINER', () => {
101-
test('should remove a specified container from the runningList and add it to the stoppedList', () => {
102-
let newState = {
103-
runningList: [{ ID: '123' }, { ID: '456' }],
104-
stoppedList: [],
105-
}
106-
const action = { type: 'STOP_RUNNING_CONTAINER', payload: '123' }
107-
newState = containerReducer(newState, action);
108-
expect(newState.runningList[0].ID).toEqual('456');
88+
test('displayErrorModal should toggle the error modal', () => {
89+
const action = displayErrorModal(true);
90+
const newState = containerReducer(initialState, action);
91+
expect(newState.errorModalOn).toBe(true);
10992
});
11093
});
94+
});
11195

112-
xdescribe('RUN_STOPPED_CONTAINER', () => {
113-
test('should remove a specified container from the stoppedList', () => {
114-
const newState = {
115-
runningList: [],
116-
stoppedList: [{ ID: '123' }, { ID: '456' }],
117-
}
118-
const action = { type: 'RUN_STOPPED_CONTAINER', payload: '123' }
119-
expect(containerReducer(newState, action).stoppedList[0].ID).toEqual(
120-
'456',
121-
);
96+
// Image Reducer Tests
97+
describe('Image Reducers', () => {
98+
let initialState;
99+
100+
let store;
101+
beforeEach(() => {
102+
initialState = {
103+
imagesList: [],
104+
timeStamp: '',
105+
isSaved: false,
106+
totalVul: 0,
107+
};
108+
store = configureStore({
109+
reducer: {
110+
images: imageSlice.reducer,
111+
},
122112
});
123113
});
124114

125-
xdescribe('REMOVE_IMAGE', () => {
126-
test('should remove a specified image from the imagesList', () => {
115+
describe('fetch images', () => {
116+
test('should return in empty array if no images have been added', async () => {
117+
expect(store.getState().images.imagesList).toEqual([])
118+
}),
119+
test('should overwrite the imagesList array in state to update it', async () => {
120+
121+
const mockImages = [
122+
{ ID: '1'},
123+
{ ID: '2'},
124+
];
125+
await store.dispatch(fetchImages.fulfilled(mockImages));
126+
expect(store.getState().images.imagesList).toEqual(mockImages);
127+
});
128+
});
129+
describe('delete image', () => {
130+
test('should remove a specified image from the imagesList', async () => {
127131
const newState = {
128-
imagesList: [{ id: '123' }, { id: '456' }],
129-
}
130-
const action = { type: 'REMOVE_IMAGE', payload: '123' }
131-
expect(imageReducer(newState, action).imagesList[0].id).toEqual('456');
132+
...initialState,
133+
imagesList: [{ ID: '1' }, { ID: '2' }],
134+
};
135+
store = configureStore({ reducer: { images: imageSlice.reducer }, preloadedState: { images: newState } });
136+
store.dispatch(deleteImage('1'))
137+
expect(store.getState().images.imagesList).toEqual([{ ID: '2' }]);
132138
});
133139
});
134-
});
140+
});
141+

0 commit comments

Comments
 (0)