useForm.spec.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import {createStore, initLocalVue, mountComposition} from '~/tests/unit/Helpers'
  2. import { form } from '~/tests/unit/fixture/state/profile'
  3. import {useForm} from "~/composables/form/useForm";
  4. import { AnyStore } from '~/types/interfaces'
  5. import {FORM_STATUS, SUBMIT_TYPE} from "~/types/enums";
  6. import {Route} from "vue-router";
  7. let store: AnyStore
  8. let useFormMount:any
  9. beforeAll(() => {
  10. store = createStore()
  11. store.registerModule('form', form)
  12. initLocalVue({store: store})
  13. const component = mountComposition(() => {
  14. useFormMount = useForm(store)
  15. });
  16. })
  17. describe('markFormAsDirty()', () => {
  18. it('should commit the form state dirty to true', async () => {
  19. useFormMount.markAsDirty()
  20. expect(store.state.form.dirty).toBeTruthy()
  21. })
  22. })
  23. describe('markAsNotDirty()', () => {
  24. it('should commit the form state dirty to false', async () => {
  25. useFormMount.markAsNotDirty()
  26. expect(store.state.form.dirty).toBeFalsy()
  27. })
  28. })
  29. describe('nextStepFactory()', () => {
  30. const route: Route = {
  31. path: 'ma_route',
  32. hash: 'hash',
  33. query: {},
  34. params: {},
  35. fullPath: 'ma_route',
  36. matched: []
  37. }
  38. it('should push the new route when the action is save and back', async () => {
  39. const router: any = []
  40. useFormMount.nextStepFactory(route, {}, router)[SUBMIT_TYPE.SAVE_AND_BACK]()
  41. expect(router).toHaveLength(1)
  42. })
  43. it('should dont push the new route when the action is save and the form is EDIT', async () => {
  44. store.commit('form/setFormStatus', FORM_STATUS.EDIT)
  45. const router: any = []
  46. useFormMount.nextStepFactory(route, {}, router)[SUBMIT_TYPE.SAVE]()
  47. expect(router).toHaveLength(0)
  48. })
  49. it('should push the new route when the action is save and the form is CREATE', async () => {
  50. store.commit('form/setFormStatus', FORM_STATUS.CREATE)
  51. const router: any = []
  52. useFormMount.nextStepFactory(route, {}, router)[SUBMIT_TYPE.SAVE]()
  53. expect(router).toHaveLength(1)
  54. })
  55. })