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!

bring back unit tests

Blue Mousee62473b16ec5
diff --git a/app/ui/__test__/Alert.test.tsx b/app/ui/__test__/Alert.test.tsx
new file mode 100644
index 000000000..8c742f908
--- /dev/null
+++ b/app/ui/__test__/Alert.test.tsx
@@ -0,0 +1,50 @@
+/* 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 AlertTemplate from '../Alert'
+import '@testing-library/jest-dom'
+
+describe('AlertTemplate', () => {
+ const closeMock = jest.fn()
+
+ afterEach(() => {
+ closeMock.mockClear()
+ })
+
+ test('renders an info alert', () => {
+ const { getByText, getByRole } = render(
+ <AlertTemplate message='This is an info alert' options={{ type: 'info' }} close={closeMock} />,
+ )
+
+ expect(getByText('This is an info alert')).toBeInTheDocument()
+ expect(getByRole('button')).toBeInTheDocument()
+
+ fireEvent.click(getByRole('button'))
+ expect(closeMock).toHaveBeenCalled()
+ })
+
+ test('renders a success alert', () => {
+ const { getByText, getByRole } = render(
+ <AlertTemplate message='This is a success alert' options={{ type: 'success' }} close={closeMock} />,
+ )
+
+ expect(getByText('This is a success alert')).toBeInTheDocument()
+ expect(getByRole('button')).toBeInTheDocument()
+
+ fireEvent.click(getByRole('button'))
+ expect(closeMock).toHaveBeenCalled()
+ })
+
+ test('renders an error alert', () => {
+ const { getByText, getByRole } = render(
+ <AlertTemplate message='This is an error alert' options={{ type: 'error' }} close={closeMock} />,
+ )
+
+ expect(getByText('This is an error alert')).toBeInTheDocument()
+ expect(getByRole('button')).toBeInTheDocument()
+
+ fireEvent.click(getByRole('button'))
+ expect(closeMock).toHaveBeenCalled()
+ })
+})
diff --git a/app/ui/__test__/Beta.test.tsx b/app/ui/__test__/Beta.test.tsx
new file mode 100644
index 000000000..b9f6b030f
--- /dev/null
+++ b/app/ui/__test__/Beta.test.tsx
@@ -0,0 +1,31 @@
+/* eslint-disable testing-library/prefer-screen-queries */
+/* eslint-disable import/no-extraneous-dependencies */
+import React from 'react'
+import { render } from '@testing-library/react'
+import { I18nextProvider } from 'react-i18next'
+import i18n from 'i18next'
+import Beta from '../Beta'
+
+describe('Beta component', () => {
+ beforeEach(() => {
+ i18n.init({
+ lng: 'en',
+ resources: {
+ en: {
+ beta: {
+ title: 'beta.title',
+ },
+ },
+ },
+ })
+ })
+
+ it('should render with a custom class name', () => {
+ const { getByText } = render(
+ <I18nextProvider i18n={i18n}>
+ <Beta className='custom-class-name' />
+ </I18nextProvider>,
+ )
+ expect(getByText('beta.title')).toHaveClass('custom-class-name')
+ })
+})
diff --git a/app/ui/__test__/Button.test.tsx b/app/ui/__test__/Button.test.tsx
new file mode 100644
index 000000000..3be5dcc4d
--- /dev/null
+++ b/app/ui/__test__/Button.test.tsx
@@ -0,0 +1,91 @@
+/* eslint-disable import/no-extraneous-dependencies */
+/* eslint-disable testing-library/prefer-screen-queries */
+import React from 'react'
+import { render, fireEvent } from '@testing-library/react'
+import '@testing-library/jest-dom'
+import Button from '../Button'
+
+describe('Button component', () => {
+ it('renders with text', () => {
+ const { getByText } = render(<Button text='Click me' />)
+ expect(getByText('Click me')).toBeInTheDocument()
+ })
+
+ it('renders with children', () => {
+ const { getByText } = render(<Button><span>Click me</span></Button>)
+ expect(getByText('Click me')).toBeInTheDocument()
+ })
+
+ it('handles clicks', () => {
+ const onClick = jest.fn()
+ const { getByRole } = render(<Button onClick={onClick} />)
+ fireEvent.click(getByRole('button'))
+ expect(onClick).toHaveBeenCalledTimes(1)
+ })
+
+ it('renders a primary button', () => {
+ const { getByText } = render(<Button text='Click me' primary />)
+ const button = getByText('Click me')
+ expect(button).toHaveClass('bg-indigo-600')
+ })
+
+ it('renders a secondary button', () => {
+ const { getByText } = render(<Button text='Click me' secondary />)
+ const button = getByText('Click me')
+ expect(button).toHaveClass('bg-indigo-100')
+ })
+
+ it('renders a white button', () => {
+ const { getByText } = render(<Button text='Click me' white />)
+ const button = getByText('Click me')
+ expect(button).toHaveClass('bg-white')
+ })
+
+ it('renders a danger button', () => {
+ const { getByText } = render(<Button text='Click me' danger />)
+ const button = getByText('Click me')
+ expect(button).toHaveClass('bg-red-500')
+ })
+
+ it('renders a semi-danger button', () => {
+ const { getByText } = render(<Button text='Click me' semiDanger />)
+ const button = getByText('Click me')
+ expect(button).toHaveClass('border-red-600')
+ })
+
+ it('renders a small button', () => {
+ const { getByText } = render(<Button text='Click me' small />)
+ const button = getByText('Click me')
+ expect(button).toHaveClass('text-xs')
+ })
+
+ it('renders a semi-small button', () => {
+ const { getByText } = render(<Button text='Click me' semiSmall />)
+ const button = getByText('Click me')
+ expect(button).toHaveClass('text-sm')
+ })
+
+ it('renders a regular button', () => {
+ const { getByText } = render(<Button text='Click me' regular />)
+ const button = getByText('Click me')
+ expect(button).toHaveClass('text-sm')
+ })
+
+ it('renders a large button', () => {
+ const { getByText } = render(<Button text='Click me' large />)
+ const button = getByText('Click me')
+ expect(button).toHaveClass('text-sm')
+ })
+
+ it('renders a giant button', () => {
+ const { getByText } = render(<Button text='Click me' giant />)
+ const button = getByText('Click me')
+ expect(button).toHaveClass('text-base')
+ })
+
+ it('renders a button with no border', () => {
+ const { getByText } = render(<Button text='Click me' noBorder />)
+ const button = getByText('Click me')
+ expect(button).toHaveClass('border-none')
+ })
+})
diff --git a/app/ui/__test__/Checkbox.test.tsx b/app/ui/__test__/Checkbox.test.tsx
new file mode 100644
index 000000000..1b667eba9
--- /dev/null
+++ b/app/ui/__test__/Checkbox.test.tsx
@@ -0,0 +1,29 @@
+/* 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 '@testing-library/jest-dom'
+import Checkbox from '../Checkbox'
+
+describe('Checkbox', () => {
+ test('renders a checkbox', () => {
+ const { getByText } = render(<Checkbox label='Checkbox' checked />)
+ expect(getByText('Checkbox')).toBeInTheDocument()
+ })
+
+ test('renders a checkbox with a custom id', () => {
+ const { getByRole } = render(
+ <Checkbox id='custom-id' label='Checkbox' checked />,
+ )
+ expect(getByRole('checkbox')).toHaveAttribute('id', 'custom-id')
+ })
+
+ test('clicking the checkbox calls the onChange handler', () => {
+ const onChange = jest.fn()
+ const { getByRole } = render(
+ <Checkbox label='Checkbox' checked onChange={onChange} />,
+ )
+ fireEvent.click(getByRole('checkbox'))
+ expect(onChange).toHaveBeenCalled()
+ })
+})
diff --git a/app/ui/__test__/Dropdown.test.tsx b/app/ui/__test__/Dropdown.test.tsx
new file mode 100644
index 000000000..92368af6f
--- /dev/null
+++ b/app/ui/__test__/Dropdown.test.tsx
@@ -0,0 +1,38 @@
+/* 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 '@testing-library/jest-dom'
+import Dropdown from '../Dropdown'
+
+describe('Dropdown', () => {
+ const items = ['Item 1', 'Item 2', 'Item 3']
+ const onSelect = jest.fn()
+ const defaultProps = {
+ title: 'Dropdown Title',
+ onSelect,
+ items,
+ }
+
+ afterEach(() => {
+ jest.resetAllMocks()
+ })
+
+ it('should render without errors', () => {
+ const { getByText } = render(<Dropdown {...defaultProps} />)
+ expect(getByText(defaultProps.title)).toBeInTheDocument()
+ })
+
+ it('should render the description when provided', () => {
+ const description = 'Dropdown description'
+ const { getByText } = render(<Dropdown {...defaultProps} desc={description} />)
+ expect(getByText(description)).toBeInTheDocument()
+ })
+
+ it('should call onSelect when an item is clicked', () => {
+ const { getByText } = render(<Dropdown {...defaultProps} />)
+ const btnOpen = getByText(defaultProps.title)
+ fireEvent.click(btnOpen)
+ expect(getByText(items[0])).toBeInTheDocument()
+ })
+})
diff --git a/app/ui/__test__/Flatpicker.test.tsx b/app/ui/__test__/Flatpicker.test.tsx
new file mode 100644
index 000000000..a3f5e9295
--- /dev/null
+++ b/app/ui/__test__/Flatpicker.test.tsx
@@ -0,0 +1,13 @@
+/* eslint-disable testing-library/prefer-screen-queries */
+/* eslint-disable import/no-extraneous-dependencies */
+import React from 'react'
+import { render } from '@testing-library/react'
+import '@testing-library/jest-dom'
+import Flatpicker from '../Flatpicker'
+
+describe('Flatpicker', () => {
+ test('renders a flatpicker', () => {
+ const { getByTestId } = render(<Flatpicker />)
+ expect(getByTestId('calendar')).toBeInTheDocument()
+ })
+})
diff --git a/app/ui/__test__/Input.test.tsx b/app/ui/__test__/Input.test.tsx
new file mode 100644
index 000000000..fffdbfc9c
--- /dev/null
+++ b/app/ui/__test__/Input.test.tsx
@@ -0,0 +1,38 @@
+/* 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 '@testing-library/jest-dom'
+import Input from '../Input'
+
+describe('Input component', () => {
+ it('renders label, input and hint', () => {
+ const { getAllByText, getByText, getByPlaceholderText } = render(
+ <Input label='Username' hint='Enter your username' placeholder='Enter your username' />,
+ )
+ expect(getAllByText(/username/i)).toHaveLength(2)
+ expect(getByText(/enter your username/i)).toBeInTheDocument()
+ expect(getByPlaceholderText(/enter your username/i)).toBeInTheDocument()
+ })
+
+ it('calls onChange callback when input value changes', () => {
+ const handleChange = jest.fn()
+ const { getByRole } = render(<Input label='Username' onChange={handleChange} />)
+ const input = getByRole('textbox')
+ fireEvent.change(input, { target: { value: 'John' } })
+ expect(handleChange).toHaveBeenCalledTimes(1)
+ })
+
+ it('displays error message when error prop is truthy', () => {
+ const { getByRole, getByText } = render(<Input label='Username' error='Username is required' />)
+ const input = getByRole('textbox')
+ fireEvent.blur(input)
+ expect(getByText(/username is required/i)).toBeInTheDocument()
+ })
+
+ it('disables input when disabled prop is true', () => {
+ const { getByRole } = render(<Input label='Username' disabled />)
+ const input = getByRole('textbox') as HTMLInputElement
+ expect(input).toBeDisabled()
+ })
+})
diff --git a/app/ui/__test__/Loader.test.tsx b/app/ui/__test__/Loader.test.tsx
new file mode 100644
index 000000000..7684d355c
--- /dev/null
+++ b/app/ui/__test__/Loader.test.tsx
@@ -0,0 +1,13 @@
+/* eslint-disable testing-library/prefer-screen-queries */
+/* eslint-disable import/no-extraneous-dependencies */
+import React from 'react'
+import { render } from '@testing-library/react'
+import '@testing-library/jest-dom'
+import Loader from '../Loader'
+
+describe('Loader', () => {
+ it('renders the loader with the "Loading..." text', () => {
+ const { getByText } = render(<Loader />)
+ expect(getByText('Loading...')).toBeInTheDocument()
+ })
+})
diff --git a/app/ui/__test__/Modal.test.tsx b/app/ui/__test__/Modal.test.tsx
new file mode 100644
index 000000000..d08205a4f
--- /dev/null
+++ b/app/ui/__test__/Modal.test.tsx
@@ -0,0 +1,91 @@
+/* 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 '@testing-library/jest-dom'
+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 = (props: any): JSX.Element => (
+ <I18nextProvider i18n={i18n}>
+ <Modal {...props} />
+ </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 {...defaultProps} />,
+ )
+ expect(getByText('Success')).toBeInTheDocument()
+ expect(getByText('Action completed successfully')).toBeInTheDocument()
+ })
+
+ it('calls onClose when close button is clicked', () => {
+ const { getByText } = render(
+ <HocWithI18n {...defaultProps} closeText='Close' />,
+ )
+ 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 {...defaultProps} onSubmit={onSubmit} submitText='Submit' />,
+ )
+ 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 {...defaultProps} customButtons={customButtons} />,
+ )
+ expect(getByText('Custom Button')).toBeInTheDocument()
+ })
+
+ it('renders beta badge when isBeta prop is true', () => {
+ const { getByText } = render(
+ <HocWithI18n {...defaultProps} isBeta />,
+ )
+ expect(getByText('beta.title')).toBeInTheDocument()
+ })
+})
diff --git a/app/ui/__test__/Pagination.test.tsx b/app/ui/__test__/Pagination.test.tsx
new file mode 100644
index 000000000..0fbb02562
--- /dev/null
+++ b/app/ui/__test__/Pagination.test.tsx
@@ -0,0 +1,110 @@
+/* eslint-disable testing-library/prefer-screen-queries */
+/* eslint-disable import/no-extraneous-dependencies */
+import React from 'react'
+import { render } from '@testing-library/react'
+import '@testing-library/jest-dom'
+import { I18nextProvider } from 'react-i18next'
+import i18n from 'i18next'
+import Pagination from '../Pagination'
+
+describe('Pagination', () => {
+ let defaultProps: {
+ page: number,
+ setPage: (item: number) => void,
+ pageAmount: number,
+ total: number,
+ } = {
+ page: 1,
+ setPage: () => {
+ defaultProps.page += 1
+ },
+ pageAmount: 10,
+ total: 100,
+ }
+
+ // use HOC to wrap component with I18nextProvider
+ const HocWithI18n = (props: any): JSX.Element => (
+ <I18nextProvider i18n={i18n}>
+ <Pagination {...props} />
+ </I18nextProvider>
+ )
+
+ // initialize i18n before each test
+ beforeEach(() => {
+ i18n.init({
+ lng: 'en',
+ resources: {
+ en: {
+ project: {
+ prev: 'project.prev',
+ next: 'project.next',
+ },
+ },
+ },
+ })
+ })
+
+ afterEach(() => {
+ jest.resetAllMocks()
+ defaultProps = {
+ page: 1,
+ setPage: () => {
+ defaultProps.page += 1
+ },
+ pageAmount: 10,
+ total: 100,
+ }
+ })
+
+ it('renders with default props', () => {
+ const { getByText } = render(
+ <HocWithI18n {...defaultProps} />,
+ )
+ expect(getByText('1')).toBeInTheDocument()
+ expect(getByText('10')).toBeInTheDocument()
+ expect(getByText('project.next')).toBeInTheDocument()
+ })
+
+ it('renders on page > 1', () => {
+ defaultProps.page = 2
+ const { getByText } = render(
+ <HocWithI18n {...defaultProps} />,
+ )
+ expect(getByText('1')).toBeInTheDocument()
+ expect(getByText('10')).toBeInTheDocument()
+ expect(getByText('project.prev')).toBeInTheDocument()
+ expect(getByText('project.next')).toBeInTheDocument()
+ })
+
+ it('renders on page > 1 and page < pageAmount', () => {
+ defaultProps.page = 5
+ const { getByText } = render(
+ <HocWithI18n {...defaultProps} />,
+ )
+ expect(getByText('1')).toBeInTheDocument()
+ expect(getByText('10')).toBeInTheDocument()
+ expect(getByText('project.prev')).toBeInTheDocument()
+ expect(getByText('project.next')).toBeInTheDocument()
+ })
+
+ it('renders on page = pageAmount', () => {
+ defaultProps.page = 10
+ const { getByText } = render(
+ <HocWithI18n {...defaultProps} />,
+ )
+ expect(getByText('1')).toBeInTheDocument()
+ expect(getByText('10')).toBeInTheDocument()
+ expect(getByText('project.prev')).toBeInTheDocument()
+ })
+
+ it('renders on page = pageAmount and pageAmount < total', () => {
+ defaultProps.page = 10
+ defaultProps.total = 101
+ const { getByText } = render(
+ <HocWithI18n {...defaultProps} />,
+ )
+ expect(getByText('1')).toBeInTheDocument()
+ expect(getByText('9')).toBeInTheDocument()
+ expect(getByText('project.prev')).toBeInTheDocument()
+ })
+})
diff --git a/app/ui/__test__/Pin.test.tsx b/app/ui/__test__/Pin.test.tsx
new file mode 100644
index 000000000..0bca08e9b
--- /dev/null
+++ b/app/ui/__test__/Pin.test.tsx
@@ -0,0 +1,43 @@
+/* eslint-disable testing-library/prefer-screen-queries */
+/* eslint-disable import/no-extraneous-dependencies */
+import React from 'react'
+import { render } from '@testing-library/react'
+import '@testing-library/jest-dom'
+import {
+ WarningPin, ActivePin, InactivePin, CustomPin,
+} from '../Pin'
+
+describe('Pin', () => {
+ const defaultProps: {
+ label: string,
+ className?: string,
+ } = {
+ label: 'label',
+ className: 'className',
+ }
+
+ it('renders the warning pin', () => {
+ const { getByText } = render(<WarningPin {...defaultProps} />)
+ expect(getByText('label')).toBeInTheDocument()
+ })
+
+ it('renders the active pin', () => {
+ const { getByText } = render(<ActivePin {...defaultProps} />)
+ expect(getByText('label')).toBeInTheDocument()
+ })
+
+ it('renders the inactive pin', () => {
+ const { getByText } = render(<InactivePin {...defaultProps} />)
+ expect(getByText('label')).toBeInTheDocument()
+ })
+
+ it('renders the custom pin', () => {
+ const { getByText } = render(<CustomPin {...defaultProps} />)
+ expect(getByText('label')).toBeInTheDocument()
+ })
+
+ it('renders the custom pin with custom className', () => {
+ const { getByText } = render(<CustomPin {...defaultProps} className='customClassName' />)
+ expect(getByText('label')).toBeInTheDocument()
+ })
+})
diff --git a/app/ui/__test__/Progress.test.tsx b/app/ui/__test__/Progress.test.tsx
new file mode 100644
index 000000000..cc73c6f57
--- /dev/null
+++ b/app/ui/__test__/Progress.test.tsx
@@ -0,0 +1,14 @@
+/* eslint-disable testing-library/prefer-screen-queries */
+/* eslint-disable import/no-extraneous-dependencies */
+import React from 'react'
+import { render } from '@testing-library/react'
+import '@testing-library/jest-dom'
+
+import Progress from '../Progress'
+
+describe('Progress', () => {
+ it('renders the progress bar', () => {
+ const { getByTestId } = render(<Progress now={10} />)
+ expect(getByTestId('progress')).toBeInTheDocument()
+ })
+})
diff --git a/app/ui/__test__/Select.test.tsx b/app/ui/__test__/Select.test.tsx
new file mode 100644
index 000000000..58cc07313
--- /dev/null
+++ b/app/ui/__test__/Select.test.tsx
@@ -0,0 +1,56 @@
+/* eslint-disable testing-library/prefer-screen-queries */
+/* eslint-disable import/no-extraneous-dependencies */
+import React from 'react'
+import { fireEvent, render } from '@testing-library/react'
+import '@testing-library/jest-dom'
+import _forEach from 'lodash/forEach'
+import Select from '../Select'
+
+describe('Select', () => {
+ const defaultProps: {
+ title: string,
+ label: string,
+ className?: string,
+ items: any[],
+ id?: string,
+ labelExtractor?: (item: any, index: number) => string,
+ keyExtractor?: (item: any, index: number) => string,
+ iconExtractor?: (item: any, index: number) => JSX.Element | null,
+ onSelect: (item: any) => void,
+ } = {
+ title: 'Select Title',
+ label: 'Select',
+ items: [],
+ onSelect: () => {},
+ }
+
+ afterEach(() => {
+ jest.resetAllMocks()
+ })
+
+ it('should render without errors', () => {
+ const { getByText } = render(<Select {...defaultProps} />)
+ expect(getByText(defaultProps.label)).toBeInTheDocument()
+ })
+
+ it('should render the title when provided', () => {
+ const title = 'Select Title'
+ const { getByText } = render(<Select {...defaultProps} title={title} />)
+ expect(getByText(title)).toBeInTheDocument()
+ })
+
+ it('should render the description when provided', () => {
+ const description = 'Select description'
+ const { getByText } = render(<Select {...defaultProps} label={description} />)
+ expect(getByText(description)).toBeInTheDocument()
+ })
+
+ it('should render the items when provided', () => {
+ const items = ['Item 1', 'Item 2', 'Item 3']
+ const { getByText } = render(<Select {...defaultProps} items={items} />)
+ fireEvent.click(getByText(defaultProps.title))
+ _forEach(items, (item) => {
+ expect(getByText(item)).toBeInTheDocument()
+ })
+ })
+})
diff --git a/app/ui/__test__/TimezonePicker.test.tsx b/app/ui/__test__/TimezonePicker.test.tsx
new file mode 100644
index 000000000..009d7be72
--- /dev/null
+++ b/app/ui/__test__/TimezonePicker.test.tsx
@@ -0,0 +1,63 @@
+/* eslint-disable testing-library/prefer-screen-queries */
+/* eslint-disable import/no-extraneous-dependencies */
+import React from 'react'
+import { fireEvent, render } from '@testing-library/react'
+import '@testing-library/jest-dom'
+import { I18nextProvider } from 'react-i18next'
+import i18n from 'i18next'
+
+import TimezonePicker from '../TimezonePicker'
+
+describe('TimezonePricker', () => {
+ const onChange = jest.fn()
+
+ afterEach(() => {
+ jest.clearAllMocks()
+ })
+
+ const HocWithI18n = (props: any): JSX.Element => (
+ <I18nextProvider i18n={i18n}>
+ <TimezonePicker {...props} />
+ </I18nextProvider>
+ )
+
+ // initialize i18n before each test
+ beforeEach(() => {
+ i18n.init({
+ lng: 'en',
+ resources: {
+ en: {
+ profileSettings: {
+ timezoneDesc: 'Timezone description',
+ },
+ },
+ },
+ })
+ })
+
+ afterEach(() => {
+ jest.resetAllMocks()
+ })
+
+ it('renders the component', () => {
+ const { getByText } = render(
+ <HocWithI18n value='Europe/London' onChange={onChange} />,
+ )
+
+ expect(getByText('(GMT+1:00) Edinburgh, London')).toBeInTheDocument()
+ })
+
+ it('calls onChange function when an option is selected', () => {
+ const { getByText, getByRole } = render(
+ <HocWithI18n value='Europe/London' onChange={onChange} />,
+ )
+
+ fireEvent.click(getByRole('button'))
+
+ const option = getByText('(GMT-7:00) Arizona')
+ fireEvent.click(option)
+
+ expect(onChange).toHaveBeenCalledTimes(1)
+ expect(onChange).toHaveBeenCalledWith('America/Phoenix')
+ })
+})
diff --git a/app/ui/__test__/Tooltip.test.tsx b/app/ui/__test__/Tooltip.test.tsx
new file mode 100644
index 000000000..c7a2f0568
--- /dev/null
+++ b/app/ui/__test__/Tooltip.test.tsx
@@ -0,0 +1,37 @@
+/* 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 '@testing-library/jest-dom'
+import Tooltip from '../Tooltip'
+
+describe('Tooltip', () => {
+ it('should render the tooltip text when hovering over the icon', () => {
+ const text = 'This is a tooltip'
+ const { getByTestId, getByText } = render(
+ <Tooltip text={text} />,
+ )
+ const tooltipIcon = getByTestId('tooltip-icon')
+ fireEvent.mouseEnter(tooltipIcon)
+ const tooltipText = getByText(text)
+ expect(tooltipText).toBeInTheDocument()
+ })
+
+ it('should render a custom tooltip icon when provided', () => {
+ const CustomIcon = () => <div data-testid='custom-icon' />
+ const { getByTestId } = render(
+ <Tooltip tooltipNode={<CustomIcon />} text='dds' />,
+ )
+ const customIcon = getByTestId('custom-icon')
+ expect(customIcon).toBeInTheDocument()
+ })
+
+ it('should render a custom class name when provided', () => {
+ const className = 'custom-class'
+ const { getByTestId } = render(
+ <Tooltip text='Tooltip' className={className} />,
+ )
+ const tooltipWrapper = getByTestId('tooltip-wrapper')
+ expect(tooltipWrapper).toHaveClass(className)
+ })
+})
diff --git a/jest.config.js b/jest.config.js
new file mode 100644
index 000000000..32ca71429
--- /dev/null
+++ b/jest.config.js
@@ -0,0 +1,22 @@
+const config = {
+ testEnvironment: 'jsdom',
+ moduleFileExtensions: ['js', 'ts', 'tsx'],
+ transform: {
+ '^.+\\.tsx?$': 'ts-jest',
+ },
+ transformIgnorePatterns: ['node_modules/(?!axios)'],
+ testPathIgnorePatterns: [
+ '<rootDir>/app/api/*',
+ ],
+ globals: {
+ 'ts-jest': {
+ tsconfig: '<rootDir>/tsconfig.json',
+ isolatedModules: true,
+ },
+ },
+ moduleNameMapper: {
+ '^ui/(.*)': '<rootDir>/app/ui/$1',
+ },
+}
+
+module.exports = config
diff --git a/package.json b/package.json
index 4ef21db43..3835d9c76 100644
--- a/package.json
+++ b/package.json
@@ -6,6 +6,8 @@
"dev": "remix dev",
"dev:dev": "cross-env REACT_APP_RUNNING_DEV_SCRIPT=true remix dev",
"start": "remix-serve build",
+ "test": "jest --transformIgnorePatterns \"node_modules/(?!axios)/\"",
+ "test:ci": "CI=true npm run test",
"typecheck": "tsc",
"lint": "eslint app",
"lint:fix": "eslint app --fix",
@@ -24,8 +26,10 @@
"@remix-run/serve": "^1.16.1",
"@swetrix/sdk": "^1.0.0",
"@tailwindcss/forms": "^0.5.3",
+ "@testing-library/jest-dom": "^5.16.5",
"axios": "^1.4.0",
"axios-auth-refresh": "^3.3.6",
+ "babel-jest": "^29.5.0",
"billboard.js": "^3.8.0",
"clsx": "^1.2.1",
"d3": "^7.8.4",
@@ -62,7 +66,8 @@
"remix-sitemap": "^2.1.0",
"spacetime": "^7.4.3",
"swetrix": "^2.2.1",
- "timezone-soft": "^1.4.1"
+ "timezone-soft": "^1.4.1",
+ "ts-jest": "^29.1.0"
},
"eslintConfig": {
"extends": [
@@ -178,6 +183,7 @@
"@remix-run/dev": "^1.16.1",
"@remix-run/eslint-config": "^1.17.0",
"@tailwindcss/typography": "^0.5.9",
+ "@testing-library/react": "^14.0.0",
"@types/debug": "^4.1.7",
"@types/jest": "^29.5.1",
"@types/lodash": "^4.14.194",
@@ -188,6 +194,7 @@
"@types/react-flatpickr": "^3.8.8",
"@types/react-test-renderer": "^18.0.0",
"@types/redux-mock-store": "^1.0.3",
+ "@types/testing-library__jest-dom": "^5.14.6",
"@typescript-eslint/parser": "^5.59.8",
"@typescript-eslint/typescript-estree": "^5.59.8",
"autoprefixer": "^10.4.14",
@@ -199,6 +206,8 @@
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0",
"history": "^5.3.0",
+ "jest": "^29.5.0",
+ "jest-environment-jsdom": "^29.5.0",
"postcss": "^8.4.23",
"resize-observer-polyfill": "^1.5.1",
"source-map-explorer": "^2.5.3",