Skip to content

Commit 2370797

Browse files
committed
Write test for PostViewer
1 parent 895a47d commit 2370797

File tree

2 files changed

+127
-2
lines changed

2 files changed

+127
-2
lines changed

src/containers/post/PostViewer.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ const PostViewer: React.FC<PostViewerProps> = ({ username, urlSlug }) => {
1818
}}
1919
>
2020
{({ loading, error, data }: QueryResult<{ post: SinglePost }>) => {
21-
console.log(error);
21+
if (error) {
22+
console.log(error);
23+
return null; // SHOW ERROR
24+
}
2225
if (loading) return null; // TODO: show placeholder
2326
if (!data || !data.post) return null;
24-
if (error) return null; // SHOW ERROR
2527
const { post } = data;
2628
return (
2729
<>
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import * as React from 'react';
2+
import { render, waitForElement } from 'react-testing-library';
3+
import PostViewer, { PostViewerProps } from '../PostViewer';
4+
import { MemoryRouter } from 'react-router-dom';
5+
import { MockedProvider } from 'react-apollo/test-utils';
6+
import { READ_POST } from '../../../lib/graphql/post';
7+
8+
describe('PostViewer', () => {
9+
const setup = (props: Partial<PostViewerProps> = {}, overrideMocks?: any) => {
10+
const initialProps: PostViewerProps = {
11+
username: 'velopert',
12+
urlSlug: 'sample',
13+
};
14+
const mocks = [
15+
{
16+
request: {
17+
query: READ_POST,
18+
variables: {
19+
username: 'velopert',
20+
url_slug: 'sample',
21+
},
22+
},
23+
result: {
24+
data: {
25+
post: {
26+
id: '6533da20-b351-11e8-9696-f1fffe8a36f1',
27+
title: '제목입니다',
28+
released_at: '2018-09-08T10:24:40.642Z',
29+
updated_at: '2019-01-28T13:51:10.999Z',
30+
tags: ['react', 'redux'],
31+
body: '내용입니다',
32+
is_markdown: true,
33+
is_private: false,
34+
is_temp: false,
35+
thumbnail:
36+
'https://images.velog.io/post-images/velopert/654650b0-b351-11e8-9696-f1fffe8a36f1/redux.png',
37+
user: {
38+
id: 'c76ccc50-b34d-11e8-b01f-598f1220d1c8',
39+
username: 'velopert',
40+
profile: {
41+
display_name: 'Minjun Kim',
42+
thumbnail:
43+
'https://images.velog.io/profiles/velopert/thumbnails/1536400727.98.png',
44+
short_bio:
45+
'velopert@Laftel Inc. 재미있는것만 골라서 하는 개발자입니다.',
46+
},
47+
velog_config: {
48+
id: '5ad9f60b-4b95-42e9-a32c-eae222c79bf1',
49+
title: 'VELOPERT.LOG',
50+
},
51+
},
52+
},
53+
},
54+
},
55+
},
56+
];
57+
const utils = render(
58+
<MockedProvider mocks={overrideMocks || mocks} addTypename={false}>
59+
<MemoryRouter>
60+
<PostViewer {...initialProps} {...props} />
61+
</MemoryRouter>
62+
</MockedProvider>,
63+
);
64+
return {
65+
...utils,
66+
};
67+
};
68+
it('renders post correctly', async () => {
69+
const { getByText, getByAltText } = setup();
70+
await waitForElement(() => getByText('제목입니다'));
71+
waitForElement(() => getByAltText('post-thumbnail'));
72+
});
73+
it('hides thumbnail if exists in body', async () => {
74+
const mocks = [
75+
{
76+
request: {
77+
query: READ_POST,
78+
variables: {
79+
username: 'velopert',
80+
url_slug: 'sample',
81+
},
82+
},
83+
result: {
84+
data: {
85+
post: {
86+
id: '6533da20-b351-11e8-9696-f1fffe8a36f1',
87+
title: '제목입니다',
88+
released_at: '2018-09-08T10:24:40.642Z',
89+
updated_at: '2019-01-28T13:51:10.999Z',
90+
tags: ['react', 'redux'],
91+
body:
92+
'내용입니다 ![](https://images.velog.io/post-images/velopert/654650b0-b351-11e8-9696-f1fffe8a36f1/redux.png)',
93+
is_markdown: true,
94+
is_private: false,
95+
is_temp: false,
96+
thumbnail:
97+
'https://images.velog.io/post-images/velopert/654650b0-b351-11e8-9696-f1fffe8a36f1/redux.png',
98+
user: {
99+
id: 'c76ccc50-b34d-11e8-b01f-598f1220d1c8',
100+
username: 'velopert',
101+
profile: {
102+
display_name: 'Minjun Kim',
103+
thumbnail:
104+
'https://images.velog.io/profiles/velopert/thumbnails/1536400727.98.png',
105+
short_bio:
106+
'velopert@Laftel Inc. 재미있는것만 골라서 하는 개발자입니다.',
107+
},
108+
velog_config: {
109+
id: '5ad9f60b-4b95-42e9-a32c-eae222c79bf1',
110+
title: 'VELOPERT.LOG',
111+
},
112+
},
113+
},
114+
},
115+
},
116+
},
117+
];
118+
const { getByText, queryByAltText } = setup(undefined, mocks);
119+
await waitForElement(() => getByText('제목입니다'));
120+
const thumbnail = queryByAltText('post-thumbnail');
121+
expect(thumbnail).toBeFalsy();
122+
});
123+
});

0 commit comments

Comments
 (0)