Skip to content

Commit

Permalink
test: add clip test case and fix eslint error
Browse files Browse the repository at this point in the history
  • Loading branch information
erweixin committed Dec 22, 2022
1 parent 0ddd8a0 commit 71d2eef
Show file tree
Hide file tree
Showing 9 changed files with 195 additions and 10 deletions.
2 changes: 1 addition & 1 deletion test/ut/spec/animation/ElementAnimation.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Polyline, Rect, init} from '../zrender';
import {Polyline, Rect} from '../zrender';
import Animation from '../../../../src/animation/Animation';

describe('ElementAnimation', function () {
Expand Down
102 changes: 102 additions & 0 deletions test/ut/spec/animation/clip.test.ts
Original file line number Diff line number Diff line change
@@ -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<keyof typeof easingFuncs>;

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));
});
});
20 changes: 17 additions & 3 deletions test/ut/spec/core/arrayDiff.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 () {
Expand Down
19 changes: 19 additions & 0 deletions test/ut/spec/core/matrix.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
2 changes: 1 addition & 1 deletion test/ut/spec/core/platform.test.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
54 changes: 52 additions & 2 deletions test/ut/spec/core/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as zrUtil from '../../../../src/core/util';

describe('zrUtil', function() {
describe('zrUtil', function () {

describe('merge', function () {

Expand Down Expand Up @@ -147,4 +147,54 @@ describe('zrUtil', function() {
});

});
});

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<typeof basicObject> = {};
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);
});
});
});
2 changes: 1 addition & 1 deletion test/ut/spec/graphic/Group.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Group, Path, Image as ZImage, Element} from '../zrender';
import {Group, Element} from '../zrender';

describe('Group', function () {

Expand Down
2 changes: 1 addition & 1 deletion test/ut/spec/graphic/Path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 1 addition & 1 deletion test/ut/spec/graphic/Text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('Text', function () {
stroke: 'blue'
}
}
}
};

text.useState('emphasis');

Expand Down

0 comments on commit 71d2eef

Please sign in to comment.