Skip to content

Commit

Permalink
Merge pull request #16 from eea/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
avoinea committed Jun 14, 2023
2 parents 8c28bfa + 7232cad commit 9b6c421
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file. Dates are d

Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog).

### [0.5.2](https://github.com/eea/volto-slate-label/compare/0.5.1...0.5.2) - 14 June 2023

### [0.5.1](https://github.com/eea/volto-slate-label/compare/0.5.0...0.5.1) - 12 June 2023

#### :house: Internal changes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eeacms/volto-slate-label",
"version": "0.5.1",
"version": "0.5.2",
"description": "@eeacms/volto-slate-label: Volto add-on",
"main": "src/index.js",
"author": "European Environment Agency: IDM2 A-Team",
Expand Down
108 changes: 108 additions & 0 deletions src/editor/LabelWrapper.test.jsx
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>,
);
});
});
87 changes: 87 additions & 0 deletions src/editor/extensions.test.js
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);
});
});

0 comments on commit 9b6c421

Please sign in to comment.