UNPKG

@silexlabs/grapesjs-symbols

Version:
114 lines 5.82 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; Object.defineProperty(exports, "__esModule", { value: true }); var utils_1 = require("./utils"); describe('utils', function () { var editor; var component; var symbolComponent; var instanceComponent; beforeEach(function () { component = { parent: jest.fn(), getId: jest.fn(function () { return 'comp1'; }), remove: jest.fn() }; symbolComponent = __assign(__assign({}, component), { getId: jest.fn(function () { return 'symbol1'; }), remove: jest.fn() }); instanceComponent = __assign(__assign({}, component), { getId: jest.fn(function () { return 'instance1'; }), remove: jest.fn() }); editor = { Components: { getSymbolInfo: jest.fn(), getSymbols: jest.fn(), addSymbol: jest.fn(), detachSymbol: jest.fn(), getSymbol: jest.fn(), } }; }); describe('allowDrop', function () { it('throws if no component', function () { expect(function () { return (0, utils_1.allowDrop)(editor, null); }).toThrow('No component provided'); }); it('returns false if component or parent is a symbol', function () { editor.Components.getSymbolInfo.mockImplementation(function (comp) { return comp === component ? { isSymbol: true } : null; }); component.parent.mockReturnValue(undefined); expect((0, utils_1.allowDrop)(editor, component)).toBe(false); }); it('returns false if parent is a symbol', function () { var parent = { parent: jest.fn(function () { return undefined; }) }; editor.Components.getSymbolInfo.mockImplementation(function (comp) { return comp === parent ? { isSymbol: true } : null; }); component.parent.mockReturnValue(parent); expect((0, utils_1.allowDrop)(editor, component)).toBe(false); }); it('returns true if neither component nor parents are symbols', function () { editor.Components.getSymbolInfo.mockReturnValue(null); component.parent.mockReturnValue(undefined); expect((0, utils_1.allowDrop)(editor, component)).toBe(true); }); }); describe('getSymbols', function () { it('returns symbol infos', function () { var symbols = [{ id: 1 }, { id: 2 }]; editor.Components.getSymbols.mockReturnValue(symbols); editor.Components.getSymbolInfo.mockImplementation(function (s) { return ({ id: s.id, isSymbol: true }); }); expect((0, utils_1.getSymbols)(editor)).toEqual([ { id: 1, isSymbol: true }, { id: 2, isSymbol: true } ]); }); }); describe('getSymbol', function () { it('returns symbol info if component is a symbol', function () { editor.Components.getSymbolInfo.mockReturnValue({ isSymbol: true, id: 'symbol1' }); expect((0, utils_1.getSymbol)(editor, symbolComponent)).toEqual({ isSymbol: true, id: 'symbol1' }); }); it('returns null if not a symbol', function () { editor.Components.getSymbolInfo.mockReturnValue({ isSymbol: false }); expect((0, utils_1.getSymbol)(editor, component)).toBeNull(); }); }); describe('createSymbol', function () { it('throws if no component', function () { expect(function () { return (0, utils_1.createSymbol)(editor, null); }).toThrow(); }); it('returns symbol info if addSymbol succeeds', function () { editor.Components.addSymbol.mockReturnValue(symbolComponent); editor.Components.getSymbolInfo.mockReturnValue({ isSymbol: true, id: 'symbol1' }); expect((0, utils_1.createSymbol)(editor, component)).toEqual({ isSymbol: true, id: 'symbol1' }); }); it('throws if addSymbol fails', function () { editor.Components.addSymbol.mockReturnValue(null); expect(function () { return (0, utils_1.createSymbol)(editor, component); }).toThrow(); }); }); describe('unbindSymbolInstance', function () { it('detaches if component is instance', function () { editor.Components.getSymbolInfo.mockReturnValue({ isInstance: true }); editor.Components.detachSymbol.mockImplementation(function () { }); (0, utils_1.unbindSymbolInstance)(editor, instanceComponent); expect(editor.Components.detachSymbol).toHaveBeenCalledWith(instanceComponent); }); it('throws if not an instance', function () { editor.Components.getSymbolInfo.mockReturnValue({ isInstance: false }); expect(function () { return (0, utils_1.unbindSymbolInstance)(editor, component); }).toThrow(); }); }); describe('deleteSymbol', function () { it('removes symbol if info exists', function () { editor.Components.getSymbolInfo.mockReturnValue({ isSymbol: true }); (0, utils_1.deleteSymbol)(editor, symbolComponent); expect(symbolComponent.remove).toHaveBeenCalled(); }); it('throws if symbol info not found', function () { editor.Components.getSymbolInfo.mockReturnValue(null); expect(function () { return (0, utils_1.deleteSymbol)(editor, symbolComponent); }).toThrow(); }); }); }); //# sourceMappingURL=utils.test.js.map