Skip to content

Commit 050fb9a

Browse files
committed
refactor ReactTestCaseContext itStatements.allIds to be an object (with key:value pairs of describeId:[]) rather than an array, in order to make feasible nested DnD functionality
1 parent a798e91 commit 050fb9a

File tree

5 files changed

+56
-33
lines changed

5 files changed

+56
-33
lines changed

src/components/ReactTestComponent/ItRenderer/ItRenderer.jsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ const ItRenderer = ({
2121
}) => {
2222
const [, dispatchToReactTestCase] = useContext(ReactTestCaseContext);
2323

24-
// filter out ids not belonging to the correct describe block
25-
const filteredIds = itStatements.allIds.filter((id) => {
26-
return itStatements.byId[id].describeId === describeId;
27-
});
28-
2924
const addRenderHandleClick = (e) => {
3025
const itId = e.target.id;
3126
dispatchToReactTestCase(addRender(describeId, itId));
@@ -43,9 +38,8 @@ const ItRenderer = ({
4338
const itId = e.target.id;
4439
dispatchToReactTestCase(addAssertion(describeId, itId));
4540
};
46-
4741

48-
return filteredIds.map((id, i) => (
42+
return itStatements.allIds[describeId].map((id, i) => (
4943
<Draggable
5044
key={id}
5145
draggableId={id}

src/components/TestCase/ReactTestCase.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ const ReactTestCase = () => {
6060
if (!result.destination) return;
6161
if (result.destination.index === result.source.index) return;
6262

63-
const list = result.draggableId.includes('describe') ? describeBlocks.allIds : itStatements.allIds;
63+
const list = result.draggableId.includes('describe') ? describeBlocks.allIds : itStatements.allIds[result.type];
6464
const func = result.draggableId.includes('describe') ? updateDescribeOrder : updateItStatementOrder;
6565

6666
const reorderedStatements = reorder(
6767
list,
6868
result.source.index,
6969
result.destination.index,
7070
);
71-
dispatchToReactTestCase(func(reorderedStatements));
71+
dispatchToReactTestCase(func(reorderedStatements, result.type));
7272
};
7373

7474
return (

src/context/actions/reactTestCaseActions.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ export const updateItStatementText = (text, itId) => ({
7676
text,
7777
});
7878

79-
export const updateItStatementOrder = (reorderedIt) => ({
79+
export const updateItStatementOrder = (reorderedIt, describeId) => ({
8080
type: actionTypes.UPDATE_ITSTATEMENT_ORDER,
8181
reorderedIt,
82+
describeId,
8283
});
8384

8485

src/context/reducers/reactTestCaseReducer.js

Lines changed: 47 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ export const reactTestCaseState = {
2727
text: '',
2828
},
2929
},
30-
allIds: ['it0'],
30+
allIds: {
31+
describe0: ['it0'],
32+
},
3133
},
3234
statements: {
3335
byId: {
@@ -102,14 +104,26 @@ const createProp = (propId, statementId) => ({
102104
propValue: '',
103105
});
104106

105-
const deleteChildren = (object, deletionId, lookup) => {
106-
const allIdCopy = object.allIds.filter((id) => object.byId[id][lookup] !== deletionId);
107-
108-
object.allIds.forEach((id) => {
109-
if (object.byId[id][lookup] === deletionId) {
107+
const deleteChildren = (object, deletionId, lookup, it) => {
108+
let allIdCopy;
109+
if (it) {
110+
// delete everything appropriate in itStatements.byId object
111+
object.allIds[deletionId].forEach((id) => {
110112
delete object.byId[id];
111-
}
112-
});
113+
});
114+
// delete everything appropriate in itStatements.allIds object
115+
delete object.allIds[deletionId];
116+
allIdCopy = object.allIds;
117+
} else {
118+
// use .filter to delete from statements.allIds array
119+
allIdCopy = object.allIds.filter((id) => object.byId[id][lookup] !== deletionId);
120+
// delete from statements.byId object
121+
object.allIds.forEach((id) => {
122+
if (object.byId[id][lookup] === deletionId) {
123+
delete object.byId[id];
124+
}
125+
});
126+
}
113127

114128
return allIdCopy;
115129
};
@@ -145,6 +159,13 @@ export const reactTestCaseReducer = (state, action) => {
145159
},
146160
allIds: [...(describeBlocks.allIds || []), describeId],
147161
},
162+
itStatements: {
163+
...itStatements,
164+
allIds: {
165+
...itStatements.allIds,
166+
[describeId]: [],
167+
},
168+
},
148169
};
149170
}
150171
case actionTypes.DELETE_DESCRIBE_BLOCK: {
@@ -153,7 +174,7 @@ export const reactTestCaseReducer = (state, action) => {
153174
delete byId[describeId];
154175
const allIds = describeBlocks.allIds.filter((id) => id !== describeId);
155176

156-
const itStatementAllIds = deleteChildren(itStatements, describeId, 'describeId');
177+
const itStatementAllIds = deleteChildren(itStatements, describeId, 'describeId', 'it');
157178
const statementAllIds = deleteChildren(statements, describeId, 'describeId');
158179

159180
return {
@@ -170,7 +191,7 @@ export const reactTestCaseReducer = (state, action) => {
170191
byId: {
171192
...itStatements.byId,
172193
},
173-
allIds: [...itStatementAllIds],
194+
allIds: itStatementAllIds,
174195
},
175196
statements: {
176197
...statements,
@@ -224,7 +245,10 @@ export const reactTestCaseReducer = (state, action) => {
224245
...itStatements.byId,
225246
[itId]: createItStatement(describeId, itId),
226247
},
227-
allIds: [...(itStatements.allIds || []), itId],
248+
allIds: {
249+
...itStatements.allIds,
250+
[describeId]: [...itStatements.allIds[describeId], itId],
251+
},
228252
},
229253
};
230254
}
@@ -248,9 +272,10 @@ export const reactTestCaseReducer = (state, action) => {
248272
}
249273
case actionTypes.DELETE_ITSTATEMENT: {
250274
const { itId } = action;
275+
const { describeId } = itStatements.byId[itId];
251276
const byId = { ...itStatements.byId };
252277
delete byId[itId];
253-
const allIds = itStatements.allIds.filter((id) => id !== itId);
278+
const allIds = itStatements.allIds[describeId].filter((id) => id !== itId);
254279
const statementAllIds = deleteChildren(statements, itId, 'itId');
255280

256281
return {
@@ -260,7 +285,10 @@ export const reactTestCaseReducer = (state, action) => {
260285
byId: {
261286
...byId,
262287
},
263-
allIds: [...allIds],
288+
allIds: {
289+
...itStatements.allIds,
290+
[describeId]: [...allIds],
291+
},
264292
},
265293
statements: {
266294
...statements,
@@ -272,12 +300,15 @@ export const reactTestCaseReducer = (state, action) => {
272300
};
273301
}
274302
case actionTypes.UPDATE_ITSTATEMENT_ORDER: {
275-
const { reorderedIt } = action;
303+
const { reorderedIt, describeId } = action;
276304
return {
277305
...state,
278306
itStatements: {
279307
...itStatements,
280-
allIds: reorderedIt,
308+
allIds: {
309+
...itStatements.allIds,
310+
[describeId]: reorderedIt,
311+
},
281312
},
282313
};
283314
}
@@ -545,7 +576,7 @@ export const reactTestCaseReducer = (state, action) => {
545576
},
546577
itStatements: {
547578
byId: {},
548-
allIds: [],
579+
allIds: {},
549580
},
550581
statements: {
551582
byId: {},

src/context/useGenerateTest.jsx

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,10 @@ function useGenerateTest(test, projectFilePath) {
3939
// React It Statements
4040
const addReactItStatement = (describeId) => {
4141
const itStatements = reactTestCase.itStatements;
42-
itStatements.allIds.forEach((itId) => {
43-
if (itStatements.byId[itId].describeId === describeId) {
44-
testFileCode += `it('${itStatements.byId[itId].text}', () => {`;
45-
addReactStatements(itId);
46-
testFileCode += '})';
47-
}
48-
testFileCode += '\n';
42+
itStatements.allIds[describeId].forEach((itId) => {
43+
testFileCode += `it('${itStatements.byId[itId].text}', () => {`;
44+
addReactStatements(itId);
45+
testFileCode += '})\n';
4946
});
5047
};
5148

0 commit comments

Comments
 (0)