Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't duplicate passages when replacing story while importing #1540

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Don't duplicate passages when replacing story while importing
Resolves #1392
  • Loading branch information
klembot committed Jul 2, 2024
commit b0bde7d4b74351c972cb27c0f3b7b4b462795db7
6 changes: 5 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 12,9 @@ module.exports = {
setupFilesAfterEnv: ['<rootDir>/src/setupTests.ts'],
testEnvironment: 'jest-environment-jsdom',
// segseg is a ESM-only module.
transformIgnorePatterns: ['node_modules/(?!segseg)']
transformIgnorePatterns: ['node_modules/(?!segseg)'],
watchPlugins: [
'jest-watch-typeahead/filename',
'jest-watch-typeahead/testname'
]
};
110 changes: 110 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 103,7 @@
"jest-canvas-mock": "^2.5.2",
"jest-environment-jsdom": "^29.7.0",
"jest-useragent-mock": "^0.1.1",
"jest-watch-typeahead": "^2.2.2",
"npm-run-all": "^4.1.5",
"rimraf": "^3.0.2",
"source-map-explorer": "^2.5.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 400,35 @@ describe('stories local storage save middleware', () => {
expect(saveStoryMock.mock.calls).toEqual([[transaction, state[0]]]);
});

it('deletes removed passages when the passages property is updated', () => {
const storyWithMultiplePassagesState = [fakeStory(2)];
const transaction = {passageIds: '', storyIds: ''};

// Need to run one action to set lastState.

saveMiddleware(storyWithMultiplePassagesState, {
type: 'init',
state: storyWithMultiplePassagesState
});
storyWithMultiplePassagesState[0].passages = [
storyWithMultiplePassagesState[0].passages[1]
];
saveMiddleware(storyWithMultiplePassagesState, {
type: 'updateStory',
props: {passages: [{...storyWithMultiplePassagesState[0].passages[1]}]},
storyId: storyWithMultiplePassagesState[0].id
});
expect(doUpdateTransactionMock).toHaveBeenCalledTimes(1);
doUpdateTransactionMock.mock.calls[0][0](transaction);
expect(saveStoryMock.mock.calls).toEqual([
[transaction, storyWithMultiplePassagesState[0]]
]);
expect(deletePassageByIdMock.mock.calls).toEqual([
[transaction, storyWithMultiplePassagesState[0].passages[0].id]
]);
});


it("throws an error if the story doesn't exist in state", () =>
expect(() =>
saveMiddleware(state, {
Expand Down
54 changes: 37 additions & 17 deletions src/store/persistence/local-storage/stories/save-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 50,7 @@ export function saveMiddleware(state: StoriesState, action: StoriesAction) {

doUpdateTransaction(transaction => {
saveStory(transaction, story);
action.props.forEach(props => {
for (const props of action.props) {
if (!props.name) {
throw new Error('Passage was created but with no name specified');
}
Expand All @@ -59,7 59,7 @@ export function saveMiddleware(state: StoriesState, action: StoriesAction) {
transaction,
passageWithName(state, story.id, props.name)
);
});
}
});
break;
}
Expand All @@ -73,7 73,10 @@ export function saveMiddleware(state: StoriesState, action: StoriesAction) {

doUpdateTransaction(transaction => {
saveStory(transaction, story);
story.passages.forEach(passage => savePassage(transaction, passage));

for (const passage of story.passages) {
savePassage(transaction, passage);
}
});
break;
}
Expand All @@ -99,9 102,10 @@ export function saveMiddleware(state: StoriesState, action: StoriesAction) {

doUpdateTransaction(transaction => {
saveStory(transaction, story);
action.passageIds.forEach(passageId =>
deletePassageById(transaction, passageId)
);

for (const passageId of action.passageIds) {
deletePassageById(transaction, passageId);
}
});
break;
}
Expand All @@ -115,9 119,9 @@ export function saveMiddleware(state: StoriesState, action: StoriesAction) {
doUpdateTransaction(transaction => {
// We have to delete all passages, then the story itself.

story.passages.forEach(passage =>
deletePassageById(transaction, passage.id)
);
for (const passage of story.passages) {
deletePassageById(transaction, passage.id);
}

deleteStory(transaction, story);
});
Expand All @@ -142,16 146,18 @@ export function saveMiddleware(state: StoriesState, action: StoriesAction) {

doUpdateTransaction(transaction => {
saveStory(transaction, story);
Object.keys(action.passageUpdates)
.filter(passageId =>

const passageIds = Object.keys(action.passageUpdates).filter(
passageId =>
isPersistablePassageChange(action.passageUpdates[passageId])
)
.forEach(passageId =>
savePassage(
transaction,
passageWithId(state, action.storyId, passageId)
)
);

for (const passageId of passageIds) {
savePassage(
transaction,
passageWithId(state, action.storyId, passageId)
);
}
});
break;
}
Expand All @@ -161,6 167,20 @@ export function saveMiddleware(state: StoriesState, action: StoriesAction) {

doUpdateTransaction(transaction => {
saveStory(transaction, story);

// Special case: if the passages property is being set, we need to
// delete any passages there were in the story, but aren't anymore.

if (action.props.passages) {
const lastStory = storyWithId(lastState, action.storyId);

for (const passage of lastStory.passages) {
if (!action.props.passages.some(({id}) => id === passage.id)) {
deletePassageById(transaction, passage.id);
}
}
}

story.passages.forEach(passage => savePassage(transaction, passage));
});
break;
Expand Down
Loading