Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong committed Jun 6, 2024
1 parent a8e46cd commit ac47a4d
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export function formatSegmentWithContentModel(
false /*includingEntity*/,
true /*mutate*/
);
let isCollapsedSelection = segmentAndParagraphs.every(
x => x[0].segmentType == 'SelectionMarker'
);
let isCollapsedSelection =
segmentAndParagraphs.length >= 1 &&
segmentAndParagraphs.every(x => x[0].segmentType == 'SelectionMarker');

if (isCollapsedSelection) {
const para = segmentAndParagraphs[0][1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ class CachePlugin implements PluginWithState<CachePluginState> {
};

private invalidateCache() {
if (!this.editor?.isInShadowEdit() && this.state.cachedModel) {
console.error('Clear cache');

if (!this.editor?.isInShadowEdit()) {
this.state.cachedModel = undefined;
this.state.cachedSelection = undefined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('setContentModel', () => {
let createModelToDomContextWithConfigSpy: jasmine.Spy;
let setDOMSelectionSpy: jasmine.Spy;
let getDOMSelectionSpy: jasmine.Spy;
let flushMutationsSpy: jasmine.Spy;

beforeEach(() => {
contentModelToDomSpy = spyOn(contentModelToDom, 'contentModelToDom');
Expand All @@ -34,8 +35,9 @@ describe('setContentModel', () => {
).and.returnValue(mockedContext);
setDOMSelectionSpy = jasmine.createSpy('setDOMSelection');
getDOMSelectionSpy = jasmine.createSpy('getDOMSelection');
flushMutationsSpy = jasmine.createSpy('flushMutations');

core = ({
core = {
physicalRoot: mockedDiv,
logicalRoot: mockedDiv,
api: {
Expand All @@ -44,13 +46,17 @@ describe('setContentModel', () => {
getDOMSelection: getDOMSelectionSpy,
},
lifecycle: {},
cache: {},
cache: {
textMutationObserver: {
flushMutations: flushMutationsSpy,
},
},
environment: {
modelToDomSettings: {
calculated: mockedConfig,
},
},
} as any) as EditorCore;
} as any;
});

it('no default option, no shadow edit', () => {
Expand All @@ -75,7 +81,7 @@ describe('setContentModel', () => {
);
expect(setDOMSelectionSpy).toHaveBeenCalledWith(core, mockedRange);
expect(core.cache.cachedSelection).toBe(mockedRange);
expect(core.cache.cachedModel).toBe(mockedModel);
expect(flushMutationsSpy).toHaveBeenCalledWith(mockedModel);
});

it('with default option, no shadow edit', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
import * as TextMutationObserver from '../../../lib/corePlugin/cache/textMutationObserver';
import * as textMutationObserver from '../../../lib/corePlugin/cache/textMutationObserver';
import { DomIndexer, TextMutationObserver } from 'roosterjs-content-model-types';
import { DomIndexerImpl } from '../../../lib/corePlugin/cache/domIndexerImpl';

describe('TextMutationObserverImpl', () => {
let domIndexer: DomIndexer;
let onSkipMutation: jasmine.Spy;
let observer: TextMutationObserver;

beforeEach(() => {
domIndexer = new DomIndexerImpl();
onSkipMutation = jasmine.createSpy('onSkipMutation');
});

afterEach(() => {
observer?.stopObserving();
});

it('init', () => {
const div = document.createElement('div');
const onMutation = jasmine.createSpy('onMutation');
TextMutationObserver.createTextMutationObserver(div, onMutation);
textMutationObserver.createTextMutationObserver(
div,
domIndexer,
onMutation,
onSkipMutation
);

expect(onMutation).not.toHaveBeenCalled();
});

it('not text change', async () => {
const div = document.createElement('div');
const onMutation = jasmine.createSpy('onMutation');
const observer = TextMutationObserver.createTextMutationObserver(div, onMutation);

observer = textMutationObserver.createTextMutationObserver(
div,
domIndexer,
onMutation,
onSkipMutation
);

observer.startObserving();

Expand All @@ -33,7 +59,12 @@ describe('TextMutationObserverImpl', () => {
div.appendChild(text);

const onMutation = jasmine.createSpy('onMutation');
const observer = TextMutationObserver.createTextMutationObserver(div, onMutation);
const observer = textMutationObserver.createTextMutationObserver(
div,
domIndexer,
onMutation,
onSkipMutation
);

observer.startObserving();

Expand All @@ -56,7 +87,12 @@ describe('TextMutationObserverImpl', () => {
div.appendChild(span);

const onMutation = jasmine.createSpy('onMutation');
const observer = TextMutationObserver.createTextMutationObserver(div, onMutation);
const observer = textMutationObserver.createTextMutationObserver(
div,
domIndexer,
onMutation,
onSkipMutation
);

observer.startObserving();

Expand All @@ -77,7 +113,12 @@ describe('TextMutationObserverImpl', () => {
div.appendChild(text);

const onMutation = jasmine.createSpy('onMutation');
const observer = TextMutationObserver.createTextMutationObserver(div, onMutation);
const observer = textMutationObserver.createTextMutationObserver(
div,
domIndexer,
onMutation,
onSkipMutation
);

observer.startObserving();

Expand All @@ -88,8 +129,10 @@ describe('TextMutationObserverImpl', () => {
window.setTimeout(resolve, 10);
});

expect(onMutation).toHaveBeenCalledTimes(1);
expect(onMutation).toHaveBeenCalledTimes(2);
expect(onMutation).toHaveBeenCalledWith(false);

observer.stopObserving();
});

it('flush mutation', async () => {
Expand All @@ -99,7 +142,12 @@ describe('TextMutationObserverImpl', () => {
div.appendChild(text);

const onMutation = jasmine.createSpy('onMutation');
const observer = TextMutationObserver.createTextMutationObserver(div, onMutation);
const observer = textMutationObserver.createTextMutationObserver(
div,
domIndexer,
onMutation,
onSkipMutation
);

observer.startObserving();

Expand All @@ -120,7 +168,12 @@ describe('TextMutationObserverImpl', () => {
div.appendChild(text);

const onMutation = jasmine.createSpy('onMutation');
const observer = TextMutationObserver.createTextMutationObserver(div, onMutation);
const observer = textMutationObserver.createTextMutationObserver(
div,
domIndexer,
onMutation,
onSkipMutation
);

observer.startObserving();
observer.flushMutations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ describe('brProcessor', () => {
onSegment: onSegmentSpy,
onTable: null!,
reconcileSelection: null!,
reconcileChildList: null!,
};

context.domIndexer = domIndexer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ describe('entityProcessor', () => {
onSegment: onSegmentSpy,
onTable: null!,
reconcileSelection: null!,
reconcileChildList: null!,
};

context.domIndexer = domIndexer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ describe('generalProcessor', () => {
onSegment: onSegmentSpy,
onTable: null!,
reconcileSelection: null!,
reconcileChildList: null!,
};

context.domIndexer = domIndexer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ describe('imageProcessor', () => {
onSegment: onSegmentSpy,
onTable: null!,
reconcileSelection: null!,
reconcileChildList: null!,
};

context.domIndexer = domIndexer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ describe('tableProcessor', () => {
onSegment: null!,
onTable: onTableSpy,
reconcileSelection: null!,
reconcileChildList: null!,
};

context.domIndexer = domIndexer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,7 @@ describe('textProcessor', () => {
onSegment: onSegmentSpy,
onTable: null!,
reconcileSelection: null!,
reconcileChildList: null!,
};

context.domIndexer = domIndexer;
Expand Down Expand Up @@ -611,6 +612,7 @@ describe('textProcessor', () => {
onSegment: onSegmentSpy,
onTable: null!,
reconcileSelection: null!,
reconcileChildList: null!,
};

context.domIndexer = domIndexer;
Expand Down Expand Up @@ -656,6 +658,7 @@ describe('textProcessor', () => {
onSegment: onSegmentSpy,
onTable: null!,
reconcileSelection: null!,
reconcileChildList: null!,
};

context.domIndexer = domIndexer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ describe('handleParagraph', () => {
onSegment: onSegmentSpy,
onTable: null!,
reconcileSelection: null!,
reconcileChildList: null!,
};

context.domIndexer = domIndexer;
Expand Down Expand Up @@ -624,6 +625,7 @@ describe('handleParagraph', () => {
onSegment: onSegmentSpy,
onTable: null!,
reconcileSelection: null!,
reconcileChildList: null!,
};

context.domIndexer = domIndexer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ describe('handleTable', () => {
onSegment: null!,
onTable: onTableSpy,
reconcileSelection: null!,
reconcileChildList: null!,
};

context.domIndexer = domIndexer;
Expand Down

0 comments on commit ac47a4d

Please sign in to comment.