@silexlabs/grapesjs-symbols
Version:
Symbols for GrapesJS
185 lines • 10.5 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (g && (g = 0, op[0] && (_ = 0)), _) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
var globals_1 = require("@jest/globals");
var SymbolsCommands_1 = require("./SymbolsCommands");
globals_1.jest.mock('./utils', function () { return ({
allowDrop: globals_1.jest.fn(function () { return true; }),
createSymbol: globals_1.jest.fn(function (_, comp) { return ({ instances: [comp, { id: 'inst2' }] }); }),
deleteSymbol: globals_1.jest.fn(),
unbindSymbolInstance: globals_1.jest.fn()
}); });
globals_1.jest.mock('lit-html', function () { return ({
html: function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return args;
},
render: globals_1.jest.fn(),
}); });
globals_1.jest.mock('lit-html/directives/unsafe-html.js', function () { return ({
unsafeHTML: function (x) { return x; },
}); });
describe('SymbolsCommands', function () {
var editor;
var component;
var symbol;
beforeEach(function () {
component = {
setName: globals_1.jest.fn(),
set: globals_1.jest.fn(),
getName: globals_1.jest.fn(function () { return 'compName'; }),
getId: globals_1.jest.fn(function () { return 'compId'; }),
append: globals_1.jest.fn(function (inst, _) { return [inst]; }),
};
symbol = {
getId: globals_1.jest.fn(function () { return 'symbolId'; }),
append: globals_1.jest.fn(function (inst, _) { return [inst]; }),
};
editor = {
getSelected: globals_1.jest.fn(function () { return component; }),
Components: {
getSymbols: globals_1.jest.fn(function () { return [symbol]; }),
allById: globals_1.jest.fn(function () { return ({ parentId: symbol }); }),
},
Modal: {
open: globals_1.jest.fn(),
close: globals_1.jest.fn(),
},
select: globals_1.jest.fn(),
};
});
describe('_addSymbol', function () {
it('creates a symbol with label and icon', function () {
var result = (0, SymbolsCommands_1._addSymbol)(editor, null, { component: component, label: 'lbl', icon: 'ico' });
expect(component.setName).toHaveBeenCalledWith('lbl');
expect(component.set).toHaveBeenCalledWith('icon', 'ico');
expect(result).toEqual({ instances: [component, { id: 'inst2' }] });
});
it('uses selected component if no component provided', function () {
editor.getSelected.mockReturnValue(component);
var result = (0, SymbolsCommands_1._addSymbol)(editor, null, { label: 'lbl', icon: 'ico' });
expect(component.setName).toHaveBeenCalledWith('lbl');
expect(component.set).toHaveBeenCalledWith('icon', 'ico');
expect(result).toEqual({ instances: [component, { id: 'inst2' }] });
});
it('usees selected component and its name if no label provided', function () {
editor.getSelected.mockReturnValue(component);
var result = (0, SymbolsCommands_1._addSymbol)(editor, null, {});
expect(component.setName).toHaveBeenCalledWith('compName');
expect(result).toEqual({ instances: [component, { id: 'inst2' }] });
});
it('throws if missing component', function () {
editor.getSelected.mockReturnValue(null);
expect(function () { return (0, SymbolsCommands_1._addSymbol)(editor, null, { component: undefined }); }).toThrow();
});
});
describe('_removeSymbol', function () {
it('removes symbol by id', function () { return __awaiter(void 0, void 0, void 0, function () {
var deleteSymbol;
return __generator(this, function (_a) {
switch (_a.label) {
case 0:
symbol.getId.mockReturnValue('symbolId');
expect(function () { return (0, SymbolsCommands_1._removeSymbol)(editor, null, { symbolId: 'symbolId' }); }).not.toThrow();
return [4 /*yield*/, Promise.resolve().then(function () { return require('./utils'); })];
case 1:
deleteSymbol = (_a.sent()).deleteSymbol;
expect(deleteSymbol).toHaveBeenCalledWith(editor, symbol);
return [2 /*return*/];
}
});
}); });
it('throws if missing symbolId', function () {
expect(function () { return (0, SymbolsCommands_1._removeSymbol)(editor, null, { symbolId: '' }); }).toThrow();
});
it('throws if symbol not found', function () {
editor.Components.getSymbols.mockReturnValue([]);
expect(function () { return (0, SymbolsCommands_1._removeSymbol)(editor, null, { symbolId: 'notfound' }); }).toThrow();
});
});
describe('_unlinkSymbolInstance', function () {
it('calls unbindSymbolInstance', function () { return __awaiter(void 0, void 0, void 0, function () {
var unbindSymbolInstance;
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, Promise.resolve().then(function () { return require('./utils'); })];
case 1:
unbindSymbolInstance = (_a.sent()).unbindSymbolInstance;
expect(function () { return (0, SymbolsCommands_1._unlinkSymbolInstance)(editor, null, { component: component }); }).not.toThrow();
expect(unbindSymbolInstance).toHaveBeenCalledWith(editor, component);
return [2 /*return*/];
}
});
}); });
it('throws if missing component', function () {
expect(function () { return (0, SymbolsCommands_1._unlinkSymbolInstance)(editor, null, { component: null }); }).toThrow();
});
});
describe('_createSymbolInstance', function () {
beforeEach(function () {
// allowDrop returns true by default
editor.Components.allById.mockReturnValue({ parentId: symbol });
});
it('creates a symbol instance and appends it', function () {
var pos = { placement: 'after', index: 0 };
var result = (0, SymbolsCommands_1._createSymbolInstance)(editor, null, { symbol: symbol, pos: pos, target: symbol });
expect(result).toEqual({ id: 'inst2' });
});
it('throws if missing params', function () {
expect(function () { return (0, SymbolsCommands_1._createSymbolInstance)(editor, null, { symbol: null, pos: {}, target: symbol }); }).toThrow();
expect(function () { return (0, SymbolsCommands_1._createSymbolInstance)(editor, null, { symbol: symbol, pos: null, target: symbol }); }).toThrow();
expect(function () { return (0, SymbolsCommands_1._createSymbolInstance)(editor, null, { symbol: symbol, pos: {}, target: null }); }).toThrow();
});
it('throws if parent not found', function () {
editor.Components.allById.mockReturnValue({});
expect(function () { return (0, SymbolsCommands_1._createSymbolInstance)(editor, null, { symbol: symbol, pos: {}, target: { getAttribute: function () { return 'parentId'; } } }); }).toThrow();
});
});
describe('displayError', function () {
it('opens modal with error', function () {
var content = document.createElement('div');
document.body.appendChild(content);
(0, SymbolsCommands_1.displayError)(editor, 'Title', 'Message');
expect(editor.Modal.open).toHaveBeenCalled();
});
});
});
//# sourceMappingURL=SymbolsCommands.test.js.map