From cfc247089c925096c590c5805a7eef916f93d8e3 Mon Sep 17 00:00:00 2001 From: Artur Arseniev Date: Tue, 30 May 2023 14:07:10 +0400 Subject: [PATCH] Add tests for usePlugin --- test/specs/grapesjs/index.ts | 48 +++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/test/specs/grapesjs/index.ts b/test/specs/grapesjs/index.ts index d810d9d76..8eb3f39e0 100644 --- a/test/specs/grapesjs/index.ts +++ b/test/specs/grapesjs/index.ts @@ -1,4 +1,7 @@ -import grapesjs from '../../../src'; +import grapesjs, { usePlugin } from '../../../src'; +import type { Plugin } from '../../../src/plugin_manager'; + +type TestPlugin = Plugin<{ cVal: string }>; describe('GrapesJS', () => { describe('Main', () => { @@ -413,6 +416,49 @@ describe('GrapesJS', () => { editor = grapesjs.init(config); expect(editor.Commands.get('export-template').test).toEqual(1); }); + + describe('usePlugin', () => { + test('Execute named plugin from PluginManager', () => { + let varToTest = ''; + const optionValue = 'TEST-PM'; + const pluginName = 'testplugin'; + grapesjs.plugins.add(pluginName, (edt, opts = {}) => { + varToTest = opts.cVal || ''; + }); + grapesjs.init({ + ...config, + plugins: [usePlugin(pluginName, { cVal: optionValue })], + }); + expect(varToTest).toEqual(optionValue); + }); + + test('Execute inline plugin', () => { + let varToTest = ''; + const optionValue = 'TEST-inline'; + const inlinePlugin: TestPlugin = (edt, opts) => { + varToTest = opts.cVal; + }; + grapesjs.init({ + ...config, + plugins: [usePlugin(inlinePlugin, { cVal: optionValue })], + }); + expect(varToTest).toEqual(optionValue); + }); + + test('Execute global plugin', () => { + let varToTest = ''; + const optionValue = 'TEST-global'; + const pluginName = 'globalPlugin'; + (window as any)[pluginName] = (edt, opts) => { + varToTest = opts.cVal; + }; + grapesjs.init({ + ...config, + plugins: [usePlugin(pluginName, { cVal: optionValue })], + }); + expect(varToTest).toEqual(optionValue); + }); + }); }); describe('Component selection', () => {