刘光辉
4 小时以前 cf11e63b659afcc9792ab3e395f1b45d844431a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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();
  });
});