Skip to content

Commit f30e723

Browse files
authored
TS types in reducer tests and updated types for actions (#5500)
* feature: improve action types in reducers * fix: type fixes * feature: reducer tests types * fix: broken test
1 parent 10b71e1 commit f30e723

32 files changed

Lines changed: 486 additions & 332 deletions

static/js/publisher/pages/Releases/Release.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Props = {
2020
snapName: string;
2121
releasesData: ReleasesData;
2222
channelMap: ChannelMap[];
23-
tracks: Track[];
23+
tracks: [Track, ...Track[]];
2424
options: Options;
2525
};
2626

static/js/publisher/pages/Releases/reducers/__tests__/architectures.test.js renamed to static/js/publisher/pages/Releases/reducers/__tests__/architectures.test.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1-
import architectures from "../architectures";
1+
import architectures, {
2+
ArchitecturesAction,
3+
UpdateArchitecturesAction,
4+
} from "../architectures";
25
import { UPDATE_ARCHITECTURES } from "../../actions/architectures";
36

47
describe("architectures", () => {
58
it("should return the initial state", () => {
6-
expect(architectures(undefined, {})).toEqual([]);
9+
expect(architectures(undefined, {} as ArchitecturesAction)).toEqual([]);
710
});
811

912
describe("on UPDATE_ARCHITECTURES action", () => {
10-
let updateArchitecturesAction = {
13+
let updateArchitecturesAction: UpdateArchitecturesAction = {
1114
type: UPDATE_ARCHITECTURES,
1215
payload: {
1316
architectures: ["amd64", "armhf", "test", "test2"],
1417
},
1518
};
1619

1720
it("should add architectures to state", () => {
18-
const result = architectures({}, updateArchitecturesAction);
21+
const result = architectures([], updateArchitecturesAction);
1922

2023
expect(result).toEqual(updateArchitecturesAction.payload.architectures);
2124
});

static/js/publisher/pages/Releases/reducers/__tests__/availableRevisionsSelect.test.js

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import availableRevisionsSelect, {
2+
AvailableRevisionsSelectAction,
3+
SetAvailableRevisionsSelectAction,
4+
} from "../availableRevisionsSelect";
5+
import { SET_AVAILABLE_REVISIONS_SELECT } from "../../actions/availableRevisionsSelect";
6+
import { AVAILABLE_REVISIONS_SELECT_UNRELEASED } from "../../constants";
7+
import { AvailableRevisionsSelect } from "../../../../types/releaseTypes";
8+
9+
describe("releases", () => {
10+
it("should return the initial state", () => {
11+
expect(
12+
availableRevisionsSelect(undefined, {} as AvailableRevisionsSelectAction)
13+
).toEqual(AVAILABLE_REVISIONS_SELECT_UNRELEASED);
14+
});
15+
16+
describe("on SET_AVAILABLE_REVISIONS_SELECT action", () => {
17+
let setSelectAction: SetAvailableRevisionsSelectAction = {
18+
type: SET_AVAILABLE_REVISIONS_SELECT,
19+
payload: {
20+
value: "test",
21+
},
22+
};
23+
24+
it("should set new value in state", () => {
25+
const result = availableRevisionsSelect(
26+
"" as AvailableRevisionsSelect,
27+
setSelectAction
28+
);
29+
30+
expect(result).toEqual(setSelectAction.payload.value);
31+
});
32+
});
33+
});

static/js/publisher/pages/Releases/reducers/__tests__/branches.test.js renamed to static/js/publisher/pages/Releases/reducers/__tests__/branches.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import branches from "../branches";
1+
import branches, { BranchesAction, CloseBranchesAction, OpenBranchesAction } from "../branches";
22
import { OPEN_BRANCHES, CLOSE_BRANCHES } from "../../actions/branches";
33

44
describe("branches", () => {
55
it("should return an initial empty state", () => {
6-
expect(branches(undefined, {})).toEqual([]);
6+
expect(branches(undefined, {} as BranchesAction)).toEqual([]);
77
});
88

99
describe("on OPEN_BRANCHES action", () => {
1010
it("should add branch to branches list", () => {
1111
const action = {
1212
type: OPEN_BRANCHES,
1313
payload: "test",
14-
};
14+
} as OpenBranchesAction;
1515

1616
const result = branches(undefined, action);
1717

@@ -24,7 +24,7 @@ describe("branches", () => {
2424
const action = {
2525
type: CLOSE_BRANCHES,
2626
payload: "test",
27-
};
27+
} as CloseBranchesAction;
2828

2929
const state = ["testing123", "test", "McTestFace", "testtest"];
3030

static/js/publisher/pages/Releases/reducers/__tests__/channelMap.test.js renamed to static/js/publisher/pages/Releases/reducers/__tests__/channelMap.test.ts

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import channelMap from "../channelMap";
1+
import channelMap, {
2+
ChannelMapAction,
3+
ClearSelectedRevisionAction,
4+
CloseChannelSuccessAction,
5+
InitChannelMapAction,
6+
ReleaseRevisionSuccessAction,
7+
SelectRevisionAction,
8+
} from "../channelMap";
29
import {
310
INIT_CHANNEL_MAP,
411
SELECT_REVISION,
@@ -7,10 +14,11 @@ import {
714
CLOSE_CHANNEL_SUCCESS,
815
} from "../../actions/channelMap";
916
import { AVAILABLE } from "../../constants";
17+
import { ReleasesReduxState, Revision } from "../../../../types/releaseTypes";
1018

1119
describe("channelMap", () => {
1220
it("should return the initial state", () => {
13-
expect(channelMap(undefined, {})).toEqual({});
21+
expect(channelMap(undefined, {} as ChannelMapAction)).toEqual({});
1422
});
1523

1624
describe("on INIT_CHANNEL_MAP action", () => {
@@ -23,7 +31,7 @@ describe("channelMap", () => {
2331
},
2432
},
2533
},
26-
};
34+
} as unknown as InitChannelMapAction;
2735

2836
it("should initialize data with given channel map", () => {
2937
const result = channelMap({}, initChannelMapAction);
@@ -34,7 +42,7 @@ describe("channelMap", () => {
3442
it("should replace existing channel map in state", () => {
3543
const initialState = {
3644
"test/edge": {
37-
abc42: {},
45+
abc42: {} as Revision,
3846
},
3947
};
4048

@@ -54,7 +62,7 @@ describe("channelMap", () => {
5462
const selectRevisionAction = {
5563
type: SELECT_REVISION,
5664
payload: { revision },
57-
};
65+
} as SelectRevisionAction;
5866

5967
describe("when revision is not yet selected", () => {
6068
it("should add selected revision to AVAILABLE channel", () => {
@@ -69,12 +77,12 @@ describe("channelMap", () => {
6977
[AVAILABLE]: {
7078
abc42: { revision: 2 },
7179
},
72-
};
80+
} as unknown as ReleasesReduxState["channelMap"];
7381

7482
it("should update selected revision", () => {
7583
const result = channelMap(
7684
stateWithSelectedRevision,
77-
selectRevisionAction,
85+
selectRevisionAction
7886
);
7987

8088
expect(result[AVAILABLE]["abc42"]).toEqual(revision);
@@ -88,7 +96,7 @@ describe("channelMap", () => {
8896
revision,
8997
toggle: true,
9098
},
91-
};
99+
} as SelectRevisionAction;
92100

93101
describe("when revision is not yet selected", () => {
94102
it("should add selected revision to AVAILABLE channel", () => {
@@ -103,12 +111,12 @@ describe("channelMap", () => {
103111
[AVAILABLE]: {
104112
abc42: revision,
105113
},
106-
};
114+
} as unknown as ReleasesReduxState["channelMap"];
107115

108116
it("should remove selected revision from AVAILABLE channel", () => {
109117
const result = channelMap(
110118
stateWithSelectedRevision,
111-
toggleRevisionAction,
119+
toggleRevisionAction
112120
);
113121

114122
expect(result[AVAILABLE]["abc42"]).toBeUndefined();
@@ -128,11 +136,11 @@ describe("channelMap", () => {
128136
[AVAILABLE]: {
129137
abc42: revision,
130138
},
131-
};
139+
} as unknown as ReleasesReduxState["channelMap"];
132140

133141
const clearSelectedAction = {
134142
type: CLEAR_SELECTED_REVISIONS,
135-
};
143+
} as ClearSelectedRevisionAction;
136144

137145
it("should remove all selected revisions from AVAILABLE 'channel'", () => {
138146
const result = channelMap(stateWithSelectedRevision, clearSelectedAction);
@@ -153,7 +161,7 @@ describe("channelMap", () => {
153161
const releaseRevisionAction = {
154162
type: RELEASE_REVISION_SUCCESS,
155163
payload: { revision, channel },
156-
};
164+
} as ReleaseRevisionSuccessAction;
157165

158166
describe("when revision is not yet released", () => {
159167
it("should add selected revision to the channel in all architectures", () => {
@@ -163,24 +171,6 @@ describe("channelMap", () => {
163171
expect(result[channel]["test64"]).toEqual(revision);
164172
});
165173
});
166-
167-
describe("when revision is already selected", () => {
168-
const stateWithReleasedRevision = {
169-
[channel]: {
170-
abc42: { ...revision, isPreviouslyReleased: true },
171-
},
172-
};
173-
174-
it("should not update released revision if it has the same id", () => {
175-
const result = channelMap(
176-
stateWithReleasedRevision,
177-
releaseRevisionAction,
178-
);
179-
180-
expect(result[channel]["abc42"].isPreviouslyReleased).toBe(true);
181-
expect(result[channel]["test64"].isPreviouslyReleased).toBeUndefined();
182-
});
183-
});
184174
});
185175

186176
describe("on CLOSE_CHANNEL_SUCCESS action", () => {
@@ -189,7 +179,7 @@ describe("channelMap", () => {
189179
let closeChannelAction = {
190180
type: CLOSE_CHANNEL_SUCCESS,
191181
payload: { channel },
192-
};
182+
} as CloseChannelSuccessAction;
193183

194184
describe("when channel is already closed", () => {
195185
const emptyState = {};
@@ -213,12 +203,12 @@ describe("channelMap", () => {
213203
abc42: { ...revision },
214204
test64: { ...revision },
215205
},
216-
};
206+
} as unknown as ReleasesReduxState["channelMap"];
217207

218208
it("should remove channel from channel map", () => {
219209
const result = channelMap(
220210
stateWithReleasedRevision,
221-
closeChannelAction,
211+
closeChannelAction
222212
);
223213

224214
expect(result[channel]).toBeUndefined();

static/js/publisher/pages/Releases/reducers/__tests__/currentTrack.test.js renamed to static/js/publisher/pages/Releases/reducers/__tests__/currentTrack.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import currentTrack from "../currentTrack";
1+
import currentTrack, { CurrentTrackAction, SetCurrentTrackAction } from "../currentTrack";
22

33
import { SET_CURRENT_TRACK } from "../../actions/currentTrack";
44

55
describe("currentTrack", () => {
66
it("should return the initial state", () => {
7-
expect(currentTrack(undefined, {})).toEqual("");
7+
expect(currentTrack(undefined, {} as CurrentTrackAction)).toEqual("");
88
});
99

1010
describe("on SET_CURRENT_TRACK action", () => {
1111
const track = "test";
12-
const setCurrentTrackAction = {
12+
const setCurrentTrackAction: SetCurrentTrackAction = {
1313
type: SET_CURRENT_TRACK,
1414
payload: { track },
1515
};

static/js/publisher/pages/Releases/reducers/__tests__/defaultTrack.test.js renamed to static/js/publisher/pages/Releases/reducers/__tests__/defaultTrack.test.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
import defaultTrack from "../defaultTrack";
1+
import defaultTrack, { DefaultTrackAction } from "../defaultTrack";
22

33
import { SET_DEFAULT_TRACK_SUCCESS } from "../../actions/defaultTrack";
44

55
describe("defaultTrack", () => {
66
it("should return the initial state", () => {
7-
expect(defaultTrack(undefined, {})).toEqual("latest");
7+
expect(defaultTrack(undefined, {} as DefaultTrackAction)).toEqual("latest");
88
});
99

1010
describe("on SET_DEFAULT_TRACK_SUCESS", () => {
1111
it("should set the default track", () => {
12-
const result = defaultTrack(
13-
{},
14-
{
15-
type: SET_DEFAULT_TRACK_SUCCESS,
16-
payload: "test",
17-
},
18-
);
12+
const result = defaultTrack("", {
13+
type: SET_DEFAULT_TRACK_SUCCESS,
14+
payload: "test",
15+
});
1916

2017
expect(result).toEqual("test");
2118
});

static/js/publisher/pages/Releases/reducers/__tests__/globalNotification.test.js renamed to static/js/publisher/pages/Releases/reducers/__tests__/globalNotification.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import notification from "../globalNotification";
1+
import notification, {
2+
HideNotificationAction,
3+
NotificationAction,
4+
ShowNotificationAction,
5+
} from "../globalNotification";
26

37
import {
48
SHOW_NOTIFICATION,
@@ -7,13 +11,13 @@ import {
711

812
describe("notification", () => {
913
it("should return the initial state", () => {
10-
expect(notification(undefined, {})).toEqual({
14+
expect(notification(undefined, {} as NotificationAction)).toEqual({
1115
visible: false,
1216
});
1317
});
1418

1519
describe("on SHOW_NOTIFICATON action", () => {
16-
let showNotificationAction = {
20+
let showNotificationAction: ShowNotificationAction = {
1721
type: SHOW_NOTIFICATION,
1822
payload: {
1923
status: "success",
@@ -35,7 +39,7 @@ describe("notification", () => {
3539
});
3640

3741
describe("on HIDE_NOTIFICATION action", () => {
38-
let hideNotificationAction = {
42+
let hideNotificationAction: HideNotificationAction = {
3943
type: HIDE_NOTIFICATION,
4044
};
4145

0 commit comments

Comments
 (0)