From b607d8061af69b19335196da0d7196d7ab14e0d2 Mon Sep 17 00:00:00 2001 From: William Durand Date: Tue, 29 Aug 2017 12:09:00 +0200 Subject: [PATCH] Fetch addon on slug update (#3018) --- src/amo/components/Addon/index.js | 9 +++++++-- tests/unit/amo/components/TestAddon.js | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/amo/components/Addon/index.js b/src/amo/components/Addon/index.js index d2239341549..d6c402383f2 100644 --- a/src/amo/components/Addon/index.js +++ b/src/amo/components/Addon/index.js @@ -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() { diff --git a/tests/unit/amo/components/TestAddon.js b/tests/unit/amo/components/TestAddon.js index a6d48090a89..3cbb0d69653 100644 --- a/tests/unit/amo/components/TestAddon.js +++ b/tests/unit/amo/components/TestAddon.js @@ -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'));