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

Commit 4977d41

Browse files
committed
update#37 add types
1 parent 9fd92ab commit 4977d41

File tree

11 files changed

+52
-41
lines changed

11 files changed

+52
-41
lines changed

app/components/List/index.tsx

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

33
import Ul from './Ul';
44
import Wrapper from './Wrapper';
5+
import { Repo } from '../../containers/RepoListItem/types';
6+
import { UserData } from '../../containers/App/types';
57

68
interface Props {
7-
component: any;
8-
items?: any[];
9+
component: React.ComponentType<any>;
10+
items?: UserData['repos'];
911
}
1012

1113
function List(props: Props) {
@@ -29,4 +31,5 @@ function List(props: Props) {
2931
);
3032
}
3133

34+
const items: UserData['repos'] = [];
3235
export default List;

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: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ 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+
interface Props {
10+
loading?: ContainerState['loading'];
11+
error?: ContainerState['error'];
12+
repos?: UserData['repos'];
13+
}
14+
15+
function ReposList({ loading, error, repos }: Props) {
1716
if (loading) {
1817
return <List component={LoadingIndicator} />;
1918
}
@@ -24,7 +23,7 @@ function ReposList({
2423
);
2524
return <List component={ErrorComponent} />;
2625
}
27-
if (repos !== false) {
26+
if (repos !== undefined) {
2827
return <List items={repos} component={RepoListItem} />;
2928
}
3029

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

Lines changed: 4 additions & 3 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,7 +50,7 @@ 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}>
@@ -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/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/reducer.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('appReducer', () => {
1111
error: false,
1212
currentUser: '',
1313
userData: {
14-
repositories: [],
14+
repos: [],
1515
},
1616
};
1717
});
@@ -27,7 +27,7 @@ describe('appReducer', () => {
2727
loading: true,
2828
error: false,
2929
userData: {
30-
repositories: [],
30+
repos: [],
3131
},
3232
};
3333
expect(appReducer(state, loadRepos())).toEqual(expectedResult);
@@ -45,7 +45,7 @@ describe('appReducer', () => {
4545
loading: false,
4646
error: false,
4747
userData: {
48-
repositories: fixture,
48+
repos: fixture,
4949
},
5050
};
5151
expect(appReducer(state, reposLoaded(fixture, username))).toEqual(
@@ -63,7 +63,7 @@ describe('appReducer', () => {
6363
error: fixture,
6464
loading: false,
6565
userData: {
66-
repositories: [],
66+
repos: [],
6767
},
6868
};
6969

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: 4 additions & 3 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';
33
import { ApplicationRootState } from 'types';
4+
import { Repo } from '../RepoListItem/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[]; // too many fields. Won't declare them all
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/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
};

0 commit comments

Comments
 (0)