SilderMenu.test.js 1.36 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
const flatMenuKeys = getFlatMenuKeys(menu);

Alan Wei's avatar
Alan Wei committed
21
describe('test convert nested menu to flat menu', () => {
22 23 24 25 26 27 28 29
  it('simple menu', () => {
    expect(flatMenuKeys).toEqual(
      ['/dashboard', '/dashboard/name', '/userinfo', '/userinfo/:id', '/userinfo/:id/info']
    );
  })
});

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

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

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

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

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