Skip to content

Commit b590902

Browse files
committed
Test ConfigLoader
1 parent 49bfb42 commit b590902

File tree

6 files changed

+70
-19
lines changed

6 files changed

+70
-19
lines changed

src/containers/velog/ConfigLoader.tsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,31 @@ import React, { useEffect } from 'react';
22
import { Query, QueryResult } from 'react-apollo';
33
import { connect } from 'react-redux';
44
import { GET_VELOG_CONFIG, VelogConfig } from '../../lib/graphql/user';
5+
import { setUserLogo } from '../../modules/header';
56

67
export interface ConfigLoaderProps {
78
username: string;
89
}
910

1011
interface ConfigEffectProps {
1112
velogConfig: VelogConfig;
13+
setUserLogo: typeof setUserLogo;
1214
}
13-
const ConfigEffect: React.FC<ConfigEffectProps> = ({ velogConfig }) => {
15+
16+
const ConfigEffect: React.FC<ConfigEffectProps> = ({
17+
velogConfig,
18+
setUserLogo,
19+
}) => {
1420
useEffect(() => {
15-
console.log(velogConfig);
16-
}, [velogConfig]);
21+
setUserLogo(velogConfig);
22+
}, [setUserLogo, velogConfig]);
1723
return null;
1824
};
1925

20-
const ConfigEffectContainer = connect(() => ({}));
26+
const ConfigEffectContainer = connect(
27+
() => ({}),
28+
{ setUserLogo },
29+
)(ConfigEffect);
2130

2231
const ConfigLoader: React.FC<ConfigLoaderProps> = ({ username }) => {
2332
return (
@@ -32,7 +41,7 @@ const ConfigLoader: React.FC<ConfigLoaderProps> = ({ username }) => {
3241
}
3342
if (error || loading) return null;
3443
if (!data) return null;
35-
return <ConfigEffect velogConfig={data.velog_config} />;
44+
return <ConfigEffectContainer velogConfig={data.velog_config} />;
3645
}}
3746
</Query>
3847
);

src/containers/velog/__tests__/ConfigLoader.test.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import ConfigLoader, { ConfigLoaderProps } from '../ConfigLoader';
44
import { MockedProvider } from 'react-apollo/test-utils';
55
import { GET_VELOG_CONFIG } from '../../../lib/graphql/user';
66
import renderWithRedux from '../../../lib/renderWithRedux';
7+
import waitUntil from '../../../lib/waitUntil';
78

89
describe('ConfigLoader', () => {
910
const setup = (props: Partial<ConfigLoaderProps> = {}) => {
@@ -35,9 +36,13 @@ describe('ConfigLoader', () => {
3536
it('renders properly', () => {
3637
setup();
3738
});
38-
it('matches snapshot', async () => {
39-
// const { container } = setup();
40-
// await waitForDomChange({ container });
41-
// expect(container).toMatchSnapshot();
39+
it('loads GET_VELOG_CONFIG and dispatches setUserLogo', async () => {
40+
const { store } = setup();
41+
const userLogo = store.getState().header.userLogo;
42+
await waitUntil(() => store.getState().header.userLogo !== userLogo);
43+
expect(store.getState().header.userLogo).toHaveProperty(
44+
'title',
45+
'VELOPERT.LOG',
46+
);
4247
});
4348
});

src/containers/velog/__tests__/__snapshots__/ConfigLoader.test.tsx.snap

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/lib/waitUntil.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
type WaitUntilOptions = {
2+
interval: number;
3+
timeout: number;
4+
};
5+
6+
const defaultOptions: WaitUntilOptions = {
7+
interval: 50,
8+
timeout: 6000,
9+
};
10+
11+
type ConditionCallback = () => boolean;
12+
13+
const waitUntil = (
14+
conditionCallback: ConditionCallback,
15+
options: Partial<WaitUntilOptions> = {},
16+
) => {
17+
const { interval, timeout } = { ...defaultOptions, ...options };
18+
const promise = new Promise((resolve, reject) => {
19+
let timeoutId: null | number = null;
20+
21+
setTimeout(() => {
22+
const e = new Error('waitUntil time out');
23+
e.name = 'WaitUntilTimeoutError';
24+
reject(e);
25+
if (timeoutId) {
26+
clearTimeout(timeoutId);
27+
}
28+
}, timeout);
29+
30+
const check = () => {
31+
if (conditionCallback()) {
32+
return resolve();
33+
}
34+
timeoutId = setTimeout(check, interval);
35+
};
36+
37+
check();
38+
});
39+
40+
return promise;
41+
};
42+
43+
export default waitUntil;

src/modules/header.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type UserLogo = {
99
logo_image: string | null;
1010
};
1111

12-
interface HeaderState {
12+
export interface HeaderState {
1313
custom: boolean;
1414
userLogo: null | UserLogo;
1515
}

src/modules/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import { combineReducers } from 'redux';
22
import core, { CoreState } from './core';
33
import write, { WriteState } from './write';
4+
import header, { HeaderState } from './header';
45

56
export type RootState = {
67
core: CoreState;
78
write: WriteState;
9+
header: HeaderState;
810
};
911

1012
const rootReducer = combineReducers({
1113
core,
1214
write,
15+
header,
1316
});
1417

1518
export default rootReducer;

0 commit comments

Comments
 (0)