pleroma-fe/test/unit/specs/components/draft.spec.js

181 lines
5.2 KiB
JavaScript
Raw Normal View History

2026-01-06 16:23:17 +02:00
import { flushPromises, mount } from '@vue/test-utils'
import { nextTick } from 'vue'
2026-01-08 17:26:52 +02:00
import PostStatusForm from 'src/components/post_status_form/post_status_form.vue'
2026-01-06 16:23:17 +02:00
import { $t, mountOpts, waitForEvent } from '../../../fixtures/setup_test'
2025-02-18 17:42:50 -05:00
const autoSaveOrNot = (caseFn, caseTitle, runFn) => {
caseFn(`${caseTitle} with auto-save`, function () {
return runFn.bind(this)(true)
})
caseFn(`${caseTitle} with no auto-save`, function () {
return runFn.bind(this)(false)
})
}
const saveManually = async (wrapper) => {
2026-01-06 16:22:52 +02:00
const morePostActions = wrapper.findByText(
'button',
$t('post_status.more_post_actions'),
)
2025-02-18 17:42:50 -05:00
await morePostActions.trigger('click')
2026-01-06 16:22:52 +02:00
const btn = wrapper.findByText(
'button',
$t('post_status.save_to_drafts_button'),
)
2025-02-18 17:42:50 -05:00
await btn.trigger('click')
}
const waitSaveTime = 4000
afterEach(() => {
2025-02-27 22:54:23 -05:00
vi.useRealTimers()
2025-02-18 17:42:50 -05:00
})
describe('Draft saving', () => {
2026-01-06 16:22:52 +02:00
autoSaveOrNot(
it,
'should save when the button is clicked',
async (autoSave) => {
const wrapper = mount(PostStatusForm, mountOpts())
await wrapper.vm.$store.dispatch('setOption', {
name: 'autoSaveDraft',
value: autoSave,
})
expect(wrapper.vm.$store.getters.draftCount).to.equal(0)
2025-02-18 17:42:50 -05:00
2026-01-06 16:22:52 +02:00
const textarea = wrapper.get('textarea')
await textarea.setValue('mew mew')
2025-02-18 17:42:50 -05:00
2026-01-06 16:22:52 +02:00
await saveManually(wrapper)
expect(wrapper.vm.$store.getters.draftCount).to.equal(1)
expect(wrapper.vm.$store.getters.draftsArray[0].status).to.equal(
'mew mew',
)
},
)
2025-02-18 17:42:50 -05:00
it('should auto-save if it is enabled', async function () {
2025-02-27 22:54:23 -05:00
vi.useFakeTimers()
2025-02-18 17:42:50 -05:00
const wrapper = mount(PostStatusForm, mountOpts())
await wrapper.vm.$store.dispatch('setOption', {
name: 'autoSaveDraft',
2026-01-06 16:22:52 +02:00
value: true,
2025-02-18 17:42:50 -05:00
})
expect(wrapper.vm.$store.getters.draftCount).to.equal(0)
const textarea = wrapper.get('textarea')
await textarea.setValue('mew mew')
expect(wrapper.vm.$store.getters.draftCount).to.equal(0)
2025-02-27 22:54:23 -05:00
await vi.advanceTimersByTimeAsync(waitSaveTime)
2025-02-18 17:42:50 -05:00
expect(wrapper.vm.$store.getters.draftCount).to.equal(1)
expect(wrapper.vm.$store.getters.draftsArray[0].status).to.equal('mew mew')
})
it('should auto-save when close if auto-save is on', async () => {
2026-01-06 16:22:52 +02:00
const wrapper = mount(
PostStatusForm,
mountOpts({
props: {
closeable: true,
},
}),
)
2025-02-18 17:42:50 -05:00
await wrapper.vm.$store.dispatch('setOption', {
name: 'autoSaveDraft',
2026-01-06 16:22:52 +02:00
value: true,
2025-02-18 17:42:50 -05:00
})
expect(wrapper.vm.$store.getters.draftCount).to.equal(0)
const textarea = wrapper.get('textarea')
await textarea.setValue('mew mew')
wrapper.vm.requestClose()
expect(wrapper.vm.$store.getters.draftCount).to.equal(1)
await waitForEvent(wrapper, 'can-close')
})
it('should save when close if auto-save is off, and unsavedPostAction is save', async () => {
2026-01-06 16:22:52 +02:00
const wrapper = mount(
PostStatusForm,
mountOpts({
props: {
closeable: true,
},
}),
)
2025-02-18 17:42:50 -05:00
await wrapper.vm.$store.dispatch('setOption', {
name: 'autoSaveDraft',
2026-01-06 16:22:52 +02:00
value: false,
2025-02-18 17:42:50 -05:00
})
await wrapper.vm.$store.dispatch('setOption', {
name: 'unsavedPostAction',
2026-01-06 16:22:52 +02:00
value: 'save',
2025-02-18 17:42:50 -05:00
})
expect(wrapper.vm.$store.getters.draftCount).to.equal(0)
const textarea = wrapper.get('textarea')
await textarea.setValue('mew mew')
wrapper.vm.requestClose()
expect(wrapper.vm.$store.getters.draftCount).to.equal(1)
await waitForEvent(wrapper, 'can-close')
})
it('should discard when close if auto-save is off, and unsavedPostAction is discard', async () => {
2026-01-06 16:22:52 +02:00
const wrapper = mount(
PostStatusForm,
mountOpts({
props: {
closeable: true,
},
}),
)
2025-02-18 17:42:50 -05:00
await wrapper.vm.$store.dispatch('setOption', {
name: 'autoSaveDraft',
2026-01-06 16:22:52 +02:00
value: false,
2025-02-18 17:42:50 -05:00
})
await wrapper.vm.$store.dispatch('setOption', {
name: 'unsavedPostAction',
2026-01-06 16:22:52 +02:00
value: 'discard',
2025-02-18 17:42:50 -05:00
})
expect(wrapper.vm.$store.getters.draftCount).to.equal(0)
const textarea = wrapper.get('textarea')
await textarea.setValue('mew mew')
wrapper.vm.requestClose()
await waitForEvent(wrapper, 'can-close')
expect(wrapper.vm.$store.getters.draftCount).to.equal(0)
})
it('should confirm when close if auto-save is off, and unsavedPostAction is confirm', async () => {
2026-01-06 16:22:52 +02:00
const wrapper = mount(
PostStatusForm,
mountOpts({
props: {
closeable: true,
},
}),
)
2025-02-27 22:54:23 -05:00
await wrapper.vm.$store.dispatch('setOption', {
name: 'autoSaveDraft',
2026-01-06 16:22:52 +02:00
value: false,
2025-02-27 22:54:23 -05:00
})
await wrapper.vm.$store.dispatch('setOption', {
name: 'unsavedPostAction',
2026-01-06 16:22:52 +02:00
value: 'confirm',
2025-02-27 22:54:23 -05:00
})
expect(wrapper.vm.$store.getters.draftCount).to.equal(0)
const textarea = wrapper.get('textarea')
await textarea.setValue('mew mew')
wrapper.vm.requestClose()
await nextTick()
2026-01-06 16:22:52 +02:00
const saveButton = wrapper.findByText(
'button',
$t('post_status.close_confirm_save_button'),
)
2025-02-27 22:54:23 -05:00
expect(saveButton).to.be.ok
await saveButton.trigger('click')
console.info('clicked')
2025-02-27 22:54:23 -05:00
expect(wrapper.vm.$store.getters.draftCount).to.equal(1)
await flushPromises()
await waitForEvent(wrapper, 'can-close')
2025-02-18 17:42:50 -05:00
})
})