-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UIBULKED-588 Displaying errors and warnings (#678)
- Loading branch information
Showing
10 changed files
with
146 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { ERROR_TYPES } from '../constants'; | ||
|
||
// empty string is used to reset the error type and show both errors and warnings | ||
const getDynamicErrorType = (condition) => (condition ? '' : ERROR_TYPES.ERROR); | ||
|
||
export const useErrorType = ({ countOfErrors, countOfWarnings }) => { | ||
const hasOnlyWarnings = countOfErrors === 0 && countOfWarnings > 0; | ||
const initialErrorType = getDynamicErrorType(hasOnlyWarnings); | ||
|
||
const [errorType, setErrorType] = useState(initialErrorType); | ||
|
||
const toggleErrorType = () => { | ||
setErrorType(getDynamicErrorType(!!errorType)); | ||
}; | ||
|
||
useEffect(() => { | ||
setErrorType(initialErrorType); | ||
}, [initialErrorType]); | ||
|
||
return { errorType, toggleErrorType }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { renderHook, act } from '@testing-library/react-hooks'; | ||
import { useErrorType } from './useErrorType'; | ||
import { ERROR_TYPES } from '../constants'; | ||
|
||
describe('useErrorType', () => { | ||
it('should initialize with no error type if only warnings exist', () => { | ||
const { result } = renderHook(() => useErrorType({ countOfErrors: 0, countOfWarnings: 1 })); | ||
expect(result.current.errorType).toBe(''); | ||
}); | ||
|
||
it('should initialize with error type if errors exist', () => { | ||
const { result } = renderHook(() => useErrorType({ countOfErrors: 1, countOfWarnings: 0 })); | ||
expect(result.current.errorType).toBe(ERROR_TYPES.ERROR); | ||
}); | ||
|
||
it('should toggle error type when toggleErrorType is called', () => { | ||
const { result } = renderHook(() => useErrorType({ countOfErrors: 1, countOfWarnings: 0 })); | ||
|
||
expect(result.current.errorType).toBe(ERROR_TYPES.ERROR); | ||
|
||
act(() => { | ||
result.current.toggleErrorType(); | ||
}); | ||
expect(result.current.errorType).toBe(''); | ||
|
||
act(() => { | ||
result.current.toggleErrorType(); | ||
}); | ||
expect(result.current.errorType).toBe(ERROR_TYPES.ERROR); | ||
}); | ||
|
||
it('should reset error type when the input props change', () => { | ||
const { result, rerender } = renderHook( | ||
({ countOfErrors, countOfWarnings }) => useErrorType({ countOfErrors, countOfWarnings }), | ||
{ | ||
initialProps: { countOfErrors: 1, countOfWarnings: 0 }, | ||
} | ||
); | ||
|
||
expect(result.current.errorType).toBe(ERROR_TYPES.ERROR); | ||
|
||
rerender({ countOfErrors: 0, countOfWarnings: 1 }); | ||
expect(result.current.errorType).toBe(''); | ||
|
||
rerender({ countOfErrors: 2, countOfWarnings: 0 }); | ||
expect(result.current.errorType).toBe(ERROR_TYPES.ERROR); | ||
}); | ||
}); |