|
| 1 | +import { describe, it, expect, nextTick } from 'vitest' |
| 2 | +import { shallowMount } from '@vue/test-utils' |
| 3 | +import { createTestingPinia } from '@pinia/testing' |
| 4 | +import { useUserStore } from '@/store/user' |
| 5 | +import vCanPiece from '@/directives/canPiece' |
| 6 | + |
| 7 | +const TestComponent = { |
| 8 | + props: ['piece'], |
| 9 | + template: `<div v-can-piece="piece">Noten anzeigen</div>` |
| 10 | +} |
| 11 | + |
| 12 | +describe('v-can-piece directive', () => { |
| 13 | + it('shows element if user has access to piece', async () => { |
| 14 | + const pinia = createTestingPinia() |
| 15 | + const wrapper = shallowMount(TestComponent, { |
| 16 | + props: { |
| 17 | + piece: { id: 1, voice: 'Tenor' } |
| 18 | + }, |
| 19 | + global: { |
| 20 | + plugins: [pinia], |
| 21 | + directives: { 'can-piece': vCanPiece } |
| 22 | + } |
| 23 | + }) |
| 24 | + |
| 25 | + const userStore = useUserStore() |
| 26 | + userStore.$patch({ primaryRole: 'Admin' }) |
| 27 | + |
| 28 | + await nextTick() |
| 29 | + expect(wrapper.element.style.display).not.toBe('none') |
| 30 | + }) |
| 31 | + |
| 32 | + it('hides element if user lacks access to piece', async () => { |
| 33 | + const pinia = createTestingPinia() |
| 34 | + const wrapper = shallowMount(TestComponent, { |
| 35 | + props: { |
| 36 | + piece: { id: 1, voice: 'Tenor' } |
| 37 | + }, |
| 38 | + global: { |
| 39 | + plugins: [pinia], |
| 40 | + directives: { 'can-piece': vCanPiece } |
| 41 | + } |
| 42 | + }) |
| 43 | + |
| 44 | + const userStore = useUserStore() |
| 45 | + userStore.$patch({ primaryRole: 'Mitglied' }) // ohne passende Stimme |
| 46 | + |
| 47 | + await nextTick() |
| 48 | + expect(wrapper.element.style.display).toBe('none') |
| 49 | + }) |
| 50 | +}) |
0 commit comments