Skip to content

Commit b9c0cfc

Browse files
committed
Increased typescript strictness
1 parent 51ef6ec commit b9c0cfc

32 files changed

+60
-293
lines changed

src/components/comment-box.spec.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as React from 'react';
2-
// import renderer from 'react-test-renderer';
32
import { shallow } from 'enzyme';
43

54
import { CommentBox } from './comment-box';

src/components/comment.spec.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import * as React from 'react';
2-
import MockDate from 'mockdate';
3-
// import renderer from 'react-test-renderer';
41
import { shallow } from 'enzyme';
2+
import MockDate from 'mockdate';
3+
import * as React from 'react';
54

6-
import { Comment } from './comment';
75
import { sampleData } from '../data/sample-data';
6+
import { Comment } from './comment';
87

98
const comment = sampleData.comments[0];
109
// Snapshot will be out of date if we don't use consistent time ago for comment

src/components/comment.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,11 @@ export class Comment extends React.Component<ICommentProps> {
3232
const props = this.props;
3333

3434
const vote = () => {
35-
console.log(this);
36-
// return "vote(event, this, &quot;up&quot;)";
35+
return undefined;
3736
};
3837

3938
const toggle = () => {
40-
console.log(this);
41-
// return "toggle(event, 15238246)";
39+
return undefined;
4240
};
4341

4442
return (

src/components/news-detail.spec.tsx

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import * as React from 'react';
2-
import MockDate from 'mockdate';
3-
// import renderer from 'react-test-renderer';
41
import { shallow } from 'enzyme';
2+
import MockDate from 'mockdate';
3+
import * as React from 'react';
54

6-
import { NewsDetail } from './news-detail';
75
import { sampleData } from '../data/sample-data';
6+
import { NewsDetail } from './news-detail';
87

98
const newsItem = sampleData.newsItems[0];
109
// Snapshot will be out of date if we don't use consistent time agoy

src/components/news-detail.tsx

+1-7
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { gql } from 'apollo-server-express';
22
import Link from 'next/link';
33
import Router from 'next/router';
44
import * as React from 'react';
5-
import { graphql, Mutation } from 'react-apollo';
5+
import { graphql } from 'react-apollo';
66

77
import { hideNewsItem } from '../data/mutations/hide-news-item';
88
import { convertNumberToTimeAgo } from '../helpers/convert-number-to-time-ago';
@@ -112,12 +112,6 @@ export class NewsDetailView extends React.Component<INewsDetailProps> {
112112
}
113113
}
114114

115-
// export const NewsDetail = () => {
116-
// return (
117-
// <Mutation query={hideNewsItem}></Query>
118-
// )
119-
// }
120-
console.log('newsDetail hideNewsItem', hideNewsItem);
121115
export const NewsDetail = graphql<INewsDetailOwnProps, INewsDetailProps, {}, {}>(gql(hideNewsItem), {
122116
props: ({ ownProps, mutate }) => ({
123117
hideNewsItem: (id: number) =>

src/components/news-feed.spec.tsx

+3-17
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
1-
import * as React from 'react';
2-
import MockDate from 'mockdate';
31
import { shallow } from 'enzyme';
2+
import MockDate from 'mockdate';
3+
import * as React from 'react';
44

5-
import { NewsFeed } from './news-feed';
65
import { sampleData } from '../data/sample-data';
6+
import { NewsFeed } from './news-feed';
77

88
MockDate.set(1506022129802);
99

1010
describe('NewsFeed component', () => {
11-
// it('shallow renders', () => {
12-
// const wrapper = shallow(
13-
// <NewsFeed />,
14-
// );
15-
// expect(wrapper).toMatchSnapshot();
16-
// });
17-
1811
it('renders news items passed in as props', () => {
1912
const wrapper = shallow(<NewsFeed newsItems={sampleData.newsItems} currentUrl="/" first={30} skip={0} />);
2013
expect(wrapper).toMatchSnapshot();
2114
});
22-
23-
// it('renders news items passed in as props', () => {
24-
// const wrapper = shallow((
25-
// <NewsFeed newsItems={data.newsItems} />
26-
// ));
27-
// expect(wrapper.contains(<div className="unique" />)).to.equal(true);
28-
// });
2915
});

src/components/news-item-with-comments.tsx

+4-21
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import * as React from 'react';
22

3-
import { NewsTitle } from './news-title';
4-
import { NewsDetail } from './news-detail';
5-
import { LoadingSpinner } from './loading-spinner';
6-
3+
import { NewsItem } from '../data/models';
74
import { CommentBox } from './comment-box';
85
import { Comments } from './comments';
9-
import { NewsItem } from '../data/models';
6+
import { LoadingSpinner } from './loading-spinner';
7+
import { NewsDetail } from './news-detail';
8+
import { NewsTitle } from './news-title';
109

1110
export interface INewsItemWithCommentsProps {
1211
error: Error;
@@ -51,19 +50,3 @@ export const NewsItemWithComments: React.SFC<INewsItemWithCommentsProps> = ({ lo
5150
</tr>
5251
);
5352
};
54-
55-
// export const NewsItemWithComments: React.SFC = ({ data: { loading, error, newsItem }, data }) => {
56-
// if (error) {
57-
// return (
58-
// <tr>
59-
// <td>Error loading news items.</td>
60-
// </tr>
61-
// );
62-
// }
63-
64-
// if (newsItem && newsItem.comments) {
65-
// return <NewsItemWithCommentsView newsItem={newsItem} />;
66-
// }
67-
68-
// return <LoadingSpinner />;
69-
// };

src/config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export const graphQLPath = '/graphql';
22
export const graphiQLPath = '/graphiql';
33

44
export const dev = process.env.NODE_ENV !== 'production';
5-
export const appPath = process.env.NODE_ENV === 'production' ? './dist' /* './build/app' */ : './src';
5+
export const appPath = process.env.NODE_ENV === 'production' ? './dist' : './src';
66

77
export const HN_DB_URI = process.env.DB_URI || 'https://hacker-news.firebaseio.com';
88
export const HN_API_VERSION = process.env.HN_API_VERSION || '/v0';

src/data/cache.ts

-6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ import { FeedSingleton, NewsItem } from './models';
55
import { FeedType } from './models/feed';
66
import { User } from './models/user';
77

8-
// Interface: Commentable (Object can be commented on) comments, commentCount, commenter
9-
// Interface: Voteable (Object can be voted on) upvotes, upvoteCount, hidden, hiddenCount,
10-
// Data Type: Comment, can be on a news item or another comment
11-
// Every time an upvote/downvote/comment is made, update the count
12-
138
const logger = debug('app:Cache');
149
logger.log = console.log.bind(console);
1510

@@ -30,7 +25,6 @@ class Cache {
3025
/* BEGIN NEWS ITEMS */
3126

3227
getNewsItem(id) {
33-
// return this.newsItems.find(newsItem => newsItem.id === id);
3428
return this.newsItemsCache.get(id);
3529
}
3630
setNewsItem(id, newsItem) {

src/data/database.ts

+3-16
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,6 @@ export function getNewsItem(id) {
1313
return sampleData.newsItems.find(newsItem => newsItem.id === id);
1414
}
1515

16-
export function rankNewsItems() {
17-
// Would implement this somewhere in the real HN system
18-
// A scheduled job to recalculate feed ranks
19-
// .sort((a, b) => (a.rank - b.rank))
20-
}
21-
2216
export function createNewsItem(newsItem) {
2317
sampleData.newsItems.push(newsItem);
2418

@@ -52,14 +46,6 @@ export function unvoteNewsItem(id, userId) {
5246
return newsItem;
5347
}
5448

55-
// export function downvoteNewsItem(id, userId) {
56-
// const newsItemData = data.newsItems.find(newsItem => newsItem.id === id);
57-
// newsItemData.downvotes.push(userId);
58-
// newsItemData.downvoteCount += 1;
59-
60-
// return newsItemData;
61-
// }
62-
6349
export function hideNewsItem(id: number, userId) {
6450
logger(`Hiding News Item ${id} by ${userId}`);
6551

@@ -71,11 +57,12 @@ export function hideNewsItem(id: number, userId) {
7157
cache.setUser(id, user);
7258

7359
newsItem.hides.push(userId);
74-
// newsItem.hiddenCount += 1;
7560
cache.setNewsItem(id, newsItem);
7661

7762
logger(`Hid News Item ${id} by ${userId}`);
78-
} else throw new Error(`Data error, user has already hidden ${id} by ${userId}`);
63+
} else {
64+
throw new Error(`Data error, user has already hidden ${id} by ${userId}`);
65+
}
7966

8067
return newsItem;
8168
}

src/data/hn-data-api.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { debug } from 'debug';
22
import * as Firebase from 'firebase';
33

4+
import { HN_API_URL, HN_API_VERSION, HN_DB_URI } from '../config';
45
import { cache } from './cache';
56
import { Comment, FeedSingleton, NewsItem, User } from './models';
6-
import { HN_API_URL, HN_API_VERSION, HN_DB_URI } from '../config';
77

88
const logger = debug('app:HNDataAPI');
99
logger.log = console.log.bind(console);
@@ -56,9 +56,9 @@ export function fetchComment(id) {
5656
const item = itemSnapshot.val();
5757
if (item !== null && !item.deleted && !item.dead) {
5858
const comment = new Comment({
59-
id: item.id,
60-
creationTime: item.time * 1000,
6159
comments: item.kids,
60+
creationTime: item.time * 1000,
61+
id: item.id,
6262
parent: item.parent,
6363
submitterId: item.by,
6464
text: item.text,
@@ -83,9 +83,9 @@ export function fetchUser(id) {
8383
const item = itemSnapshot.val();
8484
if (item !== null && !item.deleted && !item.dead) {
8585
const user = new User({
86-
id: item.id,
8786
about: item.about,
8887
creationTime: item.created * 1000,
88+
id: item.id,
8989
karma: item.karma,
9090
posts: item.submitted,
9191
});

src/data/models/news-item.spec.ts

-31
Original file line numberDiff line numberDiff line change
@@ -24,34 +24,3 @@ describe('NewsItem Model', () => {
2424
expect(newsItem);
2525
});
2626
});
27-
28-
// NewsItem.getNewsItem(id) {
29-
// return cache.getNewsItem(id) || DB.getNewsItem(id) || HNDB.fetchNewsItem(id);
30-
// }
31-
// NewsItem.getNewsItems(ids) {
32-
// return Promise.all(ids.map(id => NewsItem.getNewsItem(id)))
33-
// .then(newsItems => newsItems.filter(newsItem => newsItem !== undefined))
34-
// .catch(reason => logger(`Rejected News Items: ${reason}`));
35-
// }
36-
37-
// NewsItem.upvoteNewsItem(id) {
38-
// return DB.upvoteNewsItem(id);
39-
// }
40-
41-
// NewsItem.submitNewsItem({ submitterId, text, url }) {
42-
// const newsItem = {
43-
// id: newPostIdCounter += 1,
44-
// comments: [],
45-
// commentCount: 0,
46-
// creationTime: new Date().valueOf(),
47-
// hides: [],
48-
// hiddenCount: 0,
49-
// submitterId,
50-
// text: text || null,
51-
// url: url || null,
52-
// upvotes: [submitterId],
53-
// upvoteCount: 1,
54-
// };
55-
// return DB.submitNewsItem(newsItem.id, newsItem);
56-
// }
57-
// }

src/data/models/user.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1+
import { passwordIterations } from '../../config';
2+
import { createHash, createSalt } from '../../helpers/hash-password';
3+
import { cache } from '../cache';
14
import * as DB from '../database';
25
import * as HNDB from '../hn-data-api';
3-
import { cache } from '../cache';
4-
import { createHash, createSalt } from '../../helpers/hash-password';
5-
import { passwordIterations } from '../../config';
6-
import { isValidUser, isValidNewUser } from '../validation/user';
6+
import { isValidNewUser, isValidUser } from '../validation/user';
77

88
export class User {
99
public readonly id: string;
@@ -21,7 +21,9 @@ export class User {
2121
public readonly passwordSalt: string;
2222

2323
constructor(props) {
24-
if (!props.id) throw new Error(`Error instantiating User, id invalid: ${props.id}`);
24+
if (!props.id) {
25+
throw new Error(`Error instantiating User, id invalid: ${props.id}`);
26+
}
2527
isValidUser(props);
2628

2729
this.id = props.id;
@@ -45,7 +47,10 @@ export class User {
4547

4648
static validPassword = async (id, password) => {
4749
const user = cache.getUser(id);
48-
if (user) return (await createHash(password, user.passwordSalt, passwordIterations)) === user.hashedPassword;
50+
if (user) {
51+
return (await createHash(password, user.passwordSalt, passwordIterations)) === user.hashedPassword;
52+
}
53+
4954
return false;
5055
};
5156

@@ -54,15 +59,17 @@ export class User {
5459
isValidNewUser(user);
5560

5661
// Check if user already exists
57-
if (cache.getUser(user.id)) throw new Error('Username is taken.');
62+
if (cache.getUser(user.id)) {
63+
throw new Error('Username is taken.');
64+
}
5865

5966
// Go ahead and create the new user
6067
const passwordSalt = createSalt();
6168
const hashedPassword = await createHash(user.password, passwordSalt, passwordIterations);
6269

6370
const newUser = new User({
64-
id: user.id,
6571
hashedPassword,
72+
id: user.id,
6673
passwordSalt,
6774
});
6875

src/data/mutations/hide-news-item.ts

-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// import { gql } from 'apollo-server-express';
2-
31
export const hideNewsItem = `
42
mutation HideNewsItem($id: Int!) {
53
hideNewsItem(id: $id) {
@@ -8,4 +6,3 @@ export const hideNewsItem = `
86
}
97
}
108
`;
11-
console.log('hide', hideNewsItem);
-12
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// import { gql } from 'apollo-server-express';
2-
// import { newsFeedNewsItemFragment } from '../../components/news-feed';
3-
41
export interface ISubmitNewsItemGraphQL {
52
submitNewsItem: { id }; // Return type of submitNewsItem mutation
63
}
@@ -13,12 +10,3 @@ export const submitNewsItem = `
1310
}
1411
}
1512
`;
16-
// export const submitNewsItem = gql`
17-
// mutation SubmitNewsItem($title: String!, $url: String) {
18-
// submitNewsItem(title: $title, url: $url) {
19-
// id
20-
// ...NewsFeed
21-
// }
22-
// }
23-
// ${newsFeedNewsItemFragment}
24-
// `;

src/data/mutations/upvote-news-item.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// import { gql } from 'apollo-server-express';
2-
31
export const upvoteNewsItem = `
42
mutation UpvoteNewsItem($id: Int!) {
53
upvoteNewsItem(id: $id) {

src/data/schema.spec.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { sampleData } from './sample-data';
22
import { resolvers } from './Schema';
33

4-
// const comment = data.topStoriesCache[0].comments[0];
5-
64
describe('GraphQL', () => {
75
describe('Resolvers', () => {
86
describe('Queries', () => {

0 commit comments

Comments
 (0)