Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,33 @@ describe('ECRToolbar', () => {
editor = {
commands: {
updateAttributes: jest.fn()
}
},
state: {
selection: {
from: 0,
},
tr: {
setNodeMarkup: jest.fn(),
},
},
view: {
dispatch: jest.fn(),
nodeDOM: jest.fn().mockReturnValue({
nodeType: 1,
getBoundingClientRect: jest.fn().mockReturnValue({
top: 100,
left: 50,
width: 200,
height: 30,
}),
closest: jest.fn().mockReturnValue({
getBoundingClientRect: jest.fn().mockReturnValue({
top: 0,
left: 0,
}),
}),
}),
},
};
});

Expand All @@ -36,6 +62,10 @@ describe('ECRToolbar', () => {
classes: {},
node: {
key: 1,
attrs: {
index: '2',
value: 'moon',
},
data: {
get: (prop) => {
if (prop === 'index') {
Expand All @@ -47,6 +77,7 @@ describe('ECRToolbar', () => {
toJSON: jest.fn(),
},
},
pos: 5,
editor,
correctChoice: { value: '0', label: 'moon' },
};
Expand All @@ -62,12 +93,26 @@ describe('ECRToolbar', () => {
describe('logic', () => {
it('onDone: calls onToolbarDone and onChangeResponse', () => {
// Create an instance to test the internal method
const mockTr = {
setNodeMarkup: jest.fn(),
};
const testEditor = {
...editor,
state: {
...editor.state,
tr: mockTr,
},
};
const testInstance = new ECRToolbar({
onChangeResponse,
onToolbarDone,
classes: {},
node: {
key: 1,
attrs: {
index: '2',
value: 'moon',
},
data: {
get: (prop) => {
if (prop === 'index') {
Expand All @@ -78,7 +123,8 @@ describe('ECRToolbar', () => {
toJSON: jest.fn(),
},
},
editor,
pos: 5,
editor: testEditor,
value: {
change: jest.fn().mockReturnValue({
setNodeByKey: jest.fn().mockReturnValue({
Expand All @@ -96,10 +142,15 @@ describe('ECRToolbar', () => {
correctChoice: { value: '0', label: 'moon' },
});

testInstance.onDone();
testInstance.onDone('test markup');

expect(onToolbarDone).toBeCalled();
expect(onChangeResponse).toBeCalled();
expect(mockTr.setNodeMarkup).toHaveBeenCalledWith(5, undefined, {
index: '2',
value: 'test markup',
});
expect(testEditor.view.dispatch).toHaveBeenCalledWith(mockTr);
expect(onToolbarDone).toHaveBeenCalledWith(true);
expect(onChangeResponse).toHaveBeenCalledWith('test markup');
});

it('onRespAreaChange updates state', () => {
Expand All @@ -109,6 +160,10 @@ describe('ECRToolbar', () => {
classes: {},
node: {
key: 1,
attrs: {
index: '2',
value: 'moon',
},
data: {
get: (prop) => {
if (prop === 'index') {
Expand All @@ -119,6 +174,7 @@ describe('ECRToolbar', () => {
toJSON: jest.fn(),
},
},
pos: 5,
editor,
value: {
change: jest.fn().mockReturnValue({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class ECRToolbar extends React.Component {
static propTypes = {
correctChoice: PropTypes.object,
node: PropTypes.object,
pos: PropTypes.number,
onDone: PropTypes.func,
onChangeResponse: PropTypes.func.isRequired,
onToolbarDone: PropTypes.func.isRequired,
Expand Down Expand Up @@ -67,11 +68,15 @@ export class ECRToolbar extends React.Component {
}

onDone = (markup) => {
const { node, editor, onToolbarDone, onChangeResponse } = this.props;
const { editor, node, onToolbarDone, onChangeResponse, pos } = this.props;
const sanitizedMarkup = stripHtmlTags(markup);
this.setState({ markup: sanitizedMarkup });

editor.commands.updateAttributes('explicit_constructed_response', { value: sanitizedMarkup });
const { tr } = editor.state;

// Merge old and new attributes
tr.setNodeMarkup(pos, undefined, { ...node.attrs, value: sanitizedMarkup });
editor.view.dispatch(tr);

onToolbarDone(true);
onChangeResponse(sanitizedMarkup);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,13 +408,15 @@ export class Main extends React.Component {
duplicates: true,
},
maxResponseAreas: maxResponseAreas,
respAreaToolbar: (node, editor, onToolbarDone) => {
respAreaToolbar: (nodeInfo, editor, onToolbarDone) => {
const [node, pos] = nodeInfo;
const { model } = this.props;
const correctChoice = (model.choices[node.attrs.index] || [])[0];
return () => (
<ECRToolbar
onChangeResponse={(newVal) => this.onChangeResponse(node.attrs.index, newVal)}
node={node}
pos={pos}
editor={editor}
onToolbarDone={onToolbarDone}
correctChoice={correctChoice}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ describe('RespAreaToolbar', () => {
value: 'cow',
},
},
pos: 0,
editor,
choices: [
{
Expand Down Expand Up @@ -1195,6 +1196,7 @@ describe('MenuItem Integration Tests', () => {
key: '1',
attrs: { index: '0', value: 'cow' },
},
pos: 0,
editor,
choices,
};
Expand Down Expand Up @@ -1284,6 +1286,7 @@ describe('MenuItem Integration Tests', () => {
key: '1',
attrs: { index: '0', value: 'cow' },
},
pos: 0,
editor: localEditor,
choices,
};
Expand Down Expand Up @@ -1522,6 +1525,7 @@ describe('MenuItem Integration Tests', () => {
key: '1',
attrs: { index: '0', value: 'cow' },
},
pos: 0,
editor: localEditor,
choices,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ const ItemBuilder = styled('div')(({ theme }) => ({
class RespAreaToolbar extends React.Component {
static propTypes = {
node: PropTypes.object,
pos: PropTypes.number,
uploadSoundSupport: PropTypes.object,
onDone: PropTypes.func,
choices: PropTypes.array,
Expand Down
4 changes: 3 additions & 1 deletion packages/inline-dropdown/configure/src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,8 @@ export class Main extends React.Component {
duplicates: true,
},
maxResponseAreas: maxResponseAreas,
respAreaToolbar: (node, editor, onToolbarDone) => {
respAreaToolbar: (nodeInfo, editor, onToolbarDone) => {
const [node, pos] = nodeInfo;
const { respAreaChoices } = this.state;

return props => (
Expand All @@ -531,6 +532,7 @@ export class Main extends React.Component {
onRemoveChoice={(index) => this.onRemoveChoice(node.attrs.index, index)}
onSelectChoice={(index) => this.onSelectChoice(node.attrs.index, index)}
node={node}
pos={pos}
editor={editor}
onToolbarDone={onToolbarDone}
choices={respAreaChoices[node.attrs.index]}
Expand Down
Loading