Skip to content

Commit

Permalink
Properly show spinner when user has no repositories (canonical-web-an…
Browse files Browse the repository at this point in the history
  • Loading branch information
bartaz committed Mar 17, 2017
1 parent bddffaf commit 3af1f4b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/common/components/repositories-home/index.js
Expand Up @@ -55,7 +55,10 @@ class RepositoriesHome extends Component {
}

componentWillReceiveProps(nextProps) {
if (this.props.snaps.success !== nextProps.snaps.success) {
const snapsLength = nextProps.snaps.snaps && nextProps.snaps.snaps.length;

if ((this.props.snaps.success !== nextProps.snaps.success)
|| (snapsLength === 0)) {
this.fetchData(nextProps);
}
}
Expand Down Expand Up @@ -89,9 +92,14 @@ class RepositoriesHome extends Component {

render() {
const { snaps } = this.props;
const hasSnaps = (snaps.snaps && Object.keys(snaps.snaps).length > 0);
// show spinner until we know if user has any enabled repos
return (snaps.isFetching && !hasSnaps)
const hasSnaps = (snaps.snaps && snaps.snaps.length > 0);

// show spinner if snaps data was not yet fetched (snaps list is empty)
// (to avoid spinner during polling we are not checking `success` or `isFetching`
//
// when snaps are loaded and user doesn't have any, they will be redirected
// to select repositories (so spinner won't be showing endlessly)
return !hasSnaps
? this.renderSpinner()
: this.renderRepositoriesList();
}
Expand Down
Expand Up @@ -18,6 +18,74 @@ describe('The RepositoriesHome component', () => {
clock.restore();
});

context('when snaps are not loaded', () => {
let wrapper;

beforeEach(() => {
props = {
auth: {
authenticated: true
},
user: {},
snaps: {},
snapBuilds: {},
fetchBuilds: () => {},
updateSnaps: () => {},
router: {}
};

wrapper = shallow(<RepositoriesHome { ...props } />);
});

it('should render spinner', () => {
expect(wrapper.find('Spinner').length).toBe(1);
});
});

context('when user has no snaps', () => {
let wrapper;

beforeEach(() => {
props = {
auth: {
authenticated: true
},
user: {},
snaps: {
success: true,
snaps: []
},
snapBuilds: {},
fetchBuilds: () => {},
updateSnaps: () => {},
router: {
replace: expect.createSpy()
}
};

wrapper = shallow(<RepositoriesHome { ...props } />);
});

it('should render spinner', () => {
expect(wrapper.find('Spinner').length).toBe(1);
});

context('and component recieves props', () => {
beforeEach(() => {
wrapper.instance().componentDidMount();
wrapper.instance().componentWillReceiveProps(props);
});

afterEach(() => {
wrapper.instance().componentWillUnmount();
});

it('should redirect to select repositories', () => {
expect(props.router.replace).toHaveBeenCalledWith('/select-repositories');
});
});
});

context('when user is logged in', () => {
let wrapper;

Expand Down

0 comments on commit 3af1f4b

Please sign in to comment.