@@ -6,20 +6,28 @@ import { useTranslator } from './useTranslator';
66import { vi } from 'vitest' ;
77
88vi . spyOn ( Hooks , 'useAppSelector' ) ;
9+
10+ const ctx = vi . hoisted ( ( ) => ( { code : 'mylocale' , entity : { project : { } } } ) ) ;
911vi . mock ( 'react' , async ( importOriginal ) => {
1012 const actual = await importOriginal ( ) ;
11- return { ...actual , useContext : ( ) => ( { code : 'mylocale' } ) } ;
13+ return { ...actual , useContext : ( ) => ctx } ;
14+ } ) ;
15+
16+ beforeEach ( ( ) => {
17+ ctx . entity = { project : { } } ;
1218} ) ;
1319
1420afterAll ( ( ) => {
1521 vi . restoreAllMocks ( ) ;
1622} ) ;
1723
18- const fakeSelector = ( user ) => ( sel ) =>
19- sel ( {
20- [ PROJECT ] : { slug : 'myproject' } ,
21- [ USER ] : user ,
22- } ) ;
24+ const fakeSelector =
25+ ( user , slug = 'myproject' ) =>
26+ ( sel ) =>
27+ sel ( {
28+ [ PROJECT ] : { slug } ,
29+ [ USER ] : user ,
30+ } ) ;
2331
2432describe ( 'useTranslator' , ( ) => {
2533 it ( 'returns false for non-authenticated users' , ( ) => {
@@ -64,4 +72,51 @@ describe('useTranslator', () => {
6472 ) ;
6573 expect ( useTranslator ( ) ) . toBeTruthy ( ) ;
6674 } ) ;
75+
76+ it ( 'returns true for a project the user translates, in the All Projects view' , ( ) => {
77+ ctx . entity = { project : { slug : 'myproject' } } ;
78+ Hooks . useAppSelector . mockImplementation (
79+ fakeSelector (
80+ {
81+ isAuthenticated : true ,
82+ canManageLocales : [ ] ,
83+ canTranslateLocales : [ ] ,
84+ translatorForProjects : { 'mylocale-myproject' : true } ,
85+ } ,
86+ 'all-projects' ,
87+ ) ,
88+ ) ;
89+ expect ( useTranslator ( ) ) . toBeTruthy ( ) ;
90+ } ) ;
91+
92+ it ( 'uses the given entity rather than the selected one' , ( ) => {
93+ ctx . entity = { project : { slug : 'myproject' } } ;
94+ Hooks . useAppSelector . mockImplementation (
95+ fakeSelector (
96+ {
97+ isAuthenticated : true ,
98+ canManageLocales : [ ] ,
99+ canTranslateLocales : [ ] ,
100+ translatorForProjects : { 'mylocale-myproject' : true } ,
101+ } ,
102+ 'all-projects' ,
103+ ) ,
104+ ) ;
105+ expect ( useTranslator ( { project : { slug : 'otherproject' } } ) ) . toBeFalsy ( ) ;
106+ } ) ;
107+
108+ it ( 'falls back to the locale permission with no string in hand' , ( ) => {
109+ Hooks . useAppSelector . mockImplementation (
110+ fakeSelector (
111+ {
112+ isAuthenticated : true ,
113+ canManageLocales : [ ] ,
114+ canTranslateLocales : [ 'mylocale' ] ,
115+ translatorForProjects : { 'mylocale-myproject' : false } ,
116+ } ,
117+ 'all-projects' ,
118+ ) ,
119+ ) ;
120+ expect ( useTranslator ( ) ) . toBeTruthy ( ) ;
121+ } ) ;
67122} ) ;
0 commit comments