Best JavaScript code snippet using wpt
index.spec.ts
Source:index.spec.ts
1import { act } from '@testing-library/react';2import { renderHook } from '@testing-library/react-hooks';3import { Manifest, useStepManager } from '.';4describe('useStepManager', () => {5 it('reads the manifest', () => {6 type LastStep = 'step1';7 type Steps = 'step0' | LastStep;8 const manifest: Manifest<Steps, LastStep> = {9 start: 'step0',10 steps: {11 step0: {12 next: 'step1',13 },14 step1: {15 next: null,16 },17 },18 };19 const { result } = renderHook(() => useStepManager(manifest));20 // - step 021 expect(result.current.step).toEqual('step0');22 // - step 123 act(() => result.current.next());24 expect(result.current.step).toEqual('step1');25 // - terminated state26 act(() => result.current.next());27 expect(result.current.step).toEqual(null);28 // As state is terminated, trigger once again next should do nothing29 act(() => result.current.next());30 expect(result.current.step).toEqual(null);31 });32 it('is able to reset the step process', () => {33 type LastStep = 'step1';34 type Steps = 'step0' | LastStep;35 const manifest: Manifest<Steps, LastStep> = {36 start: 'step0',37 steps: {38 step0: {39 next: 'step1',40 },41 step1: {42 next: null,43 },44 },45 };46 const { result } = renderHook(() => useStepManager(manifest));47 // - step 048 expect(result.current.step).toEqual('step0');49 // - step 150 act(() => result.current.next());51 expect(result.current.step).toEqual('step1');52 // - Reset: should go back to step053 act(() => result.current.reset());54 expect(result.current.step).toEqual('step0');55 });56 it('is able to find the first step through steps if manifest does not have a start property', () => {57 type LastStep = 'finally';58 type Steps = 'first' | 'then' | LastStep;59 const manifest: Manifest<Steps, LastStep> = {60 steps: {61 then: {62 next: 'finally',63 },64 first: {65 next: 'then',66 },67 finally: {68 next: null,69 },70 },71 };72 const { result } = renderHook(() => useStepManager(manifest));73 expect(result.current.firstStep).toEqual('first');74 // - first75 expect(result.current.step).toEqual('first');76 // - then77 act(() => result.current.next());78 expect(result.current.step).toEqual('then');79 // - finally80 act(() => result.current.next());81 expect(result.current.step).toEqual('finally');82 // - Reset: should go back to first83 act(() => result.current.reset());84 expect(result.current.step).toEqual('first');85 });86 it('triggers onEnter and onExit hooks on step transition', () => {87 type LastStep = 'step1';88 type Steps = 'step0' | LastStep;89 const manifest: Manifest<Steps, LastStep> = {90 start: 'step0',91 steps: {92 step0: {93 next: 'step1',94 onEnter: jest.fn(),95 onExit: jest.fn(),96 },97 step1: {98 next: null,99 onEnter: jest.fn(),100 onExit: jest.fn(),101 },102 },103 };104 const { result } = renderHook(() => useStepManager(manifest));105 // - step 0106 expect(result.current.step).toEqual('step0');107 expect(manifest.steps.step0.onEnter).toHaveBeenCalledTimes(1);108 expect(manifest.steps.step0.onExit).not.toHaveBeenCalled();109 expect(manifest.steps.step1.onEnter).not.toHaveBeenCalled();110 expect(manifest.steps.step1.onExit).not.toHaveBeenCalled();111 // - step 1112 act(() => result.current.next());113 expect(result.current.step).toEqual('step1');114 expect(manifest.steps.step0.onEnter).toHaveBeenCalledTimes(1);115 expect(manifest.steps.step0.onExit).toHaveBeenCalledTimes(1);116 expect(manifest.steps.step1.onEnter).toHaveBeenCalledTimes(1);117 expect(manifest.steps.step1.onExit).not.toHaveBeenCalled();118 // - terminated state119 act(() => result.current.next());120 expect(result.current.step).toEqual(null);121 expect(manifest.steps.step0.onEnter).toHaveBeenCalledTimes(1);122 expect(manifest.steps.step0.onExit).toHaveBeenCalledTimes(1);123 expect(manifest.steps.step1.onEnter).toHaveBeenCalledTimes(1);124 expect(manifest.steps.step1.onExit).toHaveBeenCalledTimes(1);125 // - Reset: go back to step0126 act(() => result.current.reset());127 expect(manifest.steps.step0.onEnter).toHaveBeenCalledTimes(2);128 expect(manifest.steps.step0.onExit).toHaveBeenCalledTimes(1);129 expect(manifest.steps.step1.onEnter).toHaveBeenCalledTimes(1);130 expect(manifest.steps.step1.onExit).toHaveBeenCalledTimes(1);131 // - step 1132 act(() => result.current.next());133 expect(manifest.steps.step0.onEnter).toHaveBeenCalledTimes(2);134 expect(manifest.steps.step0.onExit).toHaveBeenCalledTimes(2);135 expect(manifest.steps.step1.onEnter).toHaveBeenCalledTimes(2);136 expect(manifest.steps.step1.onExit).toHaveBeenCalledTimes(1);137 // - Reset: go back to step0138 act(() => result.current.reset());139 expect(manifest.steps.step0.onEnter).toHaveBeenCalledTimes(3);140 expect(manifest.steps.step0.onExit).toHaveBeenCalledTimes(2);141 expect(manifest.steps.step1.onEnter).toHaveBeenCalledTimes(2);142 expect(manifest.steps.step1.onExit).toHaveBeenCalledTimes(2);143 });...
selectCategories.js
Source:selectCategories.js
1import {getDataFromLocalStorage} from "../../localStorage/getDataFromLocalStorage";2import {saveToLocalStorage} from "../../localStorage/saveToLocalStorage";3export const selectCategories = () => {4 const steps = document.querySelectorAll('.sign-in .steps-content');5 const localStep0 = getDataFromLocalStorage('registration-step-0');6 const localStep1 = getDataFromLocalStorage('registration-step-1');7 let arrayStep0 = !!localStep0 ? localStep0 : [];8 let arrayStep1 = !!localStep1 ? localStep1 : [];9 for (let i = 0; i < steps.length; i++) {10 const inputs = steps[i].querySelectorAll('.tree-checkboxes .input');11 if (inputs.length) {12 for (let j = 0; j < inputs.length; j++) {13 inputs[j].addEventListener('change', () => {14 const modal = document.querySelector(inputs[j].dataset.eventModalId);15 const modalList = modal.querySelector('.selected-list');16 const value = inputs[j].value;17 const title = inputs[j].dataset.eventTitle;18 const thumb = inputs[j].dataset.eventThumb;19 const category = inputs[j].dataset.eventLabel;20 const inputId = inputs[j].getAttribute('id');21 const selectedLeaf = { value, thumb, category, id: inputId, title };22 if (inputs[j].checked) {23 switch (i) {24 case 0:25 !isExist(arrayStep0, selectedLeaf) ? arrayStep0.push(selectedLeaf) : null;26 break;27 case 1:28 !isExist(arrayStep1, selectedLeaf) ? arrayStep1.push(selectedLeaf) : null;29 break;30 }31 } else {32 switch (i) {33 case 0:34 arrayStep0 = removeLeaf(arrayStep0, selectedLeaf);35 break;36 case 1:37 arrayStep1 = removeLeaf(arrayStep1, selectedLeaf);38 break;39 }40 }41 saveToLocalStorage('registration-step-0', arrayStep0);42 saveToLocalStorage('registration-step-1', arrayStep1);43 setTimeout(() => {44 document.querySelector('#leaves .selected-list').innerHTML = '';45 document.querySelector('#leavesnext .selected-list').innerHTML = '';46 appendLeavesToModal();47 }, 200);48 });49 }50 }51 }52};53const isExist = (array, object) => {54 let filteredArray = array.filter(option => option.id === object.id);55 return filteredArray.length;56}57const removeLeaf = (array, object) => {58 return array.filter(option => option.id !== object.id);59}60export const getSelectedLeaves = () => {61 const localStep0 = getDataFromLocalStorage('registration-step-0');62 const localStep1 = getDataFromLocalStorage('registration-step-1');63 if (!!localStep0) {64 for (let i = 0; i < localStep0.length; i++) {65 let id = localStep0[i].id;66 !!document.getElementById(id) ? document.getElementById(id).checked = true : false;67 }68 }69 if (!!localStep1) {70 for (let i = 0; i < localStep1.length; i++) {71 let id = localStep1[i].id;72 !!document.getElementById(id) ? document.getElementById(id).checked = true : false;73 }74 }75 appendLeavesToModal();76};77const appendLeavesToModal = () => {78 const localStep0 = getDataFromLocalStorage('registration-step-0');79 const localStep1 = getDataFromLocalStorage('registration-step-1');80 const step0ModalList = document.querySelector('#leaves .selected-list');81 const step1ModalList = document.querySelector('#leavesnext .selected-list');82 if (!!localStep0 && !!step0ModalList) {83 for (let i = 0; i < localStep0.length; i++) {84 let { value, thumb, category, id } = localStep0[i];85 step0ModalList.insertAdjacentHTML('beforeend', getTemplate(value, thumb, category, id));86 }87 }88 if (!!localStep1 && step1ModalList) {89 for (let i = 0; i < localStep1.length; i++) {90 let { value, thumb, category, id } = localStep1[i];91 step1ModalList.insertAdjacentHTML('beforeend', getTemplate(value, thumb, category, id));92 }93 }94 function getTemplate(value, thumb, category, inputId) {95 return `<li class="selected-link">96 <div class="selected-link-club">97 <div class="selected-link-img">98 <img src="${thumb}" alt="${value}">99 </div>100 <div class="selected-link-name">101 <div class="section-title">102 <h4 class="h-4">${value}</h4>103 </div>104 <p class="tag tag-${category}">${category}</p>105 </div>106 </div>107 <label for="${inputId}" class="selected-link-remove">108 <div class="link color-alumni-hover">109 <span class="link-icon"></span>110 <span class="link-title">Delete from selected</span>111 </div>112 </label>113 </li>`;114 }...
1_euclidAlgorithm.ts
Source:1_euclidAlgorithm.ts
1/*2Page 23Algorithm E (Euclidâs algorithm). Given two positive integers m and n, find4their greatest common divisor, that is, the largest positive integer that evenly5divides both m and n.6E1. [Find remainder.] Divide m by n and let r be the remainder. (We will have7 0 ⤠r < n.)8E2. [Is it zero?] If r = 0, the algorithm terminates; n is the answer.9E3. [Reduce.] Set m â n, n â r, and go back to step E110*/11export {}; // to prevent "Duplicate function implementation" error12function printAnswer(13 functionName: string,14 M: number,15 N: number,16 gcd: number,17 iterations: number18) {19 console.log(20 `${functionName}() \nGCD between ${M} and ${N} = ${gcd}, resolved with ${iterations} iterations\n`21 );22}23function findGCDLoop(m: number, n: number, iterations: number = 0): number {24 const M: number = m;25 const N: number = n;26 // Step E127 let r: number = m % n;28 iterations++;29 // Step E230 while (r !== 0) {31 // Step E332 m = n;33 n = r;34 r = m % n;35 iterations++;36 }37 printAnswer('findGCDLoop', M, N, n, iterations);38 return n;39}40function findGCDRecursion(41 m: number,42 n: number,43 iterations: number = 0,44 M: number = m,45 N: number = n46): number {47 // Step E148 let r: number = m % n;49 console.log(m + ' ' + n + ' ' + r);50 iterations++;51 // Step E252 if (r === 0) {53 printAnswer('findGCDRecursion', M, N, n, iterations);54 return n;55 }56 // Step E357 return findGCDRecursion(n, r, iterations, M, N);58}59/*60Page 461It is clear that if m < n originally, the quotient in step E1 62will always be zero and the algorithm will always proceed to 63interchange m and n in this rather cumbersome fashion.64We could insert a new step at the beginning:65E0. [Ensure m ⥠n.] If m < n, exchange m â n.66*/67function findGCDLoopStep0(68 m: number,69 n: number,70 iterations: number = 071): number {72 const M: number = m;73 const N: number = n;74 // Step E075 if (m < n) {76 [m, n] = [n, m];77 }78 // Step E179 let r: number = m % n;80 iterations++;81 // Step E282 while (r !== 0) {83 // Step E384 m = n;85 n = r;86 r = m % n;87 iterations++;88 }89 printAnswer('findGCDLoopStep0', M, N, n, iterations);90 return n;91}92function findGCDRecursionStep0(93 m: number,94 n: number,95 iterations: number = 0,96 M: number = m,97 N: number = n98): number {99 // Step E0100 if (m < n) {101 [m, n] = [n, m];102 }103 // Step E1104 let r: number = m % n;105 iterations++;106 // Step E2107 if (r === 0) {108 printAnswer('findGCDRecursionStep0', M, N, n, iterations);109 return n;110 }111 // Step E3112 return findGCDRecursionStep0(n, r, iterations, M, N);113}114// Testing the algorithm115let m = 119;116let n = 544;117findGCDLoop(m, n);118findGCDRecursion(m, n);119findGCDLoopStep0(m, n);120findGCDRecursionStep0(m, n);121m = 398;122n = 43390;123findGCDLoop(m, n);124findGCDRecursion(m, n);125findGCDLoopStep0(m, n);126findGCDRecursionStep0(m, n);127m = 10000000;128n = 1000;129findGCDLoop(m, n);130findGCDRecursion(m, n);131findGCDLoopStep0(m, n);132findGCDRecursionStep0(m, n);133m = 15;134n = 250;135findGCDLoop(m, n);136findGCDRecursion(m, n);137findGCDLoopStep0(m, n);...
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org', 'A.12345678901234567890123456789012');3 if (err) {4 console.log(err);5 } else {6 console.log(data);7 }8});9var wpt = require('wpt');10var wpt = new WebPageTest('www.webpagetest.org', 'A.12345678901234567890123456789012');11wpt.getTestResults('140619_4C_5P', function(err, data) {12 if (err) {13 console.log(err);14 } else {15 console.log(data);16 }17});18var wpt = require('wpt');19var wpt = new WebPageTest('www.webpagetest.org', 'A.12345678901234567890123456789012');20wpt.getTestResults('140619_4C_5P', function(err, data) {21 if (err) {22 console.log(err);23 } else {24 console.log(data);25 }26});27var wpt = require('wpt');28var wpt = new WebPageTest('www.webpagetest.org', 'A.12345678901234567890123456789012');29wpt.getTestResults('140619_4C_5P', function(err, data) {30 if (err) {31 console.log(err);32 } else {33 console.log(data);34 }35});36var wpt = require('wpt');37var wpt = new WebPageTest('www.webpagetest.org', 'A.12345678901234567890123456789012');38wpt.getTestResults('140619_4C_5P', function(err, data) {39 if (err) {40 console.log(err);41 } else {42 console.log(data);43 }44});
Using AI Code Generation
1var wpt = require('wpt');2var test = new wpt('your_api_key');3 if (err) {4 console.log(err);5 } else {6 console.log(data);7 }8});
Using AI Code Generation
1var wpt = require('wpt');2wpt.step0(function(err, data){3 console.log(err, data);4});5### `wpt.step0(callback)`6### `wpt.step1(callback)`7### `wpt.step2(callback)`8### `wpt.step3(callback)`9### `wpt.step4(callback)`10### `wpt.step5(callback)`11### `wpt.step6(callback)`12### `wpt.step7(callback)`13### `wpt.step8(callback)`14### `wpt.step9(callback)`
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!!