Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
diox committed Oct 28, 2024
1 parent 0392b31 commit c575f51
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 72 deletions.
5 changes: 3 additions & 2 deletions src/amo/pages/Block/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ export class BlockBase extends React.Component<InternalProps> {
}

const isSoftBlocked =
block?.soft_blocked.includes(match.params.versionId) ||
(block?.soft_blocked.length && !block.blocked.length);
block?.soft_blocked.length &&
(block?.soft_blocked.includes(match.params.versionId) ||
!block?.blocked.length);
let title;
if (isSoftBlocked) {
title =
Expand Down
141 changes: 71 additions & 70 deletions tests/unit/amo/pages/TestBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
createFakeBlockResult,
createLocalizedString,
dispatchClientMetadata,
fakeI18n,
getElement,
renderPage as defaultRender,
screen,
Expand Down Expand Up @@ -129,16 +128,14 @@ describe(__filename, () => {
expect(
within(screen.getByClassName('Block-reason')).getAllByRole('alert'),
).toHaveLength(1);
// 1. versions blocked
// 2. date and URL
expect(
within(screen.getByClassName('Block-metadata')).getAllByRole('alert'),
).toHaveLength(2);
).toHaveLength(1);
});

it('renders a generic header/title when the block has no add-on name', async () => {
const block = _createFakeBlockResult({ addonName: null });
const title = 'This add-on has been blocked for your protection.';
const title = 'This add-on is blocked for violating Mozilla policies.';
store.dispatch(loadBlock({ block }));
render();

Expand All @@ -155,7 +152,7 @@ describe(__filename, () => {
const block = _createFakeBlockResult({
addonName: createLocalizedString(name),
});
const title = `${name} has been blocked for your protection.`;
const title = `${name} is blocked for violating Mozilla policies.`;
store.dispatch(loadBlock({ block }));
render();

Expand All @@ -168,85 +165,40 @@ describe(__filename, () => {
expect(screen.getByText(title)).toBeInTheDocument();
});

it('renders a paragraph with the reason when the block has one', () => {
const reason = 'this is a reason for a block';
const block = _createFakeBlockResult({ reason });
store.dispatch(loadBlock({ block }));
render();

expect(screen.getByText(reason)).toBeInTheDocument();
expect(screen.getByText(reason)).toHaveAttribute('lang', 'en-US');
});

it('does not render a reason if the block does not have one', () => {
const block = _createFakeBlockResult({ reason: null });
store.dispatch(loadBlock({ block }));
render();

expect(screen.queryByClassName('Block-reason')).not.toBeInTheDocument();
});

it('renders "all versions" when "is_all_versions" is true', () => {
it('renders a generic soft-block header/title when the block has no add-on name', async () => {
const block = _createFakeBlockResult({
is_all_versions: true,
addonName: null,
soft_blocked: ['42.0'],
blocked: [],
});
const i18n = fakeI18n();
const title = 'This add-on is restricted for violating Mozilla policies.';
store.dispatch(loadBlock({ block }));
render();

// The version info and the block date are inside the same tag, separated
// by a </br>.
expect(
screen.getByTextAcrossTags(
`Versions blocked: all versions.Blocked on ${i18n
.moment(block.created)
.format('ll')}.`,
await waitFor(() =>
expect(getElement('title')).toHaveTextContent(
`${title} – Add-ons for Firefox (${lang})`,
),
).toBeInTheDocument();
);
expect(screen.getByText(title)).toBeInTheDocument();
});

it('renders the versions if "is_all_versions" is false', () => {
const v1 = '12';
const v2 = '34';
const block = _createFakeBlockResult({
versions: [v1, v2],
is_all_versions: false,
});
const i18n = fakeI18n();
it('renders a paragraph with the reason when the block has one', () => {
const reason = 'this is a reason for a block';
const block = _createFakeBlockResult({ reason });
store.dispatch(loadBlock({ block }));
render();

// The version info and the block date are inside the same tag, separated
// by a </br>.
expect(
screen.getByTextAcrossTags(
`Versions blocked: ${v1}, ${v2}.Blocked on ${i18n
.moment(block.created)
.format('ll')}.`,
),
).toBeInTheDocument();
expect(screen.getByText(reason)).toBeInTheDocument();
expect(screen.getByText(reason)).toHaveAttribute('lang', 'en-US');
});

it('renders the versions if "is_all_versions" is missing', () => {
const v1 = '12';
const v2 = '34';
const block = _createFakeBlockResult({
versions: [v1, v2],
is_all_versions: undefined,
});
const i18n = fakeI18n();
it('does not render a reason if the block does not have one', () => {
const block = _createFakeBlockResult({ reason: null });
store.dispatch(loadBlock({ block }));
render();

// The version info and the block date are inside the same tag, separated
// by a </br>.
expect(
screen.getByTextAcrossTags(
`Versions blocked: ${v1}, ${v2}.Blocked on ${i18n
.moment(block.created)
.format('ll')}.`,
),
).toBeInTheDocument();
expect(screen.queryByClassName('Block-reason')).not.toBeInTheDocument();
});

it('renders the reason with HTML tags removed', () => {
Expand Down Expand Up @@ -307,7 +259,56 @@ describe(__filename, () => {
});

expect(
screen.getByText(`${name} has been blocked for your protection.`),
screen.getByText(`${name} is blocked for violating Mozilla policies.`),
).toBeInTheDocument();
expect(
screen.getByText(
/It will be automatically disabled and no longer usable in Firefox./,
),
).toBeInTheDocument();
});

it('renders a soft-block page when the block has only soft-blocks', () => {
const name = 'some-addon-name';
const block = _createFakeBlockResult({
addonName: createLocalizedString(name),
soft_blocked: ['42.0'],
blocked: [],
});
store.dispatch(loadBlock({ block }));

render();

expect(
screen.getByText(`${name} is restricted for violating Mozilla policies.`),
).toBeInTheDocument();
expect(
screen.getByText(
/They may choose to enable the add-on again at their own risk./,
),
).toBeInTheDocument();
});

it('renders a soft-block page when a soft-blocked versionId is present in the URL', () => {
const name = 'some-addon-name';
const block = _createFakeBlockResult({
addonName: createLocalizedString(name),
soft_blocked: ['42.0'],
});
store.dispatch(loadBlock({ block }));

defaultRender({
initialEntries: [`${getLocation()}42.0/`],
store,
});

expect(
screen.getByText(`${name} is restricted for violating Mozilla policies.`),
).toBeInTheDocument();
expect(
screen.getByText(
/They may choose to enable the add-on again at their own risk./,
),
).toBeInTheDocument();
});

Expand Down
2 changes: 2 additions & 0 deletions tests/unit/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -1313,6 +1313,8 @@ export const createFakeBlockResult = ({
modified: '2020-01-22T10:09:01Z',
guid,
versions: ['0.1', '4.56'],
blocked: ['0.1', '4.56'],
soft_blocked: [],
is_all_versions: false,
addon_name: addonName,
reason,
Expand Down

0 comments on commit c575f51

Please sign in to comment.