Skip to content

Commit a03330f

Browse files
committed
test: improve test coverage
1 parent 1827f59 commit a03330f

File tree

11 files changed

+305
-59
lines changed

11 files changed

+305
-59
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
"plugins": ["@typescript-eslint", "react", "react-hooks", "jsx-a11y", "import", "jest"],
1414
"globals": {
1515
"importScripts": true,
16-
"workbox": true
16+
"workbox": true,
17+
"__TEST__": true
1718
},
1819
"settings": {
1920
"import/resolver": {

client/components/Button.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,4 @@ function Button({ type = 'primary', children, className = '', onClick }: Props)
3434
);
3535
}
3636

37-
Button.defaultProps = {
38-
type: 'primary',
39-
};
40-
4137
export default Button;

client/localStorage.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export enum LocalStorageKey {
2020
* @param key 键值
2121
* @param defaultValue 默认值
2222
*/
23-
function getTextValue(key: string, defaultValue: string = '') {
23+
function getTextValue(key: string, defaultValue: string) {
2424
const value = window.localStorage.getItem(key);
2525
return value || defaultValue;
2626
}
@@ -39,8 +39,7 @@ function getSwitchValue(key: string, defaultValue: boolean = true) {
3939
* 获取LocalStorage值
4040
*/
4141
export default function getData() {
42-
const defaultTheme = 'cool';
43-
const theme = getTextValue(LocalStorageKey.Theme, defaultTheme);
42+
const theme = getTextValue(LocalStorageKey.Theme, config.defaultTheme);
4443
let themeConfig = {
4544
primaryColor: '',
4645
primaryTextColor: '',
@@ -55,15 +54,15 @@ export default function getData() {
5554
themeConfig = {
5655
primaryColor: getTextValue(
5756
LocalStorageKey.PrimaryColor,
58-
config.theme[defaultTheme].primaryColor,
57+
config.theme[config.defaultTheme].primaryColor,
5958
),
6059
primaryTextColor: getTextValue(
6160
LocalStorageKey.PrimaryTextColor,
62-
config.theme[defaultTheme].primaryTextColor,
61+
config.theme[config.defaultTheme].primaryTextColor,
6362
),
6463
backgroundImage: getTextValue(
6564
LocalStorageKey.BackgroundImage,
66-
config.theme[defaultTheme].backgroundImage,
65+
config.theme[config.defaultTheme].backgroundImage,
6766
),
6867
aero: getSwitchValue(
6968
LocalStorageKey.Aero,

client/state/reducer.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ function transformTemporary(temporary: Linkman): Linkman {
231231
}
232232

233233
const localStorage = getData();
234-
const initialState: State = {
234+
export const initialState: State = {
235235
user: null,
236236
linkmans: {},
237237
focus: '',
@@ -305,16 +305,10 @@ function reducer(state: State = initialState, action: Action): State {
305305
// @ts-ignore
306306
...friends.map(transformFriend),
307307
];
308-
linkmans.forEach((linkman) => {
309-
let existMessages = {};
310-
if (state.linkmans[linkman._id]) {
311-
existMessages = state.linkmans[linkman._id].messages;
312-
}
313-
linkman.messages = existMessages;
314-
});
315308

316309
// 如果没登录过, 则将聚焦联系人设置为第一个联系人
317310
let { focus } = state;
311+
/* istanbul ignore next */
318312
if (!state.user && linkmans.length > 0) {
319313
focus = linkmans[0]._id;
320314
}
@@ -368,7 +362,10 @@ function reducer(state: State = initialState, action: Action): State {
368362
case ActionTypes.SetFocus: {
369363
const focus = action.payload as string;
370364
if (!state.linkmans[focus]) {
371-
console.warn(`ActionTypes.SetFocus Error: 联系人 ${focus} 不存在`);
365+
/* istanbul ignore next */
366+
if (!__TEST__) {
367+
console.warn(`ActionTypes.SetFocus Error: 联系人 ${focus} 不存在`);
368+
}
372369
return state;
373370
}
374371

@@ -429,7 +426,7 @@ function reducer(state: State = initialState, action: Action): State {
429426
...state,
430427
linkmans: {
431428
...state.linkmans,
432-
[linkman._id]: transformedLinkman,
429+
[transformedLinkman._id]: transformedLinkman,
433430
},
434431
focus,
435432
};
@@ -510,7 +507,10 @@ function reducer(state: State = initialState, action: Action): State {
510507
case ActionTypes.DeleteMessage: {
511508
const { linkmanId, messageId } = action.payload as DeleteMessagePayload;
512509
if (!state.linkmans[linkmanId]) {
513-
console.warn(`ActionTypes.DeleteMessage Error: 联系人 ${linkmanId} 不存在`);
510+
/* istanbul ignore next */
511+
if (!__TEST__) {
512+
console.warn(`ActionTypes.DeleteMessage Error: 联系人 ${linkmanId} 不存在`);
513+
}
514514
return state;
515515
}
516516

config/client.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
11
const MB = 1024 * 1024;
22

3-
export default {
3+
type Theme = {
4+
primaryColor: string;
5+
primaryTextColor: string;
6+
backgroundImage: string;
7+
aero: boolean;
8+
}
9+
10+
type Config = {
11+
server: string;
12+
maxImageSize: number;
13+
maxBackgroundImageSize: number;
14+
maxAvatarSize: number;
15+
theme: {
16+
default: Theme,
17+
cool: Theme,
18+
}
19+
defaultTheme: 'default' | 'cool';
20+
sound: string;
21+
tagColorMode: string;
22+
frontendMonitorAppId: string;
23+
}
24+
25+
const config: Config = {
426
server: process.env.NODE_ENV === 'development' ? '//localhost:9200' : '',
527

628
maxImageSize: MB * 3,
@@ -22,6 +44,7 @@ export default {
2244
aero: false,
2345
},
2446
},
47+
defaultTheme: 'cool',
2548
sound: 'default',
2649
tagColorMode: 'fixedColor',
2750

@@ -31,3 +54,5 @@ export default {
3154
*/
3255
frontendMonitorAppId: process.env.frontendMonitorAppId || '',
3356
};
57+
58+
export default config;

jest.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ module.exports = {
99
'ts-jest': {
1010
isolatedModules: true,
1111
},
12+
__TEST__: true,
1213
},
1314
setupFilesAfterEnv: ['./jest.setup.js'],
15+
collectCoverageFrom: [
16+
'**/*.{ts,tsx}',
17+
'!**/node_modules/**',
18+
'!**/config/**',
19+
],
1420
};

test/client/localStorage.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import getData, { LocalStorageKey } from '../../client/localStorage';
2+
import config from '../../config/client';
3+
4+
describe('client/localStorage.ts', () => {
5+
it('should return localStorage value, or default value if not exists', () => {
6+
expect(getData().sound).toBe(config.sound);
7+
localStorage.setItem(LocalStorageKey.Sound, 'huaji');
8+
expect(getData().sound).toBe('huaji');
9+
});
10+
11+
it('should return default theme config when them not exists', () => {
12+
localStorage.setItem(LocalStorageKey.Theme, 'xxx');
13+
expect(getData().primaryColor).toBe(config.theme.cool.primaryColor);
14+
});
15+
16+
it('should return boolean type value when value is true / false', () => {
17+
localStorage.setItem(LocalStorageKey.SoundSwitch, 'true');
18+
expect(getData().soundSwitch).toBe(true);
19+
localStorage.setItem(LocalStorageKey.SoundSwitch, 'false');
20+
expect(getData().soundSwitch).toBe(false);
21+
});
22+
});

0 commit comments

Comments
 (0)