Skip to content

Commit

Permalink
Fetch addon on slug update (#3018)
Browse files Browse the repository at this point in the history
  • Loading branch information
willdurand committed Aug 29, 2017
1 parent 6b976c4 commit b607d80
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/amo/components/Addon/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,17 @@ export class AddonBase extends React.Component {
}
}

componentWillReceiveProps({ addon: newAddon }) {
const { addon: oldAddon, dispatch } = this.props;
componentWillReceiveProps({ addon: newAddon, params: newParams }) {
const { addon: oldAddon, dispatch, errorHandler, params } = this.props;

const oldAddonType = oldAddon ? oldAddon.type : null;
if (newAddon && newAddon.type !== oldAddonType) {
dispatch(setViewContext(newAddon.type));
}

if (params.slug !== newParams.slug) {
dispatch(fetchAddon({ slug: newParams.slug, errorHandler }));
}
}

componentWillUnmount() {
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/amo/components/TestAddon.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,32 @@ describe('Addon', () => {
expect(root.find(AddonMeta).prop('addon')).toEqual(null);
});

it('does not dispatch fetchAddon action when slug is the same', () => {
const fakeDispatch = sinon.stub();
const errorHandler = createStubErrorHandler();
const addon = fakeAddon;
const root = shallowRender({ addon, errorHandler, dispatch: fakeDispatch });

fakeDispatch.reset();
// Update with the same slug.
root.setProps({ params: { slug: addon.slug } });

sinon.assert.notCalled(fakeDispatch);
});

it('dispatches fetchAddon action when updating with a new slug', () => {
const fakeDispatch = sinon.stub();
const errorHandler = createStubErrorHandler();
const root = shallowRender({ errorHandler, dispatch: fakeDispatch });
const slug = 'some-new-slug';

fakeDispatch.reset();
// Update with a new slug.
root.setProps({ params: { slug } });

sinon.assert.calledWith(fakeDispatch, fetchAddonAction({ errorHandler, slug }));
});

it('renders an error if there is one', () => {
const errorHandler = createStubErrorHandler(new Error('some error'));

Expand Down

0 comments on commit b607d80

Please sign in to comment.