39 lines
1.3 KiB
TypeScript
39 lines
1.3 KiB
TypeScript
import { mount } from '@vue/test-utils'
|
|
import { createPinia, setActivePinia } from 'pinia'
|
|
import { describe, expect, it, beforeEach } from 'vitest'
|
|
import { defineComponent } from 'vue'
|
|
import { permission } from '@/directives/permission'
|
|
import { useUserStore } from '@/stores/user'
|
|
|
|
const host = defineComponent({
|
|
template: '<div><button v-permission="\'account:write\'" data-q="btn">操作</button></div>',
|
|
})
|
|
|
|
describe('v-permission 指令', () => {
|
|
beforeEach(() => { setActivePinia(createPinia()) })
|
|
|
|
function setupUser(roles: string[], permissions: string[]) {
|
|
const s = useUserStore()
|
|
s.user = { account_id: 1, username: 'u', roles, permissions }
|
|
return s
|
|
}
|
|
|
|
it('admin 直通:按钮保留', () => {
|
|
setupUser(['admin'], [])
|
|
const w = mount(host, { global: { directives: { permission } } })
|
|
expect(w.find('[data-q="btn"]').exists()).toBe(true)
|
|
})
|
|
|
|
it('有对应权限码:按钮保留', () => {
|
|
setupUser(['ops'], ['account:write'])
|
|
const w = mount(host, { global: { directives: { permission } } })
|
|
expect(w.find('[data-q="btn"]').exists()).toBe(true)
|
|
})
|
|
|
|
it('无权限码:按钮被移除', () => {
|
|
setupUser(['customer'], ['message:write'])
|
|
const w = mount(host, { global: { directives: { permission } } })
|
|
expect(w.find('[data-q="btn"]').exists()).toBe(false)
|
|
})
|
|
})
|