generated from ita-social-projects/DevTemplate
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1342 from ita-social-projects/feature/issue-1529
[Main Page][Instagram block] Investigate and fix disappearance of the Instagram block
- Loading branch information
Showing
4 changed files
with
85 additions
and
2 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
src/app/common/components/ErrorBoundary/ErrorBoundary.spec.tsx
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,45 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import '@testing-library/jest-dom'; | ||
import ErrorBoundary from './ErrorBoundary'; | ||
|
||
const ProblemChild = () => { | ||
throw new Error('Error thrown from problem child'); | ||
}; | ||
|
||
describe('ErrorBoundary', () => { | ||
test('renders children without error', () => { | ||
render( | ||
<ErrorBoundary fallback={<div>Fallback</div>}> | ||
<div>Child Component</div> | ||
</ErrorBoundary> | ||
); | ||
|
||
expect(screen.getByText('Child Component')).toBeInTheDocument(); | ||
}); | ||
|
||
test('renders fallback UI on error', () => { | ||
render( | ||
<ErrorBoundary fallback={<div>Fallback</div>}> | ||
<ProblemChild /> | ||
</ErrorBoundary> | ||
); | ||
|
||
expect(screen.getByText('Fallback')).toBeInTheDocument(); | ||
}); | ||
|
||
test('sets hasError state to true on error', () => { | ||
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
|
||
render( | ||
<ErrorBoundary fallback={<div>Fallback</div>}> | ||
<ProblemChild /> | ||
</ErrorBoundary> | ||
); | ||
|
||
expect(screen.getByText('Fallback')).toBeInTheDocument(); | ||
expect(consoleErrorSpy).toHaveBeenCalled(); | ||
|
||
consoleErrorSpy.mockRestore(); | ||
}); | ||
}); |
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,35 @@ | ||
import { ReactNode, Component, ErrorInfo } from "react"; | ||
|
||
interface Props { | ||
children: ReactNode; | ||
fallback: ReactNode; | ||
} | ||
|
||
interface State { | ||
hasError: boolean; | ||
} | ||
|
||
class ErrorBoundary extends Component<Props, State> { | ||
public state: State = { | ||
hasError: false | ||
}; | ||
|
||
public static getDerivedStateFromError(_: Error): State { | ||
return { hasError: true }; | ||
} | ||
|
||
public componentDidCatch(error: Error, errorInfo: ErrorInfo) { | ||
console.error('ErrorBoundary caught an error: ', error, errorInfo); | ||
this.setState({ hasError: true }); | ||
} | ||
|
||
public render() { | ||
if (this.state.hasError) { | ||
return this.props.fallback; | ||
} | ||
|
||
return this.props.children; | ||
} | ||
} | ||
|
||
export default ErrorBoundary; |
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