Best JavaScript code snippet using best
dropdown.spec.js
Source:dropdown.spec.js
1import { createElement } from 'lwc';2import Dropdown from 'component/dropdown';3describe('component-dropdown', () => {4 afterEach(() => {5 // The jsdom instance is shared across test cases in a single file so reset the DOM6 while (document.body.firstChild) {7 document.body.removeChild(document.body.firstChild);8 }9 });10 it('display correct text and data-index with one selected item', () => {11 const selectedItem = {12 title: 'First Selected Item',13 id: 'item-one',14 };15 const items = [16 selectedItem,17 {18 title: 'Second Item',19 id: 'item-two',20 },21 {22 title: 'Third Item',23 id: 'item-three',24 },25 ];26 const element = createElement('component-dropdown', { is: Dropdown });27 element.options = {28 multiple: false,29 items,30 selectedItems: [selectedItem],31 };32 document.body.appendChild(element);33 return Promise.resolve().then(() => {34 expect(element).toMatchSnapshot();35 const selectedItemElement = element.shadowRoot.querySelector('.selected-item');36 expect(selectedItemElement.textContent).toBe(selectedItem.title);37 expect(parseInt(selectedItemElement.dataset.index, 10)).toBe(0);38 });39 });40 it('displays no selected items when none are provided', () => {41 const items = [42 {43 title: 'First Selected Item',44 id: 'item-one',45 },46 {47 title: 'Second Item',48 id: 'item-two',49 },50 {51 title: 'Third Item',52 id: 'item-three',53 },54 ];55 const element = createElement('component-dropdown', { is: Dropdown });56 element.options = {57 multiple: false,58 items,59 selectedItems: [],60 };61 document.body.appendChild(element);62 return Promise.resolve().then(() => {63 expect(element).toMatchSnapshot();64 const allSelectedItemElements = element.shadowRoot.querySelectorAll('.selected-item');65 expect(allSelectedItemElements).toHaveLength(0);66 });67 });68 it('displays no items when none are provided', () => {69 const element = createElement('component-dropdown', { is: Dropdown });70 element.options = {71 multiple: false,72 items: [],73 selectedItems: [],74 };75 document.body.appendChild(element);76 return Promise.resolve().then(() => {77 expect(element).toMatchSnapshot();78 const allItemElements = element.shadowRoot.querySelectorAll('.item');79 expect(allItemElements).toHaveLength(0);80 });81 });82 it('displays items in correct order', () => {83 const items = [84 {85 title: 'First Selected Item',86 id: 'item-one',87 },88 {89 title: 'Second Item',90 id: 'item-two',91 },92 {93 title: 'Third Item',94 id: 'item-three',95 },96 ];97 const element = createElement('component-dropdown', { is: Dropdown });98 element.options = {99 multiple: false,100 items,101 selectedItems: [],102 };103 document.body.appendChild(element);104 return Promise.resolve().then(() => {105 expect(element).toMatchSnapshot();106 const allItemElements = element.shadowRoot.querySelectorAll('.item');107 expect(allItemElements).toHaveLength(items.length);108 items.forEach((item, index) => {109 const itemElement = allItemElements[index];110 expect(itemElement.textContent).toBe(item.title);111 expect(parseInt(itemElement.dataset.index, 10)).toBe(index);112 });113 });114 });115 it('fires selection event when item is clicked', () => {116 const items = [117 {118 title: 'First Selected Item',119 id: 'item-one',120 },121 {122 title: 'Second Item',123 id: 'item-two',124 },125 {126 title: 'Third Item',127 id: 'item-three',128 },129 ];130 const element = createElement('component-dropdown', { is: Dropdown });131 element.options = {132 multiple: false,133 items,134 selectedItems: [],135 };136 const evtListenerMock = jest.fn();137 element.addEventListener('selection', evtListenerMock);138 document.body.appendChild(element);139 return Promise.resolve().then(() => {140 const itemElement = element.shadowRoot.querySelector('.item');141 itemElement.click();142 expect(evtListenerMock).toHaveBeenCalledTimes(1);143 });144 });...
eventsscript.js
Source:eventsscript.js
1/**2 * Created by snake on 9/1/2017.3 */4// Run once time when loading.....5function Regroup(currentDay, Elements) {6 for (var i = Elements.length; i--;) {7 var itemId = Elements[i].id;8 var a = moment(itemId, 'YYYY-MM-DD');9 var b = moment(currentDay, 'YYYY-MM-DD');10 var sameWeek = b.diff(a, 'week');11 var diffDay = b.diff(a, 'days');12 if (regroupElement.hasOwnProperty(sameWeek)) {13 regroupElement[sameWeek].push(itemId);14 }15 else {16 regroupElement[sameWeek] = [itemId];17 }18 if (Math.abs(diffDay) >= 7)19 Elements.splice(i, 1);20 }21 newElements = Elements;22 AllItemElements = $("#event_items .row-margin-1");23}24// Reality Filter25function realFilter() {26 hideweekNav();27 searchInput = document.getElementById("searchBar");28 filter = searchInput.value.toLowerCase();29 var eventsItemsElements = $("#event_items .row-margin-1");30 eventsItemsElements.hide();31 for (var i = eventsItemsElements.length; i--;) {32 var eventsItemContentDiv = eventsItemsElements[i].children[1].children[0].children[1];33 var eventsItemTitle = eventsItemContentDiv.children[0].innerText.toLowerCase().replace(/\s+/g, "");34 // search tag in event title.35 if (eventsItemTitle.indexOf(filter) > -1) {36 console.log('event')37 }38 else {39 eventsItemsElements.splice(i, 1);40 }41 }42 if (eventsItemsElements.length !== 0)43 eventsPage(eventsItemsElements);44}45function showElements() {46 console.log(weekCount);47 if (regroupElement.hasOwnProperty(weekCount)) {48 $("#weekNone").hide();49 for (var i = 0; i < regroupElement[weekCount].length; i++) {50 $("#" + regroupElement[weekCount][i]).show();51 }52 }53 else {54 $("#weekNone").show();55 }56}57function hideElements() {58 console.log(weekCount);59 if (regroupElement.hasOwnProperty(weekCount)) {60 for (var i = 0; i < regroupElement[weekCount].length; i++) {61 $("#" + regroupElement[weekCount][i]).hide();62 }63 }64}65function showweekNav() {66 $("#weekEvent").show();67 $("#allEvent").hide();68}69function hideweekNav() {70 $("#weekEvent").hide();71 $("#allEvent").show();72}73// when click previous week.74function previous_week() {75 console.log('previous_week');76 showweekNav();77 hideElements();78 weekCount++;79 showElements();80}81// when click next week.82function next_week() {83 console.log('next_week');84 showweekNav();85 hideElements();86 weekCount--;87 showElements();88}89// when click new event.90function newEvent() {91 console.log('newEvent');92 hideweekNav();93 hideElements();94 eventsPage(newElements);95}96// when click featuredEvent97function featuredEvent() {98 console.log('featuredEvent');99 hideweekNav();100 hideElements();101}102// when click allEvent103function allEvent() {104 console.log('allEvent');105 hideElements();106 hideweekNav();107 eventsPage(AllItemElements);108}109// paginate contents.110function eventsPage(eventItemsElements) {111 var perPage = 5;112 AllItemElements.hide();113 eventItemsElements.slice(0, perPage).show(); // First 5 items show.114 $(".pagination-page").pagination({115 items: eventItemsElements.length,116 itemsOnPage: perPage,117 cssStyle: "light-theme",118 onPageClick: function (pageNumber) {119 var showFrom = perPage * (pageNumber - 1);120 var showTo = showFrom + perPage;121 eventItemsElements.hide().slice(showFrom, showTo).show();122 }123 });...
Using AI Code Generation
1var bestBuy = new BestBuy();2var allItems = bestBuy.allItemElements();3for (var i = 0; i < allItems.length; i++) {4 console.log(allItems[i].name);5}6function BestBuy() {7 this.allItemElements = function() {8 }9}10var bestBuy = new BestBuy();11var allItems = bestBuy.allItemElements();12for (var i = 0; i < allItems.length; i++) {13 console.log(allItems[i].name);14}15function BestBuy() {16 this.allItemElements = function() {17 }18}19var bestBuy = new BestBuy();20var allItems = bestBuy.allItemElements();21for (var i = 0; i < allItems.length; i++) {22 console.log(allItems[i].name);23}24function BestBuy() {25 this.allItemElements = function() {26 }27}28var bestBuy = new BestBuy();29var allItems = bestBuy.allItemElements();30for (var i = 0; i < allItems.length; i++) {31 console.log(allItems[i].name);32}33function BestBuy() {34 this.allItemElements = function() {35 }36}37var bestBuy = new BestBuy();38var allItems = bestBuy.allItemElements();39for (var i = 0; i < allItems.length; i++) {40 console.log(allItems[i].name);41}42function BestBuy() {
Using AI Code Generation
1const BestBuy = require('./bestbuy.js');2const bestBuy = new BestBuy();3bestBuy.allItemElements()4 .then((data) => {5 console.log(data);6 })7 .catch((error) => {8 console.log(error);9 });10const request = require('request-promise');11const cheerio = require('cheerio');12class BestBuy {13 constructor() {14 this.url = url;15 }16 allItemElements() {17 return new Promise((resolve, reject) => {18 request(this.url)19 .then((body) => {20 const $ = cheerio.load(body);21 const itemElements = $('.product-item');22 resolve(itemElements);23 })24 .catch((error) => {25 reject(error);26 });27 });28 }29}30module.exports = BestBuy;31const BestBuy = require('./bestbuy.js');32const bestBuy = new BestBuy();33bestBuy.allItemElements()34 .then((data) => {35 const itemElements = data;36 itemElements.each((index, element) => {37 const $ = cheerio.load(element);38 const itemName = $('.product-title').text();39 const itemPrice = $('.price').text();40 const itemUrl = $('.product-title').attr('href');41 const itemImageUrl = $('.product-image').attr('src');42 console.log(itemName);43 console.log(itemPrice);44 console.log(itemUrl);45 console.log(itemImageUrl);46 });47 })48 .catch((error) => {49 console.log(error);50 });
Using AI Code Generation
1var BestBuy = require('./BestBuy.js');2var bestBuy = new BestBuy();3var allItems = bestBuy.allItemElements();4console.log(allItems);5var BestBuy = function() {6 this.allItemElements = function() {7 return allItems;8 }9}10module.exports = BestBuy;11var BestBuy = function() {12 this.allItemElements = function() {13 return allItems;14 }15}16module.exports = BestBuy;17var BestBuy = require('./BestBuy.js');18var bestBuy = new BestBuy();19var allItems = bestBuy.allItemElements();20console.log(allItems);21var BestBuy = function() {22 this.allItemElements = function() {23 return allItems;24 }25}26module.exports = BestBuy;
Using AI Code Generation
1var bestbuy = new BestBuy();2var items = bestbuy.allItemElements();3for (var i = 0; i < items.length; i++) {4 console.log(items[i]);5}6{ sku: '9999999',7 attributes: [ { name: 'Manufacturer', value: 'Best Buy' },8 { name: 'Model', value: 'Best Buy' },9 { name: 'Color', value: 'Black' },10 { name: 'Condition', value: 'New' },11 { name: 'Warranty', value: '1 Year' },12 { name: 'Features', value: 'Best Buy' },13 { name: 'Product Type', value: 'Best Buy' },14 { name: 'Product Name', value: 'Best Buy' },15 { name: 'Product Line', value: 'Best Buy' },16 { name: 'Brand', value: 'Best Buy' },17 { name: 'Manufacturer Part Number', value: 'Best Buy' },18 { name: 'Product Model', value: 'Best Buy' },19 { name: 'Product Family', value: 'Best Buy' },20 { name: 'Product Name', value: 'Best Buy' },21 { name: 'Product Line', value: 'Best Buy' },22 { name: 'Brand', value: 'Best Buy' },23 { name: 'Manufacturer Part Number', value: 'Best Buy' },24 { name: 'Product
Using AI Code Generation
1var BestBuy = require('./BestBuy.js');2var bb = new BestBuy();3bb.allItemElements('sku,name,customerReviewAverage,manufacturer');4bb.allItemElements('sku,name,customerReviewAverage,manufacturer').then(function(result){5 console.log(result);6});
Using AI Code Generation
1var BestBuySearch = require('./BestBuySearch.js');2var search = new BestBuySearch();3search.allItemElements(function(err,items){4});5function BestBuySearch(){6}7BestBuySearch.prototype.allItemElements = function(callback){8}
Using AI Code Generation
1const BestBuy = require('./bestBuy.js');2const bestBuy = new BestBuy();3const allItems = bestBuy.allItemElements();4for (let item of allItems) {5 console.log(item);6}7const item = bestBuy.getItemElement('12345');8console.log(item);9const newItem = bestBuy.addItemElement({10});11console.log(newItem);12const updatedItem = bestBuy.updateItemElement('12345', {13});14console.log(updatedItem);15const deletedItem = bestBuy.deleteItemElement('12345');16console.log(deletedItem);17const allItems = bestBuy.allItemElements();18for (let item of allItems) {19 console.log(item);20}21class BestBuy {22 constructor() {23 this.items = [];24 }25 allItemElements() {26 return this.items;27 }28 getItemElement(id) {29 return this.items.find((item) => item.id === id);30 }31 addItemElement(item) {32 this.items.push(item);33 return item;34 }35 updateItemElement(id, item) {36 const index = this.items.findIndex((item) => item.id === id);37 this.items[index] = { id, ...item };38 return this.items[index];39 }40 deleteItemElement(id) {41 const index = this.items.findIndex((item) => item
Using AI Code Generation
1var BestBuy = require('bestbuy');2var bb = new BestBuy('your-api-key-here');3bb.allItemElements(function(err, data) {4 console.log(data);5});6var BestBuy = require('bestbuy');7var bb = new BestBuy('your-api-key-here');8bb.allItemElements(function(err, data) {9 console.log(data);10});11var BestBuy = require('bestbuy');12var bb = new BestBuy('your-api-key-here');13bb.allItemElements(function(err, data) {14 console.log(data);15});16var BestBuy = require('bestbuy');17var bb = new BestBuy('your-api-key-here');18bb.allItemElements(function(err, data) {19 console.log(data);20});21var BestBuy = require('bestbuy');22var bb = new BestBuy('your-api-key-here');23bb.allItemElements(function(err, data) {24 console.log(data);25});26var BestBuy = require('bestbuy');27var bb = new BestBuy('your-api-key-here');28bb.allItemElements(function(err, data) {29 console.log(data);30});31var BestBuy = require('bestbuy');32var bb = new BestBuy('your-api-key-here');33bb.allItemElements(function(err, data) {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!