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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 = () => requireLogin(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. requireLogin: action => action(),
  26. getUserLoveTicket: () => Promise.resolve({ loveValue: parentLoveValue, couponIds }),
  27. proxy: { $showModal: () => {} },
  28. uni: {
  29. getStorageSync: () => JSON.stringify({ id: 'parent-1', userLevel: 0 }),
  30. navigateTo: options => navigations.push(options)
  31. },
  32. encodeURIComponent,
  33. JSON
  34. }
  35. vm.createContext(context)
  36. vm.runInContext(`${source.slice(start, end)}\nthis.handleApply = handleApply`, context)
  37. return { handleApply: context.handleApply, fail, navigations }
  38. }
  39. test('家长爱心值为零时仍可进入报名页并由后端校验所选孩子余额', async () => {
  40. const { handleApply, fail, navigations } = loadHandleApply({
  41. activityLimit: 1,
  42. valueLimit: 10,
  43. parentLoveValue: 0
  44. })
  45. await handleApply()
  46. assert.equal(fail.value, false)
  47. assert.equal(navigations.length, 1)
  48. assert.match(navigations[0].url, /^\/pagesHome\/activityApply\?activity=/)
  49. })