Skip to content

Commit

Permalink
Merge pull request #1485 from klembot/fix-duplicate-in-local-storage
Browse files Browse the repository at this point in the history
Fix story duplication moving passages instead of copying
  • Loading branch information
klembot authored Jan 3, 2024
2 parents d1a23e6 + d1a486f commit 7c4d11f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ describe('duplicateStory action creator', () => {
...story,
id: expect.any(String),
ifid: expect.any(String),
name: expect.any(String)
name: expect.any(String),
passages: story.passages.map(passage =>
expect.objectContaining({
...passage,
// Tested below.
id: expect.any(String),
story: expect.any(String)
})
)
})
}));

Expand All @@ -36,4 +44,24 @@ describe('duplicateStory action creator', () => {

it('gives the duplicate story a unique IFID', () =>
expect(duplicateStory(story, [story]).props.ifid).not.toBe(story.ifid));

it('links passages correctly', () => {
expect.assertions(story.passages.length);

const result = duplicateStory(story, [story]);

for (const passage of result.props.passages!) {
expect(passage.story).toBe(result.props.id);
}
});

it('gives the duplicate story passages unique IDs', () => {
expect.assertions(story.passages.length);

const result = duplicateStory(story, [story]);

for (let i = 0; i < story.passages.length; i++) {
expect(result.props.passages![i].id).not.toBe(story.passages[i].id);
}
});
});
11 changes: 9 additions & 2 deletions src/store/stories/action-creators/duplicate-story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@ export function duplicateStory(
story: Story,
stories: Story[]
): CreateStoryAction {
const id = uuid();

return {
type: 'createStory',
props: {
...story,
id: uuid(),
id,
ifid: uuid(),
name: unusedName(
story.name,
stories.map(story => story.name)
)
),
passages: story.passages.map(passage => ({
...passage,
id: uuid(),
story: id
}))
}
};
}

0 comments on commit 7c4d11f

Please sign in to comment.