import {Ability} from "@casl/ability"; import {createStore} from "~/tests/unit/Helpers"; import {AbilitiesType, AccessStore} from "~/types/interfaces"; import {$accessProfile} from "~/services/profile/accessProfile"; import {ABILITIES} from "~/types/enums"; import {accessProfile as accessModule} from "~/tests/unit/fixture/state/profile" let ability: Ability, store:AccessStore, accessProfile:any beforeEach( ()=>{ ability = new Ability(); store = createStore() store.registerModule('profile',{}) store.registerModule(['profile','access'], accessModule) accessProfile = $accessProfile(store, ability) }) describe('hasRole()', () => { it('should return true if there is no role', () => { expect(accessProfile.hasRole(null)).toBeTruthy() }) it('should return false if profile dont have the role', () =>{ const role_to_have = ['ROLE_EVENT'] expect(accessProfile.hasRole(role_to_have)).toBeFalsy() }) it('should return true if profile have the role', () =>{ const role_to_have = ['ROLE_USERS'] store.commit('access/setRoles', ['ROLE_USERS']) expect(accessProfile.hasRole(role_to_have)).toBeTruthy() }) }) describe('hasAbility()', () => { it('should return true if there is no ability', () => { expect(accessProfile.hasAbility(null)).toBeTruthy() }) it('should return false if profile dont have the ability', () =>{ const ability_to_have:Array = [{ action: ABILITIES.MANAGE, subject: 'bills' }] expect(accessProfile.hasAbility(ability_to_have)).toBeFalsy() }) it('should return true if profile dont have the ability', () =>{ const manage_bills:AbilitiesType = { action: ABILITIES.MANAGE, subject: 'bills' } const ability_to_have:Array = [manage_bills] ability.update([manage_bills]) expect(accessProfile.hasAbility(ability_to_have)).toBeTruthy() }) })