import { DashboardLayout, ResizeArgs, ChangeEventArgs, DragStartArgs, DragStopArgs, DraggedEventArgs } from "../src/dashboard-layout/dashboard-layout";
import { PanelModel } from "../src/dashboard-layout/dashboard-layout-model";
import { setStyleAttribute as setStyle, isNullOrUndefined, createElement, detach, EventHandler } from '@syncfusion/ej2-base';
import { profile, inMB, getMemoryProfile } from './common.spec';
import { Browser } from '@syncfusion/ej2-base';
function copyObject(source: any, destiation: any): Object {
for (let prop in source) {
destiation[prop] = source[prop];
}
return destiation;
}
function generateTemplate(content: string) {
if (!document.getElementById('temp')) {
let tempEle: HTMLElement = document.createElement('DIV');
tempEle.setAttribute('id', 'temp');
document.body.appendChild(tempEle);
}
return '
' + content + '
';
}
function getTouchEventObject(target: HTMLElement, eventName: string, x?: number, y?: number): Object {
let touchObj = new Touch({
pageX: x,
pageY: y, target: target,
identifier: null,
clientX: x,
clientY: y,
screenX: x,
screenY: y,
radiusX: 2.5,
radiusY: 2.5,
rotationAngle: 10,
altitudeAngle: 0,
azimuthAngle: 0,
force: 0.5,
touchType: null
});
let touchEvent = new TouchEvent(eventName, {
cancelable: true,
bubbles: true,
touches: [touchObj],
targetTouches: [],
changedTouches: [touchObj],
shiftKey: true,
});
let returnObject: any = copyObject(touchEvent, {});
returnObject.currentTarget = target;
return returnObject;
}
function getEventObject(eventType: string, eventName: string, currentTarget?: Element, target?: Element, x?: number, y?: number, offset?: number): Object {
let tempEvent: any = document.createEvent(eventType);
tempEvent.initEvent(eventName, true, true);
let returnObject: any = copyObject(tempEvent, {});
returnObject.preventDefault = () => { return true; };
if (!isNullOrUndefined(x)) {
returnObject.pageX = x;
returnObject.clientX = x;
}
if (!isNullOrUndefined(y)) {
returnObject.pageY = y;
returnObject.clientY = y;
}
if (!isNullOrUndefined(currentTarget)) {
returnObject.currentTarget = currentTarget;
}
if (!isNullOrUndefined(target)) {
returnObject.target = returnObject.srcElement = returnObject.toElement = target;
if (!isNullOrUndefined(offset)) {
returnObject.offsetY = offset;
} else {
returnObject.offsetY = 7;
}
}
returnObject.element = currentTarget;
return returnObject;
}
function setMouseCordinates(eventarg: any, x: number, y: number): Object {
eventarg.pageX = x;
eventarg.pageY = y;
eventarg.clientX = x;
eventarg.clientY = y;
eventarg.offsetY = 7;
return eventarg;
}
function setCss(panelElements: HTMLElement[]) {
for (let j = 0; j < panelElements.length; j++) {
setStyle(panelElements[j], { "position": "absolute" });
setStyle(panelElements[j], { "border": "1px solid black" });
}
}
describe('GridLayout', () => {
beforeAll(() => {
let Chromebrowser: string = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36";
Browser.userAgent = Chromebrowser;
const isDef = (o: any) => o !== undefined && o !== null;
if (!isDef(window.performance)) {
console.log("Unsupported environment, window.performance.memory is unavailable");
pending(); //Skips test (in Chai)
return;
}
});
let gridLayOut: any;
let ele: HTMLElement;
let parentEle: HTMLElement;
let panelObject: any = [
{
id: "one",
cssClass: "e-one",
sizeX: 2,
sizeY: 1,
row: 1,
col: 0,
body: {
content: ("#template1")
}
},
{
id: "two",
cssClass: "e-two",
sizeX: 2,
sizeY: 1,
row: 0,
col: 0,
body: {
content: "#template2"
},
},
{
id: "three",
cssClass: "e-three",
sizeX: 1,
sizeY: 1,
row: 0,
col: 2,
body: {
content: ("#template2")
}
},
{
id: "four",
cssClass: "e-four",
sizeX: 5,
sizeY: 1,
row: 2,
col: 0,
body: {
content: ("#template1")
}
},
{
id: "five",
cssClass: "e-five",
sizeX: 3,
sizeY: 1,
row: 1,
col: 2,
body: {
content: ("#template1")
}
},
{
id: "six",
cssClass: "e-six",
sizeX: 2,
sizeY: 1,
row: 0,
col: 3,
body: {
content: ("#template2")
}
}
];
let newPanels: any = [
{
id: "one",
cssClass: "e-one",
sizeX: 2,
sizeY: 1,
row: 0,
col: 0,
},
{
id: "two",
cssClass: "e-two",
sizeX: 2,
sizeY: 1,
row: 0,
col: 0,
},
{
id: "three",
cssClass: "e-three",
sizeX: 2,
sizeY: 2,
row: 0,
col: 0,
},
];
describe('layout testing', () => {
beforeEach(() => {
ele = createElement('div', { id: 'gridlayout' });
let parentEle: HTMLElement = createElement('div', { id: 'container' });
parentEle.style.width = '1264px';
parentEle.appendChild(ele);
document.body.appendChild(parentEle);
setStyle(ele, { 'position': 'relative' });
let template1: HTMLElement = createElement('script', { id: 'template1' });
template1.setAttribute("type", "text/x-template");
let UL1: HTMLElement = createElement('ul');
for (let i = 0; i < 3; i++) {
let LI: HTMLElement = createElement('li');
LI.innerHTML = i.toString();
UL1.appendChild(LI)
}
template1.appendChild(UL1);
document.body.appendChild(template1);
let template2: HTMLElement = createElement('script', { id: 'template2' });
template2.setAttribute("type", "text/x-template");
let UL2: HTMLElement = createElement('ul');
for (let i = 0; i < 3; i++) {
let LI: HTMLElement = createElement('li');
LI.innerHTML = i.toString();
UL2.appendChild(LI)
}
template2.appendChild(UL2);
document.body.appendChild(template2);
let template3: HTMLElement = createElement('script');
template3.classList.add("headertemp");
template2.setAttribute("type", "text/x-template");
let UL3: HTMLElement = createElement('ul');
for (let i = 0; i < 3; i++) {
let LI: HTMLElement = createElement('li');
LI.innerHTML = i.toString();
UL3.appendChild(LI)
}
template3.appendChild(UL2);
document.body.appendChild(template3);
let template: HTMLElement = createElement('div', { id: 'template' });
document.body.appendChild(template);
});
afterEach(() => {
if (gridLayOut) {
gridLayOut.destroy();
detach(ele);
// detach(parentEle);
}
});
it('Normal case testing for single panel rendering', () => {
let content = generateTemplate('0');
gridLayOut = new DashboardLayout({
cellAspectRatio: 1,
cellSpacing: [5, 5],
allowResizing: true,
panels: [
{ "id": '0', "sizeX": 20, "sizeY": 1, "row": 0, "col": 0, content: content },
]
});
gridLayOut.appendTo('#gridlayout');
expect(gridLayOut.element.childElementCount === 1).toBe(true);
});
it('Panel refresh after the resize', () => {
gridLayOut = new DashboardLayout({
cellAspectRatio: 1,
cellSpacing: [5, 5],
columns: 5,
panels: [
{ id: "first", sizeX: 1, sizeY: 1, row: 0, col: 0 },
{ id: "second", sizeX: 3, sizeY: 2, row: 0, col: 1 },
{ id: "third", sizeX: 1, sizeY: 3, row: 0, col: 4 },
{ id: "fourth", sizeX: 1, sizeY: 1, row: 1, col: 0 },
{ id: "fifth", sizeX: 2, sizeY: 1, row: 2, col: 0 },
{ id: "sixth", sizeX: 1, sizeY: 1, row: 2, col: 2 },
{ id: "seventh", sizeX: 1, sizeY: 1, row: 2, col: 3 }
]
});
gridLayOut.appendTo('#gridlayout');
expect(document.getElementById('container').style.width).toEqual('1264px');
expect(gridLayOut.element.querySelector('#first').style.width).toEqual('248.8px');
expect(gridLayOut.element.querySelector('#second').style.width).toEqual('756.4px');
expect(gridLayOut.element.querySelector('#fifth').style.width).toEqual('502.6px');
(document.querySelectorAll('#container')[1] as HTMLElement).style.width = "800px";
gridLayOut.refresh();
expect(gridLayOut.element.querySelector('#first').style.width).not.toEqual('248px');
expect(gridLayOut.element.querySelector('#second').style.width).not.toEqual('754px');
expect(gridLayOut.element.querySelector('#fifth').style.width).not.toEqual('501px');
});
it('Nested panel refresh after the window resize', () => {
gridLayOut = new DashboardLayout({
cellSpacing: [10, 10],
columns: 20,
allowResizing: true,
panels: [
{ id: 'first', sizeX: 20, sizeY: 10, row: 0, col: 0, content: '' },
{ id: 'second', sizeX: 15, sizeY: 2, row: 0, col: 1 }
]
});
gridLayOut.appendTo('#gridlayout');
let NestedDashboard: DashboardLayout = new DashboardLayout({
cellSpacing: [10, 10],
columns: 6,
allowResizing: true,
panels: [ { id: 'Panel1', sizeX: 4, sizeY: 2, row: 0, col: 0 } ]
});
NestedDashboard.appendTo('#nested_dashboard');
(document.querySelectorAll('#container')[0] as HTMLElement).style.width = '800px';
window.dispatchEvent(new Event('resize'));
gridLayOut.refresh();
expect(document.getElementById('container').style.width).not.toEqual('1264px');
expect(gridLayOut.element.querySelector('#first').style.width).not.toEqual('248px');
expect(gridLayOut.element.querySelector('#second').style.width).not.toEqual('1264px');
document.getElementById('container').style.width = '1264px';
});
it('updatePanel public method test case', () => {
gridLayOut = new DashboardLayout({
cellAspectRatio: 1,
columns: 8,
cellSpacing: [10, 10],
showGridLines: true,
panels: [{
id: "one", sizeX: 2, sizeY: 1, row: 1, col: 0, header: "",cssClass:"test"
},
{
id: "two", sizeX: 2, sizeY: 1, row: 0, col: 0, content: "2
"
},
{
id: "three", sizeX: 1, sizeY: 1, row: 0, col: 2, content: "3
"
},
{
id: "four", sizeX: 5, sizeY: 1, row: 2, col: 0, content: "4
"
},
{
id: "five", sizeX: 3, sizeY: 1, row: 1, col: 2, content: "5
"
},
{
id: "six", sizeX: 2, sizeY: 1, row: 0, col: 3, content: "6
"
}]
});
gridLayOut.appendTo('#gridlayout');
expect(document.getElementById('container').style.width).toEqual('1264px');
expect((gridLayOut).getCellInstance('one').row == 1).toBe(true);
expect((gridLayOut).getCellInstance('two').sizeX == 2).toBe(true);
expect((gridLayOut).getCellInstance('three').sizeY == 1).toBe(true);
expect((gridLayOut).getCellInstance('four').col == 0).toBe(true);
expect((gridLayOut).getCellInstance('five').id == "five").toBe(true);
expect((gridLayOut).getCellInstance('six').content == "6
").toBe(true);
let panel1 = { id: "one", sizeX: 3, sizeY: 3, row: 0, col: 1, header: 'Panel 2
', content: "Hello World
" }
gridLayOut.updatePanel(panel1);
expect((gridLayOut).getCellInstance('one').row == 0).toBe(true);
expect((gridLayOut).getCellInstance('one').col == 1).toBe(true);
expect((gridLayOut).getCellInstance('one').sizeX == 3).toBe(true);
expect((gridLayOut).getCellInstance('one').sizeY == 3).toBe(true);
expect((gridLayOut).getCellInstance('one').id == 'one').toBe(true);
expect((gridLayOut).getCellInstance('one').header == 'Panel 2
').toBe(true);
expect((gridLayOut).getCellInstance('one').content == "Hello World
").toBe(true);
let panel2 = { sizeX: 2, sizeY: 2, row: 2, col: 2, content: "Hello
" };
gridLayOut.updatePanel(panel2);
expect((gridLayOut).getCellInstance('one').sizeX == 3).toBe(true);
expect((gridLayOut).getCellInstance('one').sizeY == 3).toBe(true);
expect((gridLayOut).getCellInstance('two').sizeX == 2).toBe(true);
expect((gridLayOut).getCellInstance('three').sizeY == 1).toBe(true);
let panel3 = { id: "three", sizeX: 1, sizeY: 3, row: 0, col: 0, content: "333
" };
gridLayOut.updatePanel(panel3);
expect((gridLayOut).getCellInstance('three').sizeX == 1).toBe(true);
expect((gridLayOut).getCellInstance('three').sizeY == 3).toBe(true);
expect((gridLayOut).getCellInstance('three').row == 0).toBe(true);
expect((gridLayOut).getCellInstance('three').col == 0).toBe(true);
expect((gridLayOut).getCellInstance('three').content == "333
").toBe(true);
let panel4: PanelModel = { id: "one", sizeX: 1, sizeY: 1, row: 0, col: 10, content: null, header: null };
gridLayOut.updatePanel(panel4);
expect((gridLayOut).getCellInstance('one').sizeX == 1).toBe(true);
expect((gridLayOut).getCellInstance('one').sizeY == 1).toBe(true);
expect((gridLayOut).getCellInstance('one').row == 0).toBe(true);
expect((gridLayOut).getCellInstance('one').col == 7).toBe(true);
expect((gridLayOut).getCellInstance('one').content == null).toBe(true);
expect((gridLayOut).getCellInstance('one').header == null).toBe(true);
let panel5: PanelModel = {};
gridLayOut.updatePanel(panel5);
let panel6: PanelModel = { id: "one" };
gridLayOut.updatePanel(panel6);
let panel7: PanelModel = { id: "seven" };
gridLayOut.updatePanel(panel7);
});
it('Panles rendering with empty objects', () => {
gridLayOut = new DashboardLayout({
cellAspectRatio: 1,
cellSpacing: [5, 5],
allowResizing: true,
panels: [
]
});
gridLayOut.appendTo('#gridlayout');
expect(gridLayOut.element.childElementCount === 0).toBe(true);
expect(gridLayOut.panels.length).toBe(0);
});
it('XSS testing in panel content with enableHtmlSanitizer property as false', () => {
let count: number = 100;
let panelData: PanelModel[] = [{
'id': count.toString() + '_layout', 'sizeX': 1, 'sizeY': 1, 'row': 0, 'col': 0,
content: '