Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
JiuqingSong committed Apr 4, 2024
1 parent 0213a45 commit ddc53b5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { setContentModel } from '../../../lib/coreApi/setContentModel/setContent

const mockedDoc = 'DOCUMENT' as any;
const mockedModel = 'MODEL' as any;
const mockedEditorContext = 'EDITORCONTEXT' as any;
const mockedEditorContext = { context: 'EDITORCONTEXT' } as any;
const mockedContext = { name: 'CONTEXT' } as any;
const mockedDiv = { ownerDocument: mockedDoc } as any;
const mockedConfig = 'CONFIG' as any;
Expand All @@ -18,6 +18,7 @@ describe('setContentModel', () => {
let createModelToDomContextWithConfigSpy: jasmine.Spy;
let setDOMSelectionSpy: jasmine.Spy;
let getDOMSelectionSpy: jasmine.Spy;
let setEditorStyleSpy: jasmine.Spy;

beforeEach(() => {
contentModelToDomSpy = spyOn(contentModelToDom, 'contentModelToDom');
Expand All @@ -34,6 +35,7 @@ describe('setContentModel', () => {
).and.returnValue(mockedContext);
setDOMSelectionSpy = jasmine.createSpy('setDOMSelection');
getDOMSelectionSpy = jasmine.createSpy('getDOMSelection');
setEditorStyleSpy = jasmine.createSpy('setEditorStyle');

core = ({
physicalRoot: mockedDiv,
Expand All @@ -42,6 +44,7 @@ describe('setContentModel', () => {
createEditorContext,
setDOMSelection: setDOMSelectionSpy,
getDOMSelection: getDOMSelectionSpy,
setEditorStyle: setEditorStyleSpy,
},
lifecycle: {},
cache: {},
Expand Down Expand Up @@ -76,6 +79,7 @@ describe('setContentModel', () => {
expect(setDOMSelectionSpy).toHaveBeenCalledWith(core, mockedRange);
expect(core.cache.cachedSelection).toBe(mockedRange);
expect(core.cache.cachedModel).toBe(mockedModel);
expect(setEditorStyleSpy).toHaveBeenCalledWith(core, '__shadowEditSelection', null);
});

it('with default option, no shadow edit', () => {
Expand All @@ -98,6 +102,7 @@ describe('setContentModel', () => {
mockedContext
);
expect(setDOMSelectionSpy).toHaveBeenCalledWith(core, mockedRange);
expect(setEditorStyleSpy).toHaveBeenCalledWith(core, '__shadowEditSelection', null);
});

it('with default option, no shadow edit, with additional option', () => {
Expand Down Expand Up @@ -125,6 +130,7 @@ describe('setContentModel', () => {
mockedContext
);
expect(setDOMSelectionSpy).toHaveBeenCalledWith(core, mockedRange);
expect(setEditorStyleSpy).toHaveBeenCalledWith(core, '__shadowEditSelection', null);
});

it('no default option, with shadow edit', () => {
Expand All @@ -148,6 +154,12 @@ describe('setContentModel', () => {
mockedContext
);
expect(setDOMSelectionSpy).not.toHaveBeenCalled();
expect(setEditorStyleSpy).toHaveBeenCalledWith(
core,
'__shadowEditSelection',
'background-color: #ddd!important',
['.__shadowEditSelection']
);
});

it('restore selection ', () => {
Expand Down Expand Up @@ -181,6 +193,7 @@ describe('setContentModel', () => {
);
expect(setDOMSelectionSpy).not.toHaveBeenCalled();
expect(core.selection.selection).toBe(mockedRange);
expect(setEditorStyleSpy).toHaveBeenCalledWith(core, '__shadowEditSelection', null);
});

it('restore range selection ', () => {
Expand Down Expand Up @@ -215,6 +228,7 @@ describe('setContentModel', () => {
);
expect(setDOMSelectionSpy).not.toHaveBeenCalled();
expect(core.selection.selection).toBe(mockedRange);
expect(setEditorStyleSpy).toHaveBeenCalledWith(core, '__shadowEditSelection', null);
});

it('restore null selection ', () => {
Expand Down Expand Up @@ -244,5 +258,6 @@ describe('setContentModel', () => {
);
expect(setDOMSelectionSpy).not.toHaveBeenCalled();
expect(core.selection.selection).toBe(null);
expect(setEditorStyleSpy).toHaveBeenCalledWith(core, '__shadowEditSelection', null);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,41 @@ describe('handleSegmentCommon', () => {
expect(segmentNodes.length).toBe(1);
expect(segmentNodes[0]).toBe(parent);
});

it('selected text', () => {
const txt = document.createTextNode('test');
const container = document.createElement('span');
const segment = createText('test', {
textColor: 'red',
fontSize: '10pt',
lineHeight: '2',
fontWeight: 'bold',
});
const onNodeCreated = jasmine.createSpy('onNodeCreated');
const context = createModelToDomContext();

segment.isSelected = true;
context.onNodeCreated = onNodeCreated;
context.selectionClassName = 'test';

segment.link = {
dataset: {},
format: {
href: 'href',
},
};
container.appendChild(txt);
const segmentNodes: Node[] = [];

handleSegmentCommon(document, txt, container, segment, context, segmentNodes);

expect(context.regularSelection.current.segment).toBe(txt);
expect(container.outerHTML).toBe(
'<span class="test" style="font-size: 10pt; color: red; line-height: 2;"><b><a href="href">test</a></b></span>'
);
expect(onNodeCreated).toHaveBeenCalledWith(segment, txt);
expect(segmentNodes.length).toBe(2);
expect(segmentNodes[0]).toBe(txt);
expect(segmentNodes[1]).toBe(txt.parentNode!);
});
});

0 comments on commit ddc53b5

Please sign in to comment.