Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: pass user configuration to Tool prepare method #1781

Merged
merged 4 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 5 additions & 4 deletions src/components/modules/tools.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Paragraph from '../../tools/paragraph/dist/bundle';
import Module from '../__module';
import * as _ from '../utils';
import { SanitizerConfig, ToolConstructable, ToolSettings } from '../../../types';
import { SanitizerConfig, ToolConfig, ToolConstructable, ToolSettings } from '../../../types';
import BoldInlineTool from '../inline-tools/inline-tool-bold';
import ItalicInlineTool from '../inline-tools/inline-tool-italic';
import LinkInlineTool from '../inline-tools/inline-tool-link';
Expand Down Expand Up @@ -274,12 +274,12 @@ export default class Tools extends Module {
* @param config - tools config
*/
private getListOfPrepareFunctions(config: {[name: string]: ToolSettings}): {
function: (data: { toolName: string }) => void | Promise<void>;
data: { toolName: string };
function: (data: { toolName: string; config: ToolConfig }) => void | Promise<void>;
data: { toolName: string; config: ToolConfig };
}[] {
const toolPreparationList: {
function: (data: { toolName: string }) => void | Promise<void>;
data: { toolName: string };
data: { toolName: string; config: ToolConfig };
Copy link
Member

@neSpecc neSpecc Sep 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to add this to the function's return type

}[] = [];

Object
Expand All @@ -290,6 +290,7 @@ export default class Tools extends Module {
function: _.isFunction(settings.class.prepare) ? settings.class.prepare : (): void => {},
data: {
toolName,
config: settings.config,
},
});
});
Expand Down
28 changes: 27 additions & 1 deletion test/cypress/tests/modules/Tools.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,32 @@ describe('Tools module', () => {

expect(err).to.be.instanceOf(Error);
});

// eslint-disable-next-line cypress/no-async-tests
it('should call Tools prepare method with user config', async () => {
class WithSuccessfulPrepare {
// eslint-disable-next-line @typescript-eslint/no-empty-function
public static prepare = cy.stub()
}

const config = {
property: 'value',
};

const module = constructModule({
defaultBlock: 'withSuccessfulPrepare',
tools: {
withSuccessfulPrepare: {
class: WithSuccessfulPrepare as any,
config,
},
},
});

await module.prepare();

expect(WithSuccessfulPrepare.prepare).to.be.calledWithExactly({ toolName: 'withSuccessfulPrepare', config });
});
});

context('collection accessors', () => {
Expand Down Expand Up @@ -173,7 +199,7 @@ describe('Tools module', () => {
expect(module.unavailable).to.be.instanceOf(Map);
});

it('should contain only ready to use Tools', () => {
it('should contain unavailable Tools', () => {
expect(module.unavailable.has('withSuccessfulPrepare')).to.be.false;
expect(module.unavailable.has('withoutPrepare')).to.be.false;
expect(module.unavailable.has('withFailedPrepare')).to.be.true;
Expand Down