From c390d5471c70989186014707ed6aa85459f54ba5 Mon Sep 17 00:00:00 2001 From: Pankaj Jain Date: Sun, 21 Jun 2020 14:55:46 +0530 Subject: [PATCH] test --- sample/main.js | 19 +++++----- src/table.js | 51 +++++++++++++-------------- tests/tableTest.js | 88 ++++++++++++++++++++++++---------------------- 3 files changed, 82 insertions(+), 76 deletions(-) diff --git a/sample/main.js b/sample/main.js index 6752e6a..914c004 100644 --- a/sample/main.js +++ b/sample/main.js @@ -1,9 +1,13 @@ -let headData=[{text:"Name"}]; -let dataRows=[]; -for(i=0;i<300;i++){ - let currentRow=[{text:"Hello World"}]; +let headData = [{ + text: "Name" +}]; +let dataRows = []; +for (i = 0; i < 300; i++) { + let currentRow = [{ + text: "Hello World" + }]; dataRows.push({ - data: currentRow + data: currentRow }); } let newDynamicTable = new DynamicTable({ @@ -13,14 +17,13 @@ let newDynamicTable = new DynamicTable({ head2Row, footRow, functionArray, - addRowCount:true, - addHeadDataAtBottom:true, + addRowCount: true, + addHeadDataAtBottom: true, addFilter: true, addLimit: true, addCheckboxes: true, checkboxClass }); - let newDynamicTableNode = newDynamicTable.createTable(); let divNode = document.getElementById(`tableDiv`); divNode.appendChild(newDynamicTableNode); \ No newline at end of file diff --git a/src/table.js b/src/table.js index 7ba6845..6a85fa5 100644 --- a/src/table.js +++ b/src/table.js @@ -9,18 +9,18 @@ class DynamicTable { placeholder: `Type here to search...` }); this.limitNode = this._getLimitNode(); - this.loadMore = this._getNode('button',{ - className:`btn btn-primary`, - text:`Load More`, - type:`button` + this.loadMore = this._getNode('button', { + className: `btn btn-primary`, + text: `Load More`, + type: `button` }); this.countNode = this._getNode(`div`); this.bodyNode = this._getNode(`tbody`, { id: `tBody` }); this.checkBoxArray = []; - this.isClicked=false; - this.extraRowsCount=0; + this.isClicked = false; + this.extraRowsCount = 0; } createTable() { let { @@ -47,7 +47,7 @@ class DynamicTable { if (footData) { this._addTableDivision(tableNode, `tfoot`, footData); } else { - if(addHeadDataAtBottom){ + if (addHeadDataAtBottom) { this._addTableDivision(tableNode, `tfoot`, headData, head2Data); } } @@ -66,12 +66,12 @@ class DynamicTable { } if (addLimit) { this.limitNode.onchange = () => { - this.extraRowsCount=0; + this.extraRowsCount = 0; this._addTableDataRows(); } } - this.loadMore.onclick=()=>{ - this.isClicked=true; + this.loadMore.onclick = () => { + this.isClicked = true; this._addTableDataRows(); } } @@ -259,8 +259,8 @@ class DynamicTable { let serialNumber = 0; let limitNumber = 0; let rowNode; - if(this.isClicked){ - this.extraRowsCount+=parseInt(this.limitNode.value); + if (this.isClicked) { + this.extraRowsCount += parseInt(this.limitNode.value); } dataRows.forEach(currentRow => { let { @@ -282,29 +282,28 @@ class DynamicTable { } }); rowNode = this._getNode(`tr`); - let colspanForButton=(1+headData.length); - if(addCheckboxes){ - colspanForButton+=1 + let colspanForButton = (1 + headData.length); + if (addCheckboxes) { + colspanForButton += 1 } - let columnNode=this._getNode(`td`,{ - colspan:colspanForButton + let columnNode = this._getNode(`td`, { + colspan: colspanForButton }); - let center=this._getNode(`center`); + let center = this._getNode(`center`); center.appendChild(this.loadMore); columnNode.appendChild(center); rowNode.appendChild(columnNode); this.bodyNode.appendChild(rowNode); - if(this.isClicked){ - this.isClicked=false; - } - if(limitNumber===serialNumber){ - this.loadMore.style.display=`none`; + if (this.isClicked) { + this.isClicked = false; } - else{ - this.loadMore.style.display=``; + if (limitNumber === serialNumber) { + this.loadMore.style.display = `none`; + } else { + this.loadMore.style.display = ``; } this._clearNode(this.countNode); - if(addRowCount){ + if (addRowCount) { let textNode = document.createTextNode(`Showing 1 to ${limitNumber} of ${serialNumber} entries`); this.countNode.appendChild(textNode); } diff --git a/tests/tableTest.js b/tests/tableTest.js index 00eb3aa..95f17bb 100644 --- a/tests/tableTest.js +++ b/tests/tableTest.js @@ -1,7 +1,6 @@ const puppeteer = require('puppeteer'); const cheerio = require("cheerio"); - -async function autoScroll(page){ +async function autoScroll(page) { await page.evaluate(async () => { await new Promise((resolve, reject) => { var totalHeight = 0; @@ -10,49 +9,38 @@ async function autoScroll(page){ var scrollHeight = document.body.scrollHeight; window.scrollBy(0, distance); totalHeight += distance; - - if(totalHeight >= scrollHeight){ + if (totalHeight >= scrollHeight) { clearInterval(timer); resolve(); } }, 100); }); }); - } -function checkOption50($){ +function checkOption50($) { console.log($('#tableData tbody tr').length); - if($('#tableData tbody tr').length===51) - console.log("Test Passed for option whose value is 50"); - else - console.log("Test Failed for option whose value is 50"); + if ($('#tableData tbody tr').length === 51) console.log("Test Passed for option whose value is 50"); + else console.log("Test Failed for option whose value is 50"); } -function checkOption100($){ +function checkOption100($) { console.log($('#tableData tbody tr').length); - if($('#tableData tbody tr').length===101) - console.log("Test Passed for option whose value is 100"); - else - console.log("Test Failed for option whose value is 100"); + if ($('#tableData tbody tr').length === 101) console.log("Test Passed for option whose value is 100"); + else console.log("Test Failed for option whose value is 100"); } -function checkOption250($){ +function checkOption250($) { console.log($('#tableData tbody tr').length); - if($('#tableData tbody tr').length===251) - console.log("Test Passed for option whose value is 250"); - else - console.log("Test Failed for option whose value is 250"); + if ($('#tableData tbody tr').length === 251) console.log("Test Passed for option whose value is 250"); + else console.log("Test Failed for option whose value is 250"); } -function checkOptionAll($){ +function checkOptionAll($) { console.log($('#tableData tbody tr').length); - if($('#tableData tbody tr').length===301) - console.log("Test Passed for option whose value is 300"); - else - console.log("Test Failed for option whose value is 300"); + if ($('#tableData tbody tr').length === 301) console.log("Test Passed for option whose value is 300"); + else console.log("Test Failed for option whose value is 300"); } - (async () => { const browser = await puppeteer.launch({ headless: false @@ -66,41 +54,57 @@ function checkOptionAll($){ await page.goto('http://127.0.0.1:5500/index.html', { waitUntil: 'domcontentloaded' }); - let response = await page.content(); - let $=cheerio.load(response); - + let $ = cheerio.load(response); await page.type('#tableDiv > div > input', 'Vaibhav', { delay: 100 }); // Types slower, like a user - await page.click("#tableDiv > div > div > div:nth-child(1) > form > select",{delay:20}); + await page.click("#tableDiv > div > div > div:nth-child(1) > form > select", { + delay: 20 + }); await page.select('#tableDiv > div > div > div:nth-child(1) > form > select', '50'); response = await page.content(); - $=cheerio.load(response); + $ = cheerio.load(response); checkOption50($); - await page.click("body",{delay:20}); + await page.click("body", { + delay: 20 + }); await autoScroll(page); - await page.click("#loadMore",{delay:20}); + await page.click("#loadMore", { + delay: 20 + }); await autoScroll(page); - await page.click("#loadMore",{delay:20}); + await page.click("#loadMore", { + delay: 20 + }); await autoScroll(page); - await page.click("#loadMore",{delay:20}); + await page.click("#loadMore", { + delay: 20 + }); await page.select('#tableDiv > div > div > div:nth-child(1) > form > select', '100'); response = await page.content(); - $=cheerio.load(response); + $ = cheerio.load(response); checkOption100($); - await page.click("body",{delay:20}); + await page.click("body", { + delay: 20 + }); await autoScroll(page); - await page.click("#loadMore",{delay:20}); + await page.click("#loadMore", { + delay: 20 + }); await page.select('#tableDiv > div > div > div:nth-child(1) > form > select', '250'); response = await page.content(); - $=cheerio.load(response); + $ = cheerio.load(response); checkOption250($); - await page.click("body",{delay:20}); + await page.click("body", { + delay: 20 + }); await autoScroll(page); await page.select('#tableDiv > div > div > div:nth-child(1) > form > select', 'all'); response = await page.content(); - $=cheerio.load(response); + $ = cheerio.load(response); checkOptionAll($); - await page.click("body",{delay:20}); + await page.click("body", { + delay: 20 + }); })(); \ No newline at end of file