import { describe, it, expect } from 'vitest' import { transformCan } from '../src/runtime/transformer/transform-can' const TEST_FILE = `${process.cwd()}/components/Test.vue` const buildSFC = (template: string) => `\n${template}\n` const runTransform = (template: string) => { const result = transformCan({ code: buildSFC(template), id: TEST_FILE }) return result?.code ?? '' } describe('transformCan', () => { it('injects __can__ guards and a matching v-cannot block', () => { const code = runTransform(`
Refus
`) expect(code).toContain(`v-if="__can__(['employee', 'view'])"`) expect(code).toContain(`v-if="!(__can__(['employee', 'view']))"`) }) it('merges existing v-if expressions with the generated guard', () => { const code = runTransform(` `) expect(code).toContain(`v-if="(isReady) && __can__(['contract', 'create'])"`) }) it('throws when v-cannot is used without a preceding v-can', () => { const exec = () => transformCan({ code: buildSFC('Denied
'), id: TEST_FILE, }) expect(exec).toThrow(/must immediately follow its `v-can`/) }) it('throws when the expression does not start with can.*', () => { const exec = () => transformCan({ code: buildSFC(''), id: TEST_FILE, }) expect(exec).toThrow(/expressions must start with `can\.`/) }) it('throws when v-can is added to a v-else branch', () => { const exec = () => transformCan({ code: buildSFC(` `), id: TEST_FILE, }) expect(exec).toThrow(/cannot be used on `v-else`/) }) })