SilderMenu.test.js 1.41 KB
Newer Older
1 2
import { urlToList } from '../_utils/pathTools';
import { getFlatMenuKeys, getMeunMatchKeys } from './SiderMenu';
3

4 5 6 7 8 9 10 11 12 13 14 15 16 17
const menu = [{
  path: '/dashboard',
  children: [{
    path: '/dashboard/name',
  }],
}, {
  path: '/userinfo',
  children: [{
    path: '/userinfo/:id',
    children: [{
      path: '/userinfo/:id/info',
    }],
  }],
}];
18

19 20 21 22 23 24 25 26 27 28 29
const flatMenuKeys = getFlatMenuKeys(menu);

describe('test convert tree structure menu paths to flat menu paths', () => {
  it('simple menu', () => {
    expect(flatMenuKeys).toEqual(
      ['/dashboard', '/dashboard/name', '/userinfo', '/userinfo/:id', '/userinfo/:id/info']
    );
  })
});

describe('test menu match', () => {
30
  it('simple path', () => {
31
    expect(getMeunMatchKeys(flatMenuKeys, urlToList('/dashboard'), true)).toEqual(['/dashboard']);
32
  });
33

34
  it('error path', () => {
35
    expect(getMeunMatchKeys(flatMenuKeys, urlToList('/dashboardname'), true)).toEqual([]);
36 37 38
  });

  it('Secondary path', () => {
39
    expect(getMeunMatchKeys(flatMenuKeys, urlToList('/dashboard/name'), true)).toEqual(['/dashboard', '/dashboard/name']);
40 41 42
  });

  it('Parameter path', () => {
43
    expect(getMeunMatchKeys(flatMenuKeys, urlToList('/userinfo/2144'), true)).toEqual(['/userinfo', '/userinfo/:id']);
44 45 46
  });

  it('three parameter path', () => {
47
    expect(getMeunMatchKeys(flatMenuKeys, urlToList('/userinfo/2144/info'), false)).toEqual(['/userinfo', '/userinfo/:id', '/userinfo/:id/info']);
48 49
  });
});