mobile-pl-plan-form-session.test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import assert from 'node:assert/strict'
  2. import { readFileSync } from 'node:fs'
  3. import test from 'node:test'
  4. import { parse } from '@vue/compiler-sfc'
  5. import ts from 'typescript'
  6. import * as vue from 'vue'
  7. test('mobile form remounts on identity changes and rejects events from the previous form', () => {
  8. const source = parse(
  9. readFileSync(new URL('../src/views/m/pl/plan/form.vue', import.meta.url), 'utf8')
  10. ).descriptor.scriptSetup!.content
  11. const tree = ts.createSourceFile('form.ts', source, ts.ScriptTarget.Latest, true)
  12. const code = tree.statements
  13. .filter((node) => !ts.isImportDeclaration(node))
  14. .map((node) => node.getText(tree))
  15. .join('\n')
  16. const user = vue.reactive({ accessToken: 'old', info: { id: 'u1' } })
  17. const route = vue.reactive({ name: 'MpPlPlanEdit', params: { id: 'A' }, query: {}, meta: {} })
  18. const navigations: any[] = []
  19. const bindings = {
  20. computed: vue.computed,
  21. ref: vue.ref,
  22. watch: vue.watch,
  23. useUserStore: () => user,
  24. useRoute: () => route,
  25. useRouter: () => ({ replace: (to: any) => navigations.push(to) }),
  26. MP_PLAN_LIST_PATH: '/m/pl/plan'
  27. }
  28. const scope = vue.effectScope()
  29. const api: any = scope.run(() =>
  30. new Function(
  31. ...Object.keys(bindings),
  32. ts.transpile(code) +
  33. ';return {formKey,contentLoading,formEvents: typeof formEvents === "undefined" ? undefined : formEvents}'
  34. )(...Object.values(bindings))
  35. )
  36. try {
  37. const key = api.formKey.value
  38. const oldEvents = api.formEvents?.value
  39. api.contentLoading.value = false
  40. user.accessToken = 'new'
  41. assert.notEqual(api.formKey.value, key)
  42. assert.equal(api.contentLoading.value, true)
  43. oldEvents['loading-change'](false)
  44. oldEvents.success({ planId: 'OLD' })
  45. oldEvents.close()
  46. assert.equal(api.contentLoading.value, true)
  47. assert.deepEqual(navigations, [])
  48. api.formEvents.value['loading-change'](false)
  49. assert.equal(api.contentLoading.value, false)
  50. api.formEvents.value.success({ planId: 'NEW' })
  51. assert.deepEqual(navigations, ['/m/pl/plan/NEW'])
  52. } finally {
  53. scope.stop()
  54. }
  55. })