generated from eea/volto-addon-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from eea/develop
Release
- Loading branch information
Showing
4 changed files
with
198 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { render } from '@testing-library/react'; | ||
import LabelWrapper from './LabelWrapper'; | ||
|
||
jest.mock('@plone/volto-slate/editor/render', () => ({ | ||
serializeNodes: jest.fn(), | ||
serializeNodesToText: jest.fn(() => 'Tooltip Content'), | ||
})); | ||
|
||
describe('LabelWrapper', () => { | ||
it('renders without crashing', () => { | ||
const props = { | ||
attributes: {}, | ||
element: { | ||
data: { | ||
uid: '123', | ||
label_type: 'type', | ||
label_pointing: 'pointing', | ||
tooltip_content: [{ text: 'Tooltip Content' }], | ||
tooltip_pointing: 'top center', | ||
always_show: false, | ||
tooltip_type: 'info', | ||
}, | ||
}, | ||
}; | ||
render( | ||
<LabelWrapper {...props}> | ||
<div>Test</div> | ||
</LabelWrapper>, | ||
); | ||
}); | ||
|
||
it('renders without crashing with always_show true', () => { | ||
const props = { | ||
attributes: {}, | ||
element: { | ||
data: { | ||
uid: '123', | ||
label_type: 'type', | ||
label_pointing: 'pointing', | ||
tooltip_content: [{ text: 'Tooltip Content' }], | ||
tooltip_pointing: 'top center', | ||
always_show: true, | ||
tooltip_type: 'info', | ||
}, | ||
}, | ||
}; | ||
render( | ||
<LabelWrapper {...props}> | ||
<div>Test</div> | ||
</LabelWrapper>, | ||
); | ||
}); | ||
it('renders without crashing with no label_type', () => { | ||
const props = { | ||
attributes: {}, | ||
element: { | ||
data: { | ||
uid: '123', | ||
label_type: undefined, | ||
label_pointing: 'pointing', | ||
tooltip_content: [{ text: 'Tooltip Content' }], | ||
tooltip_pointing: 'top center', | ||
always_show: true, | ||
tooltip_type: 'info', | ||
}, | ||
}, | ||
}; | ||
render( | ||
<LabelWrapper {...props}> | ||
<div>Test</div> | ||
</LabelWrapper>, | ||
); | ||
}); | ||
|
||
it('renders without crashing with no tooltip _content', () => { | ||
const props = { | ||
attributes: {}, | ||
element: { | ||
data: { | ||
uid: '123', | ||
label_type: undefined, | ||
label_pointing: 'pointing', | ||
tooltip_content: undefined, | ||
tooltip_pointing: 'top center', | ||
always_show: true, | ||
tooltip_type: 'info', | ||
}, | ||
}, | ||
}; | ||
render( | ||
<LabelWrapper {...props}> | ||
<div>Test</div> | ||
</LabelWrapper>, | ||
); | ||
}); | ||
|
||
it('renders without crashing with no data field in element', () => { | ||
const props = { | ||
attributes: {}, | ||
element: {}, | ||
}; | ||
render( | ||
<LabelWrapper {...props}> | ||
<div>Test</div> | ||
</LabelWrapper>, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { withLabel, withBeforeInsertFragment } from './extensions'; | ||
import { Transforms } from 'slate'; | ||
|
||
jest.mock('slate', () => ({ | ||
Transforms: { | ||
setNodes: jest.fn(), | ||
}, | ||
})); | ||
|
||
jest.mock('@plone/volto-slate/utils', () => ({ | ||
nanoid: () => 'mockedId', | ||
})); | ||
|
||
describe('withLabel', () => { | ||
it('sets isInline correctly', () => { | ||
const editor = { | ||
isInline: jest.fn(), | ||
}; | ||
const newEditor = withLabel(editor); | ||
|
||
const elementWithCorrectLabel = { type: 'label' }; | ||
const elementWithIncorrectLabel = { type: 'LABEL' }; | ||
expect(newEditor.isInline(elementWithCorrectLabel)).toEqual(true); | ||
expect(newEditor.isInline(elementWithIncorrectLabel)).toEqual(undefined); | ||
}); | ||
|
||
it('normalizes node correctly with label that matches', () => { | ||
const editor = { | ||
normalizeNode: jest.fn(), | ||
}; | ||
const newEditor = withLabel(editor); | ||
|
||
const node = { type: 'label' }; | ||
const path = 'mockedPath'; | ||
|
||
newEditor.normalizeNode([node, path]); | ||
|
||
expect(Transforms.setNodes).toHaveBeenCalledWith( | ||
newEditor, | ||
{ data: { uid: 'mockedId' } }, | ||
{ at: path }, | ||
); | ||
}); | ||
|
||
it('normalizes node correctly with label that does not match', () => { | ||
const editor = { | ||
normalizeNode: jest.fn(), | ||
}; | ||
const newEditor = withLabel(editor); | ||
|
||
const node = { type: 'LABEL' }; | ||
const path = 'mockedPath'; | ||
|
||
newEditor.normalizeNode([node, path]); | ||
}); | ||
}); | ||
|
||
describe('withBeforeInsertFragment', () => { | ||
it('calls beforeInsertFragment correctly and generates new uid correctly', () => { | ||
const editor = { | ||
beforeInsertFragment: jest.fn((parsed) => parsed), | ||
}; | ||
const newEditor = withBeforeInsertFragment(editor); | ||
const parsed = [{ children: [{ data: { uid: 'existingId' } }] }]; | ||
const returnedValue = newEditor.beforeInsertFragment(parsed); | ||
|
||
expect(returnedValue[0].children[0].data.uid).toEqual('mockedId'); | ||
}); | ||
|
||
it('generates new uid correctly when there is no beforeInsertFragment', () => { | ||
const editor = {}; | ||
const newEditor = withBeforeInsertFragment(editor); | ||
const parsed = [{ children: [{ data: { uid: 'existingId' } }] }]; | ||
const returnedValue = newEditor.beforeInsertFragment(parsed); | ||
|
||
expect(returnedValue[0].children[0].data.uid).toEqual('mockedId'); | ||
}); | ||
|
||
it('returns the same object when beforeInsertFragment is not defined and the date does not have uid', () => { | ||
const editor = {}; | ||
const newEditor = withBeforeInsertFragment(editor); | ||
const parsed = [{ children: [{ data: {} }] }]; | ||
const returnedValue = newEditor.beforeInsertFragment(parsed); | ||
|
||
expect(returnedValue).toEqual(parsed); | ||
}); | ||
}); |