activity-signup-member-balance.test.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 detailFile = path.resolve('pagesHome/activityDetail.vue')
  7. function loadHandleApply ({ activityLimit, valueLimit, parentLoveValue, couponIds = [] }) {
  8. const source = fs.readFileSync(detailFile, 'utf8')
  9. const start = source.indexOf('\tconst handleApply = async () => {')
  10. const end = source.indexOf('\n\t//取消报名', start)
  11. assert.notEqual(start, -1, '应找到立即报名方法')
  12. assert.notEqual(end, -1, '应找到立即报名方法结束位置')
  13. const navigations = []
  14. const fail = { value: false }
  15. const context = {
  16. activityInfo: {
  17. value: {
  18. id: 'activity-1',
  19. activityLimit,
  20. valueLimit,
  21. userLevel: 0
  22. }
  23. },
  24. fail,
  25. getUserLoveTicket: () => Promise.resolve({ loveValue: parentLoveValue, couponIds }),
  26. proxy: { $showModal: () => {} },
  27. uni: {
  28. getStorageSync: () => JSON.stringify({ id: 'parent-1', userLevel: 0 }),
  29. navigateTo: options => navigations.push(options)
  30. },
  31. encodeURIComponent,
  32. JSON
  33. }
  34. vm.createContext(context)
  35. vm.runInContext(`${source.slice(start, end)}\nthis.handleApply = handleApply`, context)
  36. return { handleApply: context.handleApply, fail, navigations }
  37. }
  38. test('家长爱心值为零时仍可进入报名页并由后端校验所选孩子余额', async () => {
  39. const { handleApply, fail, navigations } = loadHandleApply({
  40. activityLimit: 1,
  41. valueLimit: 10,
  42. parentLoveValue: 0
  43. })
  44. await handleApply()
  45. assert.equal(fail.value, false)
  46. assert.equal(navigations.length, 1)
  47. assert.match(navigations[0].url, /^\/pagesHome\/activityApply\?activity=/)
  48. })