Skip to content

Commit

Permalink
test: fix test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Aarebecca committed Sep 19, 2024
1 parent 022f14e commit 5efc22e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
27 changes: 27 additions & 0 deletions __tests__/unit/color/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
import { gradient, toCSSGradient, toRGB } from '../../../src';

describe('color', function () {
let mockGetComputedStyle;
beforeAll(() => {
const colorMap = {
red: 'rgb(255, 0, 0)',
white: 'rgb(255, 255, 255)',
black: 'rgb(0, 0, 0)',
blue: 'rgb(0, 0, 255)',
'#ddd': 'rgb(221, 221, 221)',
'#eeeeee': 'rgb(238, 238, 238)',
};

// implement document defaultView getComputedStyle getPropertyValue in jsdom
mockGetComputedStyle = jest.spyOn(window, 'getComputedStyle').mockImplementation(
(el) =>
({
getPropertyValue: () => {
const color = (el as HTMLElement).style.color;
return colorMap[color] || color;
},
}) as any,
);
});

afterAll(() => {
mockGetComputedStyle?.mockRestore();
});

it('toRGB', () => {
expect(toRGB('red')).toBe('#ff0000');
expect(toRGB('white')).toBe('#ffffff');
Expand Down
3 changes: 1 addition & 2 deletions src/color/torgb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ function toRGBString(color: string): string {

iEl.style.color = color;

let rst = document.defaultView.getComputedStyle(iEl, '').getPropertyValue('color');

let rst = window.getComputedStyle(iEl, '').getPropertyValue('color');
const matches = RGB_REG.exec(rst) as string[];
const cArray: number[] = matches[1].split(/\s*,\s*/).map((s) => Number(s));

Expand Down
2 changes: 1 addition & 1 deletion src/path/process/reverse-curve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { CurveArray } from '../types';

// reverse CURVE based pathArray segments only
export function reverseCurve(pathArray: CurveArray): CurveArray {
const rotatedCurve = pathArray
const rotatedCurve: any = pathArray
.slice(1)
.map((x, i, curveOnly) =>
// @ts-ignore
Expand Down
9 changes: 4 additions & 5 deletions src/path/util/equalize-segments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import type { CurveArray, PathArray } from '../types';
import { midPoint } from './mid-point';
import { segmentCubicFactory } from './segment-cubic-factory';

function splitCubic(
pts: [number, number, number, number, number, number, number, number],
t = 0.5,
): [CurveArray, CurveArray] {
type SplitArray = [number, number, number, number, number, number, number, number, number];

function splitCubic(pts: SplitArray, t = 0.5): [CurveArray, CurveArray] {
const p0 = pts.slice(0, 2) as [number, number];
const p1 = pts.slice(2, 4) as [number, number];
const p2 = pts.slice(4, 6) as [number, number];
Expand All @@ -28,7 +27,7 @@ function splitCubic(
function getCurveArray(segments: PathArray) {
return segments.map((segment, i, pathArray) => {
// @ts-ignore
const segmentData = i && pathArray[i - 1].slice(-2).concat(segment.slice(1));
const segmentData = i && (pathArray[i - 1].slice(-2).concat(segment.slice(1)) as SplitArray);

// @ts-ignore
const curveLength = i
Expand Down

0 comments on commit 5efc22e

Please sign in to comment.