Skip to content

Commit b22ed17

Browse files
authored
refactor: move FatalError to form-component lib (#18912)
1 parent 519e565 commit b22ed17

16 files changed

Lines changed: 91 additions & 31 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import type { Meta, StoryObj } from '@storybook/react-vite';
2+
3+
import { FatalError } from './FatalError';
4+
import { FatalErrorEmpty } from './FatalErrorEmpty';
5+
6+
const meta = {
7+
title: 'AppComponents/FatalError',
8+
component: FatalError,
9+
} satisfies Meta<typeof FatalError>;
10+
11+
export default meta;
12+
13+
type Story = StoryObj<typeof meta>;
14+
15+
export const Preview: Story = {
16+
args: {
17+
children: 'An unrecoverable error has occurred.',
18+
},
19+
};
20+
21+
export const Empty: StoryObj<typeof FatalErrorEmpty> = {
22+
render: () => <FatalErrorEmpty />,
23+
parameters: {
24+
docs: {
25+
description: {
26+
story: 'Renders a hidden marker element signalling a fatal error without visible content.',
27+
},
28+
},
29+
},
30+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { render, screen } from '@testing-library/react';
2+
3+
import { FatalError } from './FatalError';
4+
import { FatalErrorEmpty } from './FatalErrorEmpty';
5+
6+
describe('FatalError', () => {
7+
it('renders children inside a div with data-fatal-error', () => {
8+
render(
9+
<FatalError>
10+
<span>Something went wrong</span>
11+
</FatalError>,
12+
);
13+
14+
const child = screen.getByText('Something went wrong');
15+
expect(child).toBeInTheDocument();
16+
expect(child.parentElement).toHaveAttribute('data-fatal-error');
17+
});
18+
19+
it('forwards additional props to the underlying div', () => {
20+
render(
21+
<FatalError className='custom' data-testid='fatal'>
22+
content
23+
</FatalError>,
24+
);
25+
26+
const wrapper = screen.getByTestId('fatal');
27+
expect(wrapper).toHaveAttribute('data-fatal-error');
28+
expect(wrapper).toHaveClass('custom');
29+
});
30+
});
31+
32+
describe('FatalErrorEmpty', () => {
33+
it('renders a hidden div with data-fatal-error and no content', () => {
34+
const { container } = render(<FatalErrorEmpty />);
35+
const div = container.querySelector('[data-fatal-error]');
36+
37+
expect(div).toBeInTheDocument();
38+
expect(div).toHaveStyle({ display: 'none' });
39+
expect(div?.children).toHaveLength(0);
40+
});
41+
});

src/App/frontend/src/app-components/error/FatalError/FatalError.tsx renamed to libs/form-component/src/app-components/FatalError/FatalError.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import React from 'react';
21
import type { HTMLAttributes, PropsWithChildren } from 'react';
32

43
/**
54
* The `data-fatal-error` signals that some unrecoverable error occured which should prevent PDF generation from happening as it would not include necessary information.
65
*/
7-
export function FatalError({ children, ...props }: PropsWithChildren<HTMLAttributes<HTMLDivElement>>) {
6+
export function FatalError({
7+
children,
8+
...props
9+
}: PropsWithChildren<HTMLAttributes<HTMLDivElement>>) {
810
return (
9-
<div
10-
data-fatal-error
11-
{...props}
12-
>
11+
<div data-fatal-error {...props}>
1312
{children}
1413
</div>
1514
);

src/App/frontend/src/app-components/error/FatalErrorEmpty/FatalErrorEmpty.tsx renamed to libs/form-component/src/app-components/FatalError/FatalErrorEmpty.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
1-
import React from 'react';
2-
31
/**
42
* The `data-fatal-error` signals that some unrecoverable error occured which should prevent PDF generation from happening as it would not include necessary information.
53
*/
64
export function FatalErrorEmpty() {
7-
return (
8-
<div
9-
data-fatal-error
10-
style={{ display: 'none' }}
11-
/>
12-
);
5+
return <div data-fatal-error style={{ display: 'none' }} />;
136
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export { FatalError } from './FatalError';
2+
export { FatalErrorEmpty } from './FatalErrorEmpty';

libs/form-component/src/app-components/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export * from './Card';
44
export * from './Datepicker';
55
export * from './DynamicForm';
66
export * from './Dropzone';
7+
export * from './FatalError';
78
export * from './Flex';
89
export * from './Input';
910
export * from './hooks';

src/App/frontend/monorepo-changed-paths.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
.claude/settings.json
21
.gitattributes
32
.github/
43
.husky/
@@ -30,6 +29,8 @@ src/app-components/Table/
3029
src/app-components/Text/
3130
src/app-components/TextArea/TextArea.tsx
3231
src/app-components/TimePicker/
32+
src/app-components/error/FatalError/FatalError.tsx
33+
src/app-components/error/FatalErrorEmpty/FatalErrorEmpty.tsx
3334
src/app-components/loading/Spinner/Spinner.tsx
3435
src/codegen/schemas/layout-sets.schema.v1.ts
3536
src/components/presentation/Header.tsx

src/App/frontend/src/components/altinnError.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import React from 'react';
22

3+
import { FatalError } from '@app/form-component';
34
import cn from 'classnames';
45

5-
import { FatalError } from 'src/app-components/error/FatalError/FatalError';
66
import classes from 'src/components/altinnError.module.css';
77
import { Lang } from 'src/features/language/Lang';
88
import { getHelpCircleIllustrationUrl } from 'src/utils/urls/appUrlHelper';

src/App/frontend/src/layout/GenericComponent.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ import React, { useEffect, useMemo, useRef } from 'react';
22
import { useSearchParams } from 'react-router';
33
import type { SetURLSearchParams } from 'react-router';
44

5-
import { Flex } from '@app/form-component';
5+
import { FatalError, FatalErrorEmpty, Flex } from '@app/form-component';
66
import classNames from 'classnames';
77

8-
import { FatalError } from 'src/app-components/error/FatalError/FatalError';
9-
import { FatalErrorEmpty } from 'src/app-components/error/FatalErrorEmpty/FatalErrorEmpty';
108
import { SearchParams } from 'src/core/routing/types';
119
import { useIsNavigating } from 'src/core/routing/useIsNavigating';
1210
import { useDevToolsStore } from 'src/features/devtools/data/DevToolsStore';

src/App/frontend/src/layout/SigneeList/SigneeListError.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import React from 'react';
22

3+
import { FatalError } from '@app/form-component';
34
import { isAxiosError } from 'axios';
45
import { z, ZodError } from 'zod';
56

6-
import { FatalError } from 'src/app-components/error/FatalError/FatalError';
77
import { Lang } from 'src/features/language/Lang';
88
import { useLanguage } from 'src/features/language/useLanguage';
99

0 commit comments

Comments
 (0)