import { describe, expect, it, vi } from 'vitest';
|
|
import { canShowButton, collectButtonCodes, getAssociatedButtonCodes } from './buttonPermission';
|
|
const getButtonAuthorizeSelector = vi.hoisted(() => vi.fn());
|
vi.mock('#/api/system/buttonAuthorize', () => ({ getButtonAuthorizeSelector }));
|
|
describe('selective button permission', () => {
|
it('collects codes from nested resource entries', () => {
|
expect(collectButtonCodes([{ enCode: 'btn_add', children: [{ enCode: 'custom.action' }] }])).toEqual(new Set(['btn_add', 'custom.action']));
|
});
|
|
it('shows unassociated buttons and filters associated buttons by user grants', () => {
|
const associated = new Set(['btn_add', 'custom.action']);
|
expect(canShowButton('btn_add', false, associated)).toBe(false);
|
expect(canShowButton('custom.action', true, associated)).toBe(true);
|
expect(canShowButton('btn_edit', false, associated)).toBe(true);
|
});
|
|
it('keeps strict filtering when association lookup is unavailable', () => {
|
expect(canShowButton('btn_edit', false, null)).toBe(false);
|
expect(canShowButton('btn_edit', true, null)).toBe(true);
|
});
|
|
it('reads all resources for the current menu and fails closed on request errors', async () => {
|
getButtonAuthorizeSelector.mockResolvedValueOnce({ data: { list: [{ enCode: 'btn_add' }] } });
|
expect(await getAssociatedButtonCodes('menu-1')).toEqual(new Set(['btn_add']));
|
expect(getButtonAuthorizeSelector).toHaveBeenCalledWith('menu-1');
|
|
getButtonAuthorizeSelector.mockRejectedValueOnce(new Error('unavailable'));
|
expect(await getAssociatedButtonCodes('menu-1')).toBeNull();
|
});
|
});
|