import { renderHook , act } from '@testing-library/react-hooks' ; import { useLog } from './use-log.hook' ; describe ( 'use-log hook spec' , ( ) => { it ( '"log" state should be an empty string by default' , ( ) => { const { result } = renderHook ( ( ) => useLog ( ) ) ; expect ( result . current . log ) . toEqual < string > ( '' ) ; } ) ; it ( '"logRef.current" should be an empty string by default' , ( ) => { const { result } = renderHook ( ( ) => useLog ( ) ) ; expect ( result . current . logRef . current ) . toEqual < string > ( '' ) ; } ) ; it ( '"appendToLog" & "setLog" should be functions by default' , ( ) => { const { result } = renderHook ( ( ) => useLog ( ) ) ; expect ( result . current . appendToLog ) . toEqual ( expect . any ( Function ) ) ; expect ( result . current . setLog ) . toEqual ( expect . any ( Function ) ) ; } ) ; it ( '"log" & "logRef.current" should return new content when calling "setLog"' , ( ) => { const expectedResult = 'new-value' ; const { result } = renderHook ( ( ) => useLog ( ) ) ; act ( ( ) => { result . current . setLog ( 'new-value' ) ; } ) ; expect ( result . current . log ) . toEqual < string > ( expectedResult ) ; expect ( result . current . logRef . current ) . toEqual < string > ( expectedResult ) ; } ) ; it ( '"log" & "logRef.current" should return old value when feed "setLog" with "undefined" parameter' , ( ) => { const expectedResult = 'old-value' ; const { result } = renderHook ( ( ) => useLog ( ) ) ; act ( ( ) => { result . current . setLog ( 'old-value' ) ; } ) ; act ( ( ) => { result . current . setLog ( undefined ) ; } ) ; expect ( result . current . log ) . toEqual < string > ( expectedResult ) ; expect ( result . current . logRef . current ) . toEqual < string > ( expectedResult ) ; } ) ; it ( '"log" & "logRef.current" should return old value when feed "setLog" with "null" parameter' , ( ) => { const expectedResult = 'old-value' ; const { result } = renderHook ( ( ) => useLog ( ) ) ; act ( ( ) => { result . current . setLog ( 'old-value' ) ; } ) ; act ( ( ) => { result . current . setLog ( null ) ; } ) ; expect ( result . current . log ) . toEqual < string > ( expectedResult ) ; expect ( result . current . logRef . current ) . toEqual < string > ( expectedResult ) ; } ) ; it ( '"log" & "logRef.current" should return new value with one space when calling "appendToLog" for the first time' , ( ) => { const expectedResult = `new-value ` ; const { result } = renderHook ( ( ) => useLog ( ) ) ; act ( ( ) => { result . current . appendToLog ( 'new-value' ) ; } ) ; expect ( result . current . log ) . toEqual < string > ( expectedResult ) ; expect ( result . current . logRef . current ) . toEqual < string > ( expectedResult ) ; } ) ; it ( '"log" & "logRef.current" should return log value with one space and new value with one space in a new line when calling "appendToLog" after first time' , ( ) => { const expectedResult = `old-value \nnew-value ` ; const { result } = renderHook ( ( ) => useLog ( ) ) ; act ( ( ) => { result . current . appendToLog ( 'old-value' ) ; } ) ; act ( ( ) => { result . current . appendToLog ( 'new-value' ) ; } ) ; expect ( result . current . log ) . toEqual < string > ( expectedResult ) ; expect ( result . current . logRef . current ) . toEqual < string > ( expectedResult ) ; } ) ; it ( '"log" & "logRef.current" should return the same text after calling "setLog" with that text' , ( ) => { const sessionLog = `first line

    *********************************
     
    second line
    
    *********************************
     
    third line
    
    *********************************` ; const { result } = renderHook ( ( ) => useLog ( ) ) ; act ( ( ) => { result . current . setLog ( sessionLog ) ; } ) ; expect ( result . current . log ) . toEqual < string > ( sessionLog ) ; expect ( result . current . logRef . current ) . toEqual < string > ( sessionLog ) ; } ) ; } ) ;