|
| 1 | +import { renderHook, act } from '@testing-library/react-hooks'; |
| 2 | +import { useLog } from './use-log.hook'; |
| 3 | + |
| 4 | +describe('use-log hook spec', () => { |
| 5 | + it('"log" state should be an empty string by default', () => { |
| 6 | + // Arrange |
| 7 | + |
| 8 | + // Act |
| 9 | + const { result } = renderHook(() => useLog()); |
| 10 | + |
| 11 | + // Assert |
| 12 | + expect(result.current.log).toEqual<string>(''); |
| 13 | + }); |
| 14 | + |
| 15 | + it('"logRef.current" should be an empty string by default', () => { |
| 16 | + // Arrange |
| 17 | + |
| 18 | + // Act |
| 19 | + const { result } = renderHook(() => useLog()); |
| 20 | + |
| 21 | + // Assert |
| 22 | + expect(result.current.logRef.current).toEqual<string>(''); |
| 23 | + }); |
| 24 | + |
| 25 | + it('"appendToLog" & "setLog" should be functions by default', () => { |
| 26 | + // Arrange |
| 27 | + |
| 28 | + // Act |
| 29 | + const { result } = renderHook(() => useLog()); |
| 30 | + |
| 31 | + // Assert |
| 32 | + expect(result.current.appendToLog).toEqual(expect.any(Function)); |
| 33 | + expect(result.current.setLog).toEqual(expect.any(Function)); |
| 34 | + }); |
| 35 | + |
| 36 | + it('"log" & "logRef.current" should return new content when calling "setLog"', () => { |
| 37 | + // Arrange |
| 38 | + const testResult: string = 'new-value'; |
| 39 | + |
| 40 | + // Act |
| 41 | + const { result } = renderHook(() => useLog()); |
| 42 | + act(() => { |
| 43 | + result.current.setLog('new-value'); |
| 44 | + }); |
| 45 | + |
| 46 | + // Assert |
| 47 | + expect(result.current.log).toEqual<string>(testResult); |
| 48 | + expect(result.current.logRef.current).toEqual<string>(testResult); |
| 49 | + }); |
| 50 | + |
| 51 | + it('"log" & "logRef.current" should return old value when feed "setLog" with "undefined" parameter', () => { |
| 52 | + // Arrange |
| 53 | + const testResult: string = 'old-value'; |
| 54 | + |
| 55 | + // Act |
| 56 | + const { result } = renderHook(() => useLog()); |
| 57 | + |
| 58 | + act(() => { |
| 59 | + result.current.setLog('old-value'); |
| 60 | + }); |
| 61 | + |
| 62 | + act(() => { |
| 63 | + result.current.setLog(undefined); |
| 64 | + }); |
| 65 | + |
| 66 | + // Assert |
| 67 | + expect(result.current.log).toEqual<string>(testResult); |
| 68 | + expect(result.current.logRef.current).toEqual<string>(testResult); |
| 69 | + }); |
| 70 | + |
| 71 | + it('"log" & "logRef.current" should return old value when feed "setLog" with "null" parameter', () => { |
| 72 | + // Arrange |
| 73 | + const testResult: string = 'old-value'; |
| 74 | + |
| 75 | + // Act |
| 76 | + const { result } = renderHook(() => useLog()); |
| 77 | + |
| 78 | + act(() => { |
| 79 | + result.current.setLog('old-value'); |
| 80 | + }); |
| 81 | + |
| 82 | + act(() => { |
| 83 | + result.current.setLog(null); |
| 84 | + }); |
| 85 | + |
| 86 | + // Assert |
| 87 | + expect(result.current.log).toEqual<string>(testResult); |
| 88 | + expect(result.current.logRef.current).toEqual<string>(testResult); |
| 89 | + }); |
| 90 | + |
| 91 | + xit('"log" & "logRef.current" should return old value and new value after a new line when calling "appendToLog"', () => { |
| 92 | + // Arrange |
| 93 | + const testResult = `New-value `; |
| 94 | + |
| 95 | + // Act |
| 96 | + const { result } = renderHook(() => useLog()); |
| 97 | + act(() => { |
| 98 | + result.current.appendToLog('New-value'); |
| 99 | + }); |
| 100 | + |
| 101 | + // Assert |
| 102 | + expect(result.current.log).toEqual(testResult); |
| 103 | + expect(result.current.logRef.current).toEqual(testResult); |
| 104 | + }); |
| 105 | +}); |
0 commit comments