Best JavaScript code snippet using storybook-root
node-remove-listener.js
Source:node-remove-listener.js
...11 // not to do anything if no listeners have been added to that event12 {13 let result = 'ok';14 let expected_data = TestBase.USER_DEFINED_OBJECT();15 if (cordova.channel.eventNames().length !== 216 || !cordova.channel.eventNames().includes('control')17 || !cordova.channel.eventNames().includes('test-' + testName)18 ) {19 result = 'ko: event listened to are not the expected when the test starts';20 }21 function echoListener(replyObj) {22 if (typeof replyObj !== 'object') {23 result = 'ko: did not receive an object';24 }25 if (!TestBase.checkIfIsEqualToUSER_DEFINED_OBJECT(replyObj)) {26 result = 'ko: the received object did not match the sent one';27 }28 cordova.channel.post('node-remove-listener-result', result);29 };30 cordova.channel.on('echo', echoListener);31 if (cordova.channel.eventNames().length !== 332 || !cordova.channel.eventNames().includes('control')33 || !cordova.channel.eventNames().includes('test-' + testName)34 || !cordova.channel.eventNames().includes('echo')35 || cordova.channel.listeners('echo').length !== 136 || !cordova.channel.listeners('echo').includes(echoListener)37 ) {38 result = 'ko: events listened to are not the expected after adding echoListener'39 }40 cordova.channel.removeListener('not-echo', echoListener); // Should not remove anything.41 if (cordova.channel.eventNames().length !== 342 || !cordova.channel.eventNames().includes('control')43 || !cordova.channel.eventNames().includes('test-' + testName)44 || !cordova.channel.eventNames().includes('echo')45 || cordova.channel.listeners('echo').length !== 146 || !cordova.channel.listeners('echo').includes(echoListener)47 ) {48 // Should still have listeners for echo.49 result = 'ko: events listened to are not the expected after removing from wrong event'50 }51 cordova.channel.post('cordova-echo', expected_data);52 }53 break;54 case 'remove-one-listener':55 // remove a previously added listener56 {57 let result = 'ok';58 let calledOnce = false;59 let expected_data = TestBase.USER_DEFINED_OBJECT();60 if (cordova.channel.eventNames().length !== 261 || !cordova.channel.eventNames().includes('control')62 || !cordova.channel.eventNames().includes('test-' + testName)63 ) {64 result = 'ko: event listened to are not the expected when the test starts';65 }66 function echoListener(replyObj) {67 if (calledOnce) {68 result = 'ko: called more than once';69 return;70 }71 calledOnce=true;72 if (typeof replyObj !== 'object') {73 result = 'ko: did not receive an object';74 }75 if (!TestBase.checkIfIsEqualToUSER_DEFINED_OBJECT(replyObj)) {76 result = 'ko: the received object did not match the sent one';77 }78 if (cordova.channel.eventNames().length !== 379 || !cordova.channel.eventNames().includes('control')80 || !cordova.channel.eventNames().includes('test-' + testName)81 || !cordova.channel.eventNames().includes('echo')82 || cordova.channel.listeners('echo').length !== 183 || !cordova.channel.listeners('echo').includes(echoListener)84 ) {85 // Should still have listeners for echo.86 result = 'ko: events listened to are not the expected before removing the listener'87 }88 cordova.channel.removeListener('echo', echoListener); // Should remove the listener.89 if (cordova.channel.eventNames().length !== 290 || !cordova.channel.eventNames().includes('control')91 || !cordova.channel.eventNames().includes('test-' + testName)92 ) {93 result = 'ko: event listened to are not the expected after removing the listener';94 }95 setTimeout( () => {96 cordova.channel.post('node-remove-listener-result', result);97 }, TestBase.WAIT_FOR_EVENTS_MILLISECONDS()); // Wait for the second event before passing to the next test.98 cordova.channel.post('cordova-echo', expected_data); // Recall99 };100 cordova.channel.on('echo', echoListener);101 if (cordova.channel.eventNames().length !== 3102 || !cordova.channel.eventNames().includes('control')103 || !cordova.channel.eventNames().includes('test-' + testName)104 || !cordova.channel.eventNames().includes('echo')105 || cordova.channel.listeners('echo').length !== 1106 || !cordova.channel.listeners('echo').includes(echoListener)107 ) {108 result = 'ko: events listened to are not the expected after adding echoListener'109 }110 cordova.channel.post('cordova-echo', expected_data);111 }112 break;113 case 'remove-one-listener-from-many':114 // remove one listener after multiple listeners have been added115 {116 let result = 'ok';117 let totalListenerCalls = 0;118 let listener1Called = 0;119 let listener2Called = 0;120 let listener1Received = [];121 let listener2Received = [];122 if (cordova.channel.eventNames().length !== 2123 || !cordova.channel.eventNames().includes('control')124 || !cordova.channel.eventNames().includes('test-' + testName)125 ) {126 result = 'ko: events listened to are not the expected when the test starts';127 }128 function listener1(replyObj) {129 totalListenerCalls++;130 listener1Called++;131 listener1Received.push(replyObj);132 if (typeof replyObj !== 'string') {133 result = 'ko: listener did not receive a string';134 }135 waitForInitialCallOnBothListeners();136 }137 function listener2(replyObj) {138 totalListenerCalls++;139 listener2Called++;140 listener2Received.push(replyObj);141 if (typeof replyObj !== 'string') {142 result = 'ko: listener did not receive a string';143 }144 waitForInitialCallOnBothListeners();145 }146 function waitForInitialCallOnBothListeners() {147 if(totalListenerCalls==2) {148 // Both listeners have received the 'foo' message.149 if (listener1Called!==1150 || listener2Called!==1151 ) {152 result = 'ko: events were not called the expected number of times before removing a listener';153 }154 if (!listener1Received.includes('foo')155 ||listener1Received.includes('bar')156 ||!listener2Received.includes('foo')157 ||listener2Received.includes('bar')158 ) {159 result = 'ko: events did not receive the expected values before removing a listener';160 }161 if (cordova.channel.eventNames().length !== 3162 || !cordova.channel.eventNames().includes('control')163 || !cordova.channel.eventNames().includes('test-' + testName)164 || !cordova.channel.eventNames().includes('echo')165 || cordova.channel.listeners('echo').length !== 2166 || !cordova.channel.listeners('echo').includes(listener1)167 || !cordova.channel.listeners('echo').includes(listener2)168 ) {169 result = 'ko: events listened to are not the expected after adding the listeners'170 }171 // Remove listener2.172 cordova.channel.removeListener('echo', listener2);173 setTimeout(() => {174 if (totalListenerCalls!==3175 || listener1Called!==2176 || listener2Called!==1177 ) {178 result = 'ko: events were not called the expected number of times after removing a listener';179 }180 if (!listener1Received.includes('foo')181 ||!listener1Received.includes('bar')182 ||!listener2Received.includes('foo')183 ||listener2Received.includes('bar')184 ) {185 result = 'ko: events did not receive the expected values after removing a listener';186 }187 if (cordova.channel.eventNames().length !== 3188 || !cordova.channel.eventNames().includes('control')189 || !cordova.channel.eventNames().includes('test-' + testName)190 || !cordova.channel.eventNames().includes('echo')191 || cordova.channel.listeners('echo').length !== 1192 || !cordova.channel.listeners('echo').includes(listener1)193 || cordova.channel.listeners('echo').includes(listener2)194 ) {195 result = 'ko: events listened to are not the expected after removing a listener'196 }197 cordova.channel.post('node-remove-listener-result', result);198 }, TestBase.WAIT_FOR_EVENTS_MILLISECONDS()); //Wait for the next event call to arrive.199 cordova.channel.post('cordova-echo', 'bar');200 }201 }202 cordova.channel.on('echo', listener1);203 cordova.channel.on('echo', listener2);204 if (cordova.channel.eventNames().length !== 3205 || !cordova.channel.eventNames().includes('control')206 || !cordova.channel.eventNames().includes('test-' + testName)207 || !cordova.channel.eventNames().includes('echo')208 || cordova.channel.listeners('echo').length !== 2209 || !cordova.channel.listeners('echo').includes(listener1)210 || !cordova.channel.listeners('echo').includes(listener2)211 ) {212 result = 'ko: events listened to are not the expected after adding the listeners'213 }214 cordova.channel.post('cordova-echo', 'foo');215 }216 break;217 default:218 cordova.channel.post('node-remove-listener-result', 'ko: unexpected test case');219 }220 });221 }...
TreeSelector.js
Source:TreeSelector.js
1/*2 Copyright (c) 2004-2006, The Dojo Foundation3 All Rights Reserved.4 Licensed under the Academic Free License version 2.1 or above OR the5 modified BSD license. For more information on Dojo licensing, see:6 http://dojotoolkit.org/community/licensing.shtml7*/8djd43.provide("djd43.widget.TreeSelector");9djd43.require("djd43.widget.HtmlWidget");10djd43.widget.defineWidget("djd43.widget.TreeSelector", djd43.widget.HtmlWidget, function () {11 this.eventNames = {};12 this.listenedTrees = [];13}, {widgetType:"TreeSelector", selectedNode:null, dieWithTree:false, eventNamesDefault:{select:"select", destroy:"destroy", deselect:"deselect", dblselect:"dblselect"}, initialize:function () {14 for (var name in this.eventNamesDefault) {15 if (djd43.lang.isUndefined(this.eventNames[name])) {16 this.eventNames[name] = this.widgetId + "/" + this.eventNamesDefault[name];17 }18 }19}, destroy:function () {20 djd43.event.topic.publish(this.eventNames.destroy, {source:this});21 return djd43.widget.HtmlWidget.prototype.destroy.apply(this, arguments);22}, listenTree:function (tree) {23 djd43.event.topic.subscribe(tree.eventNames.titleClick, this, "select");24 djd43.event.topic.subscribe(tree.eventNames.iconClick, this, "select");25 djd43.event.topic.subscribe(tree.eventNames.collapse, this, "onCollapse");26 djd43.event.topic.subscribe(tree.eventNames.moveFrom, this, "onMoveFrom");27 djd43.event.topic.subscribe(tree.eventNames.removeNode, this, "onRemoveNode");28 djd43.event.topic.subscribe(tree.eventNames.treeDestroy, this, "onTreeDestroy");29 this.listenedTrees.push(tree);30}, unlistenTree:function (tree) {31 djd43.event.topic.unsubscribe(tree.eventNames.titleClick, this, "select");32 djd43.event.topic.unsubscribe(tree.eventNames.iconClick, this, "select");33 djd43.event.topic.unsubscribe(tree.eventNames.collapse, this, "onCollapse");34 djd43.event.topic.unsubscribe(tree.eventNames.moveFrom, this, "onMoveFrom");35 djd43.event.topic.unsubscribe(tree.eventNames.removeNode, this, "onRemoveNode");36 djd43.event.topic.unsubscribe(tree.eventNames.treeDestroy, this, "onTreeDestroy");37 for (var i = 0; i < this.listenedTrees.length; i++) {38 if (this.listenedTrees[i] === tree) {39 this.listenedTrees.splice(i, 1);40 break;41 }42 }43}, onTreeDestroy:function (message) {44 this.unlistenTree(message.source);45 if (this.dieWithTree) {46 this.destroy();47 }48}, onCollapse:function (message) {49 if (!this.selectedNode) {50 return;51 }52 var node = message.source;53 var parent = this.selectedNode.parent;54 while (parent !== node && parent.isTreeNode) {55 parent = parent.parent;56 }57 if (parent.isTreeNode) {58 this.deselect();59 }60}, select:function (message) {61 var node = message.source;62 var e = message.event;63 if (this.selectedNode === node) {64 if (e.ctrlKey || e.shiftKey || e.metaKey) {65 this.deselect();66 return;67 }68 djd43.event.topic.publish(this.eventNames.dblselect, {node:node});69 return;70 }71 if (this.selectedNode) {72 this.deselect();73 }74 this.doSelect(node);75 djd43.event.topic.publish(this.eventNames.select, {node:node});76}, onMoveFrom:function (message) {77 if (message.child !== this.selectedNode) {78 return;79 }80 if (!djd43.lang.inArray(this.listenedTrees, message.newTree)) {81 this.deselect();82 }83}, onRemoveNode:function (message) {84 if (message.child !== this.selectedNode) {85 return;86 }87 this.deselect();88}, doSelect:function (node) {89 node.markSelected();90 this.selectedNode = node;91}, deselect:function () {92 var node = this.selectedNode;93 this.selectedNode = null;94 node.unMarkSelected();95 djd43.event.topic.publish(this.eventNames.deselect, {node:node});...
events.spec.ts
Source:events.spec.ts
1import { EventNames } from "@c11/engine.types";2import { engine, producers } from "../src";3const flushPromises = () => {4 return new Promise(setImmediate);5};6jest.useFakeTimers("legacy");7const extractEvents = (...fn) => {8 return fn.reduce((acc, x) => {9 acc = acc.concat(x.mock.calls.map((x) => x[0].eventName));10 return acc;11 }, []);12};13const printEvents = (...fn) => {14 const result = fn.reduce((acc, x) => {15 acc = acc.concat(x.mock.calls.map((x) => x[0]));16 return acc;17 }, []);18 console.log(JSON.stringify(result, null, " "));19};20test("should catch events with fn", async () => {21 const onEvents = jest.fn();22 const app = engine({23 onEvents,24 });25 app.start();26 app.stop();27 jest.runAllTimers();28 await flushPromises();29 expect(extractEvents(onEvents)).toEqual([30 EventNames.ENGINE_STARTED,31 EventNames.STATE_UPDATED,32 EventNames.ENGINE_STOPPED,33 ]);34});35test("should catch events with object", async () => {36 const started = jest.fn();37 const stopped = jest.fn();38 const app = engine({39 onEvents: {40 [EventNames.ENGINE_STARTED]: started,41 [EventNames.ENGINE_STOPPED]: stopped,42 },43 });44 app.start();45 app.stop();46 jest.runAllTimers();47 await flushPromises();48 expect(extractEvents(started, stopped)).toEqual([49 EventNames.ENGINE_STARTED,50 EventNames.ENGINE_STOPPED,51 ]);52});53test("should support modules and producers", async () => {54 const onEvents = jest.fn();55 const sample: producer = ({ foo = update.foo }) => {56 foo.set("123");57 };58 const app = engine({59 onEvents,60 state: {61 foo: 123,62 },63 use: [producers(sample)],64 });65 app.start();66 jest.runAllTimers();67 await flushPromises();68 app.stop();69 jest.runAllTimers();70 await flushPromises();71 expect(extractEvents(onEvents)).toEqual([72 EventNames.ENGINE_STARTED,73 EventNames.STATE_UPDATED,74 EventNames.MODULE_MOUNTED,75 EventNames.PRODUCER_MOUNTED,76 EventNames.PRODUCER_CALLED,77 EventNames.PATCH_APPLIED,78 EventNames.STATE_UPDATED,79 EventNames.PRODUCER_UNMOUNTED,80 EventNames.MODULE_UNMOUNTED,81 EventNames.ENGINE_STOPPED,82 ]);...
Using AI Code Generation
1import { eventNames } from 'storybook-root';2import { eventNames } from 'storybook-root';3import { eventNames } from 'storybook-root';4import { eventNames } from 'storybook-root';5import { eventNames } from 'storybook-root';6import { eventNames } from 'storybook-root';7import { eventNames } from 'storybook-root';8import { eventNames } from 'storybook-root';9import { eventNames } from 'storybook-root';10import { eventNames } from 'storybook-root';11import { eventNames } from 'storybook-root';12import { eventNames } from 'storybook-root';13import { eventNames } from 'storybook-root';
Using AI Code Generation
1import { eventNames } from 'storybook-root';2console.log(eventNames);3import { eventNames } from 'storybook-root';4console.log(eventNames);5import { eventNames } from 'storybook-root';6console.log(eventNames);7import { eventNames } from 'storybook-root';8console.log(eventNames);9import { eventNames } from 'storybook-root';10console.log(eventNames);11import { eventNames } from 'storybook-root';12console.log(eventNames);13import { eventNames } from 'storybook-root';14console.log(eventNames);15import { eventNames } from 'storybook-root';16console.log(eventNames);17import { eventNames } from 'storybook-root';18console.log(eventNames);19import { eventNames } from 'storybook-root';20console.log(eventNames);21import { eventNames } from 'storybook-root';22console.log(eventNames);23import { eventNames } from 'storybook-root';24console.log(eventNames);25import { eventNames } from 'storybook-root';26console.log(eventNames);27import { eventNames } from 'storybook-root';28console.log(eventNames);29import { eventNames } from 'storybook-root';30console.log(eventNames);31import { eventNames } from 'storybook-root';
Using AI Code Generation
1import { eventNames } from 'storybook-root';2console.log(eventNames);3import { eventNames } from 'storybook-root';4console.log(eventNames);5import { eventNames } from 'storybook-root';6console.log(eventNames);7import { eventNames } from 'storybook-root';8console.log(eventNames);9import { eventNames } from 'storybook-root';10console.log(eventNames);11import { eventNames } from 'storybook-root';12console.log(eventNames);13import { eventNames } from 'storybook-root';14console.log(eventNames);15import { eventNames } from 'storybook-root';16console.log(eventNames);17import { eventNames } from 'storybook-root';18console.log(eventNames);19import { eventNames } from 'storybook-root';20console.log(eventNames);21import { eventNames } from 'storybook-root';22console.log(eventNames);23import { eventNames } from 'storybook-root';24console.log(eventNames);
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!!