SilderMenu.test.js 1.54 KB
Newer Older
1
import { urlToList } from '../_utils/pathTools';
jim's avatar
fix ci  
jim committed
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
import { getFlatMenuKeys, getMenuMatchKeys } from './SiderMenu';

const menu = [
  {
    path: '/dashboard',
    children: [
      {
        path: '/dashboard/name',
      },
    ],
  },
  {
    path: '/userinfo',
    children: [
      {
        path: '/userinfo/:id',
        children: [
          {
            path: '/userinfo/:id/info',
          },
        ],
      },
    ],
  },
];
27

28 29
const flatMenuKeys = getFlatMenuKeys(menu);

Alan Wei's avatar
Alan Wei committed
30
describe('test convert nested menu to flat menu', () => {
31
  it('simple menu', () => {
jim's avatar
fix ci  
jim committed
32 33 34 35 36 37 38 39
    expect(flatMenuKeys).toEqual([
      '/dashboard',
      '/dashboard/name',
      '/userinfo',
      '/userinfo/:id',
      '/userinfo/:id/info',
    ]);
  });
40 41 42
});

describe('test menu match', () => {
43
  it('simple path', () => {
jim's avatar
fix ci  
jim committed
44
    expect(getMenuMatchKeys(flatMenuKeys, urlToList('/dashboard'))).toEqual(['/dashboard']);
45
  });
46

47
  it('error path', () => {
jim's avatar
fix ci  
jim committed
48
    expect(getMenuMatchKeys(flatMenuKeys, urlToList('/dashboardname'))).toEqual([]);
49 50 51
  });

  it('Secondary path', () => {
jim's avatar
fix ci  
jim committed
52 53 54 55
    expect(getMenuMatchKeys(flatMenuKeys, urlToList('/dashboard/name'))).toEqual([
      '/dashboard',
      '/dashboard/name',
    ]);
56 57 58
  });

  it('Parameter path', () => {
jim's avatar
fix ci  
jim committed
59 60 61 62
    expect(getMenuMatchKeys(flatMenuKeys, urlToList('/userinfo/2144'))).toEqual([
      '/userinfo',
      '/userinfo/:id',
    ]);
63 64 65
  });

  it('three parameter path', () => {
jim's avatar
fix ci  
jim committed
66 67 68 69 70
    expect(getMenuMatchKeys(flatMenuKeys, urlToList('/userinfo/2144/info'))).toEqual([
      '/userinfo',
      '/userinfo/:id',
      '/userinfo/:id/info',
    ]);
71 72
  });
});