Skip to content

Commit

Permalink
PR tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
tofumatt committed Sep 26, 2017
1 parent 531ad32 commit 9247b29
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 42 deletions.
5 changes: 3 additions & 2 deletions src/amo/components/AddonMeta.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow */
import React from 'react';
import { connect } from 'react-redux';
import { compose } from 'redux';
Expand Down Expand Up @@ -55,7 +56,7 @@ export class AddonMetaBase extends React.Component {
<div className="AddonMeta-item AddonMeta-users">
<h3 className="visually-hidden">{i18n.gettext('Used by')}</h3>
<p className="AddonMeta-text AddonMeta-user-count">
{addon && isAddonAuthor({ addon, userId }) ? (
{isAddonAuthor({ addon, userId }) ? (
<Link
href={`/addon/${addon.slug}/statistics/`}
title={i18n.gettext('Click to view statistics')}
Expand All @@ -81,7 +82,7 @@ export class AddonMetaBase extends React.Component {

export const mapStateToProps = (state) => {
return {
userId: state.user ? state.user.id : null,
userId: state.user.id,
};
};

Expand Down
3 changes: 2 additions & 1 deletion src/amo/components/AddonMoreInfo/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow */
import React from 'react';
import { connect } from 'react-redux';
import { compose } from 'redux';
Expand Down Expand Up @@ -182,7 +183,7 @@ export class AddonMoreInfoBase extends React.Component {

export const mapStateToProps = (state) => {
return {
userId: state.user ? state.user.id : null,
userId: state.user.id,
};
};

Expand Down
55 changes: 43 additions & 12 deletions tests/unit/amo/components/TestAddonMeta.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import React from 'react';

import AddonMeta, { AddonMetaBase } from 'amo/components/AddonMeta';
import Link from 'amo/components/Link';
import { createInternalAddon } from 'core/reducers/addons';
import {
dispatchSignInActions,
dispatchClientMetadata,
dispatchSignInActions,
fakeAddon,
} from 'tests/unit/amo/helpers';
import { getFakeI18nInst, shallowUntilTarget } from 'tests/unit/helpers';
import LoadingText from 'ui/components/LoadingText';
import Rating from 'ui/components/Rating';


describe('<AddonMeta>', () => {
describe(__filename, () => {
function render({
addon = fakeAddon,
addon = createInternalAddon(fakeAddon),
store = dispatchClientMetadata().store,
...props
} = {}) {
Expand Down Expand Up @@ -45,44 +46,74 @@ describe('<AddonMeta>', () => {

it('renders the user count', () => {
const root = render({
addon: { ...fakeAddon, average_daily_users: 2 },
addon: createInternalAddon({ ...fakeAddon, average_daily_users: 2 }),
});
expect(getUserCount(root)).toEqual('2 users');
});

it('renders one user', () => {
const root = render({
addon: { ...fakeAddon, average_daily_users: 1 },
addon: createInternalAddon({ ...fakeAddon, average_daily_users: 1 }),
});
expect(getUserCount(root)).toEqual('1 user');
});

it('localizes the user count', () => {
const i18n = getFakeI18nInst({ lang: 'de' });
const root = render({
addon: { ...fakeAddon, average_daily_users: 1000 },
addon: createInternalAddon({
...fakeAddon,
average_daily_users: 1000,
}),
i18n,
});
expect(getUserCount(root)).toMatch(/^1\.000/);
});

it('does not link to stats if user is not author of the add-on', () => {
const authorUserId = 11;
const addon = createInternalAddon({
...fakeAddon,
slug: 'coolio',
authors: [
{
...fakeAddon.authors[0],
id: authorUserId,
name: 'tofumatt',
picture_url: 'http://cdn.a.m.o/myphoto.jpg',
url: 'http://a.m.o/en-GB/firefox/user/tofumatt/',
username: 'tofumatt',
},
],
});
const root = render({
addon,
store: dispatchSignInActions({ userId: 5 }).store,
});

const statsLink = root.find('.AddonMeta-user-count').find(Link);
expect(statsLink).toHaveLength(0);
});

it('links to stats if add-on author is viewing the page', () => {
const addon = {
const authorUserId = 11;
const addon = createInternalAddon({
...fakeAddon,
slug: 'coolio',
authors: [
{
id: 11,
...fakeAddon.authors[0],
id: authorUserId,
name: 'tofumatt',
picture_url: 'http://cdn.a.m.o/myphoto.jpg',
url: 'http://a.m.o/en-GB/firefox/user/tofumatt/',
username: 'tofumatt',
},
],
};
});
const root = render({
addon,
store: dispatchSignInActions({ userId: 11 }).store,
store: dispatchSignInActions({ userId: authorUserId }).store,
});

const statsLink = root.find('.AddonMeta-user-count').find(Link);
Expand All @@ -95,13 +126,13 @@ describe('<AddonMeta>', () => {
describe('ratings', () => {
function renderRatings(ratings = {}, otherProps = {}) {
return render({
addon: {
addon: createInternalAddon({
...fakeAddon,
ratings: {
...fakeAddon.ratings,
...ratings,
},
},
}),
...otherProps,
});
}
Expand Down
86 changes: 61 additions & 25 deletions tests/unit/amo/components/TestAddonMoreInfo.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { oneLine } from 'common-tags';
import deepcopy from 'deepcopy';
import React from 'react';

import AddonMoreInfo, {
AddonMoreInfoBase,
} from 'amo/components/AddonMoreInfo';
import Link from 'amo/components/Link';
import { createInternalAddon } from 'core/reducers/addons';
import {
dispatchSignInActions,
dispatchClientMetadata,
dispatchSignInActions,
fakeAddon,
} from 'tests/unit/amo/helpers';
import { getFakeI18nInst, shallowUntilTarget } from 'tests/unit/helpers';
Expand All @@ -21,7 +21,7 @@ describe(__filename, () => {
function render(props) {
return shallowUntilTarget(
<AddonMoreInfo
addon={fakeAddon}
addon={createInternalAddon(fakeAddon)}
i18n={getFakeI18nInst()}
store={store}
{...props}
Expand All @@ -37,19 +37,19 @@ describe(__filename, () => {
});

it('does renders a link <dt> if links exist', () => {
const addon = {
const addon = createInternalAddon({
...fakeAddon,
homepage: null,
support_url: 'foo.com',
};
});
const root = render({ addon });

expect(root.find('.AddonMoreInfo-links-title'))
.toIncludeText('Add-on Links');
});

it('does not render a link <dt> if no links exist', () => {
const partialAddon = deepcopy(fakeAddon);
const partialAddon = createInternalAddon(fakeAddon);
delete partialAddon.homepage;
delete partialAddon.support_url;
const root = render({ addon: partialAddon });
Expand All @@ -58,15 +58,18 @@ describe(__filename, () => {
});

it('does not render a homepage if none exists', () => {
const partialAddon = deepcopy(fakeAddon);
const partialAddon = createInternalAddon(fakeAddon);
delete partialAddon.homepage;
const root = render({ addon: partialAddon });

expect(root.find('.AddonMoreInfo-homepage-link')).toHaveLength(0);
});

it('renders the homepage of an add-on', () => {
const addon = { ...fakeAddon, homepage: 'http://hamsterdance.com/' };
const addon = createInternalAddon({
...fakeAddon,
homepage: 'http://hamsterdance.com/',
});
const root = render({ addon });
const link = root.find('.AddonMoreInfo-homepage-link');

Expand All @@ -75,18 +78,18 @@ describe(__filename, () => {
});

it('does not render a support link if none exists', () => {
const partialAddon = deepcopy(fakeAddon);
const partialAddon = createInternalAddon(fakeAddon);
delete partialAddon.support_url;
const root = render({ addon: partialAddon });

expect(root.find('.AddonMoreInfo-support-link')).toHaveLength(0);
});

it('renders the support link of an add-on', () => {
const addon = {
const addon = createInternalAddon({
...fakeAddon,
support_url: 'http://support.hampsterdance.com/',
};
});
const root = render({ addon });
const link = root.find('.AddonMoreInfo-support-link');

Expand All @@ -95,26 +98,26 @@ describe(__filename, () => {
});

it('renders the version number of an add-on', () => {
const addon = {
const addon = createInternalAddon({
...fakeAddon,
current_version: {
...fakeAddon.current_version,
version: '2.0.1',
},
};
});
const root = render({ addon });

expect(root.find('.AddonMoreInfo-version')).toHaveText('2.0.1');
});

it('renders the license and link', () => {
const addon = {
const addon = createInternalAddon({
...fakeAddon,
current_version: {
...fakeAddon.current_version,
license: { name: 'tofulicense', url: 'http://license.com/' },
},
};
});
const root = render({ addon });

expect(root.find('.AddonMoreInfo-license-title')).toHaveText('License');
Expand All @@ -125,7 +128,10 @@ describe(__filename, () => {
});

it('does not render a privacy policy if none exists', () => {
const addon = { ...fakeAddon, has_privacy_policy: false };
const addon = createInternalAddon({
...fakeAddon,
has_privacy_policy: false,
});
const root = render({ addon });

expect(root.find('.AddonMoreInfo-privacy-policy-title'))
Expand All @@ -135,7 +141,10 @@ describe(__filename, () => {
});

it('renders the privacy policy and link', () => {
const addon = { ...fakeAddon, has_privacy_policy: true };
const addon = createInternalAddon({
...fakeAddon,
has_privacy_policy: true,
});
const root = render({ addon });

expect(root.find('.AddonMoreInfo-privacy-policy-title'))
Expand All @@ -147,15 +156,15 @@ describe(__filename, () => {
});

it('does not render a EULA if none exists', () => {
const addon = { ...fakeAddon, has_eula: false };
const addon = createInternalAddon({ ...fakeAddon, has_eula: false });
const root = render({ addon });

expect(root.find('.AddonMoreInfo-eula-title')).toHaveLength(0);
expect(root.find('.AddonMoreInfo-eula-link')).toHaveLength(0);
});

it('renders the EULA and link', () => {
const addon = { ...fakeAddon, has_eula: true };
const addon = createInternalAddon({ ...fakeAddon, has_eula: true });
const root = render({ addon });

expect(root.find('.AddonMoreInfo-eula-title'))
Expand All @@ -167,7 +176,7 @@ describe(__filename, () => {
});

it('does not render an add-on ID if none exists', () => {
const partialAddon = { ...fakeAddon };
const partialAddon = createInternalAddon({ ...fakeAddon });
delete partialAddon.id;
const root = render({ addon: partialAddon });

Expand All @@ -176,7 +185,7 @@ describe(__filename, () => {
});

it('renders the ID and title attribute', () => {
const addon = { ...fakeAddon, id: 9001 };
const addon = createInternalAddon({ ...fakeAddon, id: 9001 });
const root = render({ addon });

expect(root.find('.AddonMoreInfo-database-id-title'))
Expand All @@ -188,23 +197,50 @@ describe(__filename, () => {
.toHaveText('9001');
});

it('does not link to stats if user is not author of the add-on', () => {
const authorUserId = 11;
const addon = createInternalAddon({
...fakeAddon,
slug: 'coolio',
authors: [
{
...fakeAddon.authors[0],
id: authorUserId,
name: 'tofumatt',
picture_url: 'http://cdn.a.m.o/myphoto.jpg',
url: 'http://a.m.o/en-GB/firefox/user/tofumatt/',
username: 'tofumatt',
},
],
});
const root = render({
addon,
store: dispatchSignInActions({ userId: 5 }).store,
});

const statsLink = root.find('.AddonMoreInfo-stats-content').find(Link);
expect(statsLink).toHaveLength(0);
});

it('links to stats if add-on author is viewing the page', () => {
const addon = {
const authorUserId = 11;
const addon = createInternalAddon({
...fakeAddon,
slug: 'coolio',
authors: [
{
id: 11,
...fakeAddon.authors[0],
id: authorUserId,
name: 'tofumatt',
picture_url: 'http://cdn.a.m.o/myphoto.jpg',
url: 'http://a.m.o/en-GB/firefox/user/tofumatt/',
username: 'tofumatt',
},
],
};
});
const root = render({
addon,
store: dispatchSignInActions({ userId: 11 }).store,
store: dispatchSignInActions({ userId: authorUserId }).store,
});

const statsLink = root.find('.AddonMoreInfo-stats-content').find(Link);
Expand Down

0 comments on commit 9247b29

Please sign in to comment.