11import { act , render , screen , waitFor } from '@testing-library/react' ;
2- import { useContext , useRef } from 'react' ;
2+ import { useContext } from 'react' ;
33import { ProgressContext , ProgressProvider , useProgress } from '@/components/ProgressProvider' ;
44
55// Mock the problems module
@@ -59,22 +59,22 @@ function TestComponent() {
5959 < div data-testid = "solved-problems" > { Array . from ( solvedProblems ) . join ( ',' ) } </ div >
6060 < div data-testid = "is-solved-1" > { isSolved ( 'problem-1' ) ? 'true' : 'false' } </ div >
6161 < div data-testid = "is-solved-2" > { isSolved ( 'problem-2' ) ? 'true' : 'false' } </ div >
62- < button onClick = { ( ) => markSolved ( 'problem-1' ) } data-testid = "mark-solved-1" >
62+ < button type = "button" onClick = { ( ) => markSolved ( 'problem-1' ) } data-testid = "mark-solved-1" >
6363 Mark Solved 1
6464 </ button >
65- < button onClick = { ( ) => markSolved ( 'problem-2' ) } data-testid = "mark-solved-2" >
65+ < button type = "button" onClick = { ( ) => markSolved ( 'problem-2' ) } data-testid = "mark-solved-2" >
6666 Mark Solved 2
6767 </ button >
68- < button onClick = { ( ) => markSolved ( 'problem-3' ) } data-testid = "mark-solved-3" >
68+ < button type = "button" onClick = { ( ) => markSolved ( 'problem-3' ) } data-testid = "mark-solved-3" >
6969 Mark Solved 3
7070 </ button >
71- < button onClick = { ( ) => markUnsolved ( 'problem-1' ) } data-testid = "mark-unsolved-1" >
71+ < button type = "button" onClick = { ( ) => markUnsolved ( 'problem-1' ) } data-testid = "mark-unsolved-1" >
7272 Mark Unsolved 1
7373 </ button >
74- < button onClick = { ( ) => markUnsolved ( 'problem-2' ) } data-testid = "mark-unsolved-2" >
74+ < button type = "button" onClick = { ( ) => markUnsolved ( 'problem-2' ) } data-testid = "mark-unsolved-2" >
7575 Mark Unsolved 2
7676 </ button >
77- < button onClick = { resetProgress } data-testid = "reset" >
77+ < button type = "button" onClick = { resetProgress } data-testid = "reset" >
7878 Reset
7979 </ button >
8080 </ div >
@@ -753,9 +753,9 @@ describe('ProgressProvider', () => {
753753 it ( 'should handle localStorage errors gracefully on save' , async ( ) => {
754754 const consoleError = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
755755 const originalSetItem = localStorageMock . setItem ;
756- localStorageMock . setItem = jest . fn ( ( ) => {
756+ localStorageMock . setItem = jest . fn ( ) . mockImplementation ( ( ) => {
757757 throw new Error ( 'Storage full' ) ;
758- } ) ;
758+ } ) as typeof localStorageMock . setItem ;
759759
760760 render (
761761 < ProgressProvider >
@@ -788,9 +788,9 @@ describe('ProgressProvider', () => {
788788 localStorageMock . setItem ( 'js-ts-tricks-progress' , JSON . stringify ( savedProgress ) ) ;
789789
790790 const originalRemoveItem = localStorageMock . removeItem ;
791- localStorageMock . removeItem = jest . fn ( ( ) => {
791+ localStorageMock . removeItem = jest . fn ( ) . mockImplementation ( ( ) => {
792792 throw new Error ( 'Storage error' ) ;
793- } ) ;
793+ } ) as typeof localStorageMock . removeItem ;
794794
795795 render (
796796 < ProgressProvider >
@@ -944,7 +944,7 @@ describe('ProgressProvider', () => {
944944 < div >
945945 < div data-testid = "is-solved-special" > { isSolved ( specialId ) ? 'true' : 'false' } </ div >
946946 < div data-testid = "solved-list" > { Array . from ( solvedProblems ) . join ( ',' ) } </ div >
947- < button onClick = { ( ) => markSolved ( specialId ) } data-testid = "mark-special" >
947+ < button type = "button" onClick = { ( ) => markSolved ( specialId ) } data-testid = "mark-special" >
948948 Mark Special
949949 </ button >
950950 </ div >
@@ -1011,20 +1011,17 @@ describe('ProgressProvider', () => {
10111011 } ) ;
10121012
10131013 it ( 'should provide all expected functions from the hook' , async ( ) => {
1014- let hookResultRef : { current : ReturnType < typeof useProgress > | null } = { current : null } ;
1015-
10161014 function HookInspectorComponent ( ) {
10171015 const result = useProgress ( ) ;
1018- hookResultRef . current = result ;
10191016
10201017 return (
10211018 < div >
10221019 < div data-testid = "has-functions" >
1023- { hookResultRef . current &&
1024- typeof hookResultRef . current . markSolved === 'function' &&
1025- typeof hookResultRef . current . markUnsolved === 'function' &&
1026- typeof hookResultRef . current . isSolved === 'function' &&
1027- typeof hookResultRef . current . resetProgress === 'function'
1020+ { result &&
1021+ typeof result . markSolved === 'function' &&
1022+ typeof result . markUnsolved === 'function' &&
1023+ typeof result . isSolved === 'function' &&
1024+ typeof result . resetProgress === 'function'
10281025 ? 'true'
10291026 : 'false' }
10301027 </ div >
@@ -1044,10 +1041,10 @@ describe('ProgressProvider', () => {
10441041
10451042 // Verify all functions are present and are functions
10461043 expect ( hookResultRef . current ) . not . toBeNull ( ) ;
1047- expect ( typeof hookResultRef . current ! . markSolved ) . toBe ( 'function' ) ;
1048- expect ( typeof hookResultRef . current ! . markUnsolved ) . toBe ( 'function' ) ;
1049- expect ( typeof hookResultRef . current ! . isSolved ) . toBe ( 'function' ) ;
1050- expect ( typeof hookResultRef . current ! . resetProgress ) . toBe ( 'function' ) ;
1044+ expect ( typeof hookResultRef . current ? .markSolved ) . toBe ( 'function' ) ;
1045+ expect ( typeof hookResultRef . current ? .markUnsolved ) . toBe ( 'function' ) ;
1046+ expect ( typeof hookResultRef . current ? .isSolved ) . toBe ( 'function' ) ;
1047+ expect ( typeof hookResultRef . current ? .resetProgress ) . toBe ( 'function' ) ;
10511048 } ) ;
10521049 } ) ;
10531050
@@ -1071,5 +1068,4 @@ describe('ProgressProvider', () => {
10711068 } ) ;
10721069 } ) ;
10731070 } ) ;
1074-
10751071} ) ;
0 commit comments