Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
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(Group, Controls): Fix interactive group actions when negative scaling is involved [#9811](https://github.com/fabricjs/fabric.js/pull/9811)
- fix(): Replace 'hasOwn' with 'in' operator in typeAssertions check [#9812](https://github.com/fabricjs/fabric.js/pull/9812)

Expand Down
85 changes: 85 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,86 @@ describe('text imperative changes', () => {
expect(iText.missingNewlineOffset(0)).toBe(1);
});
});

describe('IText cursor animation snapshot', () => {
let currentAnimation: string[] = [];
const origCalculate = ValueAnimation.prototype.calculate;
beforeAll(() => {
jest
.spyOn(ValueAnimation.prototype, 'calculate')
.mockImplementation(function (timeElapsed: number) {
const value = origCalculate.call(this, timeElapsed);
currentAnimation.push(value.value.toFixed(3));
return value;
});
jest.useFakeTimers();
});
beforeEach(() => {
jest.runAllTimers();
currentAnimation = [];
});
afterAll(() => {
jest.resetAllMocks();
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');
jest.spyOn(iText, '_tick').mockImplementation(_tickMock);
iText.enterEditing();
expect(_tickMock).toHaveBeenCalledWith();
});
test('mouse up will fire an animation restart with 0 delay if is a click', () => {
const iText = new IText('hello\nhello');
jest.spyOn(iText, '_tick').mockImplementation(_tickMock);
iText.enterEditing();
expect(_tickMock).toHaveBeenCalledWith();
_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(_tickMock).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