Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(): Add cursor animation testing and migrate some easy one to jest #9829

Merged
merged 22 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .codesandbox/templates/vanilla/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fabric from 'fabric';
import './styles.css';
import { testCase } from './testcases/nestedSelections';
import { testCase } from './testcases/simpleTextbox';

const el = document.getElementById('canvas');
const canvas = (window.canvas = new fabric.Canvas(el));
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## [next]

- test(): Add cursor animation testing and migrate some easy one to jest [#9829](https://github.com/fabricjs/fabric.js/pull/9829)
- fix(): Replace 'hasOwn' with 'in' operator in typeAssertions check [#9812](https://github.com/fabricjs/fabric.js/pull/9812)

## [6.0.0-rc1]
Expand Down
83 changes: 83 additions & 0 deletions src/shapes/IText/ITextBehavior.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { roundSnapshotOptions } from '../../../jest.extend';
import { IText } from './IText';

import { ValueAnimation } from '../../util/animation/ValueAnimation';

export function matchTextStateSnapshot(text: IText) {
const {
styles,
Expand Down Expand Up @@ -90,3 +92,84 @@ describe('text imperative changes', () => {
expect(iText.missingNewlineOffset(0)).toBe(1);
});
});

describe('IText cursor animation snapshot', () => {
let currentAnimation: string[] = [];
const origCalculate = ValueAnimation.prototype.calculate;
beforeAll(() => {
ValueAnimation.prototype.calculate = function (timeElapsed: number) {
asturur marked this conversation as resolved.
Show resolved Hide resolved
const value = origCalculate.call(this, timeElapsed);
currentAnimation.push(value.value.toFixed(3));
return value;
};
jest.useFakeTimers();
});
beforeEach(() => {
jest.runAllTimers();
currentAnimation = [];
});
afterAll(() => {
ValueAnimation.prototype.calculate = origCalculate;
jest.useRealTimers();
});
test('initDelayedCursor false - with delay', () => {
const iText = new IText('', { canvas: {} });
iText.initDelayedCursor();
jest.advanceTimersByTime(2000);
expect(currentAnimation).toMatchSnapshot();
iText.abortCursorAnimation();
});
test('initDelayedCursor true - with NO delay', () => {
const iText = new IText('', { canvas: {} });
iText.initDelayedCursor(true);
jest.advanceTimersByTime(2000);
expect(currentAnimation).toMatchSnapshot();
iText.abortCursorAnimation();
});
test('selectionStart/selection end will abort animation', () => {
const iText = new IText('asd', { canvas: {} });
iText.initDelayedCursor(true);
jest.advanceTimersByTime(160);
iText.selectionStart = 0;
iText.selectionEnd = 3;
jest.advanceTimersByTime(2000);
expect(currentAnimation).toMatchSnapshot();
iText.abortCursorAnimation();
});
test('exiting from a canvas will abort animation', () => {
const iText = new IText('asd', { canvas: {} });
iText.initDelayedCursor(true);
jest.advanceTimersByTime(160);
iText.canvas = undefined;
jest.advanceTimersByTime(2000);
expect(currentAnimation).toMatchSnapshot();
iText.abortCursorAnimation();
});
});

describe('IText _tick', () => {
const _tickMock = jest.fn();
beforeEach(() => {
_tickMock.mockClear();
});
test('enter Editing will call _tick', () => {
const iText = new IText('hello\nhello');
iText._tick = _tickMock;
asturur marked this conversation as resolved.
Show resolved Hide resolved
iText.enterEditing();
expect(iText._tick).toHaveBeenCalledWith();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be
expect(tickMock)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the opposite, i did wrong at doing expect(_initDelayedMock) in the other test.

});
test('mouse up will fire an animation restart with 0 delay if is a click', () => {
const iText = new IText('hello\nhello');
iText._tick = _tickMock;
iText.enterEditing();
expect(iText._tick).toHaveBeenCalledWith();
asturur marked this conversation as resolved.
Show resolved Hide resolved
_tickMock.mockClear();
iText.__lastSelected = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think setting internal state makes a robust test

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is how it was before, otherwise i have to go with a full canvas where i click and select, i just moved the old tests as much as possible

iText.mouseUpHandler({
e: {
button: 0,
},
});
expect(iText._tick).toHaveBeenCalledWith(0);
});
});
21 changes: 12 additions & 9 deletions src/shapes/IText/ITextBehavior.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,10 @@ export abstract class ITextBehavior<
duration,
delay,
onComplete,
abort: () => {
return (
!this.canvas ||
// we do not want to animate a selection, only cursor
this.selectionStart !== this.selectionEnd
);
},
abort: () =>
!this.canvas ||
// we do not want to animate a selection, only cursor
this.selectionStart !== this.selectionEnd,
onChange: (value) => {
this._currentCursorOpacity = value;
this.renderCursorOrSelection();
Expand Down Expand Up @@ -197,6 +194,10 @@ export abstract class ITextBehavior<
}
}

/**
* Restart tue cursor animation if either is in complete state ( between animations )
* or if it never started before
*/
restartCursorIfNeeded() {
if (
[this._currentTickState, this._currentTickCompleteState].some(
Expand Down Expand Up @@ -336,10 +337,11 @@ export abstract class ITextBehavior<
}

/**
* TODO fix: selectionStart set as 0 will be ignored?
* Selects a word based on the index
* @param {Number} selectionStart Index of a character
*/
selectWord(selectionStart: number) {
selectWord(selectionStart?: number) {
selectionStart = selectionStart || this.selectionStart;
// search backwards
const newSelectionStart = this.searchWordBoundary(selectionStart, -1),
Expand All @@ -357,10 +359,11 @@ export abstract class ITextBehavior<
}

/**
* TODO fix: selectionStart set as 0 will be ignored?
* Selects a line based on the index
* @param {Number} selectionStart Index of a character
*/
selectLine(selectionStart: number) {
selectLine(selectionStart?: number) {
selectionStart = selectionStart || this.selectionStart;
const newSelectionStart = this.findLineBoundaryLeft(selectionStart),
newSelectionEnd = this.findLineBoundaryRight(selectionStart);
Expand Down
Loading
Loading