UNPKG

@silexlabs/grapesjs-symbols

Version:
83 lines 5.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var backbone_1 = require("backbone"); var globals_1 = require("@jest/globals"); // Mock module lit-html // This is needed because lit-html fails to load in jest (Must use import to load ES Module: node_modules/lit-html/lit-html.js) globals_1.jest.mock('lit-html', function () { var render = globals_1.jest.fn(); var html = globals_1.jest.fn(); return { render: render, html: html }; }); globals_1.jest.mock('lit-html/directives/unsafe-html.js', function () { return { unsafeHTML: globals_1.jest.fn(), }; }); var SymbolsCommands_1 = require("./SymbolsCommands"); var SymbolsCommands_2 = require("./SymbolsCommands"); var test_utils_1 = require("./test-utils"); test('Command symbols:add', function () { var editor = (0, test_utils_1.getTestSymbols)().editor; editor.Symbols = new backbone_1.default.Collection(); var sender = {}, label = 'label', icon = 'icon'; (0, globals_1.expect)(function () { return (0, SymbolsCommands_1.addSymbol)(editor, sender, { label: label, icon: icon, component: undefined }); }).toThrow('Can not create the symbol: missing required param'); (0, globals_1.expect)(editor.Symbols).toHaveLength(0); var component = editor.addComponents([{ tagName: 'div', components: [ { tagName: 'h1', content: 'Content text', style: { color: 'red' }, attributes: { title: 'here' } }, { tagName: 'p', content: 'Content text', style: { color: 'red' }, attributes: { title: 'here' } }, ], style: { 'background-color': 'blue', 'padding': '20px' }, }])[0]; (0, globals_1.expect)(function () { return (0, SymbolsCommands_1.addSymbol)(editor, sender, { label: label, icon: icon, component: component }); }).not.toThrow(); (0, globals_1.expect)(editor.Symbols).toHaveLength(1); var model = editor.Symbols.models[0].get('model'); (0, globals_1.expect)(model.attributes.symbolId).not.toBeUndefined(); (0, globals_1.expect)(model.components().models[0].attributes.symbolChildId).not.toBeUndefined(); (0, globals_1.expect)(component.attributes.symbolId).not.toBeUndefined(); (0, globals_1.expect)(component.components().models[0].attributes.symbolChildId).not.toBeUndefined(); }); test('Command symbols:remove', function () { var _a = (0, test_utils_1.getTestSymbols)(), editor = _a.editor, s1 = _a.s1, s2 = _a.s2; editor.Symbols = new backbone_1.default.Collection(); var sender = {}, symbolId = 'symbolId'; (0, globals_1.expect)(function () { return (0, SymbolsCommands_2.removeSymbol)(editor, sender, {}); }).toThrow('missing param symbolId'); (0, globals_1.expect)(function () { return (0, SymbolsCommands_2.removeSymbol)(editor, sender, { symbolId: symbolId }); }).toThrow('symbol not found'); editor.Symbols = new backbone_1.default.Collection([s1, s2]); (0, globals_1.expect)(editor.Symbols).toHaveLength(2); (0, globals_1.expect)(function () { return (0, SymbolsCommands_2.removeSymbol)(editor, sender, { symbolId: s1.id }); }).not.toThrow(); (0, globals_1.expect)(editor.Symbols).toHaveLength(1); }); test('Command symbols:unlink', function () { var _a = (0, test_utils_1.getTestSymbols)(), editor = _a.editor, s1 = _a.s1, s2 = _a.s2, comp1 = _a.comp1; editor.Symbols = new backbone_1.default.Collection([s1, s2]); var sender = {}; (0, globals_1.expect)(function () { return (0, SymbolsCommands_2.unlinkSymbolInstance)(editor, sender, {}); }).toThrow('missing param component'); var component = comp1.clone(); (0, globals_1.expect)(function () { return (0, SymbolsCommands_2.unlinkSymbolInstance)(editor, sender, { component: component }); }).not.toThrow(); (0, globals_1.expect)(component.get('symbolId')).toBeUndefined(); }); test('Command symbols:create', function () { var _a, _b; var _c = (0, test_utils_1.getTestSymbols)(), s1 = _c.s1, comp1 = _c.comp1, s2 = _c.s2, editor = _c.editor; var sender = {}, component = comp1, pos = {}, target = { getAttribute: globals_1.jest.fn(function () { return comp1.getId(); }) }; (0, globals_1.expect)(function () { return (0, SymbolsCommands_2.createSymbolInstance)(editor, sender, {}); }).toThrow('missing param symbol'); (0, globals_1.expect)(function () { return (0, SymbolsCommands_2.createSymbolInstance)(editor, sender, { symbol: s1, pos: pos, target: target }); }).not.toThrow(); // Add a symbol to the target (0, globals_1.expect)((_a = (0, SymbolsCommands_2.createSymbolInstance)(editor, sender, { symbol: s2, pos: pos, target: target })) === null || _a === void 0 ? void 0 : _a.get('symbolId')).toBe(s2.id); // Try to add a symbol to a target that already has the symbol (S1) (0, globals_1.expect)((_b = (0, SymbolsCommands_2.createSymbolInstance)(editor, sender, { symbol: s1, pos: pos, target: target })) === null || _b === void 0 ? void 0 : _b.get('symbolId')).toBeUndefined(); (0, globals_1.expect)(component.attributes.symbolId).not.toBeUndefined(); }); //# sourceMappingURL=SymbolsCommands.test.js.map