Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create instanceService #733

Merged
merged 14 commits into from
Jun 7, 2022
Merged
Prev Previous commit
Next Next commit
test: add test cases for instanceService
  • Loading branch information
mortalYoung committed May 11, 2022
commit fd8e67d97311c8d08e053f353e069de4c129e11b
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`The InstanceService Should support render workbench 1`] = `
<DocumentFragment>
<div>
123
</div>
</DocumentFragment>
`;
98 changes: 98 additions & 0 deletions src/services/__tests__/instanceService.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import React from 'react';
import { defaultExtensions } from 'mo/extensions';
import InstanceService from '../instanceService';
import { render } from '@testing-library/react';
import { ExtendsLocales } from 'mo/extensions/locales-defaults';

describe('The InstanceService', () => {
test('Constuctor with default config', () => {
const instance = new InstanceService({}, {} as any);
const config = instance.getConfig();
expect(config.defaultLocale).toBe('en');
expect(config.extensions).toEqual(defaultExtensions);
});

test('Should init with params', () => {
const instance = new InstanceService(
{
defaultLocale: 'test',
extensions: [
{
id: 1,
name: 'test',
activate: () => {},
dispose: () => {},
},
],
},
{} as any
);
const config = instance.getConfig();
expect(config.defaultLocale).toBe('test');
expect(config.extensions).toHaveLength(defaultExtensions.length + 1);
});

test('Should support render workbench', () => {
const mockFn = jest.fn();
const instance = new InstanceService(
{},
{
// @ts-ignore
extension: {
load: jest.fn(),
splitLanguagesExts: jest.fn((ext) => [ext, []]),
},
// @ts-ignore
i18n: {
initialize: mockFn,
},
// @ts-ignore
monacoService: {
initWorkspace: jest.fn(),
},
// @ts-ignore
layout: {
container: document.body,
},
}
);
const { asFragment } = render(instance.render(<div>123</div>));
expect(asFragment()).toMatchSnapshot();

expect(mockFn.mock.calls[0][0]).toHaveLength(3);
expect(mockFn.mock.calls[0][0]).toEqual(
ExtendsLocales.contributes!.languages
);
});

test('Should support liftCycle hooks', () => {
const instance = new InstanceService(
{},
{
// @ts-ignore
extension: {
splitLanguagesExts: jest.fn(() => [[], []] as any),
load: jest.fn(),
},
// @ts-ignore
i18n: {
initialize: jest.fn(),
},
// @ts-ignore
monacoService: {
initWorkspace: jest.fn(),
},
// @ts-ignore
layout: {
container: document.body,
},
}
);
const mockFn = jest.fn();
instance.onBeforeInit(mockFn);
instance.onBeforeLoad(mockFn);
instance.render(<div>123</div>);

expect(mockFn).toBeCalledTimes(2);
});
});
9 changes: 7 additions & 2 deletions src/services/instanceService.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { IExtension } from 'mo/model';
import { STORE_KEY } from 'mo/i18n/localeService';

interface IInstanceServiceProps {
getConfig: () => IConfigProps;
render: (dom: ReactElement) => ReactElement;
onBeforeInit: (callback: () => void) => void;
onBeforeLoad: (callback: () => void) => void;
Expand All @@ -26,7 +27,7 @@ export default class InstanceService
{
private _services: IServiceCollection;
private _config = {
extensions: defaultExtensions,
extensions: defaultExtensions.concat(),
defaultLocale: 'en',
};

Expand All @@ -43,7 +44,7 @@ export default class InstanceService
}
}

public initialLocaleService = (languagesExts: IExtension[]) => {
private initialLocaleService = (languagesExts: IExtension[]) => {
const locales = languagesExts.reduce((pre, cur) => {
const languages = cur.contributes?.languages || [];
return pre.concat(languages);
Expand All @@ -55,6 +56,10 @@ export default class InstanceService
);
};

public getConfig: () => IConfigProps = () => {
return Object.assign({}, this._config);
};

public render = (workbench: ReactElement) => {
this.emit(InstanceHookKind.beforeInit);

Expand Down