Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

add: test for Modal and fix exception ResizeObserver is not defined

kruzhambus8e3d1ed27adf
diff --git a/package.json b/package.json
index f7ea3e705..97a5f3fd2 100644
--- a/package.json
+++ b/package.json
@@ -207,6 +207,7 @@
"postcss": "^8.4.21",
"react-test-renderer": "^18.2.0",
"redux-mock-store": "^1.5.4",
+ "resize-observer-polyfill": "^1.5.1",
"source-map-explorer": "^2.5.3",
"tailwindcss": "^3.3.1",
"typescript": "^5.0.3"
diff --git a/snapd b/snapd
new file mode 160000
index 000000000..42b55d70b
--- /dev/null
+++ b/snapd
@@ -0,0 +1 @@
+Subproject commit 42b55d70b1823b7bfec83375bd41e939084fcc74
diff --git a/src/setupTests.ts b/src/setupTests.ts
index 336cd390f..273a77136 100644
--- a/src/setupTests.ts
+++ b/src/setupTests.ts
@@ -1,5 +1,7 @@
/* eslint-disable import/no-extraneous-dependencies */
import { configure } from '@testing-library/react'
+import * as ResizeObserverModule from 'resize-observer-polyfill'
import '@testing-library/jest-dom/extend-expect'
+(global as any).ResizeObserver = ResizeObserverModule.default
configure({ testIdAttribute: 'data-testid' })
diff --git a/src/ui/test/Modal.test.tsx b/src/ui/test/Modal.test.tsx
new file mode 100644
index 000000000..56237c09f
--- /dev/null
+++ b/src/ui/test/Modal.test.tsx
@@ -0,0 +1,102 @@
+/* eslint-disable testing-library/prefer-screen-queries */
+/* eslint-disable import/no-extraneous-dependencies */
+import React from 'react'
+import { render, fireEvent } from '@testing-library/react'
+import { I18nextProvider } from 'react-i18next'
+import i18n from 'i18next'
+import Modal from '../Modal'
+
+describe('Modal component', () => {
+ const defaultProps: {
+ type: 'error' | 'success' | 'info' | 'warning' | 'confirmed',
+ title: string,
+ message: string,
+ isOpened: boolean,
+ onClose: () => void,
+ } = {
+ type: 'success',
+ title: 'Success',
+ message: 'Action completed successfully',
+ isOpened: true,
+ onClose: jest.fn(),
+ }
+
+ // use HOC to wrap component with I18nextProvider
+ const HocWithI18n = ({ children }: {
+ children: React.ReactNode,
+ }): JSX.Element => (
+ <I18nextProvider i18n={i18n}>
+ {children}
+ </I18nextProvider>
+ )
+
+ // initialize i18n before each test
+ beforeEach(() => {
+ i18n.init({
+ lng: 'en',
+ resources: {
+ en: {
+ beta: {
+ title: 'beta.title',
+ },
+ },
+ },
+ })
+ })
+
+ afterEach(() => {
+ jest.resetAllMocks()
+ })
+
+ it('renders with default props', () => {
+ const { getByText } = render(
+ <HocWithI18n>
+ <Modal {...defaultProps} />
+ </HocWithI18n>,
+ )
+ expect(getByText('Success')).toBeInTheDocument()
+ expect(getByText('Action completed successfully')).toBeInTheDocument()
+ })
+
+ it('calls onClose when close button is clicked', () => {
+ const { getByText } = render(
+ <HocWithI18n>
+ <Modal {...defaultProps} closeText='Close' />
+ </HocWithI18n>,
+ )
+ const closeButton = getByText('Close')
+ fireEvent.click(closeButton)
+ expect(defaultProps.onClose).toHaveBeenCalled()
+ })
+
+ it('calls onSubmit when submit button is clicked', () => {
+ const onSubmit = jest.fn()
+ const { getByText } = render(
+ <HocWithI18n>
+ <Modal {...defaultProps} onSubmit={onSubmit} submitText='Submit' />
+ </HocWithI18n>,
+ )
+ const submitButton = getByText('Submit')
+ fireEvent.click(submitButton)
+ expect(onSubmit).toHaveBeenCalled()
+ })
+
+ it('renders custom buttons', () => {
+ const customButtons = <button type='button'>Custom Button</button>
+ const { getByText } = render(
+ <HocWithI18n>
+ <Modal {...defaultProps} customButtons={customButtons} />
+ </HocWithI18n>,
+ )
+ expect(getByText('Custom Button')).toBeInTheDocument()
+ })
+
+ it('renders beta badge when isBeta prop is true', () => {
+ const { getByText } = render(
+ <HocWithI18n>
+ <Modal {...defaultProps} isBeta />
+ </HocWithI18n>,
+ )
+ expect(getByText('beta.title')).toBeInTheDocument()
+ })
+})