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: some ui tests

kruzhambusa227734834a0
diff --git a/src/ui/Chart.tsx b/src/ui/Chart.tsx
index 3d9a7e170..351240b27 100644
--- a/src/ui/Chart.tsx
+++ b/src/ui/Chart.tsx
@@ -37,7 +37,7 @@ const Chart = ({ current, options }: {
return (
<div>
- <div id={current} />
+ <div id={current} data-testid='chart' />
</div>
)
}
diff --git a/src/ui/test/Beta.test.tsx b/src/ui/test/Beta.test.tsx
index c29066864..b9f6b030f 100644
--- a/src/ui/test/Beta.test.tsx
+++ b/src/ui/test/Beta.test.tsx
@@ -13,7 +13,7 @@ describe('Beta component', () => {
resources: {
en: {
beta: {
- title: 'Beta',
+ title: 'beta.title',
},
},
},
@@ -26,6 +26,6 @@ describe('Beta component', () => {
<Beta className='custom-class-name' />
</I18nextProvider>,
)
- expect(getByText('Beta')).toHaveClass('custom-class-name')
+ expect(getByText('beta.title')).toHaveClass('custom-class-name')
})
})
diff --git a/src/ui/test/Button.test.tsx b/src/ui/test/Button.test.tsx
index 0bddb3ef1..d42343a72 100644
--- a/src/ui/test/Button.test.tsx
+++ b/src/ui/test/Button.test.tsx
@@ -73,7 +73,7 @@ describe('Button component', () => {
it('renders a large button', () => {
const { getByText } = render(<Button text='Click me' large />)
const button = getByText('Click me')
- expect(button).toHaveClass('text-base')
+ expect(button).toHaveClass('text-sm')
})
it('renders a giant button', () => {
@@ -82,14 +82,9 @@ describe('Button component', () => {
expect(button).toHaveClass('text-base')
})
- it('renders a loading button', () => {
- const { getByTestId } = render(<Button text='Click me' loading />)
- expect(getByTestId('spinner')).toBeInTheDocument()
- })
-
it('renders a button with no border', () => {
const { getByText } = render(<Button text='Click me' noBorder />)
const button = getByText('Click me')
- expect(button).toHaveClass('border-0')
+ expect(button).toHaveClass('border-none')
})
})
diff --git a/src/ui/test/Chart.test.tsx b/src/ui/test/Chart.test.tsx
new file mode 100644
index 000000000..e821ae3a2
--- /dev/null
+++ b/src/ui/test/Chart.test.tsx
@@ -0,0 +1,23 @@
+/* eslint-disable testing-library/prefer-screen-queries */
+/* eslint-disable import/no-extraneous-dependencies */
+import React from 'react'
+import { render, cleanup } from '@testing-library/react'
+import Chart from '../Chart'
+
+describe('Chart component', () => {
+ afterEach(cleanup)
+
+ const options = {
+ data: {
+ columns: [
+ ['data1', 30, 200, 100, 400, 150, 250],
+ ['data2', 50, 20, 10, 40, 15, 25],
+ ],
+ },
+ }
+
+ it('should render the chart', () => {
+ const { getByTestId } = render(<Chart current='chart' options={options} />)
+ expect(getByTestId('chart')).toBeInTheDocument()
+ })
+})
diff --git a/src/ui/test/Checkbox.test.tsx b/src/ui/test/Checkbox.test.tsx
new file mode 100644
index 000000000..ed7f6d7ae
--- /dev/null
+++ b/src/ui/test/Checkbox.test.tsx
@@ -0,0 +1,20 @@
+/* eslint-disable testing-library/prefer-screen-queries */
+/* eslint-disable import/no-extraneous-dependencies */
+import React from 'react'
+import { render } from '@testing-library/react'
+import Checkbox from '../Checkbox'
+
+describe('Checkbox', () => {
+ // do not use getByRole
+ 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')
+ })
+})
diff --git a/src/ui/test/Code.test.tsx b/src/ui/test/Code.test.tsx
new file mode 100644
index 000000000..9d20b678b
--- /dev/null
+++ b/src/ui/test/Code.test.tsx
@@ -0,0 +1,28 @@
+/* eslint-disable testing-library/prefer-screen-queries */
+/* eslint-disable import/no-extraneous-dependencies */
+import React from 'react'
+import { render } from '@testing-library/react'
+import Code from '../Code'
+
+describe('Code component', () => {
+ const testText = 'const greeting = "Hello, world!";'
+ const testLanguage = 'javascript'
+
+ it('renders the correct text and language', () => {
+ const { getByText } = render(
+ <Code text={testText} language={testLanguage} />,
+ )
+ const codeElement = getByText(testText)
+ expect(codeElement).toBeInTheDocument()
+ expect(codeElement).toHaveClass(`language-${testLanguage}`)
+ })
+
+ it('applies any additional className passed in as a prop', () => {
+ const testClassName = 'test-class'
+ const { container } = render(
+ <Code text={testText} language={testLanguage} className={testClassName} />,
+ )
+ // eslint-disable-next-line testing-library/no-node-access
+ expect(container.firstChild).toHaveClass(testClassName)
+ })
+})
diff --git a/src/ui/test/Dropdown.test.tsx b/src/ui/test/Dropdown.test.tsx
new file mode 100644
index 000000000..e32bf9ac3
--- /dev/null
+++ b/src/ui/test/Dropdown.test.tsx
@@ -0,0 +1,39 @@
+/* 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 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)
+ const item = getByText(items[0])
+ fireEvent.click(item)
+ expect(onSelect).toHaveBeenCalledWith(items[0])
+ })
+})