Skip to content

Commit

Permalink
Merge pull request #1342 from ita-social-projects/feature/issue-1529
Browse files Browse the repository at this point in the history
[Main Page][Instagram block] Investigate and fix disappearance of the Instagram block
  • Loading branch information
sashapanasiuk5 authored Sep 14, 2024
2 parents 219f294 + 7836685 commit deb2a67
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
45 changes: 45 additions & 0 deletions src/app/common/components/ErrorBoundary/ErrorBoundary.spec.tsx
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();
});
});
35 changes: 35 additions & 0 deletions src/app/common/components/ErrorBoundary/ErrorBoundary.ts
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;
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const CategoriesMainPage: React.FC = observer(() => {
modalStore.setConfirmationModal('confirmation');
}
},
'Ви впевнені, що хочете видалити чю новину?',
'Ви впевнені, що хочете видалити цю новину?',
);
}}
/>
Expand Down
5 changes: 4 additions & 1 deletion src/features/MainPage/MainPage.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import StaticBanner from './StaticBanners/StaticBanner.component';
import StreetcodeSliderComponent from './StreetcodeSlider/StreetcodeSlider.component';
import TeamComponent from './TeamSlider/TeamComponent.component';
import TopCarouselComponent from './TopCarousel/TopCarousel.component';
import ErrorBoundary from '@/app/common/components/ErrorBoundary/ErrorBoundary';

const mainPageContent = () => (
<>
Expand All @@ -28,7 +29,9 @@ const mainPageContent = () => (
<NewsSliderComponent />
<TeamComponent />
<PartnersBlockComponent />
<InstagramBlock />
<ErrorBoundary fallback={<></>}>
<InstagramBlock />
</ErrorBoundary>
<StaticBanner
id="support"
blockName="Слід в історії у кожного різний. У тебе може бути свій"
Expand Down

0 comments on commit deb2a67

Please sign in to comment.