Best JavaScript code snippet using wpt
index.test.js
Source:index.test.js
1import React from 'react';2import { mount } from 'enzyme';3import { checkIfImageCached, loadImage } from './utils';4import ImageLoader, { tempImg } from './index';5import styles, {6 REVEAL_DURATION,7} from './styles';8jest.mock('./utils', () => jest.genMockFromModule('./utils'));9describe('ImageLoader', () => {10 const imgURL = 'http://fake.com/not/real.jpg';11 let wrapper;12 let img;13 let loaderProps;14 it('should add a custom CSS class to the container if one was supplied', () => {15 const altText = 'alt text';16 const className = 'custom-class';17 checkIfImageCached.mockReturnValue(true);18 wrapper = mount(<ImageLoader className={className} src={imgURL} alt={altText} />);19 expect(wrapper.childAt(0).props().className).toEqual(className);20 });21 it('should load an image if not cached', () => {22 const altText = 'alt text';23 let callback;24 checkIfImageCached.mockReturnValue(false);25 loadImage.mockImplementation((src, cb) => { callback = cb; });26 // render out everything in a loading state27 wrapper = mount(<ImageLoader src={imgURL} alt={altText} />);28 img = wrapper.find('img').instance();29 loaderProps = wrapper.find('LoadIndicator').props();30 expect(loadImage).toHaveBeenCalledWith(imgURL, expect.any(Function));31 expect(img.src).toEqual(tempImg);32 expect(img.alt).toEqual(altText);33 expect(loaderProps.visible).toBe(true);34 // loading has completed35 callback();36 wrapper.update();37 img = wrapper.find('img').instance();38 loaderProps = wrapper.find('LoadIndicator').props();39 expect(img.src).toEqual(imgURL);40 expect(loaderProps.visible).toBe(false);41 });42 it('should set the source if the image is cached', () => {43 checkIfImageCached.mockReturnValue(true);44 // render out everything in a loaded state45 wrapper = mount(<ImageLoader src={imgURL} />);46 img = wrapper.find('img').instance();47 loaderProps = wrapper.find('LoadIndicator').props();48 expect(img.src).toEqual(imgURL);49 expect(loaderProps.visible).toBe(false);50 });51 it('should NOT try to set state if component was unmounted before load listener completed', () => {52 let callback;53 checkIfImageCached.mockReturnValue(false);54 loadImage.mockImplementation((src, cb) => { callback = cb; });55 wrapper = mount(<ImageLoader src={imgURL} />);56 const instance = wrapper.instance();57 instance.componentWillUnmount();58 callback();59 wrapper.update();60 expect(instance.state.loaded).toBe(false);61 });62 describe('Animate image on load', () => {63 const mountIt = () => {64 wrapper = mount(<ImageLoader {...props} />);65 instance = wrapper.instance();66 instance.handleLoadedImage();67 wrapper.update();68 };69 let rafCB, instance, props;70 beforeEach(() => {71 jest.spyOn(global, 'requestAnimationFrame');72 global.requestAnimationFrame.mockImplementation((cb) => { rafCB = cb; });73 props = {74 src: imgURL,75 };76 jest.useFakeTimers();77 });78 afterEach(() => {79 global.requestAnimationFrame.mockRestore();80 });81 it('should add class to fade image in', () => {82 jest.spyOn(global, 'setTimeout');83 mountIt();84 expect(instance.state.loaded).toBe(true);85 expect(instance.state.revealImage).toBe(false);86 expect(wrapper.find('img').props().className).toEqual(`${ styles.img }`);87 // shouldn't call setState if not mounted88 instance.mounted = false;89 rafCB();90 jest.runTimersToTime(REVEAL_DURATION * 1000);91 wrapper.update();92 expect(instance.state.revealImage).not.toBe(true);93 // should update state and reveal image when mounted94 instance.mounted = true;95 rafCB();96 jest.runTimersToTime(REVEAL_DURATION * 1000);97 wrapper.update();98 expect(instance.state.revealImage).toBe(true);99 expect(wrapper.find('img').props().className).toEqual(`${ styles.img } is--loaded`);100 expect(global.setTimeout).not.toHaveBeenCalled();101 global.setTimeout.mockRestore();102 });103 it('should execute callback after the image has transitioned', () => {104 props.onLoad = jest.fn();105 mountIt();106 rafCB();107 jest.runTimersToTime(REVEAL_DURATION * 1000);108 wrapper.update();109 expect(props.onLoad).toHaveBeenCalled();110 });111 });...
sketch.js
Source:sketch.js
1let animReq 2let cvs3export function defineSketch(sketch, settings) {4 const {resize} = settings5 if(!cvs) {6 cvs = document.createElement('canvas')7 document.body.prepend(cvs)8 } else {9 clearAnimReq()10 }11 if (typeof (resize) === 'function') {12 window.onresize = () => {13 console.log("Window resized")14 const {width,height} = resize()15 clearAnimReq()16 start(cvs, sketch, {...settings, width, height})17 }18 }19 start(cvs, sketch, settings)20}21function clearAnimReq() {22 window.cancelAnimationFrame(animReq)23}24function start(cvs, sketch, settings) {25 const {width, height, animate} = settings26 cvs.width = width;27 cvs.height = height;28 const context = cvs.getContext("2d")29 30 const initialProps = {...settings, context, canvas: cvs}31 const renderFn = sketch( initialProps)32 let frame = 0;33 let time = 034 const startTime = new Date()35 const rafCb = (timestamp) => {36 const renderProps = {...initialProps, frame, time, timestamp}37 const {animate} = { ...renderProps, ...renderFn(renderProps)}38 frame++39 time = (new Date()) - startTime40 if (animate) animReq=window.requestAnimationFrame(rafCb)41 }42 if (animate) {43 animReq=window.requestAnimationFrame(rafCb)44 } else {45 renderFn(initialProps)46 }47 48}49export function newContext(w,h) {50 const c = document.createElement("canvas")51 c.width=w52 c.height=h53 return c.getContext("2d")...
setInterval.js
Source:setInterval.js
1// åºäºrAFå®ç°ä¸ä¸ªåç¡®çå®æ¶å¨2function setIntervalNew (time,cb) {3 var lastTime = 04 function rAFcb (timestamp) {5 if(lastTime === 0) {6 lastTime = timestamp7 }8 if(timestamp - lastTime >= time) {9 cb()10 lastTime = timestamp11 }12 window.$zhtid = window.requestAnimationFrame(rAFcb)13 }14 window.$zhtid = window.requestAnimationFrame(rAFcb)15}16setIntervalNew(30000,() => {17 console.log(111)18})...
Using AI Code Generation
1var wpt = require('webpagetest');2var wptClient = wpt('www.webpagetest.org');3wptClient.runTest(url, {location:'Dulles:Chrome'}, function(err, data) {4 wptClient.pollTest(data.data.testId, function(err, data) {5 wptClient.rafCb(data.data.testId, function(err, data) {6 console.log(data);7 });8 });9});
Using AI Code Generation
1var rafCb = wpt.rafCb;2var rafCb = wpt.rafCb;3var rafCb = wpt.rafCb;4var rafCb = wpt.rafCb;5var rafCb = wpt.rafCb;6var rafCb = wpt.rafCb;7var rafCb = wpt.rafCb;8var rafCb = wpt.rafCb;9var rafCb = wpt.rafCb;10var rafCb = wpt.rafCb;11var rafCb = wpt.rafCb;12var rafCb = wpt.rafCb;
Using AI Code Generation
1const wpt = require('webpagetest');2const options = {3};4const client = wpt.createClient(options);5 if (err) return console.error(err);6 client.getTestResults(data.data.testId, function (err, data) {7 if (err) return console.error(err);8 console.log(data.data.runs[1].firstView.SpeedIndex);9 });10});11const wpt = require('webpagetest');12const options = {13};14const client = wpt.createClient(options);15 if (err) return console.error(err);16 client.getTestResults(data.data.testId, function (err, data) {17 if (err) return console.error(err);18 console.log(data.data.runs[1].firstView.SpeedIndex);19 });20});21const wpt = require('webpagetest');22const options = {23};24const client = wpt.createClient(options);25 if (err) return console.error(err);26 client.getTestResults(data.data.testId, function (err, data) {
Using AI Code Generation
1var wpt = require('webpagetest');2var options = {3};4var wpt = new WebPageTest(options);5wpt.runTest(url, function(err, data) {6 if (err) return console.error(err);7 wpt.getTestResults(data.data.testId, function(err, data) {8 if (err) return console.error(err);9 console.log(data.data.median.firstView.TTFB);10 });11});
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!!