Skip to content

Commit

Permalink
test(Components): ✅ add tests to Slider component item and root
Browse files Browse the repository at this point in the history
  • Loading branch information
emiliosheinz committed Nov 29, 2023
1 parent fa237d0 commit 0442dd9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/components/slider/__tests__/item.component.test.tsx
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')
})
})
54 changes: 54 additions & 0 deletions src/components/slider/__tests__/root.component.test.tsx
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)
})
})

0 comments on commit 0442dd9

Please sign in to comment.