activity-draft-validation.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import test from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import fs from 'node:fs'
  4. import path from 'node:path'
  5. import vm from 'node:vm'
  6. const addFile = path.resolve('src/views/modules/activity/add.vue')
  7. function loadHandleTempSave () {
  8. const source = fs.readFileSync(addFile, 'utf8')
  9. const start = source.indexOf(' handleTempSave () {')
  10. const end = source.indexOf('\n handlePrevious () {', start)
  11. assert.notEqual(start, -1, '应找到保存草稿方法')
  12. assert.notEqual(end, -1, '应找到保存草稿方法结束位置')
  13. const method = source.slice(start, end)
  14. .replace(' handleTempSave () {', 'function handleTempSave () {')
  15. .replace(/\n },\s*$/, '\n}')
  16. const context = { Date, JSON }
  17. vm.createContext(context)
  18. vm.runInContext(`${method}\nthis.handleTempSave = handleTempSave`, context)
  19. return context.handleTempSave
  20. }
  21. function loadAutoSave () {
  22. const source = fs.readFileSync(addFile, 'utf8')
  23. const start = source.indexOf(' autoSave () {')
  24. const end = source.indexOf('\n handleSave () {', start)
  25. assert.notEqual(start, -1, '应找到自动保存方法')
  26. assert.notEqual(end, -1, '应找到自动保存方法结束位置')
  27. const method = source.slice(start, end)
  28. .replace(' autoSave () {', 'function autoSave () {')
  29. .replace(/\n },\s*$/, '\n}')
  30. const context = { JSON }
  31. vm.createContext(context)
  32. vm.runInContext(`${method}\nthis.autoSave = autoSave`, context)
  33. return context.autoSave
  34. }
  35. test('空白活动名称不能提交保存草稿请求', () => {
  36. const handleTempSave = loadHandleTempSave()
  37. const errors = []
  38. const requests = []
  39. const component = {
  40. basicForm: {
  41. id: '',
  42. activityName: ' ',
  43. categoryIdsArr: [],
  44. signupStartTime: '',
  45. signupEndTime: '',
  46. activityStartTime: '',
  47. activityEndTime: ''
  48. },
  49. moreForm: {},
  50. syncCategoryIds: () => {},
  51. $message: {
  52. error: message => errors.push(message),
  53. success: () => {}
  54. },
  55. $http: {
  56. post: (...args) => {
  57. requests.push(args)
  58. return Promise.resolve({ data: { code: 0 } })
  59. }
  60. },
  61. $router: { push: () => {} }
  62. }
  63. handleTempSave.call(component)
  64. assert.deepEqual(errors, ['请填写活动名称后再保存草稿'])
  65. assert.equal(requests.length, 0)
  66. })
  67. test('编辑草稿自动保存时保留草稿状态', async () => {
  68. const source = fs.readFileSync(addFile, 'utf8')
  69. const initialForm = source.slice(
  70. source.indexOf('const getInitialBasicForm = () => ({'),
  71. source.indexOf('const getInitialMoreForm = () => ({')
  72. )
  73. assert.match(initialForm, /activeState:\s*null/, '表单必须接收详情接口返回的活动状态')
  74. const autoSave = loadAutoSave()
  75. const requests = []
  76. const component = {
  77. basicForm: { id: '1', activityName: '测试草稿', activeState: -2 },
  78. moreForm: { state: false },
  79. autoSaving: false,
  80. $message: { error: () => {} },
  81. $http: {
  82. put: (...args) => {
  83. requests.push(args)
  84. return Promise.resolve({ data: { code: 0 } })
  85. }
  86. }
  87. }
  88. autoSave.call(component)
  89. await Promise.resolve()
  90. assert.equal(requests.length, 1)
  91. assert.equal(requests[0][1].activeState, -2)
  92. })
  93. test('编辑草稿名称为空时不执行自动保存', () => {
  94. const autoSave = loadAutoSave()
  95. const errors = []
  96. const requests = []
  97. const component = {
  98. basicForm: { id: '1', activityName: ' ', activeState: -2 },
  99. moreForm: { state: false },
  100. autoSaving: false,
  101. $message: { error: message => errors.push(message) },
  102. $http: {
  103. put: (...args) => {
  104. requests.push(args)
  105. return Promise.resolve({ data: { code: 0 } })
  106. }
  107. }
  108. }
  109. autoSave.call(component)
  110. assert.deepEqual(errors, ['请填写活动名称后再自动保存草稿'])
  111. assert.equal(requests.length, 0)
  112. })