Skip to content

Commit

Permalink
refactor: improve the MenuBarService (#365)
Browse files Browse the repository at this point in the history
* test: rename unit test for menuBarService

* refactor: rename the api of menuBarService and refactor the private function
  • Loading branch information
mortalYoung authored Aug 26, 2021
1 parent a9c6ded commit 1d75efc
Show file tree
Hide file tree
Showing 6 changed files with 315 additions and 221 deletions.
2 changes: 1 addition & 1 deletion src/extensions/menuBar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import { builtInMenuBarData } from 'mo/model';

export const ExtendsMenuBar: IExtension = {
activate(extensionCtx: IExtensionService) {
molecule.menuBar.initMenus(builtInMenuBarData());
molecule.menuBar.setMenus(builtInMenuBarData());
},
};
2 changes: 1 addition & 1 deletion src/model/workbench/menuBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface IMenuBarItem {
id?: string;
name?: React.ReactNode;
icon?: string | JSX.Element;
data?: any;
data?: IMenuBarItem[];
render?: () => React.ReactNode | JSX.Element;
}
export interface IMenuBar {
Expand Down
212 changes: 212 additions & 0 deletions src/services/__tests__/menuBarService.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
import logger from 'mo/common/logger';
import { MenuBarEvent } from 'mo/model';
import 'reflect-metadata';
import { container } from 'tsyringe';

import { IMenuBarService, MenuBarService } from '../workbench/menuBarService';

const menuBarService = container.resolve<IMenuBarService>(MenuBarService);

const mockData = [
{
id: '1',
name: 'root',
data: [
{
id: '2',
name: 'a',
data: [{ id: '3', name: 'c' }],
},
{
id: '4',
name: 'b',
},
],
},
];

/**
* 测试某一行为是否会输出错误日志
* @param action
*/
function logError(action) {
const original = logger.error;
logger.error = jest.fn();

action();

expect(logger.error).toBeCalled();
logger.error = original;
}

describe('Test menuBarService', () => {
afterEach(() => {
menuBarService.reset();
});
test('Should have default value data', () => {
const state = menuBarService.getState();
expect(state.data).toEqual([]);
});

test('Should support to set menu data', () => {
menuBarService.setMenus(mockData);

const state = menuBarService.getState();
expect(state.data).toEqual(mockData);
});

test('Should support append into data ', () => {
menuBarService.setMenus(mockData);
menuBarService.append(
{
id: '5',
name: 'd',
icon: 'check',
},
'2'
);
const state = menuBarService.getState();
expect(state.data).toEqual([
{
id: '1',
name: 'root',
data: [
{
id: '2',
name: 'a',
data: [
{ id: '3', name: 'c' },
{ id: '5', name: 'd', icon: 'check' },
],
},
{
id: '4',
name: 'b',
},
],
},
]);
});

test('Should support to append a data without data property', () => {
menuBarService.setMenus(mockData);
menuBarService.append(
{
id: '5',
name: 'd',
icon: 'check',
},
'4'
);
const state = menuBarService.getState();
expect(state.data).toEqual([
{
id: '1',
name: 'root',
data: [
{
id: '2',
name: 'a',
data: [{ id: '3', name: 'c' }],
},
{
id: '4',
name: 'b',
data: [{ id: '5', name: 'd', icon: 'check' }],
},
],
},
]);
});

test('Should log error when append failed', () => {
logError(() =>
menuBarService.append(
{
id: '5',
name: 'd',
icon: 'check',
},
'2'
)
);
});

test('Should support to remove the specific menu', () => {
menuBarService.setMenus(mockData);
menuBarService.remove('4');
const state = menuBarService.getState();
expect(state.data).toEqual([
{
id: '1',
name: 'root',
data: [
{
id: '2',
name: 'a',
data: [{ id: '3', name: 'c' }],
},
],
},
]);
});

test('Should log error when remove failed', () => {
logError(() => menuBarService.remove('4'));
});

test('Should support to update the specific menu', () => {
menuBarService.setMenus(mockData);
menuBarService.update('3', {
id: '3',
icon: 'check',
name: 'modifierc',
});
const result = menuBarService.getState();
expect(result?.data).toEqual([
{
id: '1',
name: 'root',
data: [
{
id: '2',
name: 'a',
data: [{ id: '3', name: 'modifierc', icon: 'check' }],
},
{
id: '4',
name: 'b',
},
],
},
]);
});

test('Should support to log error when update failed', () => {
logError(() => menuBarService.update('4', {}));
});

test('Should support to get nested menu data', () => {
menuBarService.setMenus(mockData);
const currentMenu = menuBarService.getMenuById('3');
expect(currentMenu).toEqual({
id: '3',
name: 'c',
});

const nilMenu = menuBarService.getMenuById('10');
expect(nilMenu).toBeUndefined();
});

test('Should support to subscribe the onSelect event', () => {
const mockFn = jest.fn();
const mockMenuId = '1';
menuBarService.onSelect(mockFn);

expect(mockFn).not.toBeCalled();
menuBarService.emit(MenuBarEvent.onSelect, mockMenuId);

expect(mockFn).toBeCalled();
expect(mockFn.mock.calls[0][0]).toBe(mockMenuId);
});
});
123 changes: 0 additions & 123 deletions src/services/workbench/__tests__/menuBarService.test.ts

This file was deleted.

Loading

0 comments on commit 1d75efc

Please sign in to comment.