forked from janhq/jan
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests to model settings's Slider Input (janhq#3615)
* test: add tests to model settings's Slider Input * test: add slider test * test: fix expectation * test: correct slider test
- Loading branch information
Showing
9 changed files
with
128 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,3 +43,5 @@ electron/test-data | |
electron/test-results | ||
core/test_results.html | ||
coverage | ||
.yarn | ||
.yarnrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import React from 'react' | ||
import { render } from '@testing-library/react' | ||
import { fireEvent } from '@testing-library/dom' | ||
import SliderRightPanel from './index' | ||
import '@testing-library/jest-dom' | ||
|
||
class ResizeObserverMock { | ||
observe() {} | ||
unobserve() {} | ||
disconnect() {} | ||
} | ||
|
||
global.ResizeObserver = ResizeObserverMock | ||
|
||
jest.mock('@janhq/joi', () => ({ | ||
...jest.requireActual('@janhq/joi'), | ||
Slider: ({ children, onValueChange, ...props }: any) => ( | ||
<div data-testid="slider-root" {...props}> | ||
<input | ||
data-testid="slider-input" | ||
type="number" | ||
{...props} | ||
onChange={(e: any) => | ||
onValueChange && onValueChange([parseInt(e.target.value)]) | ||
} | ||
/> | ||
{children} | ||
</div> | ||
), | ||
})) | ||
|
||
describe('SliderRightPanel', () => { | ||
const defaultProps = { | ||
title: 'Test Slider', | ||
disabled: false, | ||
min: 0, | ||
max: 100, | ||
step: 1, | ||
description: 'This is a test slider', | ||
value: 50, | ||
onValueChanged: jest.fn(), | ||
} | ||
|
||
it('renders correctly with given props', () => { | ||
const { getByText } = render(<SliderRightPanel {...defaultProps} />) | ||
expect(getByText('Test Slider')).toBeInTheDocument() | ||
}) | ||
|
||
it('calls onValueChanged with correct value when input is changed', () => { | ||
defaultProps.onValueChanged = jest.fn() | ||
const { getByRole } = render(<SliderRightPanel {...defaultProps} />) | ||
|
||
const input = getByRole('textbox') | ||
fireEvent.change(input, { target: { value: '75' } }) | ||
expect(defaultProps.onValueChanged).toHaveBeenCalledWith(75) | ||
}) | ||
|
||
it('calls onValueChanged with correct value when slider is changed', () => { | ||
defaultProps.onValueChanged = jest.fn() | ||
const { getByTestId } = render(<SliderRightPanel {...defaultProps} />) | ||
|
||
const input = getByTestId('slider-input') | ||
fireEvent.change(input, { target: { value: '75' } }) | ||
expect(defaultProps.onValueChanged).toHaveBeenCalledWith(75) | ||
}) | ||
|
||
it('calls onValueChanged with max value when input exceeds max', () => { | ||
defaultProps.onValueChanged = jest.fn() | ||
const { getByRole } = render(<SliderRightPanel {...defaultProps} />) | ||
const input = getByRole('textbox') | ||
fireEvent.change(input, { target: { value: '150' } }) | ||
fireEvent.focusOut(input) | ||
expect(defaultProps.onValueChanged).toHaveBeenCalledWith(100) | ||
}) | ||
|
||
it('calls onValueChanged with min value when input is below min', () => { | ||
defaultProps.onValueChanged = jest.fn() | ||
const { getByRole } = render(<SliderRightPanel {...defaultProps} />) | ||
const input = getByRole('textbox') | ||
fireEvent.change(input, { target: { value: '0' } }) | ||
fireEvent.focusOut(input) | ||
expect(defaultProps.onValueChanged).toHaveBeenCalledWith(0) | ||
}) | ||
|
||
it('does not call onValueChanged when input is invalid', () => { | ||
defaultProps.onValueChanged = jest.fn() | ||
const { getByRole } = render(<SliderRightPanel {...defaultProps} />) | ||
const input = getByRole('textbox') | ||
fireEvent.change(input, { target: { value: 'invalid' } }) | ||
expect(defaultProps.onValueChanged).not.toHaveBeenCalledWith(0) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,15 @@ | ||
module.exports = { | ||
preset: 'ts-jest', | ||
testEnvironment: 'node', | ||
runner: './testRunner.js', | ||
const nextJest = require('next/jest') | ||
|
||
/** @type {import('jest').Config} */ | ||
const createJestConfig = nextJest({}) | ||
|
||
// Add any custom config to be passed to Jest | ||
const config = { | ||
coverageProvider: 'v8', | ||
testEnvironment: 'jsdom', | ||
// Add more setup options before each test is run | ||
// setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'], | ||
} | ||
|
||
// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async | ||
module.exports = createJestConfig(config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters