-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: clear references to a task key after deleting a task
A small helper function which iterates through `workflow.tasks` and deletes references to a given task key. It's called after deleting a task, so that other tasks no longer link to the deleted task.
- Loading branch information
1 parent
9ca8f70
commit ff0d887
Showing
3 changed files
with
60 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export default function removeTaskKeyFromWorkflow(workflow, taskKey) { | ||
const changes = {}; | ||
Object.entries(workflow.tasks).forEach(([key, task]) => { | ||
if (task.next === taskKey) { | ||
changes[`tasks.${key}.next`] = ''; | ||
} | ||
task.answers?.forEach((answer, index) => { | ||
if (answer.next === taskKey) { | ||
changes[`tasks.${key}.answers.${index}.next`] = ''; | ||
} | ||
}); | ||
}); | ||
return workflow.update(changes); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { expect } from 'chai'; | ||
import sinon from 'sinon'; | ||
import removeTaskKeyFromWorkflow from './removeTaskKeyFromWorkflow.js'; | ||
|
||
describe('removeTaskKeyFromWorkflow', function () { | ||
let workflow; | ||
|
||
beforeEach(function () { | ||
workflow = { | ||
tasks: { | ||
'T1': { | ||
type: 'single', | ||
answers: [ | ||
{ label: 'Yes', next: 'T3' }, | ||
{ label: 'No', next: 'T2' } | ||
] | ||
}, | ||
'T2': { | ||
type: 'multiple', | ||
next: 'T3', | ||
answers: [ | ||
{ label: 'Blue' }, | ||
{ label: 'Green' } | ||
] | ||
}, | ||
'T3': { | ||
type: 'text', | ||
next: '' | ||
} | ||
}, | ||
update: sinon.stub(), | ||
save: sinon.stub() | ||
}; | ||
}); | ||
|
||
it('should delete the selected key from each task', function () { | ||
removeTaskKeyFromWorkflow(workflow, 'T3'); | ||
expect(workflow.update).to.have.been.calledWith({ | ||
'tasks.T1.answers.0.next': '', | ||
'tasks.T2.next': '' | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters