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
67 changes: 61 additions & 6 deletions translate/src/hooks/useTranslator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,28 @@ import { useTranslator } from './useTranslator';
import { vi } from 'vitest';

vi.spyOn(Hooks, 'useAppSelector');

const ctx = vi.hoisted(() => ({ code: 'mylocale', entity: { project: {} } }));
vi.mock('react', async (importOriginal) => {
const actual = await importOriginal();
return { ...actual, useContext: () => ({ code: 'mylocale' }) };
return { ...actual, useContext: () => ctx };
});

beforeEach(() => {
ctx.entity = { project: {} };
});

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

const fakeSelector = (user) => (sel) =>
sel({
[PROJECT]: { slug: 'myproject' },
[USER]: user,
});
const fakeSelector =
(user, slug = 'myproject') =>
(sel) =>
sel({
[PROJECT]: { slug },
[USER]: user,
});

describe('useTranslator', () => {
it('returns false for non-authenticated users', () => {
Expand Down Expand Up @@ -64,4 +72,51 @@ describe('useTranslator', () => {
);
expect(useTranslator()).toBeTruthy();
});

it('returns true for a project the user translates, in the All Projects view', () => {
ctx.entity = { project: { slug: 'myproject' } };
Hooks.useAppSelector.mockImplementation(
fakeSelector(
{
isAuthenticated: true,
canManageLocales: [],
canTranslateLocales: [],
translatorForProjects: { 'mylocale-myproject': true },
},
'all-projects',
),
);
expect(useTranslator()).toBeTruthy();
});

it('uses the given entity rather than the selected one', () => {
ctx.entity = { project: { slug: 'myproject' } };
Hooks.useAppSelector.mockImplementation(
fakeSelector(
{
isAuthenticated: true,
canManageLocales: [],
canTranslateLocales: [],
translatorForProjects: { 'mylocale-myproject': true },
},
'all-projects',
),
);
expect(useTranslator({ project: { slug: 'otherproject' } })).toBeFalsy();
});

it('falls back to the locale permission with no string in hand', () => {
Hooks.useAppSelector.mockImplementation(
fakeSelector(
{
isAuthenticated: true,
canManageLocales: [],
canTranslateLocales: ['mylocale'],
translatorForProjects: { 'mylocale-myproject': false },
},
'all-projects',
),
);
expect(useTranslator()).toBeTruthy();
});
});
12 changes: 10 additions & 2 deletions translate/src/hooks/useTranslator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useContext } from 'react';

import type { Entity } from '~/api/entity';
import { EntityView } from '~/context/EntityView';
import { Locale } from '~/context/Locale';
import { useProject } from '~/modules/project';
import { USER } from '~/modules/user';
Expand All @@ -8,10 +10,13 @@ import { useAppSelector } from '~/hooks';
/**
* Return true if the user has translator permission for the current project
* and locale.
*
* @param entity Used in the All Projects view to check the permission.
*/
export function useTranslator(): boolean {
export function useTranslator(entity?: Entity): boolean {
const { code } = useContext(Locale);
const { slug } = useProject();
const selected = useContext(EntityView).entity;
const {
isAuthenticated,
canManageLocales,
Expand All @@ -27,7 +32,10 @@ export function useTranslator(): boolean {
return true;
}

const localeProject = `${code}-${slug}`;
const projectSlug =
slug === 'all-projects' ? (entity ?? selected).project.slug : slug;

const localeProject = `${code}-${projectSlug}`;
if (Object.hasOwnProperty.call(translatorForProjects, localeProject)) {
return translatorForProjects[localeProject];
}
Expand Down
2 changes: 1 addition & 1 deletion translate/src/modules/entitieslist/components/Entity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function Entity({
selectEntity,
toggleForBatchEditing,
}: Props): React.ReactElement<'li'> {
const isTranslator = useTranslator();
const isTranslator = useTranslator(entity);
const [areSiblingsActive, setSiblingsActive] = useState(false);
const entitiesList = useContext(EntitiesList);

Expand Down
Loading