From 027c4b3c2366a9f3bce3659d1323f7d342373c5d Mon Sep 17 00:00:00 2001 From: Ayush Ranjan Date: Wed, 26 Aug 2026 20:43:24 -0400 Subject: [PATCH 1/3] log StringNotFound actions and add tests --- .../components/StringNotFound.test.jsx | 92 ++++++++++++++++++- .../entities/components/StringNotFound.tsx | 56 ++++++++--- 2 files changed, 133 insertions(+), 15 deletions(-) diff --git a/translate/src/modules/entities/components/StringNotFound.test.jsx b/translate/src/modules/entities/components/StringNotFound.test.jsx index 6583376d3a..9c5818ad48 100644 --- a/translate/src/modules/entities/components/StringNotFound.test.jsx +++ b/translate/src/modules/entities/components/StringNotFound.test.jsx @@ -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'; @@ -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, @@ -49,6 +58,18 @@ function mount( } describe('', () => { + 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); @@ -116,4 +137,73 @@ describe('', () => { 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', + { + all_projects: false, + same_project: false, + same_resource: true, + filtered_out: false, + }, + ); + }); + + it('logs a UX action for each of the two panel actions', () => { + const { getByRole } = mount(ENTITY_LOCATION, undefined, { + authenticated: true, + }); + + fireEvent.click(getByRole('link', { name: 'Show the string' })); + expect(mockLogUXAction).toHaveBeenLastCalledWith( + 'Click: String not found, go to string', + 'String Not Found 1.0', + expect.anything(), + ); + + fireEvent.click(getByRole('link', { name: 'Keep the parameters' })); + expect(mockLogUXAction).toHaveBeenLastCalledWith( + 'Click: String not found, show matching', + 'String Not Found 1.0', + expect.anything(), + ); + }); + + it('logs why the string was filtered out', () => { + mount( + { + pk: 99, + project: 'firefox', + project_name: 'Firefox', + resource: 'foo.ftl', + filters: ['missing'], + }, + '/kg/firefox/all-resources/?status=warnings&string=99', + { authenticated: true }, + ); + + expect(mockLogUXAction).toHaveBeenCalledWith( + 'Render: String not found', + 'String Not Found 1.0', + { + all_projects: false, + same_project: true, + same_resource: true, + filtered_out: true, + }, + ); + }); + + it('logs nothing for anonymous users', () => { + const { getByRole } = mount(ENTITY_LOCATION); + + fireEvent.click(getByRole('link', { name: 'Show the string' })); + + expect(mockLogUXAction).not.toHaveBeenCalled(); + }); }); diff --git a/translate/src/modules/entities/components/StringNotFound.tsx b/translate/src/modules/entities/components/StringNotFound.tsx index 211de440ff..eaf308f728 100644 --- a/translate/src/modules/entities/components/StringNotFound.tsx +++ b/translate/src/modules/entities/components/StringNotFound.tsx @@ -1,13 +1,21 @@ 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'; +const UX_EXPERIMENT = 'String Not Found 1.0'; +const UX_RENDER = 'Render: String not found'; +const UX_GO_TO_STRING = 'Click: String not found, go to string'; +const UX_SHOW_MATCHING = 'Click: String not found, show matching'; + function Detail({ labelId, children, @@ -38,20 +46,36 @@ export function StringNotFound({ const locale = useContext(Locale); const { name: viewProjectName } = useProject(); const { l10n } = useLocalization(); + const isAuthUser = useAppSelector((state) => state[USER].isAuthenticated); - 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 sameProject = entityLocation?.project === location.project; const sameResource = - allResources || entityLocation.resource === location.resource; + allResources || entityLocation?.resource === location.resource; const filteredOut = allProjects || (sameProject && sameResource); + const uxData = { + all_projects: allProjects, + same_project: sameProject, + same_resource: sameResource, + filtered_out: filteredOut, + }; + + const requestedPk = entityLocation?.pk; + useEffect(() => { + if (isAuthUser && requestedPk) { + logUXAction(UX_RENDER, UX_EXPERIMENT, uxData); + } + }, [isAuthUser, requestedPk]); + + if (!entityLocation) { + return null; + } + + const { push } = location; + const goToString = () => push({ ...emptyParams, @@ -67,10 +91,14 @@ export function StringNotFound({ showMatchingUrl.searchParams.delete('string'); const showMatchingHref = showMatchingUrl.pathname + showMatchingUrl.search; - const onClick = (navigate: () => void) => (ev: React.MouseEvent) => { - ev.preventDefault(); - navigate(); - }; + const onClick = + (actionType: string, navigate: () => void) => (ev: React.MouseEvent) => { + ev.preventDefault(); + if (isAuthUser) { + logUXAction(actionType, UX_EXPERIMENT, uxData); + } + navigate(); + }; const requestProject = allProjects ? l10n.getString( @@ -114,7 +142,7 @@ export function StringNotFound({ className='primary' href={goToStringHref} title={goToStringHref} - onClick={onClick(goToString)} + onClick={onClick(UX_GO_TO_STRING, goToString)} /> @@ -122,7 +150,7 @@ export function StringNotFound({ className='secondary' href={showMatchingHref} title={showMatchingHref} - onClick={onClick(showMatching)} + onClick={onClick(UX_SHOW_MATCHING, showMatching)} /> From 833fc8655c66afe6cffec1a4c3c5672e591f9197 Mon Sep 17 00:00:00 2001 From: Ayush Ranjan Date: Thu, 27 Aug 2026 19:12:53 -0400 Subject: [PATCH 2/3] Fix testing bug and refactor code --- .../components/StringNotFound.test.jsx | 60 +++++++++---------- .../entities/components/StringNotFound.tsx | 49 +++++++-------- 2 files changed, 50 insertions(+), 59 deletions(-) diff --git a/translate/src/modules/entities/components/StringNotFound.test.jsx b/translate/src/modules/entities/components/StringNotFound.test.jsx index 9c5818ad48..0df052a32f 100644 --- a/translate/src/modules/entities/components/StringNotFound.test.jsx +++ b/translate/src/modules/entities/components/StringNotFound.test.jsx @@ -19,6 +19,12 @@ const ENTITY_LOCATION = { filters: [], }; +const UX_DATA = { + all_projects: false, + same_project: false, + same_resource: true, +}; + const FTL = ` entities-StringNotFound--title = String not found entities-StringNotFound--description = doesn’t match @@ -145,36 +151,31 @@ describe('', () => { expect(mockLogUXAction).toHaveBeenCalledWith( 'Render: String not found', 'String Not Found 1.0', - { - all_projects: false, - same_project: false, - same_resource: true, - filtered_out: false, - }, + UX_DATA, ); }); - it('logs a UX action for each of the two panel actions', () => { - const { getByRole } = mount(ENTITY_LOCATION, undefined, { - authenticated: true, - }); - - fireEvent.click(getByRole('link', { name: 'Show the string' })); - expect(mockLogUXAction).toHaveBeenLastCalledWith( - 'Click: String not found, go to string', - 'String Not Found 1.0', - expect.anything(), - ); - - fireEvent.click(getByRole('link', { name: 'Keep the parameters' })); - expect(mockLogUXAction).toHaveBeenLastCalledWith( - 'Click: String not found, show matching', - 'String Not Found 1.0', - expect.anything(), - ); - }); + it.each([ + ['Show the string', 'Click: String not found, go to string'], + ['Keep the parameters', 'Click: String not found, show matching'], + ])( + 'logs %s with the dimensions of 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', + UX_DATA, + ); + }, + ); - it('logs why the string was filtered out', () => { + it('logs a string hidden by filters as living in the current view', () => { mount( { pk: 99, @@ -190,12 +191,7 @@ describe('', () => { expect(mockLogUXAction).toHaveBeenCalledWith( 'Render: String not found', 'String Not Found 1.0', - { - all_projects: false, - same_project: true, - same_resource: true, - filtered_out: true, - }, + { all_projects: false, same_project: true, same_resource: true }, ); }); diff --git a/translate/src/modules/entities/components/StringNotFound.tsx b/translate/src/modules/entities/components/StringNotFound.tsx index eaf308f728..3056da31bd 100644 --- a/translate/src/modules/entities/components/StringNotFound.tsx +++ b/translate/src/modules/entities/components/StringNotFound.tsx @@ -11,11 +11,6 @@ import { USER } from '~/modules/user'; import './StringNotFound.css'; -const UX_EXPERIMENT = 'String Not Found 1.0'; -const UX_RENDER = 'Render: String not found'; -const UX_GO_TO_STRING = 'Click: String not found, go to string'; -const UX_SHOW_MATCHING = 'Click: String not found, show matching'; - function Detail({ labelId, children, @@ -56,19 +51,21 @@ export function StringNotFound({ allResources || entityLocation?.resource === location.resource; const filteredOut = allProjects || (sameProject && sameResource); - const uxData = { - all_projects: allProjects, - same_project: sameProject, - same_resource: sameResource, - filtered_out: filteredOut, + const logAction = (action: string) => { + if (isAuthUser) { + logUXAction(action, 'String Not Found 1.0', { + all_projects: allProjects, + same_project: sameProject, + same_resource: sameResource, + }); + } }; - const requestedPk = entityLocation?.pk; useEffect(() => { - if (isAuthUser && requestedPk) { - logUXAction(UX_RENDER, UX_EXPERIMENT, uxData); + if (entityLocation) { + logAction('Render: String not found'); } - }, [isAuthUser, requestedPk]); + }, [isAuthUser, entityLocation?.pk]); if (!entityLocation) { return null; @@ -76,30 +73,28 @@ export function StringNotFound({ const { push } = location; - 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 = - (actionType: string, navigate: () => void) => (ev: React.MouseEvent) => { - ev.preventDefault(); - if (isAuthUser) { - logUXAction(actionType, UX_EXPERIMENT, uxData); - } - navigate(); - }; - const requestProject = allProjects ? l10n.getString( 'entities-StringNotFound--all-projects', @@ -142,7 +137,7 @@ export function StringNotFound({ className='primary' href={goToStringHref} title={goToStringHref} - onClick={onClick(UX_GO_TO_STRING, goToString)} + onClick={goToString} /> @@ -150,7 +145,7 @@ export function StringNotFound({ className='secondary' href={showMatchingHref} title={showMatchingHref} - onClick={onClick(UX_SHOW_MATCHING, showMatching)} + onClick={showMatching} /> From 06530446f34e84d7c37a6bb9ce37cea6124e68f7 Mon Sep 17 00:00:00 2001 From: Ayush Ranjan Date: Tue, 1 Sep 2026 01:04:08 -0400 Subject: [PATCH 3/3] Refactor filteredOut and log enum --- .../components/StringNotFound.test.jsx | 66 +++++++++++-------- .../entities/components/StringNotFound.tsx | 18 ++--- 2 files changed, 49 insertions(+), 35 deletions(-) diff --git a/translate/src/modules/entities/components/StringNotFound.test.jsx b/translate/src/modules/entities/components/StringNotFound.test.jsx index 0df052a32f..39c0b44a33 100644 --- a/translate/src/modules/entities/components/StringNotFound.test.jsx +++ b/translate/src/modules/entities/components/StringNotFound.test.jsx @@ -19,12 +19,6 @@ const ENTITY_LOCATION = { filters: [], }; -const UX_DATA = { - all_projects: false, - same_project: false, - same_resource: true, -}; - const FTL = ` entities-StringNotFound--title = String not found entities-StringNotFound--description = doesn’t match @@ -151,32 +145,29 @@ describe('', () => { expect(mockLogUXAction).toHaveBeenCalledWith( 'Render: String not found', 'String Not Found 1.0', - UX_DATA, + { 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 with the dimensions of 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', - UX_DATA, - ); - }, - ); + ])('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 })); - it('logs a string hidden by filters as living in the current view', () => { - mount( + 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', @@ -188,10 +179,33 @@ describe('', () => { { 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', - { all_projects: false, same_project: true, same_resource: true }, + { panel: 'resource' }, ); }); diff --git a/translate/src/modules/entities/components/StringNotFound.tsx b/translate/src/modules/entities/components/StringNotFound.tsx index 3056da31bd..8932313ab4 100644 --- a/translate/src/modules/entities/components/StringNotFound.tsx +++ b/translate/src/modules/entities/components/StringNotFound.tsx @@ -49,15 +49,15 @@ export function StringNotFound({ const sameProject = entityLocation?.project === location.project; const sameResource = allResources || entityLocation?.resource === location.resource; - const filteredOut = allProjects || (sameProject && sameResource); + 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', { - all_projects: allProjects, - same_project: sameProject, - same_resource: sameResource, - }); + logUXAction(action, 'String Not Found 1.0', { panel }); } }; @@ -119,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 (
@@ -184,13 +184,13 @@ export function StringNotFound({

- {filteredOut && entityLocation.filters.length > 0 ? ( + {panel === 'filters' ? ( {stringFilterList} ) : ( <> - {!sameProject && ( + {panel === 'project' && ( {entityLocation.project_name}