Skip to content

Commit 6f21a00

Browse files
committed
Prepares test cases for HeaderLogo
1 parent f089128 commit 6f21a00

File tree

4 files changed

+76
-26
lines changed

4 files changed

+76
-26
lines changed

src/components/base/Header.tsx

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { logout } from '../../lib/api/auth';
1111
import storage from '../../lib/storage';
1212
import { UserLogo } from '../../modules/header';
1313
import { Link } from 'react-router-dom';
14+
import HeaderLogo from './HeaderLogo';
1415

1516
const HeaderBlock = styled.div<{
1617
floating: boolean;
@@ -59,17 +60,6 @@ interface HeaderProps {
5960

6061
const { useCallback } = React;
6162

62-
const CustomLogo = styled(Link)``;
63-
64-
const createFallbackTitle = (username: string | null) => {
65-
if (!username) return null;
66-
const lastChar = username.slice(-1).toLowerCase();
67-
if (lastChar === 's') {
68-
return `${username}' velog`;
69-
}
70-
return `${username}'s velog`;
71-
};
72-
7363
const Header: React.SFC<HeaderProps> = ({
7464
floating,
7565
floatingMargin,
@@ -97,17 +87,11 @@ const Header: React.SFC<HeaderProps> = ({
9787
>
9888
<div className="wrapper">
9989
<div className="brand">
100-
{custom ? (
101-
userLogo ? (
102-
<CustomLogo to="/">
103-
{userLogo.title
104-
? userLogo.title
105-
: createFallbackTitle(velogUsername)}
106-
</CustomLogo>
107-
) : null
108-
) : (
109-
<Logo />
110-
)}
90+
<HeaderLogo
91+
custom={custom}
92+
userLogo={userLogo}
93+
velogUsername={velogUsername}
94+
/>
11195
</div>
11296
<div className="right">
11397
{user ? (

src/components/base/HeaderLogo.tsx

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,50 @@
11
import * as React from 'react';
22
import styled from 'styled-components';
3+
import { Link } from 'react-router-dom';
34
import { Logo } from '../../static/svg';
45
import { UserLogo } from '../../modules/header';
56

6-
const HeaderLogoBlock = styled.div``;
7+
const createFallbackTitle = (username: string | null) => {
8+
if (!username) return null;
9+
const lastChar = username.slice(-1).toLowerCase();
10+
if (lastChar === 's') {
11+
return `${username}' velog`;
12+
}
13+
return `${username}'s velog`;
14+
};
15+
16+
const HeaderLogoBlock = styled(Link)`
17+
display: flex;
18+
align-items: center;
19+
justify-content: center;
20+
`;
721

822
export interface HeaderLogoProps {
923
custom: boolean;
1024
userLogo: UserLogo | null;
1125
velogUsername: string | null;
1226
}
1327

14-
const HeaderLogo: React.FC<HeaderLogoProps> = props => {
15-
return <HeaderLogoBlock />;
28+
const HeaderLogo: React.FC<HeaderLogoProps> = ({
29+
custom,
30+
userLogo,
31+
velogUsername,
32+
}) => {
33+
if (!custom) {
34+
return (
35+
<HeaderLogoBlock to="/">
36+
<Logo data-testid="velog-logo" />
37+
</HeaderLogoBlock>
38+
);
39+
}
40+
41+
if (!velogUsername) return null;
42+
const velogPath = `/@${velogUsername}`;
43+
return (
44+
<HeaderLogoBlock to={velogPath}>
45+
{createFallbackTitle(velogUsername)}
46+
</HeaderLogoBlock>
47+
);
1648
};
1749

1850
export default HeaderLogo;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import * as React from 'react';
2+
import { render } from 'react-testing-library';
3+
import HeaderLogo, { HeaderLogoProps } from '../HeaderLogo';
4+
import { MemoryRouter } from 'react-router-dom';
5+
6+
describe('HeaderLogo', () => {
7+
const setup = (props: Partial<HeaderLogoProps> = {}) => {
8+
const initialProps: HeaderLogoProps = {
9+
custom: false,
10+
userLogo: null,
11+
velogUsername: null,
12+
};
13+
const utils = render(
14+
<MemoryRouter>
15+
<HeaderLogo {...initialProps} {...props} />
16+
</MemoryRouter>,
17+
);
18+
return {
19+
...utils,
20+
};
21+
};
22+
23+
it('shows logo svg when custom is false', () => {
24+
const { getByTestId } = setup();
25+
getByTestId('velog-logo');
26+
});
27+
28+
it('shows null when custom is true and data is not loaded', () => {});
29+
it('shows custom velog title when custom is true data is loaded', () => {});
30+
it('shows fallback velog title when velog title is null', () => {});
31+
});

src/components/velog/__tests__/VelogPageTemplate.test.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as React from 'react';
2+
import { MemoryRouter } from 'react-router-dom';
23
import { render } from 'react-testing-library';
34
import VelogPageTemplate, {
45
VelogPageTemplateProps,
@@ -9,7 +10,9 @@ describe('VelogPageTemplate', () => {
910
const setup = (props: Partial<VelogPageTemplateProps> = {}) => {
1011
const initialProps: VelogPageTemplateProps = {};
1112
const utils = renderWithRedux(
12-
<VelogPageTemplate {...initialProps} {...props} />,
13+
<MemoryRouter>
14+
<VelogPageTemplate {...initialProps} {...props} />
15+
</MemoryRouter>,
1316
);
1417
return {
1518
...utils,

0 commit comments

Comments
 (0)