Skip to content
This repository was archived by the owner on Sep 6, 2022. It is now read-only.

Commit 0c6559a

Browse files
authored
Update types (#45)
Update types
2 parents c2ff5c9 + 23348c6 commit 0c6559a

File tree

18 files changed

+64
-80
lines changed

18 files changed

+64
-80
lines changed

app/components/List/index.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import * as React from 'react';
22

33
import Ul from './Ul';
44
import Wrapper from './Wrapper';
5+
import { Repo } from '../../containers/RepoListItem/types';
56

67
interface Props {
7-
component: any;
8-
items?: any[];
8+
component: React.ComponentType<any>;
9+
items?: Repo[];
910
}
1011

1112
function List(props: Props) {

app/components/List/tests/index.test.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import { render } from '@testing-library/react';
33

44
import List from '../index';
5+
import { Repo } from '../../../containers/RepoListItem/types';
56

67
describe('<List />', () => {
78
it('should render the passed component if no items are passed', () => {
@@ -11,7 +12,7 @@ describe('<List />', () => {
1112
});
1213

1314
it('should pass all items props to rendered component', () => {
14-
const items = [{ id: 1, name: 'Hello' }, { id: 2, name: 'World' }];
15+
const items = [{ id: 1, name: 'Hello' }, { id: 2, name: 'World' }] as Repo[];
1516

1617
const component = ({ item }) => <li>{item.name}</li>;
1718

@@ -20,7 +21,7 @@ describe('<List />', () => {
2021
);
2122
const elements = container.querySelectorAll('li');
2223
expect(elements).toHaveLength(2);
23-
expect(queryByText(items[0].name)).toBeInTheDocument();
24-
expect(queryByText(items[1].name)).toBeInTheDocument();
24+
expect(queryByText(items[0].name!)).toBeInTheDocument();
25+
expect(queryByText(items[1].name!)).toBeInTheDocument();
2526
});
2627
});

app/components/ReposList/index.tsx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@ import List from 'components/List';
44
import ListItem from 'components/ListItem';
55
import LoadingIndicator from 'components/LoadingIndicator';
66
import RepoListItem from 'containers/RepoListItem';
7+
import { ContainerState, UserData } from '../../containers/App/types';
78

8-
function ReposList({
9-
loading,
10-
error,
11-
repos,
12-
}: {
13-
loading?: boolean;
14-
error?: any;
15-
repos?: any;
16-
}) {
9+
export type ReposListProps = Pick<ContainerState, 'loading' | 'error'> & Pick<UserData, 'repos'>;
10+
11+
function ReposList({ loading, error, repos }: ReposListProps) {
1712
if (loading) {
1813
return <List component={LoadingIndicator} />;
1914
}
@@ -24,7 +19,7 @@ function ReposList({
2419
);
2520
return <List component={ErrorComponent} />;
2621
}
27-
if (repos !== false) {
22+
if (repos !== undefined) {
2823
return <List items={repos} component={RepoListItem} />;
2924
}
3025

app/components/ReposList/tests/index.test.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { render } from '@testing-library/react';
66
import ReposList from '../index';
77
import configureStore from '../../../configureStore';
88
import history from '../../../utils/history';
9+
import { Repo } from '../../../containers/RepoListItem/types';
910

1011
describe('<ReposList />', () => {
1112
it('should render the loading indicator when its loading', () => {
@@ -31,7 +32,7 @@ describe('<ReposList />', () => {
3132
error: false,
3233
loading: false,
3334
userData: {
34-
repositories: false,
35+
repos: false,
3536
},
3637
},
3738
};
@@ -49,12 +50,12 @@ describe('<ReposList />', () => {
4950
open_issues_count: 20,
5051
full_name: 'react-boilerplate/react-boilerplate',
5152
},
52-
];
53+
] as Repo[];
5354
const { container } = render(
5455
// tslint:disable-next-line: jsx-wrap-multiline
5556
<Provider store={store}>
5657
<IntlProvider locale="en">
57-
<ReposList repos={repos} error={false} />
58+
<ReposList repos={repos} error={false} loading={false}/>
5859
</IntlProvider>
5960
</Provider>,
6061
);
@@ -64,7 +65,7 @@ describe('<ReposList />', () => {
6465

6566
it('should not render anything if nothing interesting is provided', () => {
6667
const { container } = render(
67-
<ReposList repos={false} error={false} loading={false} />,
68+
<ReposList repos={undefined} error={false} loading={false} />,
6869
);
6970

7071
expect(container).toBeEmpty();

app/containers/App/actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { action } from 'typesafe-actions';
2-
// import {} from './types';
32

43
import ActionTypes from './constants';
4+
import { Repo } from '../RepoListItem/types';
55

66
export const loadRepos = () => action(ActionTypes.LOAD_REPOS);
7-
export const reposLoaded = (repos: any[], username: string) =>
7+
export const reposLoaded = (repos: Repo[], username: string) =>
88
action(ActionTypes.LOAD_REPOS_SUCCESS, { repos: repos, username: username });
99
export const repoLoadingError = (error: object) =>
1010
action(ActionTypes.LOAD_REPOS_ERROR, error);

app/containers/App/reducer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const initialState: ContainerState = {
77
error: false,
88
currentUser: '',
99
userData: {
10-
repositories: [],
10+
repos: [],
1111
},
1212
};
1313

@@ -23,7 +23,7 @@ function appReducer(
2323
loading: true,
2424
error: false,
2525
userData: {
26-
repositories: [],
26+
repos: [],
2727
},
2828
};
2929
case ActionTypes.LOAD_REPOS_SUCCESS:
@@ -32,7 +32,7 @@ function appReducer(
3232
loading: false,
3333
error: state.error,
3434
userData: {
35-
repositories: action.payload.repos,
35+
repos: action.payload.repos,
3636
},
3737
};
3838
case ActionTypes.LOAD_REPOS_ERROR:

app/containers/App/selectors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const makeSelectError = () =>
2424

2525
const makeSelectRepos = () =>
2626
createSelector(selectGlobal, globalState =>
27-
globalState.userData.repositories);
27+
globalState.userData.repos);
2828

2929
const makeSelectLocation = () =>
3030
createSelector(selectRoute, routeState => routeState.location);

app/containers/App/tests/actions.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import ActionTypes from '../constants';
22

33
import { loadRepos, reposLoaded, repoLoadingError } from '../actions';
44
import { action } from 'typesafe-actions';
5+
import { Repo } from '../../RepoListItem/types';
56

67
describe('App Actions', () => {
78
describe('loadRepos', () => {
@@ -16,7 +17,7 @@ describe('App Actions', () => {
1617

1718
describe('reposLoaded', () => {
1819
it('should return the correct type and the passed repos', () => {
19-
const fixture = ['Test'];
20+
const fixture = [{}] as Repo[];
2021
const username = 'test';
2122
const expectedResult = action(
2223
ActionTypes.LOAD_REPOS_SUCCESS,

app/containers/App/tests/reducer.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import appReducer from '../reducer';
22
import { loadRepos, reposLoaded, repoLoadingError } from '../actions';
33
import { ContainerState } from '../types';
4+
import { Repo } from '../../RepoListItem/types';
45

56
describe('appReducer', () => {
67
let state: ContainerState;
@@ -10,7 +11,7 @@ describe('appReducer', () => {
1011
error: false,
1112
currentUser: '',
1213
userData: {
13-
repositories: [],
14+
repos: [],
1415
},
1516
};
1617
});
@@ -26,7 +27,7 @@ describe('appReducer', () => {
2627
loading: true,
2728
error: false,
2829
userData: {
29-
repositories: [],
30+
repos: [],
3031
},
3132
};
3233
expect(appReducer(state, loadRepos())).toEqual(expectedResult);
@@ -37,14 +38,14 @@ describe('appReducer', () => {
3738
{
3839
name: 'My Repo',
3940
},
40-
];
41+
] as Repo[];
4142
const username = 'test';
4243
const expectedResult = {
4344
currentUser: username,
4445
loading: false,
4546
error: false,
4647
userData: {
47-
repositories: fixture,
48+
repos: fixture,
4849
},
4950
};
5051
expect(appReducer(state, reposLoaded(fixture, username))).toEqual(
@@ -62,7 +63,7 @@ describe('appReducer', () => {
6263
error: fixture,
6364
loading: false,
6465
userData: {
65-
repositories: [],
66+
repos: [],
6667
},
6768
};
6869

app/containers/App/tests/selectors.test.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@ import {
66
makeSelectRepos,
77
makeSelectLocation,
88
} from '../selectors';
9+
import { ApplicationRootState } from '../../../types';
10+
import { Repo } from '../../RepoListItem/types';
911

1012
describe('selectGlobal', () => {
1113
it('should select the global state', () => {
12-
const globalState = {} as any;
13-
const mockedState: any = {
14+
const globalState = {};
15+
// tslint:disable-next-line:no-object-literal-type-assertion
16+
const mockedState = {
1417
global: globalState,
15-
};
18+
} as ApplicationRootState;
1619
expect(selectGlobal(mockedState)).toEqual(globalState);
1720
});
1821
});
@@ -21,11 +24,12 @@ describe('makeSelectCurrentUser', () => {
2124
it('should select the current user', () => {
2225
const currentUserSelector = makeSelectCurrentUser();
2326
const username = 'mxstbr';
24-
const mockedState: any = {
27+
// tslint:disable-next-line:no-object-literal-type-assertion
28+
const mockedState = {
2529
global: {
2630
currentUser: username,
2731
},
28-
};
32+
} as ApplicationRootState;
2933
expect(currentUserSelector(mockedState)).toEqual(username);
3034
});
3135
});
@@ -59,15 +63,16 @@ describe('makeSelectError', () => {
5963
describe('makeSelectRepos', () => {
6064
it('should select the repos', () => {
6165
const reposSelector = makeSelectRepos();
62-
const repositories: any[] = [];
63-
const mockedState: any = {
66+
const repos: Repo[] = [];
67+
// tslint:disable-next-line:no-object-literal-type-assertion
68+
const mockedState = {
6469
global: {
6570
userData: {
66-
repositories,
71+
repos: repos,
6772
},
6873
},
69-
};
70-
expect(reposSelector(mockedState)).toEqual(repositories);
74+
} as ApplicationRootState;
75+
expect(reposSelector(mockedState)).toEqual(repos);
7176
});
7277
});
7378

app/containers/App/types.d.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { ActionType } from 'typesafe-actions';
22
import * as actions from './actions';
3-
import { ApplicationRootState } from 'types';
3+
import { Repo } from '../RepoListItem/types';
4+
import { ApplicationRootState } from '../../types';
45

56
/* --- STATE --- */
67

78
interface AppState {
89
readonly loading: boolean;
9-
readonly error: object | boolean;
10+
readonly error?: object | boolean;
1011
readonly currentUser: string;
1112
readonly userData: UserData;
1213
}
1314

1415
interface UserData {
15-
readonly repositories: object[] | boolean; // too many fields. Won't declare them all
16+
readonly repos?: Repo[];
1617
}
1718

1819

@@ -26,4 +27,4 @@ type RootState = ApplicationRootState;
2627
type ContainerState = AppState;
2728
type ContainerActions = AppActions;
2829

29-
export { RootState, ContainerState, ContainerActions };
30+
export { RootState, ContainerState, ContainerActions, UserData };

app/containers/HomePage/index.tsx

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,15 @@
44
* This is the first thing users see of our App, at the '/' route
55
*/
66

7-
import React, { useEffect, memo } from 'react';
7+
import React, { useEffect } from 'react';
88
import { Helmet } from 'react-helmet';
99
import { FormattedMessage } from 'react-intl';
10-
import { connect, useSelector, useDispatch } from 'react-redux';
11-
import { compose, Dispatch } from 'redux';
10+
import { useDispatch, useSelector } from 'react-redux';
1211
import { createStructuredSelector } from 'reselect';
1312

1413
import { useInjectReducer } from 'utils/injectReducer';
1514
import { useInjectSaga } from 'utils/injectSaga';
16-
import {
17-
makeSelectRepos,
18-
makeSelectLoading,
19-
makeSelectError,
20-
} from 'containers/App/selectors';
15+
import { makeSelectError, makeSelectLoading, makeSelectRepos } from 'containers/App/selectors';
2116
import H2 from 'components/H2';
2217
import ReposList from 'components/ReposList';
2318
import AtPrefix from './AtPrefix';

app/containers/HomePage/tests/saga.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import ActionTypes from 'containers/App/constants';
88
import { reposLoaded, repoLoadingError } from 'containers/App/actions';
99

1010
import githubData, { getRepos } from '../saga';
11+
import { Repo } from '../../RepoListItem/types';
1112

1213
const username = 'mxstbr';
1314

@@ -34,7 +35,7 @@ describe('getRepos Saga', () => {
3435
{
3536
name: 'Second repo',
3637
},
37-
];
38+
] as Repo[];
3839
const putDescriptor = getReposGenerator.next(response).value;
3940
expect(putDescriptor).toEqual(put(reposLoaded(response, username)));
4041
});

app/containers/RepoListItem/tests/index.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const renderComponent = (item, currentUser) => {
1818
error: false,
1919
loading: false,
2020
userData: {
21-
repositories: false,
21+
repos: false,
2222
},
2323
},
2424
};

app/utils.d.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,5 @@
1-
// declare module '*.css' {
2-
// const content: string|any;
3-
// export default content;
4-
// }
1+
declare module '*.svg';
52

6-
declare module '*.svg' {
7-
const content: string | any;
8-
export default content;
9-
}
3+
declare module '*.jpg';
104

11-
declare module '*.jpg' {
12-
const content: string | any;
13-
export default content;
14-
}
15-
16-
declare module '*.png' {
17-
const content: any;
18-
export default content;
19-
}
20-
21-
// declare module '*!raw' {
22-
// const content: string|any;
23-
// export default content;
24-
// }
5+
declare module '*.png';

0 commit comments

Comments
 (0)