This commit is contained in:
2024-10-19 08:16:51 +03:00
parent 17697d7f77
commit 07728cbbb0
24 changed files with 1875 additions and 16 deletions

View File

@@ -0,0 +1,18 @@
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import ActionButton from '../src/components/account/ActionButton.jsx';
describe('ActionButton Component', () => {
it('should render with the correct title and call the action when clicked', () => {
const mockAction = jest.fn();
const title = 'Click Me';
render(<ActionButton action={mockAction} title={title} />);
const buttonElement = screen.getByText(title);
expect(buttonElement).toBeInTheDocument();
fireEvent.click(buttonElement);
expect(mockAction).toHaveBeenCalledTimes(1);
});
});

View File

@@ -0,0 +1,23 @@
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import Card from '../src/components/home/Card.jsx';
describe('Card Component', () => {
it('should render the Card component with the given ID', () => {
const testId = '123';
render(<Card id={testId} />);
const cardElement = screen.getByText(/123/i);
expect(cardElement).toBeInTheDocument();
});
it('should store the ID in local storage when clicked', () => {
const testId = '456';
render(<Card id={testId} />);
const cardElement = screen.getByText(/456/i);
fireEvent.click(cardElement);
expect(localStorage.setItem).toHaveBeenCalledWith('selectedId', testId);
});
});

View File

@@ -0,0 +1,24 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import HelloItem from '../src/components/account/HelloItem.jsx';
describe('HelloItem Component', () => {
it('should display a personalized greeting when nickname is provided', () => {
const nickname = 'JohnDoe';
const id = '12345';
render(<HelloItem nickname={nickname} id={id} />);
const greetingElement = screen.getByText(`Hello, ${nickname}!`);
const idElement = screen.getByText(`Your ID: ${id}`);
expect(greetingElement).toBeInTheDocument();
expect(idElement).toBeInTheDocument();
});
it('should display a default message when nickname is not provided', () => {
render(<HelloItem nickname="" id="12345" />);
const defaultMessage = screen.getByText("You don't have an account :(");
expect(defaultMessage).toBeInTheDocument();
});
});

View File

@@ -0,0 +1,41 @@
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import InputField from '../src/components/reg/InputField.jsx';
describe('InputField Component', () => {
it('should render with the correct title and placeholder', () => {
const title = 'Username';
const placeholder = 'Enter your username';
render(<InputField title={title} placeholder={placeholder} value="" setValue={() => {}} />);
const titleElement = screen.getByText(title);
const inputElement = screen.getByPlaceholderText(placeholder);
expect(titleElement).toBeInTheDocument();
expect(inputElement).toBeInTheDocument();
});
it('should call setValue on input change', () => {
const mockSetValue = jest.fn();
const newValue = 'testUser';
render(<InputField title="Username" value="" setValue={mockSetValue} />);
const inputElement = screen.getByRole('textbox');
fireEvent.change(inputElement, { target: { value: newValue } });
expect(mockSetValue).toHaveBeenCalledWith(newValue);
});
it('should call submit function when Enter key is pressed', () => {
const mockSubmit = jest.fn();
render(<InputField title="Username" value="" setValue={() => {}} submit={mockSubmit} />);
const inputElement = screen.getByRole('textbox');
fireEvent.keyDown(inputElement, { key: 'Enter', code: 'Enter' });
expect(mockSubmit).toHaveBeenCalledTimes(1);
});
});

View File

@@ -0,0 +1,16 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import NavButton from '../src/components/init/NavButton.jsx';
describe('NavButton Component', () => {
it('should render the NavButton with the correct text and link', () => {
const navLink = '/home';
const buttonText = 'Go Home';
render(<NavButton nav={navLink} text={buttonText} />);
const linkElement = screen.getByText(buttonText);
expect(linkElement).toBeInTheDocument();
expect(linkElement.closest('a')).toHaveAttribute('href', navLink);
});
});

23
__tests__/Search.test.tsx Normal file
View File

@@ -0,0 +1,23 @@
import React from 'react';
import { render, screen, fireEvent } from '@testing-library/react';
import Search from '../src/components/home/Search.jsx';
describe('Search Component', () => {
it('should render the Search button', () => {
render(<Search search={() => {}} item="testItem" />);
const searchButton = screen.getByText(/find/i);
expect(searchButton).toBeInTheDocument();
});
it('should call the search function with the correct item when clicked', () => {
const mockSearch = jest.fn();
const item = 'testItem';
render(<Search search={mockSearch} item={item} />);
const searchButton = screen.getByText(/find/i);
fireEvent.click(searchButton);
expect(mockSearch).toHaveBeenCalledWith(item);
});
});