Skip to content

Commit

Permalink
FieldBlock: add test proving options are constructed and passed down
Browse files Browse the repository at this point in the history
  • Loading branch information
Stormheg committed Aug 3, 2023
1 parent 9068676 commit 5dd5cc8
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions client/src/components/StreamField/blocks/FieldBlock.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ window.comments = {
};

// Define some callbacks in global scope that can be mocked in tests
let constructor = (_widgetName, _name, _id, _initialState) => {};
let constructor = (
_widgetName,
_name,
_id,
_initialState,
_parentCapabilities,
_options,
) => {};
let setState = (_widgetName, _state) => {};
let getState = (_widgetName) => {};
let getValue = (_widgetName) => {};
Expand All @@ -20,13 +27,19 @@ class DummyWidgetDefinition {
this.throwErrorOnRender = throwErrorOnRender;
}

render(placeholder, name, id, initialState) {
render(placeholder, name, id, initialState, parentCapabilities, options) {
if (this.throwErrorOnRender) {
throw new Error('Mock rendering error');
}

const widgetName = this.widgetName;
constructor(widgetName, { name, id, initialState });
constructor(widgetName, {
name,
id,
initialState,
parentCapabilities,
options,
});

$(placeholder).replaceWith(
`<p name="${name}" id="${id}">${widgetName}</p>`,
Expand Down Expand Up @@ -100,6 +113,24 @@ describe('telepath: wagtail.blocks.FieldBlock', () => {
name: 'the-prefix',
id: 'the-prefix',
initialState: 'Test initial state',
options: {
// Options should have been passed to the block definition
attributes: {
'aria-describedby': 'the-prefix-helptext',
'required': '',
},
},
parentCapabilities: new Map(),
});
});

test('getAttributes() returns aria-describedby and required attributes', () => {
const attributes = boundBlock.getAttributes();
expect(attributes).toEqual({
// Added because FieldBlockDefinition has a helpText in its meta options
'aria-describedby': 'the-prefix-helptext',
// Added because FieldBlockDefinition has required set in its meta options
'required': '',
});
});

Expand Down

0 comments on commit 5dd5cc8

Please sign in to comment.