diff --git a/test/ut/spec/animation/ElementAnimation.test.ts b/test/ut/spec/animation/ElementAnimation.test.ts index 26b528124..1bef83fdc 100644 --- a/test/ut/spec/animation/ElementAnimation.test.ts +++ b/test/ut/spec/animation/ElementAnimation.test.ts @@ -1,4 +1,4 @@ -import {Polyline, Rect, init} from '../zrender'; +import {Polyline, Rect} from '../zrender'; import Animation from '../../../../src/animation/Animation'; describe('ElementAnimation', function () { diff --git a/test/ut/spec/animation/clip.test.ts b/test/ut/spec/animation/clip.test.ts new file mode 100644 index 000000000..957466087 --- /dev/null +++ b/test/ut/spec/animation/clip.test.ts @@ -0,0 +1,102 @@ +import Clip from '../../../../src/animation/Clip'; +import easingFuncs from '../../../../src/animation/easing'; + +describe('clip', function () { + const life = 2000; + const interval = 200; + const delay = 300; + /** '2022/12/22 00:42:45' */ + const now = 1671640965219; + it('normal clip call onframe correct', () => { + const onframe = jest.fn(); + const attClip = new Clip({ + life, + onframe + }); + attClip.step(now, 100); + attClip.step(now + interval, 100); + attClip.step(now + interval + life, 100); + expect(onframe).toHaveBeenNthCalledWith(1, 0); + expect(onframe).toHaveBeenNthCalledWith(2, interval / life); + expect(onframe).toHaveBeenNthCalledWith(3, 1); + }); + + it('delay clip call onframe correct', () => { + const onframe = jest.fn(); + + const attClip = new Clip({ + life, + onframe, + delay + }); + attClip.step(now, 100); + attClip.step(now + interval, 100); + attClip.step(now + interval + delay, 100); + expect(onframe).toHaveBeenNthCalledWith(1, 0); + expect(onframe).toHaveBeenNthCalledWith(2, 0); + expect(onframe).toHaveBeenNthCalledWith(3, interval / life); + }); + + it('loop clip call onframe correct', () => { + const onframe = jest.fn(); + const onrestart = jest.fn(); + + const attClip = new Clip({ + life, + onframe, + loop: true, + onrestart + }); + attClip.step(now, 100); + attClip.step(now + interval, 100); + attClip.step(now + interval + life, 100); + attClip.step(now + interval + life + 100, 100); + expect(onframe).toHaveBeenNthCalledWith(1, 0); + expect(onframe).toHaveBeenNthCalledWith(2, interval / life); + expect(onframe).toHaveBeenNthCalledWith(3, 1); + expect(onframe).toHaveBeenNthCalledWith(4, (interval + 100) / life); + expect(onrestart).toBeCalledTimes(1); + }); + + it('clip pause correct', () => { + const onframe = jest.fn(); + const onrestart = jest.fn(); + + const attClip = new Clip({ + life, + onframe, + loop: true, + onrestart + }); + attClip.pause(); + attClip.step(now, interval); + attClip.step(now + interval, interval); + attClip.resume(); + // pause two interval + attClip.step(now + interval + interval + interval, interval); + expect(onframe).toBeCalledTimes(1); + expect(onframe).toHaveBeenNthCalledWith(1, interval / life); + }); + + const buildInEasing = Object.keys(easingFuncs) as Array; + + test.each(buildInEasing)('setEasing buildIn %s correct', (easingName) => { + const onframe = jest.fn(); + const onrestart = jest.fn(); + + const attClip = new Clip({ + life, + onframe, + onrestart + }); + attClip.setEasing(easingName); + /** init */ + attClip.step(now, interval); + attClip.step(now + interval, interval); + attClip.step(now + 2 * interval, interval); + expect(onframe).toBeCalledTimes(3); + expect(onframe).toHaveBeenNthCalledWith(1, easingFuncs[easingName](0)); + expect(onframe).toHaveBeenNthCalledWith(2, easingFuncs[easingName](interval / life)); + expect(onframe).toHaveBeenNthCalledWith(3, easingFuncs[easingName](2 * interval / life)); + }); +}); diff --git a/test/ut/spec/core/arrayDiff.test.ts b/test/ut/spec/core/arrayDiff.test.ts index a984a38ad..80a52d4e6 100644 --- a/test/ut/spec/core/arrayDiff.test.ts +++ b/test/ut/spec/core/arrayDiff.test.ts @@ -3,8 +3,22 @@ import arrayDiff from '../../../../src/core/arrayDiff'; describe('arrayDiff', function () { it('Basic', function () { - const newArr = [{"name":"类目12"},{"name":"类目13"},{"name":"类目14"},{"name":"类目15"},{"name":"类目16"},{"name":"类目17"}]; - const oldArr = [{"name":"类目11"},{"name":"类目12"},{"name":"类目13"},{"name":"类目14"},{"name":"类目15"},{"name":"类目16"}]; + const newArr = [ + {'name': '类目12'}, + {'name': '类目13'}, + {'name': '类目14'}, + {'name': '类目15'}, + {'name': '类目16'}, + {'name': '类目17'} + ]; + const oldArr = [ + {'name': '类目11'}, + {'name': '类目12'}, + {'name': '类目13'}, + {'name': '类目14'}, + {'name': '类目15'}, + {'name': '类目16'} + ]; const result = arrayDiff(newArr, oldArr, function (a, b) { return a.name === b.name; @@ -25,7 +39,7 @@ describe('arrayDiff', function () { const result = arrayDiff([1, 2, 3, 4], [1, 2, 3, 4]); expect(result[0].added).toBe(false); expect(result[0].removed).toBe(false); - expect(result[0].indices).toEqual([0, 1, 2, 3]) + expect(result[0].indices).toEqual([0, 1, 2, 3]); }); it('All different array', function () { diff --git a/test/ut/spec/core/matrix.test.ts b/test/ut/spec/core/matrix.test.ts new file mode 100644 index 000000000..78104bfa5 --- /dev/null +++ b/test/ut/spec/core/matrix.test.ts @@ -0,0 +1,19 @@ +import * as matrix from '../../../../src/core/matrix'; + +describe('zrUtil', function () { + const identity = [1, 0, 0, 1, 0, 0]; + it('create', function () { + expect(matrix.create()).toStrictEqual(identity); + }); + it('identity', function () { + const origin = [1, 2, 3, 1, 2, 3]; + matrix.identity(origin); + expect(origin).toStrictEqual(identity); + }); + it('copy', function () { + const origin = [1, 2, 3, 4, 5, 6]; + const target = [0]; + matrix.copy(target, origin); + expect(target).toStrictEqual(origin); + }); +}); diff --git a/test/ut/spec/core/platform.test.ts b/test/ut/spec/core/platform.test.ts index f96ca5b44..7b4a5ee7e 100644 --- a/test/ut/spec/core/platform.test.ts +++ b/test/ut/spec/core/platform.test.ts @@ -1,6 +1,6 @@ import * as platform from '../../../../src/core/platform'; -describe('platform', function() { +describe('platform', function () { it('Default font should be correct', function () { expect(platform.DEFAULT_FONT_SIZE).toBe(12); diff --git a/test/ut/spec/core/util.test.ts b/test/ut/spec/core/util.test.ts index 618471853..d82fb4ad1 100755 --- a/test/ut/spec/core/util.test.ts +++ b/test/ut/spec/core/util.test.ts @@ -1,6 +1,6 @@ import * as zrUtil from '../../../../src/core/util'; -describe('zrUtil', function() { +describe('zrUtil', function () { describe('merge', function () { @@ -147,4 +147,54 @@ describe('zrUtil', function() { }); }); -}); \ No newline at end of file + + describe('each', function () { + + it('array base', function () { + const basicArray = ['z', 'r', 'e', 'n', 'd', 'e', 'r']; + const target: string[] = []; + const thisObject = { + count: 0 + }; + const cb = jest.fn(function (char, index) { + this.count++; + target[index] = char + 'E'; + }); + zrUtil.each(basicArray, cb, thisObject); + expect(cb).toBeCalledTimes(basicArray.length); + new Array(basicArray.length).forEach((_, index) => { + expect(cb).toHaveBeenNthCalledWith(index + 1, basicArray[index], index, basicArray); + }); + expect(target).toEqual(['zE', 'rE', 'eE', 'nE', 'dE', 'eE', 'rE']); + expect(thisObject.count).toBe(7); + }); + + it('object base', function () { + const basicObject = { + name: 'zRender', + version: '5.4.1' + }; + const ObjectKeys = Object.keys(basicObject); + const ObjectKeysLength = ObjectKeys.length; + const ObjectValues = Object.values(basicObject); + const target: Partial = {}; + const thisObject = { + count: 0 + }; + const cb = jest.fn(function (value: string, key: keyof typeof basicObject) { + this.count++; + target[key] = value + 'E'; + }); + zrUtil.each(basicObject, cb, thisObject); + expect(cb).toBeCalledTimes(ObjectKeysLength); + new Array(ObjectKeysLength).forEach((_, index) => { + expect(cb).toHaveBeenNthCalledWith(index + 1, ObjectValues[index], ObjectKeys[index], basicObject); + }); + expect(target).toEqual({ + name: 'zRenderE', + version: '5.4.1E' + }); + expect(thisObject.count).toBe(2); + }); + }); +}); diff --git a/test/ut/spec/graphic/Group.test.ts b/test/ut/spec/graphic/Group.test.ts index c72d2e977..0f79306c7 100644 --- a/test/ut/spec/graphic/Group.test.ts +++ b/test/ut/spec/graphic/Group.test.ts @@ -1,4 +1,4 @@ -import {Group, Path, Image as ZImage, Element} from '../zrender'; +import {Group, Element} from '../zrender'; describe('Group', function () { diff --git a/test/ut/spec/graphic/Path.test.ts b/test/ut/spec/graphic/Path.test.ts index 5dfda2e77..18e622786 100644 --- a/test/ut/spec/graphic/Path.test.ts +++ b/test/ut/spec/graphic/Path.test.ts @@ -194,7 +194,7 @@ describe('Path', function () { // TODO textContent }); - it('Path#useStates. Mutiple states should be merged properly', function () { + it('Path#useStates. Multiple states should be merged properly', function () { const rect = createRectForStateTest(); rect.states = { diff --git a/test/ut/spec/graphic/Text.test.ts b/test/ut/spec/graphic/Text.test.ts index a2fb78f97..7cd18730a 100644 --- a/test/ut/spec/graphic/Text.test.ts +++ b/test/ut/spec/graphic/Text.test.ts @@ -19,7 +19,7 @@ describe('Text', function () { stroke: 'blue' } } - } + }; text.useState('emphasis');