@@ -5,14 +5,14 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
55import { MemoryRouter } from 'react-router-dom' ;
66import { QualityVoter } from '../src/components/QualityVoter' ;
77
8- function renderVoter ( props : { hash : string ; score ?: number | null } ) {
8+ function renderVoter ( props : { hash : string ; score ?: number | null ; compact ?: boolean } ) {
99 const queryClient = new QueryClient ( {
1010 defaultOptions : { queries : { retry : false } } ,
1111 } ) ;
1212 return render (
1313 < QueryClientProvider client = { queryClient } >
1414 < MemoryRouter >
15- < QualityVoter hash = { props . hash } score = { props . score } />
15+ < QualityVoter hash = { props . hash } score = { props . score } compact = { props . compact } />
1616 </ MemoryRouter >
1717 </ QueryClientProvider >
1818 ) ;
@@ -23,56 +23,37 @@ beforeEach(() => {
2323} ) ;
2424
2525describe ( 'QualityVoter' , ( ) => {
26- it ( 'affiche le score via QualityIndicator' , ( ) => {
27- renderVoter ( { hash : 'hash_aaa' , score : 0.85 } ) ;
28- expect ( screen . getByText ( '85%' ) ) . toBeDefined ( ) ;
26+ it ( 'affiche 5 boutons etoiles' , ( ) => {
27+ renderVoter ( { hash : 'hash_aaa' , score : 0.6 } ) ;
28+ const buttons = screen . getAllByRole ( 'button' ) ;
29+ expect ( buttons ) . toHaveLength ( 5 ) ;
2930 } ) ;
3031
31- it ( 'affiche -- quand le score est null' , ( ) => {
32+ it ( 'affiche le score N/A quand score est null' , ( ) => {
3233 renderVoter ( { hash : 'hash_aaa' , score : null } ) ;
33- expect ( screen . getByText ( '-- ' ) ) . toBeDefined ( ) ;
34+ expect ( screen . getByText ( 'N/A ' ) ) . toBeDefined ( ) ;
3435 } ) ;
3536
36- it ( 'affiche les boutons vote up et vote down' , ( ) => {
37- renderVoter ( { hash : 'hash_aaa' , score : 0.5 } ) ;
38- expect ( screen . getByRole ( 'button' , { name : / v o t e u p / i } ) ) . toBeDefined ( ) ;
39- expect ( screen . getByRole ( 'button' , { name : / v o t e d o w n / i } ) ) . toBeDefined ( ) ;
37+ it ( 'affiche le score en etoiles (0.6 = 3/5)' , ( ) => {
38+ renderVoter ( { hash : 'hash_aaa' , score : 0.6 } ) ;
39+ expect ( screen . getByText ( '3/5' ) ) . toBeDefined ( ) ;
4040 } ) ;
4141
42- it ( 'appelle POST /api/memories/:hash/rate avec up au clic sur vote up' , async ( ) => {
43- const fetchSpy = vi . spyOn ( globalThis , 'fetch' ) . mockResolvedValue ( {
44- ok : true ,
45- json : ( ) => Promise . resolve ( { quality_score : 0.9 } ) ,
46- } as Response ) ;
47-
48- renderVoter ( { hash : 'hash_aaa' , score : 0.5 } ) ;
49-
50- const user = userEvent . setup ( ) ;
51- await user . click ( screen . getByRole ( 'button' , { name : / v o t e u p / i } ) ) ;
52-
53- await waitFor ( ( ) => {
54- const calls = fetchSpy . mock . calls ;
55- const rateCall = calls . find ( ( c ) => {
56- const url = typeof c [ 0 ] === 'string' ? c [ 0 ] : ( c [ 0 ] as Request ) . url ;
57- return url . includes ( '/api/memories/hash_aaa/rate' ) ;
58- } ) ;
59- expect ( rateCall ) . toBeDefined ( ) ;
60- const opts = rateCall ! [ 1 ] as RequestInit ;
61- expect ( opts . method ) . toBe ( 'POST' ) ;
62- expect ( JSON . parse ( opts . body as string ) ) . toEqual ( { vote : 'up' } ) ;
63- } ) ;
42+ it ( 'affiche le score en etoiles (1.0 = 5/5)' , ( ) => {
43+ renderVoter ( { hash : 'hash_aaa' , score : 1.0 } ) ;
44+ expect ( screen . getByText ( '5/5' ) ) . toBeDefined ( ) ;
6445 } ) ;
6546
66- it ( 'appelle POST /api/memories/:hash/rate avec down au clic sur vote down ' , async ( ) => {
47+ it ( 'envoie score 0.6 au clic sur la 3e etoile ' , async ( ) => {
6748 const fetchSpy = vi . spyOn ( globalThis , 'fetch' ) . mockResolvedValue ( {
6849 ok : true ,
69- json : ( ) => Promise . resolve ( { quality_score : 0.3 } ) ,
50+ json : ( ) => Promise . resolve ( { quality_score : 0.6 } ) ,
7051 } as Response ) ;
7152
72- renderVoter ( { hash : 'hash_aaa' , score : 0.5 } ) ;
53+ renderVoter ( { hash : 'hash_aaa' , score : 0.4 } ) ;
7354
7455 const user = userEvent . setup ( ) ;
75- await user . click ( screen . getByRole ( 'button' , { name : / v o t e d o w n / i } ) ) ;
56+ await user . click ( screen . getByRole ( 'button' , { name : / 3 e t o i l e s / i } ) ) ;
7657
7758 await waitFor ( ( ) => {
7859 const calls = fetchSpy . mock . calls ;
@@ -83,7 +64,12 @@ describe('QualityVoter', () => {
8364 expect ( rateCall ) . toBeDefined ( ) ;
8465 const opts = rateCall ! [ 1 ] as RequestInit ;
8566 expect ( opts . method ) . toBe ( 'POST' ) ;
86- expect ( JSON . parse ( opts . body as string ) ) . toEqual ( { vote : 'down' } ) ;
67+ expect ( JSON . parse ( opts . body as string ) ) . toEqual ( { score : 0.6 } ) ;
8768 } ) ;
8869 } ) ;
70+
71+ it ( 'masque le label score en mode compact' , ( ) => {
72+ renderVoter ( { hash : 'hash_aaa' , score : 0.8 , compact : true } ) ;
73+ expect ( screen . queryByText ( '4/5' ) ) . toBeNull ( ) ;
74+ } ) ;
8975} ) ;
0 commit comments