Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 101 additions & 1 deletion translate/src/modules/entities/components/StringNotFound.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { createMemoryHistory } from 'history';
import { fireEvent } from '@testing-library/react';
import { expect, vi } from 'vitest';

import { createReduxStore, mountComponentWithStore } from '~/test/store';
import * as uxaction from '~/api/uxaction';
import {
createDefaultUser,
createReduxStore,
mountComponentWithStore,
} from '~/test/store';

import { StringNotFound } from './StringNotFound';

Expand Down Expand Up @@ -33,11 +38,15 @@ entities-StringNotFound--all-resources = All Resources
function mount(
entityLocation,
url = '/kg/firefox/all-resources/?status=missing&string=99',
{ authenticated = false } = {},
) {
const history = createMemoryHistory({ initialEntries: [url] });
const spy = vi.fn();
history.listen(spy);
const store = createReduxStore();
if (authenticated) {
createDefaultUser(store);
}
const result = mountComponentWithStore(
StringNotFound,
store,
Expand All @@ -49,6 +58,18 @@ function mount(
}

describe('<StringNotFound>', () => {
let mockLogUXAction;

beforeAll(() => {
mockLogUXAction = vi
.spyOn(uxaction, 'logUXAction')
.mockImplementation(() => {});
});

beforeEach(() => mockLogUXAction.mockClear());

afterAll(() => vi.restoreAllMocks());

it('lays out where the requested string lives', () => {
const { getByText } = mount(ENTITY_LOCATION);

Expand Down Expand Up @@ -116,4 +137,83 @@ describe('<StringNotFound>', () => {
const { container } = mount(null);
expect(container).toBeEmptyDOMElement();
});

it('logs a UX action when an authenticated user is shown the panel', () => {
mount(ENTITY_LOCATION, undefined, { authenticated: true });

expect(mockLogUXAction).toHaveBeenCalledTimes(1);
expect(mockLogUXAction).toHaveBeenCalledWith(
'Render: String not found',
'String Not Found 1.0',
{ panel: 'project' },
);
});

it.each([
['Show the string', 'Click: String not found, go to string'],
['Keep the parameters', 'Click: String not found, show matching'],
])('logs %s against the panel it was shown on', (label, action) => {
const { getByRole } = mount(ENTITY_LOCATION, undefined, {
authenticated: true,
});

fireEvent.click(getByRole('link', { name: label }));

expect(mockLogUXAction).toHaveBeenLastCalledWith(
action,
'String Not Found 1.0',
{ panel: 'project' },
);
});

it('logs the filters panel when it names the filters that hid the string', () => {
const { getByText } = mount(
{
pk: 99,
project: 'firefox',
project_name: 'Firefox',
resource: 'foo.ftl',
filters: ['missing'],
},
'/kg/firefox/all-resources/?status=warnings&string=99',
{ authenticated: true },
);

getByText('missing');
expect(mockLogUXAction).toHaveBeenCalledWith(
'Render: String not found',
'String Not Found 1.0',
{ panel: 'filters' },
);
});

it('logs the resource panel when no filter can be named', () => {
const { getByText, queryByText } = mount(
{
pk: 99,
project: 'firefox',
project_name: 'Firefox',
resource: 'foo.ftl',
filters: [],
},
'/kg/firefox/all-resources/?tag=ui&string=99',
{ authenticated: true },
);

getByText('foo.ftl');
expect(queryByText('Firefox')).not.toBeInTheDocument();
expect(mockLogUXAction).toHaveBeenCalledWith(
'Render: String not found',
'String Not Found 1.0',
{ panel: 'resource' },
);
});

it('logs nothing for anonymous users', () => {
const { getByRole } = mount(ENTITY_LOCATION);

fireEvent.click(getByRole('link', { name: 'Show the string' }));

expect(mockLogUXAction).not.toHaveBeenCalled();
});
});
63 changes: 43 additions & 20 deletions translate/src/modules/entities/components/StringNotFound.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Localized, useLocalization } from '@fluent/react';
import React, { useContext } from 'react';
import React, { useContext, useEffect } from 'react';

import type { RequestedEntityLocation } from '~/api/entity';
import { logUXAction } from '~/api/uxaction';
import { Locale } from '~/context/Locale';
import { emptyParams, Location } from '~/context/Location';
import { useAppSelector } from '~/hooks';
import { useProject } from '~/modules/project';
import { USER } from '~/modules/user';

import './StringNotFound.css';

Expand Down Expand Up @@ -38,40 +41,60 @@ export function StringNotFound({
const locale = useContext(Locale);
const { name: viewProjectName } = useProject();
const { l10n } = useLocalization();
const isAuthUser = useAppSelector((state) => state[USER].isAuthenticated);

const allProjects = location.project === 'all-projects';
const allResources =
!location.resource || location.resource === 'all-resources';
const sameProject = entityLocation?.project === location.project;
const sameResource =
allResources || entityLocation?.resource === location.resource;
const stringFilters = entityLocation?.filters ?? [];
const filteredOut =
(allProjects || (sameProject && sameResource)) && stringFilters.length > 0;

const panel = filteredOut ? 'filters' : !sameProject ? 'project' : 'resource';

const logAction = (action: string) => {
if (isAuthUser) {
logUXAction(action, 'String Not Found 1.0', { panel });

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the data is an enum-ish string value, why wrap it inside an object?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest it's because the only consumption is manual queries. This way just seems more self-describing to me in case we want to track this a lot later.

}
};

useEffect(() => {
if (entityLocation) {
logAction('Render: String not found');
}
}, [isAuthUser, entityLocation?.pk]);

if (!entityLocation) {
return null;
}

const { push } = location;
const allProjects = location.project === 'all-projects';
const allResources =
!location.resource || location.resource === 'all-resources';
const sameProject = entityLocation.project === location.project;
const sameResource =
allResources || entityLocation.resource === location.resource;
const filteredOut = allProjects || (sameProject && sameResource);

const goToString = () =>
const goToString = (ev: React.MouseEvent) => {
ev.preventDefault();
logAction('Click: String not found, go to string');
push({
...emptyParams,
project: entityLocation.project,
resource: entityLocation.resource,
entity: entityLocation.pk,
});
};

const showMatching = () => push({ entity: 0 });
const showMatching = (ev: React.MouseEvent) => {
ev.preventDefault();
logAction('Click: String not found, show matching');
push({ entity: 0 });
};

const goToStringHref = `/${location.locale}/${entityLocation.project}/${entityLocation.resource}/?string=${entityLocation.pk}`;
const showMatchingUrl = new URL(window.location.href);
showMatchingUrl.searchParams.delete('string');
const showMatchingHref = showMatchingUrl.pathname + showMatchingUrl.search;

const onClick = (navigate: () => void) => (ev: React.MouseEvent) => {
ev.preventDefault();
navigate();
};

const requestProject = allProjects
? l10n.getString(
'entities-StringNotFound--all-projects',
Expand All @@ -96,7 +119,7 @@ export function StringNotFound({
).flatMap((key) => location[key]?.split(',') ?? []);
const filterList = formatFilters(filters);

const stringFilterList = formatFilters(entityLocation.filters);
const stringFilterList = formatFilters(stringFilters);

return (
<section id='string-not-found'>
Expand All @@ -114,15 +137,15 @@ export function StringNotFound({
className='primary'
href={goToStringHref}
title={goToStringHref}
onClick={onClick(goToString)}
onClick={goToString}
/>
</Localized>
<Localized id='entities-StringNotFound--show-matching'>
<a
className='secondary'
href={showMatchingHref}
title={showMatchingHref}
onClick={onClick(showMatching)}
onClick={showMatching}
/>
</Localized>
</div>
Expand Down Expand Up @@ -161,13 +184,13 @@ export function StringNotFound({
<h3 />
</Localized>
<div className='fields'>
{filteredOut && entityLocation.filters.length > 0 ? (
{panel === 'filters' ? (
<Detail labelId='entities-StringNotFound--label-filters'>
{stringFilterList}
</Detail>
) : (
<>
{!sameProject && (
{panel === 'project' && (
<Detail labelId='entities-StringNotFound--label-project'>
{entityLocation.project_name}
</Detail>
Expand Down