@silexlabs/grapesjs-symbols
Version:
Symbols for GrapesJS
156 lines • 9.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var globals_1 = require("@jest/globals");
var test_utils_1 = require("../test-utils");
var globals_2 = require("@jest/globals");
test('Initialize symbol with values', function () {
var _a = (0, test_utils_1.getTestSymbols)(), s1 = _a.s1, comp1 = _a.comp1, child11 = _a.child11, child12 = _a.child12, child111 = _a.child111;
(0, globals_1.expect)(s1.get('instances')).not.toBeUndefined();
(0, globals_1.expect)(s1.get('instances') instanceof Map).toBe(true);
(0, globals_1.expect)(s1.get('instances').size).toBe(2);
(0, globals_1.expect)(s1.get('label')).toBe('S1');
(0, globals_1.expect)(s1.get('icon')).toBe('fa-cog');
(0, globals_1.expect)(comp1.get('symbolId')).toBe(s1.id);
(0, globals_1.expect)(comp1.get('symbolChildId')).toBeUndefined();
(0, globals_1.expect)(child11.get('symbolChildId')).not.toBeUndefined();
(0, globals_1.expect)(child11.get('symbolId')).toBeUndefined();
(0, globals_1.expect)(s1.get('model').get('symbolId')).toBe(s1.id);
(0, globals_1.expect)(comp1.get('symbolId')).toBe(s1.id);
(0, globals_1.expect)(child11.get('symbolChildId')).toBe(child11.cid);
(0, globals_1.expect)(child12.get('symbolChildId')).toBe(child12.cid);
(0, globals_1.expect)(s1.get('model').components().models[0].get('test')).toBe(child11.get('test'));
(0, globals_1.expect)(s1.get('model').components().models[0].components().models[0].get('test')).toBe(child111.get('test'));
});
test('Initialize symbol with default values', function () {
var s2 = (0, test_utils_1.getTestSymbols)().s2;
(0, globals_1.expect)(s2.get('label')).not.toBeUndefined();
(0, globals_1.expect)(s2.get('icon')).not.toBeUndefined();
});
test('Test data to save has only needed data', function () {
var _a = (0, test_utils_1.getTestSymbols)(), s1 = _a.s1, comp1 = _a.comp1;
(0, globals_1.expect)(s1.get('model').get('symbolId')).toBe(s1.id);
(0, globals_1.expect)(s1.get('model').get('symbolId')).toBe(comp1.get('symbolId'));
(0, globals_1.expect)(s1.toJSON().instances).toBeUndefined();
(0, globals_1.expect)(s1.toJSON().model.attributes.symbolId).toBe(s1.id);
});
test('Test getAll method', function () {
var _a = (0, test_utils_1.getTestSymbols)(), s1 = _a.s1, comp1 = _a.comp1, comp2 = _a.comp2;
var tmp = {};
// no params
(0, globals_1.expect)(s1.getAll()).toHaveLength(2);
// add 1
(0, globals_1.expect)(s1.getAll(tmp)).toHaveLength(3);
(0, globals_1.expect)(s1.getAll(tmp)[0]).toBe(tmp);
// exclude 1
(0, globals_1.expect)(s1.getAll(null, comp1)).toHaveLength(1);
(0, globals_1.expect)(s1.getAll(null, comp1)[0]).toBe(comp2);
// add 1 and exclude 1
(0, globals_1.expect)(s1.getAll(tmp, comp1)).toHaveLength(2);
(0, globals_1.expect)(s1.getAll(tmp, comp1)[0]).toBe(tmp);
(0, globals_1.expect)(s1.getAll(tmp, comp1)[1]).toBe(comp2);
});
test('Test browseInstancesAndModel method', function () {
// In this test:
// - s1 is a symbol
// - comp1 and comp2 are instances of s1
// - child11 and child12 are children of comp1
// - child21 and child22 are children of comp2
var _a = (0, test_utils_1.getTestSymbols)(), s1 = _a.s1, comp1 = _a.comp1, child11 = _a.child11, child12 = _a.child12, comp2 = _a.comp2, child21 = _a.child21, child22 = _a.child22, child111 = _a.child111, child211 = _a.child211;
var model = s1.get('model');
var model11 = model.components().models[0];
var model12 = model.components().models[1];
var model111 = model11.components().models[0];
// First make sure the structure is right
(0, globals_1.expect)(comp1.get('symbolId')).toBe(s1.id);
(0, globals_1.expect)(child12.get('symbolChildId')).toBe(child12.cid);
(0, globals_1.expect)(child11.get('symbolChildId')).toBe(child11.cid);
(0, globals_1.expect)(comp2.get('symbolId')).toBe(s1.id);
(0, globals_1.expect)(child21.get('symbolChildId')).toBe(child11.cid);
(0, globals_1.expect)(child22.get('symbolChildId')).toBe(child12.cid);
(0, globals_1.expect)(model.get('symbolId')).toBe(s1.id);
(0, globals_1.expect)(model11.get('symbolChildId')).toBe(child11.cid);
(0, globals_1.expect)(model12.get('symbolChildId')).toBe(child12.cid);
(0, globals_1.expect)(model111.get('symbolChildId')).toBe(child111.cid);
// Find the 2 symbols in s1 which correspond to child11 in the model and in comp2
// This makes jest crash: expect(cbk).toHaveBeenCalledWith(child11)
var cbk = globals_2.jest.fn();
s1.browseInstancesAndModel(comp1, [child11], cbk);
(0, globals_1.expect)(cbk).toHaveBeenCalledTimes(2);
(0, globals_1.expect)(cbk.mock.calls[0][0][0].cid).toBe(model11.cid);
(0, globals_1.expect)(cbk.mock.calls[1][0][0].cid).toBe(child21.cid);
cbk.mockReset();
s1.browseInstancesAndModel(comp1, [child111], cbk);
(0, globals_1.expect)(cbk).toHaveBeenCalledTimes(2);
(0, globals_1.expect)(cbk.mock.calls[0][0][0].cid).toBe(model111.cid);
(0, globals_1.expect)(cbk.mock.calls[1][0][0].cid).toBe(child211.cid);
(0, globals_1.expect)(cbk).toHaveBeenCalledTimes(2); // called with model and 1 instance
// 1 result element correspondint to child111
(0, globals_1.expect)(cbk.mock.calls[0][0]).toHaveLength(1);
cbk.mockReset();
// FIXME: not sure why this breaks the test
// // child111 not found in S3
// expect(cbk.mock.calls[0][0][0]).toBe(null)
// // 1 result element correspondint to child111
// expect(cbk.mock.calls[1][0]).toHaveLength(1)
// // child111 not found in S3
// expect(cbk.mock.calls[1][0][0]).toBe(null)
});
test('Test getAll method', function () {
var _a = (0, test_utils_1.getTestSymbols)(), s1 = _a.s1, comp1 = _a.comp1;
(0, globals_1.expect)(function () { return s1.getAll(); }).not.toThrow();
(0, globals_1.expect)(s1.getAll() instanceof Array).toBe(true);
(0, globals_1.expect)(s1.get('instances').size).toBe(2);
(0, globals_1.expect)(s1.getAll()).toHaveLength(2);
(0, globals_1.expect)(s1.getAll({})).toHaveLength(3);
(0, globals_1.expect)(s1.getAll(null, {})).toHaveLength(2);
(0, globals_1.expect)(s1.getAll(null, comp1)).toHaveLength(1);
});
describe('Test applyAttributes method', function () {
test('update attributes of instances', function () {
var _a = (0, test_utils_1.getTestSymbols)(), s1 = _a.s1, comp1 = _a.comp1, comp2 = _a.comp2;
var changed = { test: 'test' };
comp1.getChangedProps = function () { return changed; };
comp1._previousAttributes = comp1.attributes;
(0, globals_1.expect)(function () { return s1.applyAttributes(comp1, comp1); }).not.toThrow();
s1.applyAttributes(comp1, comp1);
(0, globals_1.expect)(comp2.get('test')).toBe(comp1.get('test'));
(0, globals_1.expect)(s1.get('model').get('test')).toBe(comp1.get('test'));
});
test('update attributes of comp1 children', function () {
var _a = (0, test_utils_1.getTestSymbols)(), s1 = _a.s1, comp1 = _a.comp1, child11 = _a.child11, child21 = _a.child21, child12 = _a.child12, child22 = _a.child22;
var changed = { test: 'test' };
child11.getChangedProps = function () { return changed; };
child11._previousAttributes = child11.attributes;
child11.set('symbolChildId', 'symbolChildId1');
child21.set('symbolChildId', 'symbolChildId1');
child12.set('symbolChildId', 'symbolChildId2');
child22.set('symbolChildId', 'symbolChildId2');
s1.get('model').components().models[0].set('symbolChildId', 'symbolChildId1');
s1.get('model').components().models[1].set('symbolChildId', 'symbolChildId2');
s1.applyAttributes(comp1, child11);
(0, globals_1.expect)(child21.get('test')).toBe(child11.get('test'));
// the model children should be updated too
(0, globals_1.expect)(s1.get('model').components().models[0].get('test')).toBe(child11.get('test'));
});
});
test('Test create instance', function () {
var _a = (0, test_utils_1.getTestSymbols)(), s1 = _a.s1, comp1 = _a.comp1, child11 = _a.child11, child111 = _a.child111;
var comp1Clone = s1.createInstance();
(0, globals_1.expect)(comp1Clone.components()).toHaveLength(2);
(0, globals_1.expect)(comp1Clone.cid).not.toBe(comp1.cid);
(0, globals_1.expect)(comp1Clone.get('symbolId')).not.toBeUndefined();
(0, globals_1.expect)(comp1Clone.get('symbolId')).toBe(comp1.get('symbolId'));
(0, globals_1.expect)(comp1Clone.components()).toHaveLength(2);
var child11Clone = comp1Clone.components().models[0];
(0, globals_1.expect)(child11Clone.cid).not.toBe(child11.cid);
(0, globals_1.expect)(child11Clone.get('symbolChildId')).not.toBeUndefined();
(0, globals_1.expect)(child11Clone.get('symbolChildId')).toBe(child11.get('symbolChildId'));
(0, globals_1.expect)(child11Clone.get('test')).toBe(child11.get('test'));
(0, globals_1.expect)(child11Clone.components().models).toHaveLength(1);
var child111Clone = child11Clone.components().models[0];
(0, globals_1.expect)(child111Clone.cid).not.toBe(child111.cid);
(0, globals_1.expect)(child111Clone.get('symbolChildId')).not.toBeUndefined();
(0, globals_1.expect)(child111Clone.get('symbolChildId')).toBe(child111.get('symbolChildId'));
(0, globals_1.expect)(child111Clone.get('test')).toBe(child111.get('test'));
});
//# sourceMappingURL=Symbol.test.js.map