Best JavaScript code snippet using istanbul
gesture.js
Source:gesture.js
1/**2 * Triggers the move callback until the mouse drag is finished. Create in a mousedown event handler.3 */4export function watchMouseDrag({onMove, onEnd}) {5 const handleMove = event => {6 onMove(event);7 };8 const handleEnd = event => {9 destroy();10 onEnd(event);11 };12 window.addEventListener('mousemove', handleMove);13 window.addEventListener('mouseup', handleEnd);14 window.addEventListener('mouseleave', handleEnd);15 const destroy = () => {16 window.removeEventListener('mousemove', handleMove);17 window.removeEventListener('mouseup', handleEnd);18 window.removeEventListener('mouseleave', handleEnd);19 };20 return {destroy};21}22/**23 * Triggers the move callback until the touch move is finished. Create in a touchstart event handler.24 */25export function watchTouchDrag({startTouch, eventTarget = window, onMove, onEnd}) {26 const getTouch = event => {27 for (const touch of event.changedTouches) {28 if (touch.identifier === startTouch.identifier) {29 return touch;30 }31 }32 return null;33 };34 const handleMove = event => {35 const touch = getTouch(event);36 if (touch) {37 onMove(touch);38 }39 };40 const handleEnd = event => {41 const touch = getTouch(event);42 if (touch) {43 destroy();44 onEnd(touch);45 }46 };47 eventTarget.addEventListener('touchmove', handleMove);48 eventTarget.addEventListener('touchend', handleEnd);49 eventTarget.addEventListener('touchcancel', handleEnd);50 const destroy = () => {51 eventTarget.removeEventListener('touchmove', handleMove);52 eventTarget.removeEventListener('touchend', handleEnd);53 eventTarget.removeEventListener('touchcancel', handleEnd);54 };55 return {destroy};56}57export function watchHover({element, onMove, onEnd, checkHover = event => true}) {58 let hoverId = null;59 const handleMove = event => {60 eachSubEvent(event, (id, subEvent) => {61 if ((hoverId === null || id === hoverId)) {62 if (checkHover(subEvent)) {63 hoverId = id;64 onMove(subEvent);65 } else if (hoverId !== null) {66 hoverId = null;67 onEnd(subEvent);68 }69 }70 });71 };72 const handleEnd = event => {73 eachSubEvent(event, (id, subEvent) => {74 if (hoverId === id) {75 event.preventDefault(); // To prevent the frozen hover on touch devices76 hoverId = null;77 onEnd(subEvent);78 }79 });80 };81 const moveEvents = ['mouseenter', 'mousemove', 'touchstart', 'touchmove'];82 const endEvents = ['mouseleave', 'touchend', 'touchcancel'];83 for (const name of moveEvents) {84 element.addEventListener(name, handleMove);85 }86 for (const name of endEvents) {87 element.addEventListener(name, handleEnd);88 }89 return {90 destroy() {91 for (const name of moveEvents) {92 element.removeEventListener(name, handleMove);93 }94 for (const name of endEvents) {95 element.removeEventListener(name, handleEnd);96 }97 }98 };99}100export function watchLongTap(element, onShortTap, onLongTap, longTapTime = 500, maxTapDistance = 10) {101 let tapId;102 let tapStartCoordinates;103 let timeoutId;104 const permanentEventHandlers = {105 mousedown: handleTapStart,106 touchstart: handleTapStart,107 touchmove: handleTapMove,108 touchend: handleTapEnd,109 touchcancel: handleTapEnd,110 contextmenu: preventDefault // To prevent the context menu on a long touch111 };112 for (const event in permanentEventHandlers) {113 if (permanentEventHandlers.hasOwnProperty(event)) {114 element.addEventListener(event, permanentEventHandlers[event]);115 }116 }117 return {118 destroy() {119 clearAfterTap();120 for (const event in permanentEventHandlers) {121 if (permanentEventHandlers.hasOwnProperty(event)) {122 element.removeEventListener(event, permanentEventHandlers[event]);123 }124 }125 }126 };127 function handleTapStart(event) {128 eachSubEvent(event, (id, subEvent) => {129 clearAfterTap();130 tapId = id;131 tapStartCoordinates = getEventCoordinates(subEvent);132 timeoutId = setTimeout(handleTapTimeout, longTapTime);133 if (id === 'mouse') {134 event.preventDefault();135 window.addEventListener('mousemove', handleTapMove);136 window.addEventListener('mouseup', handleTapEnd);137 }138 });139 }140 function handleTapMove(event) {141 eachSubEvent(event, (id, subEvent) => {142 if (id === tapId) {143 if (!isInTapDistance(getEventCoordinates(subEvent))) {144 clearAfterTap();145 }146 }147 });148 }149 function handleTapEnd(event) {150 event.preventDefault(); // To prevent excess artificial mouse events after releasing the element151 eachSubEvent(event, id => {152 if (id === tapId) {153 clearAfterTap();154 onShortTap();155 }156 });157 }158 function handleTapTimeout() {159 clearAfterTap();160 onLongTap();161 }162 function clearAfterTap() {163 if (tapId === 'mouse') {164 window.removeEventListener('mousemove', handleTapMove);165 window.removeEventListener('mouseup', handleTapEnd);166 }167 if (timeoutId !== undefined) {168 clearTimeout(timeoutId);169 timeoutId = undefined;170 }171 tapId = undefined;172 tapStartCoordinates = undefined;173 }174 function isInTapDistance(coordinates) {175 if (!tapStartCoordinates) {176 return false;177 }178 return (coordinates[0] - tapStartCoordinates[0]) ** 2 + (coordinates[1] - tapStartCoordinates[1]) ** 2179 <= maxTapDistance ** 2;180 }181 function getEventCoordinates(event) {182 return [event.clientX, event.clientY];183 }184}185function eachSubEvent(event, callback) {186 if (event.type.startsWith('mouse')) {187 callback('mouse', event);188 } else if (event.type.startsWith('touch')) {189 for (const touch of event.changedTouches) {190 callback(`touch${touch.identifier}`, touch);191 }192 }193}194function preventDefault(event) {195 event.preventDefault();...
keyboard.js
Source:keyboard.js
...3 state = {4 num: "-1",5 color: "#62a8ff"6 }7 handleEnd(num) {8 this.setState({9 num: "-1"10 })11 if (this.props.handleAdd && typeof this.props.handleAdd == "function") {12 this.props.handleAdd(num);13 }14 }15 handleStart(num) {16 this.setState({17 num: num18 })19 }20 handleDelete() {21 this.setState({...
callback.js
Source:callback.js
1class EndCallback extends Component {2 componentDidMount() {3 this.handleEnd = this.handleEnd.bind(this);4 }5 handleEnd({ steps, values }) {6 // console.log(steps);7 // console.log(values);8 alert(`Chat handleEnd callback! Number: ${values[0]}`);9 }10 render() {11 return (12 <div className="docs-example-1">13 <ChatBot14 handleEnd={this.handleEnd}15 steps={[16 {17 id: '1',18 message: 'Pick a number',19 trigger: '2',...
Using AI Code Generation
1var istanbul = require('istanbul-middleware');2istanbul.hookLoader(__dirname);3var istanbul = require('istanbul-middleware');4istanbul.hookLoader(__dirname);5istanbul.handleEnd();6var istanbul = require('istanbul-middleware');7istanbul.hookLoader(__dirname);8istanbul.handleEnd();9istanbul.handleEnd();10var istanbul = require('istanbul-middleware');11istanbul.hookLoader(__dirname);12istanbul.handleEnd();13istanbul.handleEnd();14istanbul.handleEnd();15var istanbul = require('istanbul-middleware');16istanbul.hookLoader(__dirname);17istanbul.handleEnd();18istanbul.handleEnd();19istanbul.handleEnd();20istanbul.handleEnd();21var istanbul = require('istanbul-middleware');22istanbul.hookLoader(__dirname);23istanbul.handleEnd();24istanbul.handleEnd();25istanbul.handleEnd();26istanbul.handleEnd();27istanbul.handleEnd();28var istanbul = require('istanbul-middleware');29istanbul.hookLoader(__dirname);30istanbul.handleEnd();31istanbul.handleEnd();32istanbul.handleEnd();33istanbul.handleEnd();34istanbul.handleEnd();35istanbul.handleEnd();36var istanbul = require('istanbul-middleware');37istanbul.hookLoader(__dirname);38istanbul.handleEnd();39istanbul.handleEnd();40istanbul.handleEnd();41istanbul.handleEnd();42istanbul.handleEnd();43istanbul.handleEnd();44istanbul.handleEnd();45var istanbul = require('istanbul-middleware');46istanbul.hookLoader(__dirname);47istanbul.handleEnd();48istanbul.handleEnd();49istanbul.handleEnd();50istanbul.handleEnd();51istanbul.handleEnd();52istanbul.handleEnd();53istanbul.handleEnd();54istanbul.handleEnd();55var istanbul = require('istanbul-m
Using AI Code Generation
1app.get('/coverage', function(req, res) {2 middleware.hookLoader(__dirname);3 middleware.hookRequire();4 middleware.hookRunInThisContext();5 middleware.hookRunInContext();6 middleware.hookRunInNewContext();7 middleware.hookCreateScript();8 middleware.hookScript();9 middleware.hookModuleLoad();10 middleware.handleRun();11 middleware.hookProcessExit();12 middleware.hookProcessBeforeExit();13 middleware.hookProcessRemoveAllListeners();14 middleware.hookProcessOn();15 middleware.hookProcessOnce();16 middleware.hookProcessEmit();17 middleware.hookProcessAddListener();18 middleware.hookProcessListeners();19 middleware.hookProcessMaxListeners();20 middleware.hookProcessListenerCount();21 middleware.hookProcessSetMaxListeners();22 middleware.hookProcessRemoveListener();23 middleware.hookProcessPrependListener();24 middleware.hookProcessPrependOnceListener();25 middleware.hookProcessRawListeners();26 middleware.hookProcessEventNames();27 middleware.hookProcessListenerCount()
Using AI Code Generation
1var middleware = require('istanbul-middleware');2middleware.hookLoader(__dirname);3middleware.hookRequire();4var assert = require('assert');5var fs = require('fs');6var path = require('path');7var vm = require('vm');8var util = require('util');9var _ = require('lodash');10var async = require('async');11var _ = require('lodash');12var async = require('async');13var debug = require('debug')('mocha-istanbul:reporter');14var istanbul = require('istanbul');15var mkdirp = require('mkdirp');16var Mocha = require('mocha');17var path = require('path');18var util = require('util');19var Collector = istanbul.Collector;20var Report = istanbul.Report;21var Reporter = Mocha.reporters.Base;22var reporter = module.exports = function (runner, options) {23 var self = this;24 var mochaReporter = options.reporter || 'spec';25 var mochaReporterOptions = options.reporterOptions || {};26 var mochaReporterOptionsOutput = mochaReporterOptions.output;27 var mocha = new Mocha({28 });29 mocha.reporters = [];30 mocha.addReporter(mochaReporter, mochaReporterOptions);31 mocha.addReporter(self);32 mocha.run(function () {33 self.writeReport(runner, options, function (err) {34 if (err) {35 return self.emit('error', err);36 }37 self.emit('end');38 });39 });40};41util.inherits(reporter, Reporter);42reporter.prototype.writeReport = function (runner, options, callback) {43 var self = this;44 var collector = new Collector();45 var reporterOptions = options.reporterOptions || {};46 var reports = reporterOptions.reports || ['json', 'lcov', 'text', 'text-summary'];47 var reportDir = reporterOptions.dir || 'coverage';48 var reportOpts = reporterOptions.reportOpts || { dir: reportDir };
Using AI Code Generation
1var istanbulMiddleware = require('istanbul-middleware');2app.use('/coverage', istanbulMiddleware.createHandler());3app.get('/coverage', function (req, res) {4 istanbulMiddleware.writeReport();5 res.end();6});
Using AI Code Generation
1var assert = require('assert');2var myModule = require('../src/index.js');3assert.equal(myModule.add(1,1), 2, '1 + 1 = 2');4assert.equal(myModule.add(2,2), 4, '2 + 2 = 4');5assert.equal(myModule.add(3,3), 6, '3 + 3 = 6');6assert.equal(myModule.add(4,4), 8, '4 + 4 = 8');7assert.equal(myModule.add(5,5), 10, '5 + 5 = 10');8assert.equal(myModule.add(6,6), 12, '6 + 6 = 12');9assert.equal(myModule.add(7,7), 14, '7 + 7 = 14');10assert.equal(myModule.add(8,8), 16, '8 + 8 = 16');11assert.equal(myModule.add(9,9), 18, '9 + 9 = 18');12assert.equal(myModule.add(10,10), 20, '10 + 10 = 20');13assert.equal(myModule.add(11,11), 22, '11 + 11 = 22');14assert.equal(myModule.add(12,12), 24, '12 + 12 = 24');15assert.equal(myModule.add(13,13), 26, '13 + 13 = 26');16assert.equal(myModule.add(14,14), 28, '14 + 14 = 28');17assert.equal(myModule.add(15,15), 30, '15 + 15 = 30');18assert.equal(myModule.add(16,16), 32, '16 + 16 = 32');19assert.equal(myModule.add(17,17), 34, '17 + 17
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!!