1+ import React from 'react' ;
2+ import { render , cleanup } from '@testing-library/react' ;
3+ import '@testing-library/jest-dom' ;
4+ import ProgressOverlay from 'src/components/QuestionsDisplay/ProgressOverlay/ProgressOverlay' ;
5+
6+ // Test constants
7+ const TEST_PERCENTAGES = {
8+ ZERO : 0 ,
9+ QUARTER : 25 ,
10+ THIRD : 33.33 ,
11+ HALF : 50 ,
12+ THREE_QUARTERS : 75 ,
13+ FULL : 100 ,
14+ SIXTY : 60
15+ } as const ;
16+
17+ const COLOR_CLASSES = {
18+ CORRECT : 'progress-overlay-correct' ,
19+ INCORRECT : 'progress-overlay-incorrect'
20+ } as const ;
21+
22+ describe ( 'ProgressOverlay Component' , ( ) => {
23+ afterEach ( ( ) => {
24+ cleanup ( ) ;
25+ } ) ;
26+
27+ test ( 'should render when show is true' , ( ) => {
28+ const { container } = render ( < ProgressOverlay percentage = { TEST_PERCENTAGES . HALF } show = { true } /> ) ;
29+
30+ const overlay = container . firstChild as HTMLElement ;
31+ expect ( overlay ) . toBeInTheDocument ( ) ;
32+ expect ( overlay ) . toHaveClass ( 'MuiBox-root' ) ;
33+ } ) ;
34+
35+ test ( 'should render with different percentages' , ( ) => {
36+ const percentages = [ TEST_PERCENTAGES . THREE_QUARTERS , TEST_PERCENTAGES . QUARTER , TEST_PERCENTAGES . ZERO , TEST_PERCENTAGES . FULL ] ;
37+
38+ percentages . forEach ( percentage => {
39+ const { container } = render ( < ProgressOverlay percentage = { percentage } show = { true } /> ) ;
40+ const overlay = container . firstChild as HTMLElement ;
41+ expect ( overlay ) . toBeInTheDocument ( ) ;
42+ cleanup ( ) ;
43+ } ) ;
44+ } ) ;
45+
46+ test ( 'should render with different colors' , ( ) => {
47+ const { container : container1 } = render (
48+ < ProgressOverlay percentage = { TEST_PERCENTAGES . HALF } show = { true } colorClass = { COLOR_CLASSES . CORRECT } />
49+ ) ;
50+ const overlay1 = container1 . firstChild as HTMLElement ;
51+ expect ( overlay1 ) . toBeInTheDocument ( ) ;
52+ expect ( overlay1 ) . toHaveClass ( COLOR_CLASSES . CORRECT ) ;
53+ cleanup ( ) ;
54+
55+ const { container : container2 } = render (
56+ < ProgressOverlay percentage = { TEST_PERCENTAGES . HALF } show = { true } colorClass = { COLOR_CLASSES . INCORRECT } />
57+ ) ;
58+ const overlay2 = container2 . firstChild as HTMLElement ;
59+ expect ( overlay2 ) . toBeInTheDocument ( ) ;
60+ expect ( overlay2 ) . toHaveClass ( COLOR_CLASSES . INCORRECT ) ;
61+ } ) ;
62+
63+ test ( 'should not render when show is false' , ( ) => {
64+ const { container } = render ( < ProgressOverlay percentage = { TEST_PERCENTAGES . HALF } show = { false } /> ) ;
65+
66+ expect ( container . firstChild ) . toBeNull ( ) ;
67+ } ) ;
68+
69+ test ( 'should render as MUI Box component' , ( ) => {
70+ const { container } = render ( < ProgressOverlay percentage = { TEST_PERCENTAGES . SIXTY } show = { true } /> ) ;
71+
72+ const overlay = container . firstChild as HTMLElement ;
73+ expect ( overlay ) . toBeInTheDocument ( ) ;
74+ expect ( overlay ) . toHaveClass ( 'MuiBox-root' ) ;
75+ } ) ;
76+
77+ test ( 'should handle prop changes correctly' , ( ) => {
78+ const TestWrapper = ( {
79+ percentage = TEST_PERCENTAGES . HALF ,
80+ show = true ,
81+ colorClass
82+ } : {
83+ percentage ?: number ;
84+ show ?: boolean ;
85+ colorClass ?: string ;
86+ } ) => (
87+ < div data-testid = "wrapper" >
88+ < ProgressOverlay percentage = { percentage } show = { show } colorClass = { colorClass } />
89+ </ div >
90+ ) ;
91+
92+ const { rerender, getByTestId } = render ( < TestWrapper percentage = { TEST_PERCENTAGES . HALF } /> ) ;
93+ let wrapper = getByTestId ( 'wrapper' ) ;
94+ let overlay = wrapper . firstChild as HTMLElement ;
95+ expect ( overlay ) . toHaveClass ( 'MuiBox-root' ) ;
96+
97+ // Test show=false
98+ rerender ( < TestWrapper percentage = { TEST_PERCENTAGES . HALF } show = { false } /> ) ;
99+ wrapper = getByTestId ( 'wrapper' ) ;
100+ expect ( wrapper . firstChild ) . toBeNull ( ) ;
101+
102+ // Test show=true again
103+ rerender ( < TestWrapper percentage = { TEST_PERCENTAGES . THREE_QUARTERS } show = { true } /> ) ;
104+ wrapper = getByTestId ( 'wrapper' ) ;
105+ overlay = wrapper . firstChild as HTMLElement ;
106+ expect ( overlay ) . toHaveClass ( 'MuiBox-root' ) ;
107+
108+ // Test with colorClass
109+ rerender ( < TestWrapper percentage = { TEST_PERCENTAGES . THREE_QUARTERS } show = { true } colorClass = { COLOR_CLASSES . CORRECT } /> ) ;
110+ wrapper = getByTestId ( 'wrapper' ) ;
111+ overlay = wrapper . firstChild as HTMLElement ;
112+ expect ( overlay ) . toHaveClass ( 'MuiBox-root' ) ;
113+ expect ( overlay ) . toHaveClass ( COLOR_CLASSES . CORRECT ) ;
114+ } ) ;
115+
116+ test ( 'should work with edge case values' , ( ) => {
117+ const edgeCases = [ TEST_PERCENTAGES . ZERO , TEST_PERCENTAGES . FULL , TEST_PERCENTAGES . THIRD ] ;
118+
119+ edgeCases . forEach ( percentage => {
120+ const { container } = render ( < ProgressOverlay percentage = { percentage } show = { true } /> ) ;
121+ expect ( container . firstChild ) . toBeInTheDocument ( ) ;
122+ cleanup ( ) ;
123+ } ) ;
124+ } ) ;
125+ } ) ;
0 commit comments