| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- import test from 'node:test'
- import assert from 'node:assert/strict'
- import fs from 'node:fs'
- import path from 'node:path'
- import vm from 'node:vm'
- const detailFile = path.resolve('pagesHome/activityDetail.vue')
- function loadHandleApply ({ activityLimit, valueLimit, parentLoveValue, couponIds = [] }) {
- const source = fs.readFileSync(detailFile, 'utf8')
- const start = source.indexOf('\tconst handleApply = () => requireLogin(async () => {')
- const end = source.indexOf('\n\t//取消报名', start)
- assert.notEqual(start, -1, '应找到立即报名方法')
- assert.notEqual(end, -1, '应找到立即报名方法结束位置')
- const navigations = []
- const fail = { value: false }
- const context = {
- activityInfo: {
- value: {
- id: 'activity-1',
- activityLimit,
- valueLimit,
- userLevel: 0
- }
- },
- fail,
- requireLogin: action => action(),
- getUserLoveTicket: () => Promise.resolve({ loveValue: parentLoveValue, couponIds }),
- proxy: { $showModal: () => {} },
- uni: {
- getStorageSync: () => JSON.stringify({ id: 'parent-1', userLevel: 0 }),
- navigateTo: options => navigations.push(options)
- },
- encodeURIComponent,
- JSON
- }
- vm.createContext(context)
- vm.runInContext(`${source.slice(start, end)}\nthis.handleApply = handleApply`, context)
- return { handleApply: context.handleApply, fail, navigations }
- }
- test('家长爱心值为零时仍可进入报名页并由后端校验所选孩子余额', async () => {
- const { handleApply, fail, navigations } = loadHandleApply({
- activityLimit: 1,
- valueLimit: 10,
- parentLoveValue: 0
- })
- await handleApply()
- assert.equal(fail.value, false)
- assert.equal(navigations.length, 1)
- assert.match(navigations[0].url, /^\/pagesHome\/activityApply\?activity=/)
- })
|