-
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(Components): ✅ add tests to Slider component item and root
- Loading branch information
1 parent
fa237d0
commit 0442dd9
Showing
2 changed files
with
69 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { render, screen } from '@testing-library/react' | ||
import { Item } from '../item.component' | ||
|
||
describe('Slider.Item', () => { | ||
it('should render children within a div element', () => { | ||
render( | ||
<Item> | ||
<div>TEST</div> | ||
</Item> | ||
) | ||
|
||
expect(screen.getByText('TEST')).toBeVisible() | ||
expect(screen.getByText('TEST').parentElement?.tagName).toBe('DIV') | ||
}) | ||
}) |
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,54 @@ | ||
import React from 'react' | ||
import { render, screen } from '@testing-library/react' | ||
import exp from 'constants' | ||
|
||
async function renderSliderRoot() { | ||
const { Root } = await import('../root.component') | ||
|
||
return render( | ||
<Root> | ||
<div>TEST</div> | ||
</Root> | ||
) | ||
} | ||
|
||
describe('Slider.Root', () => { | ||
it('should render children', async () => { | ||
await renderSliderRoot() | ||
expect(screen.getByText('TEST')).toBeVisible() | ||
}) | ||
|
||
it('should set left drag constraint according to scroll and offset widths', async () => { | ||
const scrollWidth = 100 | ||
const offsetWidth = 50 | ||
const mockSetState = jest.fn() | ||
|
||
jest | ||
.spyOn(HTMLElement.prototype, 'scrollWidth', 'get') | ||
.mockReturnValueOnce(scrollWidth) | ||
jest | ||
.spyOn(HTMLElement.prototype, 'offsetWidth', 'get') | ||
.mockReturnValueOnce(offsetWidth) | ||
jest.spyOn(React, 'useState').mockReturnValueOnce([0, mockSetState]) | ||
|
||
await renderSliderRoot() | ||
|
||
expect(mockSetState).toHaveBeenCalledWith(scrollWidth - offsetWidth) | ||
}) | ||
|
||
it('should set left drag constraint with default values', async () => { | ||
const mockSetState = jest.fn() | ||
|
||
jest | ||
.spyOn(HTMLElement.prototype, 'scrollWidth', 'get') | ||
.mockReturnValueOnce(undefined!) | ||
jest | ||
.spyOn(HTMLElement.prototype, 'offsetWidth', 'get') | ||
.mockReturnValueOnce(undefined!) | ||
jest.spyOn(React, 'useState').mockReturnValueOnce([0, mockSetState]) | ||
|
||
await renderSliderRoot() | ||
|
||
expect(mockSetState).toHaveBeenCalledWith(0) | ||
}) | ||
}) |