|
| 1 | +import { act, describe, expect, fireEvent, it, render, vi } from '@test/utils'; |
| 2 | +import React from 'react'; |
| 3 | + |
| 4 | +import { Color, getColorObject } from '../../_common/js/color-picker'; |
| 5 | +import ColorPicker, { ColorPickerProps } from '../index'; |
| 6 | +import { ColorFormat, TypeEnum } from '../type'; |
| 7 | + |
| 8 | +const prefix = 't'; |
| 9 | +const name = `.${prefix}-color-picker`; |
| 10 | + |
| 11 | +const makeTouch = ( |
| 12 | + el: Element, |
| 13 | + eventName: string, |
| 14 | + touchPosition?: { clientX?: number; clientY?: number; pageX?: number; pageY?: number }, |
| 15 | +) => { |
| 16 | + const touchInit = { |
| 17 | + changedTouches: [ |
| 18 | + new Touch({ |
| 19 | + identifier: 0, |
| 20 | + target: el, |
| 21 | + clientX: touchPosition?.clientX || 0, |
| 22 | + clientY: touchPosition?.clientY || 0, |
| 23 | + pageX: touchPosition?.pageX || 0, |
| 24 | + pageY: touchPosition?.pageY || 0, |
| 25 | + }), |
| 26 | + ], |
| 27 | + bubbles: true, |
| 28 | + cancelable: true, |
| 29 | + }; |
| 30 | + |
| 31 | + const event = new TouchEvent(eventName, touchInit); |
| 32 | + el.dispatchEvent(event); |
| 33 | +}; |
| 34 | + |
| 35 | +const mockBoundingClientRect = (info) => { |
| 36 | + window.HTMLElement.prototype.getBoundingClientRect = () => info; |
| 37 | +}; |
| 38 | + |
| 39 | +const renderColorPicker = (props: ColorPickerProps) => render(<ColorPicker {...props} />); |
| 40 | + |
| 41 | +describe('ColorPicker', () => { |
| 42 | + describe('props', () => { |
| 43 | + it(': multiple', () => { |
| 44 | + const testCurrentProp = (type: TypeEnum, target: number) => { |
| 45 | + const { container } = renderColorPicker({ type }); |
| 46 | + const dom = container.querySelectorAll(`${name}__saturation`); |
| 47 | + expect(dom).toHaveLength(target); |
| 48 | + }; |
| 49 | + testCurrentProp(undefined, 0); |
| 50 | + testCurrentProp('base', 0); |
| 51 | + testCurrentProp('multiple', 1); |
| 52 | + }); |
| 53 | + |
| 54 | + it(': enableAlpha', () => { |
| 55 | + const testEnableAlpha = (enableAlpha: boolean) => { |
| 56 | + const { container } = renderColorPicker({ enableAlpha, type: 'multiple' }); |
| 57 | + const alphaDom = container.querySelectorAll(`${name}__slider-wrapper--alpha-type`); |
| 58 | + expect(alphaDom).toHaveLength(enableAlpha ? 1 : 0); |
| 59 | + }; |
| 60 | + testEnableAlpha(false); |
| 61 | + testEnableAlpha(true); |
| 62 | + }); |
| 63 | + |
| 64 | + it(': swatchColors', () => { |
| 65 | + const testSwatchColors = (swatchColors: Array<string> | null, target: number) => { |
| 66 | + const { container } = renderColorPicker({ swatchColors }); |
| 67 | + const dom = container.querySelectorAll(`${name}__swatches-item`); |
| 68 | + expect(dom).toHaveLength(target); |
| 69 | + }; |
| 70 | + testSwatchColors(null, 0); |
| 71 | + testSwatchColors([], 0); |
| 72 | + testSwatchColors(undefined, 10); |
| 73 | + testSwatchColors(['red', 'blur'], 2); |
| 74 | + }); |
| 75 | + |
| 76 | + it(': format', () => { |
| 77 | + const testFormat = (format: string, target: ColorFormat) => { |
| 78 | + const { container } = renderColorPicker({ format: format as ColorFormat, type: 'multiple' }); |
| 79 | + const dom = container.querySelector(`${name}__format-item--first`); |
| 80 | + expect(dom.innerHTML).toBe(target); |
| 81 | + }; |
| 82 | + testFormat('RGB', 'RGB'); |
| 83 | + testFormat('123', 'RGB'); |
| 84 | + testFormat('HEX', 'HEX'); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('events', () => { |
| 89 | + it(': preset change', async () => { |
| 90 | + const onChange = vi.fn(); |
| 91 | + const { container } = renderColorPicker({ onChange }); |
| 92 | + const swatch = container.querySelector(`${name}__swatches-item`); |
| 93 | + |
| 94 | + fireEvent.click(swatch); |
| 95 | + const result = 'rgb(236, 242, 254)'; |
| 96 | + |
| 97 | + expect(onChange).toHaveBeenCalledTimes(1); |
| 98 | + expect(onChange).toHaveBeenLastCalledWith(result, { |
| 99 | + trigger: 'preset', |
| 100 | + color: getColorObject(new Color(result)), |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + it(': saturation change', async () => { |
| 105 | + const testSaturation = async (fixed = false) => { |
| 106 | + const onPaletteBarChange = vi.fn(); |
| 107 | + const { container } = renderColorPicker({ onPaletteBarChange, type: 'multiple', fixed }); |
| 108 | + const el = container.querySelector('.t-color-picker__saturation'); |
| 109 | + |
| 110 | + mockBoundingClientRect({ |
| 111 | + left: 0, |
| 112 | + top: 0, |
| 113 | + width: 300, |
| 114 | + height: 50, |
| 115 | + }); |
| 116 | + |
| 117 | + act(() => { |
| 118 | + makeTouch(el, 'touchstart'); |
| 119 | + makeTouch(el, 'touchmove', { pageY: 40, pageX: 0, clientY: 40 }); |
| 120 | + makeTouch(el, 'touchmove', { pageY: 40, pageX: 0, clientY: 40 }); |
| 121 | + makeTouch(el, 'touchmove', { pageY: 30, pageX: 0, clientY: 30 }); |
| 122 | + makeTouch(el, 'touchend', { pageY: 30, pageX: 30, clientY: 20 }); |
| 123 | + }); |
| 124 | + |
| 125 | + expect(onPaletteBarChange).toHaveBeenCalledTimes(3); |
| 126 | + const result = 'rgba(80, 80, 80, 1)'; |
| 127 | + const color = new Color(result); |
| 128 | + color.saturation = 0; |
| 129 | + color.value = fixed ? 0.4 : 0.8214285714285714; |
| 130 | + |
| 131 | + expect(onPaletteBarChange).toHaveBeenLastCalledWith({ |
| 132 | + color: getColorObject(color), |
| 133 | + }); |
| 134 | + }; |
| 135 | + |
| 136 | + await testSaturation(); |
| 137 | + await testSaturation(true); |
| 138 | + }); |
| 139 | + |
| 140 | + it(': hue slider change', async () => { |
| 141 | + const onChange = vi.fn(); |
| 142 | + const { container } = renderColorPicker({ onChange, type: 'multiple' }); |
| 143 | + const el = container.querySelector('.t-color-picker__slider'); |
| 144 | + |
| 145 | + mockBoundingClientRect({ |
| 146 | + left: 0, |
| 147 | + top: 0, |
| 148 | + width: 300, |
| 149 | + height: 50, |
| 150 | + }); |
| 151 | + |
| 152 | + act(() => { |
| 153 | + makeTouch(el, 'touchstart', { pageY: 0, pageX: 0, clientY: 30 }); |
| 154 | + makeTouch(el, 'touchmove', { pageY: 0, pageX: 30, clientY: 30 }); |
| 155 | + makeTouch(el, 'touchend', { pageY: 30, pageX: 40, clientY: 30 }); |
| 156 | + }); |
| 157 | + |
| 158 | + expect(onChange).toHaveBeenCalledTimes(2); |
| 159 | + const result = 'rgb(151, 91, 0)'; |
| 160 | + expect(onChange).toHaveBeenLastCalledWith(result, { |
| 161 | + trigger: 'palette-hue-bar', |
| 162 | + color: getColorObject(new Color(result)), |
| 163 | + }); |
| 164 | + }); |
| 165 | + |
| 166 | + it(': alpha slider change', async () => { |
| 167 | + const onChange = vi.fn(); |
| 168 | + const { container } = renderColorPicker({ onChange, type: 'multiple', enableAlpha: true }); |
| 169 | + const el = container.querySelector('.t-color-picker__slider-wrapper--alpha-type .t-color-picker__slider'); |
| 170 | + |
| 171 | + mockBoundingClientRect({ |
| 172 | + left: 0, |
| 173 | + top: 0, |
| 174 | + width: 300, |
| 175 | + height: 50, |
| 176 | + }); |
| 177 | + act(() => { |
| 178 | + makeTouch(el, 'touchstart', { pageY: 0, pageX: 0, clientY: 30 }); |
| 179 | + makeTouch(el, 'touchmove', { pageY: 0, pageX: 40, clientY: 30 }); |
| 180 | + makeTouch(el, 'touchend', { pageY: 30, pageX: 40, clientY: 30 }); |
| 181 | + }); |
| 182 | + |
| 183 | + expect(onChange).toHaveBeenCalledTimes(2); |
| 184 | + const result = 'rgb(0, 31, 151)'; |
| 185 | + const color = new Color(result); |
| 186 | + color.alpha = 0.13; |
| 187 | + expect(onChange).toHaveBeenLastCalledWith(result, { |
| 188 | + trigger: 'palette-alpha-bar', |
| 189 | + color: getColorObject(color), |
| 190 | + }); |
| 191 | + }); |
| 192 | + }); |
| 193 | +}); |
0 commit comments