it('normalizes the response', () => { mockWindow.expects('fetch').once().returns(mockResponse()); return api.fetchAddon('foo').then((results) => { const foo = {slug: 'foo', name: 'Foo!'}; assert.deepEqual(results.result, 'foo'); assert.deepEqual(results.entities, {addons: {foo}}); }); });
export function loadAddonIfNeeded({ store: { dispatch, getState }, params: { slug } }) { const state = getState(); const addon = findAddon(state, slug); if (addon) { return addon; } return fetchAddon({ slug, api: state.api }) .then(({ entities }) => dispatch(loadEntities(entities))); }
it('sets the lang and slug', () => { mockWindow.expects('fetch') .withArgs( 'https://addons.mozilla.org/api/v3/addons/addon/foo/?lang=en-US', {headers: {}, method: 'get'}) .once() .returns(mockResponse()); return api.fetchAddon({slug: 'foo'}).then(() => mockWindow.verify()); });
it('sets the lang and slug', () => { mockWindow.expects('fetch') .withArgs( `${apiHost}/api/v3/addons/addon/foo/?lang=en-US`, { headers: {}, method: 'GET' }) .once() .returns(mockResponse()); return api.fetchAddon({ api: { lang: 'en-US' }, slug: 'foo' }) .then(() => mockWindow.verify()); });
it('fails when the add-on is not found', () => { mockWindow .expects('fetch') .withArgs( `${apiHost}/api/v3/addons/addon/foo/?lang=en-US`, { headers: {}, method: 'GET' }) .once() .returns(mockResponse({ ok: false })); return api.fetchAddon({ api: { lang: 'en-US' }, slug: 'foo' }) .then(unexpectedSuccess, (error) => assert.equal(error.message, 'Error calling API')); });
it('fails when the add-on is not found', () => { mockWindow .expects('fetch') .withArgs( 'https://addons.mozilla.org/api/v3/addons/addon/foo/?lang=en-US', {headers: {}, method: 'get'}) .once() .returns(Promise.resolve({ok: false})); return api.fetchAddon({slug: 'foo'}).then( () => assert.fail(null, null, 'expected API call to fail'), (error) => assert.equal(error.message, 'Error calling API')); });
export function loadAddonIfNeeded( { store: { dispatch, getState }, params: { slug } } ) { const state = getState(); const addon = findAddon(state, slug); if (addon) { log.info(`Found addon ${addon.id} in state`); return addon; } log.info(`Fetching addon ${slug} from API`); return fetchAddon({ slug, api: state.api }) .then(({ entities }) => dispatch(loadEntities(entities))); }
it('includes the authorization token if available', () => { const token = 'bAse64.enCodeD.JWT'; mockWindow .expects('fetch') .withArgs( 'https://addons.mozilla.org/api/v3/addons/addon/bar/?lang=en-US', {headers: {authorization: `Bearer ${token}`}, method: 'get'}) .once() .returns(mockResponse()); return api.fetchAddon({api: {token}, slug: 'bar'}).then((results) => { const foo = {slug: 'foo', name: 'Foo!'}; assert.deepEqual(results.result, 'foo'); assert.deepEqual(results.entities, {addons: {foo}}); mockWindow.verify(); }); });
it('fails when the add-on is not found', () => { mockWindow .expects('fetch') .withArgs(`${apiHost}/api/v3/addons/addon/foo/?lang=en-US`, { body: undefined, credentials: undefined, headers: {}, method: 'GET', }) .once() .returns(mockResponse({ ok: false })); return api.fetchAddon({ api: { lang: 'en-US' }, slug: 'foo' }) .then(unexpectedSuccess, (error) => { expect(error.message).toEqual('Error calling: /api/v3/addons/addon/foo/'); }); });
it('includes the authorization token if available', () => { const token = userAuthToken(); mockWindow .expects('fetch') .withArgs(`${apiHost}/api/v3/addons/addon/bar/?lang=en-US`, { body: undefined, credentials: undefined, headers: { authorization: `Bearer ${token}` }, method: 'GET', }) .once() .returns(mockResponse()); return api.fetchAddon({ api: { lang: 'en-US', token }, slug: 'bar' }) .then((results) => { const foo = { slug: 'foo', name: 'Foo!' }; expect(results.result).toEqual('foo'); expect(results.entities).toEqual({ addons: { foo } }); mockWindow.verify(); }); });
(({ addonSlug, apiState }) => { return fetchAddon({ slug: addonSlug, api: apiState }).then( ({ entities }) => dispatch(loadAddons(entities)), ); }),