warningActionDrawer.test.mjs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import test from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import { readFileSync } from 'node:fs'
  4. import ts from 'typescript'
  5. import { computed, nextTick, reactive, ref, watch } from 'vue'
  6. const source = readFileSync(new URL('../src/components/WarningActionDrawer.vue', import.meta.url), 'utf8')
  7. const script = ts.transpileModule(source.match(/<script setup lang="ts">([\s\S]*?)<\/script>/)[1]
  8. .replace(/^import .*\r?\n/gm, ''), { compilerOptions: { target: ts.ScriptTarget.ES2020 } }).outputText
  9. function setup(reviewers) {
  10. const props = reactive({ modelValue: false, action: 'PUBLISH', handlers: [{ userName: 'handler' }], reviewers })
  11. const events = [], messages = []
  12. const state = new Function('computed', 'ref', 'watch', 'useUserStore', 'defineProps', 'withDefaults', 'defineEmits', 'uni',
  13. `${script}; return { handlerIndex, reviewerIndex, selectIndex, submit }`)(computed, ref, watch,
  14. () => ({ profile: { userName: 'current' } }), () => props, p => p,
  15. () => (...args) => events.push(args), { showToast: message => messages.push(message) })
  16. return { ...state, props, events, messages }
  17. }
  18. test('移动端仅对服务端返回的合格核验候选默认当前账户', async () => {
  19. for (const eligible of [true, false]) {
  20. const s = setup(eligible ? [{ userName: 'current' }, { userName: 'other' }] : [{ userName: 'other' }])
  21. s.props.modelValue = true
  22. await nextTick()
  23. assert.equal(s.handlerIndex.value, -1)
  24. assert.equal(s.reviewerIndex.value, eligible ? 0 : -1)
  25. s.submit()
  26. assert.equal(s.events.length, 0)
  27. assert.equal(s.messages.length, 1)
  28. }
  29. })
  30. test('首位候选可以明确选中,核验人支持改选其他人员', async () => {
  31. const s = setup([{ userName: 'current' }, { userName: 'other' }])
  32. s.props.modelValue = true
  33. await nextTick()
  34. s.selectIndex({ detail: { value: '1' } }, 'handler')
  35. s.selectIndex({ detail: { value: '2' } }, 'reviewer')
  36. s.submit()
  37. assert.equal(s.events[0][0], 'submit')
  38. assert.equal(s.events[0][1].handler, 'handler')
  39. assert.equal(s.events[0][1].reviewer, 'other')
  40. s.selectIndex({ detail: { value: '0' } }, 'reviewer')
  41. s.submit()
  42. assert.equal(s.events.length, 1)
  43. })