コード例 #1
0
 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}});
   });
 });
コード例 #2
0
ファイル: index.js プロジェクト: davehunt/addons-frontend
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)));
}
コード例 #3
0
 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());
 });
コード例 #4
0
 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());
 });
コード例 #5
0
 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'));
 });
コード例 #6
0
 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'));
 });
コード例 #7
0
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)));
}
コード例 #8
0
 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();
   });
 });
コード例 #9
0
ファイル: test_api.js プロジェクト: mathjazz/addons-frontend
 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/');
       });
 });
コード例 #10
0
ファイル: test_api.js プロジェクト: mathjazz/addons-frontend
 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();
     });
 });
コード例 #11
0
 (({ addonSlug, apiState }) => {
   return fetchAddon({ slug: addonSlug, api: apiState }).then(
     ({ entities }) => dispatch(loadAddons(entities)),
   );
 }),