logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://anongit.hacktivis.me/git/pleroma-fe.git/

draft.spec.js (5409B)


  1. import { mount, flushPromises } from '@vue/test-utils'
  2. import { nextTick } from 'vue'
  3. import sinon from 'sinon'
  4. import PostStatusForm from 'src/components/post_status_form/post_status_form.vue'
  5. import { mountOpts, waitForEvent, $t } from '../../../fixtures/setup_test'
  6. const autoSaveOrNot = (caseFn, caseTitle, runFn) => {
  7. caseFn(`${caseTitle} with auto-save`, function () {
  8. return runFn.bind(this)(true)
  9. })
  10. caseFn(`${caseTitle} with no auto-save`, function () {
  11. return runFn.bind(this)(false)
  12. })
  13. }
  14. const saveManually = async (wrapper) => {
  15. const morePostActions = wrapper.findByText('button', $t('post_status.more_post_actions'))
  16. await morePostActions.trigger('click')
  17. const btn = wrapper.findByText('button', $t('post_status.save_to_drafts_button'))
  18. await btn.trigger('click')
  19. }
  20. const waitSaveTime = 4000
  21. afterEach(() => {
  22. sinon.restore()
  23. })
  24. describe('Draft saving', () => {
  25. autoSaveOrNot(it, 'should save when the button is clicked', async (autoSave) => {
  26. const wrapper = mount(PostStatusForm, mountOpts())
  27. await wrapper.vm.$store.dispatch('setOption', {
  28. name: 'autoSaveDraft',
  29. value: autoSave
  30. })
  31. expect(wrapper.vm.$store.getters.draftCount).to.equal(0)
  32. const textarea = wrapper.get('textarea')
  33. await textarea.setValue('mew mew')
  34. await saveManually(wrapper)
  35. expect(wrapper.vm.$store.getters.draftCount).to.equal(1)
  36. expect(wrapper.vm.$store.getters.draftsArray[0].status).to.equal('mew mew')
  37. console.log('done')
  38. })
  39. it('should auto-save if it is enabled', async function () {
  40. this.timeout(5000)
  41. const clock = sinon.useFakeTimers(Date.now())
  42. const wrapper = mount(PostStatusForm, mountOpts())
  43. await wrapper.vm.$store.dispatch('setOption', {
  44. name: 'autoSaveDraft',
  45. value: true
  46. })
  47. expect(wrapper.vm.$store.getters.draftCount).to.equal(0)
  48. const textarea = wrapper.get('textarea')
  49. await textarea.setValue('mew mew')
  50. expect(wrapper.vm.$store.getters.draftCount).to.equal(0)
  51. await clock.tickAsync(waitSaveTime)
  52. expect(wrapper.vm.$store.getters.draftCount).to.equal(1)
  53. expect(wrapper.vm.$store.getters.draftsArray[0].status).to.equal('mew mew')
  54. clock.restore()
  55. })
  56. it('should auto-save when close if auto-save is on', async () => {
  57. const wrapper = mount(PostStatusForm, mountOpts({
  58. props: {
  59. closeable: true
  60. }
  61. }))
  62. await wrapper.vm.$store.dispatch('setOption', {
  63. name: 'autoSaveDraft',
  64. value: true
  65. })
  66. expect(wrapper.vm.$store.getters.draftCount).to.equal(0)
  67. const textarea = wrapper.get('textarea')
  68. await textarea.setValue('mew mew')
  69. wrapper.vm.requestClose()
  70. expect(wrapper.vm.$store.getters.draftCount).to.equal(1)
  71. await waitForEvent(wrapper, 'can-close')
  72. console.log('done')
  73. })
  74. it('should save when close if auto-save is off, and unsavedPostAction is save', async () => {
  75. const wrapper = mount(PostStatusForm, mountOpts({
  76. props: {
  77. closeable: true
  78. }
  79. }))
  80. await wrapper.vm.$store.dispatch('setOption', {
  81. name: 'autoSaveDraft',
  82. value: false
  83. })
  84. await wrapper.vm.$store.dispatch('setOption', {
  85. name: 'unsavedPostAction',
  86. value: 'save'
  87. })
  88. expect(wrapper.vm.$store.getters.draftCount).to.equal(0)
  89. const textarea = wrapper.get('textarea')
  90. await textarea.setValue('mew mew')
  91. wrapper.vm.requestClose()
  92. expect(wrapper.vm.$store.getters.draftCount).to.equal(1)
  93. await waitForEvent(wrapper, 'can-close')
  94. console.log('done')
  95. })
  96. it('should discard when close if auto-save is off, and unsavedPostAction is discard', async () => {
  97. const wrapper = mount(PostStatusForm, mountOpts({
  98. props: {
  99. closeable: true
  100. }
  101. }))
  102. await wrapper.vm.$store.dispatch('setOption', {
  103. name: 'autoSaveDraft',
  104. value: false
  105. })
  106. await wrapper.vm.$store.dispatch('setOption', {
  107. name: 'unsavedPostAction',
  108. value: 'discard'
  109. })
  110. expect(wrapper.vm.$store.getters.draftCount).to.equal(0)
  111. const textarea = wrapper.get('textarea')
  112. await textarea.setValue('mew mew')
  113. wrapper.vm.requestClose()
  114. await waitForEvent(wrapper, 'can-close')
  115. expect(wrapper.vm.$store.getters.draftCount).to.equal(0)
  116. console.log('done')
  117. })
  118. it('should confirm when close if auto-save is off, and unsavedPostAction is confirm', async () => {
  119. try {
  120. const wrapper = mount(PostStatusForm, mountOpts({
  121. props: {
  122. closeable: true
  123. }
  124. }))
  125. await wrapper.vm.$store.dispatch('setOption', {
  126. name: 'autoSaveDraft',
  127. value: false
  128. })
  129. await wrapper.vm.$store.dispatch('setOption', {
  130. name: 'unsavedPostAction',
  131. value: 'confirm'
  132. })
  133. expect(wrapper.vm.$store.getters.draftCount).to.equal(0)
  134. const textarea = wrapper.get('textarea')
  135. await textarea.setValue('mew mew')
  136. wrapper.vm.requestClose()
  137. await nextTick()
  138. const saveButton = wrapper.findByText('button', $t('post_status.close_confirm_save_button'))
  139. expect(saveButton).to.be.ok
  140. await saveButton.trigger('click')
  141. console.log('clicked')
  142. expect(wrapper.vm.$store.getters.draftCount).to.equal(1)
  143. await flushPromises()
  144. await waitForEvent(wrapper, 'can-close')
  145. console.log('done')
  146. } catch (e) {
  147. console.log('error:', e)
  148. throw e
  149. }
  150. })
  151. })