diff --git a/webapp/components/Editor/Editor.spec.js b/webapp/components/Editor/Editor.spec.js index 2367fe975..20041c741 100644 --- a/webapp/components/Editor/Editor.spec.js +++ b/webapp/components/Editor/Editor.spec.js @@ -157,6 +157,10 @@ describe('Editor.vue', () => { value, } } + + beforeAll(() => jest.useFakeTimers()) + afterAll(() => jest.useRealTimers()) + describe('when false', () => { let routerWrapper @@ -170,9 +174,8 @@ describe('Editor.vue', () => { }) it('does nothing', () => { - jest.useFakeTimers() - const content = '
NOOP WIP
' + routerWrapper.vm.editor.setContent(content, true) jest.runAllTimers() @@ -181,31 +184,52 @@ describe('Editor.vue', () => { }) describe('when editing a post', () => { - let routerWrapper const content = 'Post WIP
' - const setItemSpy = jest.spyOn(Storage.prototype, 'setItem') - beforeEach(() => { + beforeEach(async () => { router = new VueRouter({ routes: [{ path: 'post/create' }], }) router.push('/post/create') - routerWrapper = Wrapper() + + Wrapper().vm.editor.setContent(content, true) + await jest.runAllTimers() }) - afterEach(setItemSpy.mockReset) + afterEach(() => { + localStorage.clear() + }) it('saves editor content to localStorage on input', async () => { - jest.useFakeTimers() - - routerWrapper.vm.editor.setContent(content, true) - await jest.runAllTimers() - const { storageKey, value } = getFirst() - expect(setItemSpy).toHaveBeenCalled() expect(storageKey.startsWith('draft:post:')).toBe(true) expect(value).toBe(content) }) }) + + describe('when editing a comment', () => { + const postId = '33739246-fa27-42ae-94de-2f590a2d92c4' + const content = 'Comment WIP
' + + beforeEach(async () => { + router = new VueRouter({ + routes: [{ path: `/post/${postId}/foo-title-slug` }], + }) + router.push(`/post/${postId}/foo-title-slug`) + + Wrapper().vm.editor.setContent(content, true) + await jest.runAllTimers() + }) + + afterEach(() => { + localStorage.clear() + }) + + it('saves editor content to localStorage on input', async () => { + const { storageKey, value } = getFirst() + expect(storageKey).toBe(`draft:${postId}`) + expect(value).toBe(content) + }) + }) }) }) diff --git a/webapp/components/Editor/plugins/autoSave.js b/webapp/components/Editor/plugins/autoSave.js index b0e3197e0..c25b8ebfe 100644 --- a/webapp/components/Editor/plugins/autoSave.js +++ b/webapp/components/Editor/plugins/autoSave.js @@ -23,6 +23,13 @@ export default class AutoSave extends Extension { if (this.route.path === '/post/create') { return `draft:post:${this._postId}` } + + const commentMatch = this.route.path.match(/^\/post\/([0-9a-f-]*)\/[\w-]*$/) + if (commentMatch) { + const key = `draft:${commentMatch[1]}` + return key + } + return null } @@ -39,16 +46,6 @@ export default class AutoSave extends Extension { } return tr }, - state: { - init() { - return { - saveNextUpdate: false, - } - }, - apply(_, prev) { - return { ...prev } - }, - }, }), ] }