cmvr-wms/ruoyi-ui-vue3/tests/runtime/assistant-notice.spec.js

39 lines
2.9 KiB
JavaScript
Raw Normal View History

import { expect, it, vi } from 'vitest'
import { mount } from '@vue/test-utils'
import ElementPlus from 'element-plus'
import Content from '@/components/BusinessAssistant/AssistantNoticeContent.vue'
import { safeNoticeHtml } from '@/utils/assistant-notice'
vi.mock('@/components/Editor/index.vue', () => ({ default: { name: 'NoticeEditorFixture', props: ['modelValue', 'readOnly'], emits: ['update:modelValue'], template: '<textarea :value="modelValue" @input="$emit(\'update:modelValue\', $event.target.value)" />' } }))
it('previews text and real tutorial images without exposing model HTML as plain source', () => {
const row = { noticeTitle: '反馈使用说明', noticeType: '1', noticeContent: '<h3>提交反馈</h3><p>填写标题和说明</p><img src="/static/assistant-guides/feedback-1.svg" alt="操作示意图">' }
const wrapper = mount(Content, { props: { row, editable: true }, global: { plugins: [ElementPlus] } })
expect(wrapper.find('article h3').text()).toBe('提交反馈')
expect(wrapper.find('article img').attributes('src')).toBe('/static/assistant-guides/feedback-1.svg')
expect(wrapper.text()).toContain('确认后发布')
expect(wrapper.find('textarea').exists()).toBe(false)
expect(wrapper.emitted('change')).toBeUndefined()
wrapper.unmount()
})
it('editing emits a draft-only body change while read-only cards have no editor controls', async () => {
const wrapper = mount(Content, { props: { row: { _rowId: 'one', noticeContent: '<p>原文</p>' }, editable: true }, global: { plugins: [ElementPlus] } })
await wrapper.findAll('input[type="radio"]')[1].setValue(true)
await wrapper.find('textarea').setValue('<p>新内容</p>')
expect(wrapper.emitted('change')[0][0]).toEqual({ key: 'noticeContent', value: '<p>新内容</p>' })
await wrapper.setProps({ busy: true })
await wrapper.find('textarea').setValue('<p>忙时不改</p>')
expect(wrapper.emitted('change')).toHaveLength(1)
await wrapper.setProps({ editable: false })
expect(wrapper.find('textarea').exists()).toBe(false)
expect(wrapper.find('.el-radio-group').exists()).toBe(false)
wrapper.unmount()
})
it('never renders scripts, inline handlers, CSS overlays, embedded frames or unsafe image/link schemes', () => {
const html = safeNoticeHtml('<script>alert(1)</script><svg onload="evil()"></svg><p style="position:fixed" onclick="evil()">说明</p><img src="javascript:evil()" onerror="evil()"><a href="jav&#x61;script:evil()">链接</a><iframe src="https://example.invalid"></iframe><img src="//external.invalid/a.png"><img src="/static/assistant-guides/feedback-2.svg">')
const container = document.createElement('div'); container.innerHTML = html
expect(container.querySelectorAll('script,svg,iframe,[onclick],[onerror],[style]')).toHaveLength(0)
expect(container.querySelectorAll('img')).toHaveLength(1)
expect(container.querySelector('a').getAttribute('href')).toBeNull()
expect(container.textContent).toContain('说明')
})