Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.

Commit 3af1f4b

Browse files
authored
Properly show spinner when user has no repositories (#516)
Fixes #507
1 parent bddffaf commit 3af1f4b

File tree

2 files changed

+80
-4
lines changed

2 files changed

+80
-4
lines changed

src/common/components/repositories-home/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ class RepositoriesHome extends Component {
5555
}
5656

5757
componentWillReceiveProps(nextProps) {
58-
if (this.props.snaps.success !== nextProps.snaps.success) {
58+
const snapsLength = nextProps.snaps.snaps && nextProps.snaps.snaps.length;
59+
60+
if ((this.props.snaps.success !== nextProps.snaps.success)
61+
|| (snapsLength === 0)) {
5962
this.fetchData(nextProps);
6063
}
6164
}
@@ -89,9 +92,14 @@ class RepositoriesHome extends Component {
8992

9093
render() {
9194
const { snaps } = this.props;
92-
const hasSnaps = (snaps.snaps && Object.keys(snaps.snaps).length > 0);
93-
// show spinner until we know if user has any enabled repos
94-
return (snaps.isFetching && !hasSnaps)
95+
const hasSnaps = (snaps.snaps && snaps.snaps.length > 0);
96+
97+
// show spinner if snaps data was not yet fetched (snaps list is empty)
98+
// (to avoid spinner during polling we are not checking `success` or `isFetching`
99+
//
100+
// when snaps are loaded and user doesn't have any, they will be redirected
101+
// to select repositories (so spinner won't be showing endlessly)
102+
return !hasSnaps
95103
? this.renderSpinner()
96104
: this.renderRepositoriesList();
97105
}

test/unit/src/common/components/repositories-home/t_repositories-home.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,74 @@ describe('The RepositoriesHome component', () => {
1818
clock.restore();
1919
});
2020

21+
context('when snaps are not loaded', () => {
22+
let wrapper;
23+
24+
beforeEach(() => {
25+
props = {
26+
auth: {
27+
authenticated: true
28+
},
29+
user: {},
30+
snaps: {},
31+
snapBuilds: {},
32+
fetchBuilds: () => {},
33+
updateSnaps: () => {},
34+
router: {}
35+
};
36+
37+
wrapper = shallow(<RepositoriesHome { ...props } />);
38+
});
39+
40+
it('should render spinner', () => {
41+
expect(wrapper.find('Spinner').length).toBe(1);
42+
});
43+
});
44+
45+
context('when user has no snaps', () => {
46+
let wrapper;
47+
48+
beforeEach(() => {
49+
props = {
50+
auth: {
51+
authenticated: true
52+
},
53+
user: {},
54+
snaps: {
55+
success: true,
56+
snaps: []
57+
},
58+
snapBuilds: {},
59+
fetchBuilds: () => {},
60+
updateSnaps: () => {},
61+
router: {
62+
replace: expect.createSpy()
63+
}
64+
};
65+
66+
wrapper = shallow(<RepositoriesHome { ...props } />);
67+
});
68+
69+
it('should render spinner', () => {
70+
expect(wrapper.find('Spinner').length).toBe(1);
71+
});
72+
73+
context('and component recieves props', () => {
74+
beforeEach(() => {
75+
wrapper.instance().componentDidMount();
76+
wrapper.instance().componentWillReceiveProps(props);
77+
});
78+
79+
afterEach(() => {
80+
wrapper.instance().componentWillUnmount();
81+
});
82+
83+
it('should redirect to select repositories', () => {
84+
expect(props.router.replace).toHaveBeenCalledWith('/select-repositories');
85+
});
86+
});
87+
});
88+
2189
context('when user is logged in', () => {
2290
let wrapper;
2391

0 commit comments

Comments
 (0)