Skip to content

Commit 3093a21

Browse files
authored
feat: create instanceService (#733)
* feat: create instanceService * feat: improve the interface * refactor: improve the inject initial values of localeService * refactor: improve the localeService * test: update test cases * refactor: make provider compatible with create function * fix: provider should create in useLayoutEffect rather that useEffect * test: add test cases for instanceService * test: increase code coverage * chore: add module.hot.accept * refactor: fix website generate api structure * build: fix web script failed * docs: add builtin doc
1 parent 753ba64 commit 3093a21

39 files changed

+631
-444
lines changed

README-koKR.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ yarn add @dtinsight/molecule
5050
```javascript
5151
import React from 'react';
5252
import ReactDOM from 'react-dom';
53-
import { MoleculeProvider, Workbench } from '@dtinsight/molecule';
53+
import { create, Workbench } from '@dtinsight/molecule';
5454
import '@dtinsight/molecule/esm/style/mo.css';
5555

56-
const App = () => (
57-
<MoleculeProvider extensions={[]}>
58-
<Workbench />
59-
</MoleculeProvider>
60-
);
56+
const moInstance = create({
57+
extensions: [],
58+
});
59+
60+
const App = () => moInstance.render(<Workbench />);
6161

6262
ReactDOM.render(<App />, document.getElementById('root'));
6363
```

README-zhCN.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ yarn add @dtinsight/molecule
4949
```javascript
5050
import React from 'react';
5151
import ReactDOM from 'react-dom';
52-
import { MoleculeProvider, Workbench } from '@dtinsight/molecule';
52+
import { create, Workbench } from '@dtinsight/molecule';
5353
import '@dtinsight/molecule/esm/style/mo.css';
5454

55-
const App = () => (
56-
<MoleculeProvider extension={[]}>
57-
<Workbench />
58-
</MoleculeProvider>
59-
);
55+
const moInstance = create({
56+
extensions: [],
57+
});
58+
59+
const App = () => moInstance.render(<Workbench />);
6060

6161
ReactDOM.render(<App />, document.getElementById('root'));
6262
```

README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ yarn add @dtinsight/molecule
4949
```javascript
5050
import React from 'react';
5151
import ReactDOM from 'react-dom';
52-
import { MoleculeProvider, Workbench } from '@dtinsight/molecule';
52+
import { create, Workbench } from '@dtinsight/molecule';
5353
import '@dtinsight/molecule/esm/style/mo.css';
5454

55-
const App = () => (
56-
<MoleculeProvider extensions={[]}>
57-
<Workbench />
58-
</MoleculeProvider>
59-
);
55+
const moInstance = create({
56+
extensions: [],
57+
});
58+
59+
const App = () => moInstance.render(<Workbench />);
6060

6161
ReactDOM.render(<App />, document.getElementById('root'));
6262
```

build/web/app.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
import React, { StrictMode } from 'react';
22
import ReactDOM from 'react-dom';
3-
import { Workbench, MoleculeProvider } from '../../esm';
3+
import { create, Workbench } from '../../esm';
44
import '../../esm/style/mo.css';
55

6-
const App = () => (
6+
const moInstance = create({
7+
extensions: [],
8+
});
9+
10+
export const App = () => moInstance.render(<Workbench />);
11+
12+
ReactDOM.render(
713
<StrictMode>
8-
<MoleculeProvider>
9-
<Workbench />
10-
</MoleculeProvider>
11-
</StrictMode>
14+
<App />
15+
</StrictMode>,
16+
document.getElementById('root')
1217
);
13-
14-
ReactDOM.render(<App />, document.getElementById('root'));

src/controller/settings.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export class SettingsController
6969
id: SETTING_ID!,
7070
value: next,
7171
render(value) {
72+
/* istanbul ignore next */
7273
return <LocaleNotification key={next.id} locale={next.name} />;
7374
},
7475
};

src/extensions/__tests__/folderTree.test.tsx

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react';
22
import '@testing-library/jest-dom';
3-
import molecule, { MoleculeProvider, Workbench } from 'mo';
3+
import molecule, { create, Workbench } from 'mo';
44
import { cleanup, fireEvent, render } from '@testing-library/react';
55
import type { ITreeNodeItemProps } from 'mo/components';
66
import type { IEditorTab } from 'mo/model/workbench/editor';
@@ -35,11 +35,10 @@ describe('folderTree extension', () => {
3535
afterEach(cleanup);
3636

3737
test('Execute the listener function of onUpdateFileName', () => {
38-
const { getByRole } = render(
39-
<MoleculeProvider>
40-
<Workbench />
41-
</MoleculeProvider>
42-
);
38+
const container = create({
39+
extensions: [],
40+
}).render(<Workbench />);
41+
const { getByRole } = render(container);
4342

4443
molecule.folderTree.setState({ folderTree: { data: mockTreeData } });
4544
expect(molecule.folderTree.getState().folderTree?.data).toEqual(

src/i18n/__tests__/localeService.test.ts

+68-66
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
import 'reflect-metadata';
22
import { container } from 'tsyringe';
3+
import { expectLoggerErrorToBeCalled } from '@test/utils';
4+
import { ILocale } from '../localization';
35
import { LocaleService } from '..';
4-
import { BuiltInLocales, BuiltInDefault, ILocale } from '../localization';
56

67
describe('The Locale Service', () => {
7-
const TestLocale = {
8+
const TestLocale: ILocale = {
89
id: 'test',
910
source: new Map(),
1011
name: 'test',
1112
};
1213

14+
// LocaleService is support to add an object source but ILocale is banned
15+
const TestEnLocale: ILocale = {
16+
id: 'en',
17+
name: 'English',
18+
source: {
19+
// @ts-ignore
20+
'molecule.welcome': 'Welcome to Molecule',
21+
'test.id': 'hello ${i}',
22+
},
23+
};
24+
1325
afterEach(() => {
1426
localStorage.clear();
1527
});
@@ -19,82 +31,72 @@ describe('The Locale Service', () => {
1931
expect(localeService).not.toBeUndefined();
2032
});
2133

22-
test('Reset the LocaleService', () => {
34+
test('Initialize the locales with testLocale', () => {
2335
const localeService = new LocaleService();
24-
expect(localeService.getCurrentLocale()!.id).toBe(BuiltInDefault.id);
25-
localeService.reset();
26-
expect(localeService.getCurrentLocale()!.id).toBe(BuiltInDefault.id);
27-
});
36+
localeService.initialize([TestLocale], TestLocale.id);
2837

29-
test('Get default Locale', () => {
30-
const localeService = new LocaleService();
31-
const defaultLocale = localeService.getDefaultLocale();
32-
expect(defaultLocale).toEqual(BuiltInDefault);
33-
});
38+
expect(localeService.getCurrentLocale()?.id).toEqual(TestLocale.id);
39+
expect(localeService.getLocales().length).toBe(1);
3440

35-
test('Get default Locales', () => {
36-
const localeService = new LocaleService();
37-
const defaultLocale = localeService.getDefaultLocales();
38-
expect(defaultLocale).toEqual(BuiltInLocales);
41+
localeService.reset();
42+
expectLoggerErrorToBeCalled(() => {
43+
// @ts-ignore
44+
localeService.initialize([TestEnLocale, TestLocale]);
45+
});
3946
});
4047

41-
test('The size of Built-in Locales should be 3', () => {
48+
test('Reset the LocaleService', () => {
4249
const localeService = new LocaleService();
43-
const locales = localeService.getLocales();
44-
expect(locales.length).toBe(3);
45-
});
50+
expect(localeService.getCurrentLocale()).toBeUndefined();
4651

47-
test('Initialize the locales', () => {
48-
const localeService = new LocaleService();
49-
localeService.initialize([TestLocale]);
50-
expect(localeService.getCurrentLocale()!.id).toEqual(
51-
localeService.getDefaultLocale().id
52-
);
53-
expect(localeService.getLocales().length).toBe(4);
54-
localeService.initialize([], 'test');
55-
expect(localeService.getCurrentLocale()!.id).toEqual(BuiltInDefault.id);
56-
// Clear the cached locale value
57-
localStorage.clear();
58-
localeService.initialize([], 'test');
59-
expect(localeService.getCurrentLocale()!.id).toEqual('test');
60-
localeService.initialize([]);
61-
// Get from the localStorage cache
62-
expect(localeService.getCurrentLocale()!.id).toEqual('test');
52+
localeService.initialize([TestLocale], TestLocale.id);
53+
expect(localeService.getCurrentLocale()).toEqual(TestLocale);
54+
55+
localeService.reset();
56+
expect(localeService.getCurrentLocale()).toBeUndefined();
6357
});
6458

6559
test('Get/Set current locale', () => {
6660
const localeService = new LocaleService();
67-
(localeService as any)._current = undefined;
68-
expect(localeService.getCurrentLocale()).toBe(BuiltInDefault);
69-
localeService.addLocales([TestLocale]);
70-
localeService.setCurrentLocale(TestLocale.id);
71-
expect(localeService.getCurrentLocale()!.id).toEqual(TestLocale.id);
61+
expect(localeService.getCurrentLocale()).toBeUndefined();
62+
63+
localeService.initialize([TestLocale, TestEnLocale], TestLocale.id);
64+
65+
expect(localeService.getCurrentLocale()?.id).toEqual(TestLocale.id);
7266

67+
localeService.setCurrentLocale(TestEnLocale.id);
68+
expect(localeService.getCurrentLocale()?.id).toEqual(TestEnLocale.id);
69+
// set an unknow locale will fail
7370
expect(localeService.setCurrentLocale('unknown')).toEqual(false);
7471
});
7572

7673
test('Add locales', () => {
7774
const localeService = new LocaleService();
78-
expect(localeService.getLocales().length).toBe(3);
75+
expect(localeService.getLocales().length).toBe(0);
76+
7977
localeService.addLocales([TestLocale]);
80-
expect(localeService.getLocales().length).toBe(4);
78+
expect(localeService.getLocales().length).toBe(1);
79+
8180
localeService.addLocales([]);
82-
expect(localeService.getLocales().length).toBe(4);
81+
expect(localeService.getLocales().length).toBe(1);
82+
8383
// Add an existed locale
8484
localeService.addLocales([TestLocale]);
85-
expect(localeService.getLocales().length).toBe(4);
85+
expect(localeService.getLocales().length).toBe(1);
8686
});
8787

8888
test('Add an locale inherit the en', () => {
8989
const localeService = new LocaleService();
90+
localeService.initialize([TestEnLocale], TestEnLocale.id);
91+
9092
expect(TestLocale.source.size).toBe(0);
91-
(TestLocale as ILocale).inherit = 'en';
93+
TestLocale.inherit = 'en';
9294
localeService.addLocales([TestLocale]);
9395
expect(localeService.getLocale(TestLocale.id)?.source.size).not.toBe(0);
9496

9597
// Inherit an not exist locale
9698
localeService.removeLocale(TestLocale.id);
97-
(TestLocale as ILocale).inherit = 'unknown';
99+
TestLocale.inherit = 'unknown';
98100
localeService.addLocales([TestLocale]);
99101
expect(localeService.getLocale(TestLocale.id)?.source.size).toBe(0);
100102
});
@@ -110,38 +112,47 @@ describe('The Locale Service', () => {
110112

111113
test('Remove a locale', () => {
112114
const localeService = new LocaleService();
113-
localeService.addLocales([TestLocale]);
115+
localeService.initialize([TestLocale, TestEnLocale], TestLocale.id);
114116
expect(localeService.getLocale(TestLocale.id)?.id).toEqual(
115117
TestLocale.id
116118
);
117-
localeService.removeLocale(TestLocale.id);
119+
120+
const removedLocale = localeService.removeLocale(TestLocale.id);
118121
expect(localeService.getLocale(TestLocale.id)).toBeUndefined();
122+
expect(removedLocale).toEqual(TestLocale);
123+
119124
localeService.addLocales([TestLocale]);
120125
localeService.setCurrentLocale(TestLocale.id);
121126

122127
//Remove the current locale
123-
expect(localeService.getCurrentLocale()!.id).toEqual(TestLocale.id);
128+
expect(localeService.getCurrentLocale()?.id).toEqual(TestLocale.id);
124129
localeService.removeLocale(TestLocale.id);
125-
expect(localeService.getCurrentLocale()!.id).toEqual(
126-
localeService.getDefaultLocale().id
127-
);
130+
expect(localeService.getCurrentLocale()!.id).toEqual(TestEnLocale.id);
128131

129132
// Remove an undefined
130133
expect(localeService.removeLocale(TestLocale.id));
134+
135+
expect(localeService.getLocales().length).toBe(1);
136+
// The last one couldn't be removed
137+
expect(localeService.removeLocale(TestEnLocale.id)).toBeFalsy();
131138
});
132139

133140
test('Listen to the current locale change event', () => {
134-
const target = 'zh-CN';
135141
const localeService = new LocaleService();
136142
const fn = jest.fn();
137143
localeService.onChange(fn);
138-
localeService.setCurrentLocale(target);
144+
145+
localeService.initialize([TestLocale, TestEnLocale], TestLocale.id);
146+
localeService.setCurrentLocale(TestEnLocale.id);
147+
139148
expect(fn).toBeCalledTimes(1);
140-
expect(localeService.getCurrentLocale()!.id).toEqual(target);
149+
expect(localeService.getCurrentLocale()!.id).toEqual(TestEnLocale.id);
141150
});
142151

143152
test('Localize the source key', () => {
144153
const localeService = new LocaleService();
154+
155+
localeService.initialize([TestLocale, TestEnLocale], TestEnLocale.id);
145156
let res = localeService.localize('test');
146157
expect(res).toEqual('');
147158

@@ -154,19 +165,10 @@ describe('The Locale Service', () => {
154165
res = localeService.localize('molecule.welcome', 'default');
155166
expect(res).toEqual('Welcome to Molecule');
156167

157-
const map = new Map();
158-
map.set('test.id', 'hello ${i}');
159-
const mockData = {
160-
id: 'mock',
161-
name: 'mock',
162-
source: map,
163-
};
164-
localeService.addLocales([mockData]);
165-
localeService.setCurrentLocale(mockData.id);
166168
res = localeService.localize('test.id', '', 'world');
167169
expect(res).toEqual('hello world');
168170

169-
(localeService as any)._current = null;
171+
localeService.setCurrentLocale(TestLocale.id);
170172
res = localeService.localize('molecule.welcome', 'default');
171173
expect(res).toEqual('default');
172174
});

0 commit comments

Comments
 (0)