/* jest */ import React from 'react' import {renderHook} from '@testing-library/react-hooks' import {render, act, fireEvent, screen} from '@testing-library/react' import { Slider, Thumb, Track, useFocused, useControls, useValue, useDisabled, useOrientation, } from './index' import * as raf from '@essentials/raf' // @ts-ignore beforeEach(raf.reset) describe(``, () => { it('should provide context to function child', () => { let value render( {(context) => { value = context return
}} ) expect(value).toMatchSnapshot('context') }) it(`should add 'min' attribute to input`, () => { expect( render(
).asFragment() ).toMatchSnapshot('min=20') }) it(`should add 'max' attribute to input`, () => { expect( render(
).asFragment() ).toMatchSnapshot('max=50') }) it(`should add 'step' attribute to input`, () => { expect( render(
).asFragment() ).toMatchSnapshot('step=2') }) it(`should have defaultValue`, () => { expect( render(
).asFragment() ).toMatchSnapshot('value=2') }) it(`should have controlled value`, () => { let value = 2 const onChange = (nextValue: number) => { value = nextValue } render(
).asFragment() fireEvent.change(screen.getByTestId('slider'), {target: {value: '3'}}) expect(value).toBe(3) }) it(`should round based upon the step`, () => { expect( render(
).asFragment() ).toMatchSnapshot('value=40') }) it(`should throw if value < min`, () => { expect(() => render(
) ).toThrowErrorMatchingSnapshot() }) it(`should throw if value > max`, () => { expect(() => render(
) ).toThrowErrorMatchingSnapshot() }) it(`should throw if min > max`, () => { expect(() => render(
) ).toThrowErrorMatchingSnapshot() }) it(`should override w/ child aria-hidden`, () => { expect( render(
).asFragment() ).toMatchSnapshot('aria-hidden=false') }) it(`should add disabled attribute to input`, () => { expect( render(
).asFragment() ).toMatchSnapshot('disabled=""') }) it('should provide orientation to context', () => { let value render( {(context) => { value = context.orientation return
}} ) expect(value).toBe('vertical') }) it('should provide disabled to context', () => { let value render( {(context) => { value = context.disabled return
}} ) expect(value).toBe(true) }) it('should provide step to context', () => { let value render( {(context) => { value = context.step return
}} ) expect(value).toBe(2) }) it('should provide min to context', () => { let value render( {(context) => { value = context.min return
}} ) expect(value).toBe(2) }) it('should provide max to context', () => { let value render( {(context) => { value = context.max return
}} ) expect(value).toBe(200) }) it('should provide value to context', () => { let value render( {(context) => { value = context.value return
}} ) expect(value).toBe(20) }) }) describe(``, () => { it('should update progress when value changes', () => { const result = render(
) expect(result.asFragment()).toMatchSnapshot('20%') result.rerender(
) expect(result.asFragment()).toMatchSnapshot('30%') }) it('should change position w/ orientation', () => { const result = render(
) expect(result.asFragment()).toMatchSnapshot('left=20%') result.rerender(
) expect(result.asFragment()).toMatchSnapshot('bottom=30%') }) it('should apply custom styles', () => { const result = render(
) expect(result.asFragment()).toMatchSnapshot('transform') }) it('should update value on change', () => { const {getByTestId, asFragment} = render(
) fireEvent.change(getByTestId('slider'), {target: {value: 20}}) expect(asFragment()).toMatchSnapshot('value=20') }) }) describe(``, () => { const mockRect = (values) => { // @ts-ignore Element.prototype.getBoundingClientRect = jest.fn(() => { return { width: 300, height: 20, top: 0, left: 0, bottom: 0, right: 0, ...values, } }) } const fireMouse = (node, eventType, values) => { node.dispatchEvent( new (eventType.includes('mouse') ? MouseEvent : TouchEvent)(eventType, { bubbles: true, cancelable: true, ...values, }) ) } it(`should respond to mouse events`, () => { mockRect({width: 300, right: 300, height: 20, top: 20}) const {getByTestId, asFragment} = render(
) fireEvent.mouseDown(getByTestId('track'), {clientX: 30, clientY: 10}) expect(asFragment()).toMatchSnapshot('value=10') act(() => { fireMouse(window, 'mousemove', {clientX: 60, clientY: 10}) // @ts-ignore raf.step({count: 1}) }) expect(asFragment()).toMatchSnapshot('value=20') act(() => { fireMouse(window, 'mouseup', {clientX: 120, clientY: 10}) }) expect(asFragment()).toMatchSnapshot('value=40') act(() => { fireMouse(window, 'mousemove', {clientX: 90, clientY: 10}) // @ts-ignore raf.step({count: 1}) }) expect(asFragment()).toMatchSnapshot('value=40 [despite move]') }) it(`should not respond to mousemove if current tick is unresolved`, () => { mockRect({width: 300, right: 300, height: 20, top: 20}) const {getByTestId, asFragment} = render(
) fireEvent.mouseDown(getByTestId('track'), {clientX: 150, clientY: 10}) act(() => { fireMouse(window, 'mousemove', {clientX: 60, clientY: 10}) }) expect(asFragment()).toMatchSnapshot('value=50') act(() => { fireMouse(window, 'mousemove', {clientX: 90, clientY: 10}) }) expect(asFragment()).toMatchSnapshot('value=50 [despite move]') act(() => { // @ts-ignore raf.step({count: 1}) }) // should use the last event despite ticks expect(asFragment()).toMatchSnapshot('value=30') }) it(`should respond to touch events`, () => { mockRect({width: 300, right: 300, height: 20, bottom: 20}) const {getByTestId, asFragment} = render(
) fireEvent.touchStart(getByTestId('track'), { changedTouches: [{clientX: 30, clientY: 10}], }) expect(asFragment()).toMatchSnapshot('value=10') act(() => { fireMouse(window, 'touchmove', { changedTouches: [{clientX: 60, clientY: 10}], }) // @ts-ignore raf.step({count: 1}) }) expect(asFragment()).toMatchSnapshot('value=20') act(() => { fireMouse(window, 'touchend', { changedTouches: [{clientX: 120, clientY: 10}], }) }) expect(asFragment()).toMatchSnapshot('value=40') act(() => { fireMouse(window, 'touchmove', { changedTouches: [{clientX: 90, clientY: 10}], }) // @ts-ignore raf.step({count: 1}) }) expect(asFragment()).toMatchSnapshot('value=40 [despite move]') }) it(`should not respond to up without a preceding down`, () => { mockRect({width: 300, right: 300, height: 20, top: 20}) const {asFragment} = render(
) act(() => { fireMouse(window, 'mouseup', {clientX: 120, clientY: 10}) }) expect(asFragment()).toMatchSnapshot('value=50 [despite up]') }) it(`should not respond to down if already down`, () => { mockRect({width: 300, right: 300, height: 20, top: 20}) const {getByTestId, asFragment} = render(
) fireEvent.mouseDown(getByTestId('track'), {clientX: 30, clientY: 10}) expect(asFragment()).toMatchSnapshot('value=10') fireEvent.mouseDown(getByTestId('track'), {clientX: 60, clientY: 10}) expect(asFragment()).toMatchSnapshot('value=10 [despite down]') }) it(`should change w/ orientation`, () => { mockRect({width: 20, right: 20, height: 300, bottom: 300}) const {getByTestId, asFragment} = render(
) fireEvent.mouseDown(getByTestId('track'), {clientX: 10, clientY: 270}) expect(asFragment()).toMatchSnapshot('value=10') act(() => { fireMouse(window, 'mousemove', {clientX: 10, clientY: 240}) // @ts-ignore raf.step({count: 1}) }) expect(asFragment()).toMatchSnapshot('value=20') act(() => { fireMouse(window, 'mouseup', {clientX: 10, clientY: 180}) }) expect(asFragment()).toMatchSnapshot('value=40') act(() => { fireMouse(window, 'mousemove', {clientX: 10, clientY: 90}) // @ts-ignore raf.step({count: 1}) }) expect(asFragment()).toMatchSnapshot('value=40 [despite move]') }) it(`should apply custom styles`, () => { expect( render(
).asFragment() ).toMatchSnapshot('width=20, height=300') }) }) describe('useFocused()', () => { it('should be `true` when focused, `false` when blurred', () => { const Focusable = () => { return <>{String(useFocused())} } const result = render( ) expect(result.asFragment()).toMatchSnapshot('false') fireEvent.focus(result.getByTestId('slider')) expect(result.asFragment()).toMatchSnapshot('true') fireEvent.blur(result.getByTestId('slider')) expect(result.asFragment()).toMatchSnapshot('false') }) }) describe('useControls()', () => { it('should have `incr`, `decr`, `set` keys', () => { const {result} = renderHook(() => useControls(), {wrapper: Slider}) expect(Object.keys(result.current)).toStrictEqual(['incr', 'decr', 'set']) }) it('should change value', () => { const Component = () => { const {incr, decr, set} = useControls() return ( <>