Best JavaScript code snippet using devicefarmer-stf
CircularArray.test.ts
Source:CircularArray.test.ts
1import { CircularArray } from './CircularArray';2describe('CircularArray test', () => {3 test('basic adding', () => {4 const circularArray = new CircularArray(3);5 expect(circularArray.capacity).toEqual(3);6 expect(circularArray).toHaveLength(0);7 expect(circularArray.isEmpty).toBe(true);8 expect(circularArray.isFull).toBe(false);9 expect(Array.from(circularArray)).toEqual([]);10 circularArray.add(1);11 expect(circularArray).toHaveLength(1);12 expect(1).toEqual(circularArray.get(0));13 expect(circularArray.isEmpty).toBe(false);14 expect(circularArray.isFull).toBe(false);15 expect(Array.from(circularArray)).toEqual([1]);16 circularArray.add(2);17 expect(circularArray).toHaveLength(2);18 expect(1).toEqual(circularArray.get(0));19 expect(2).toEqual(circularArray.get(1));20 expect(circularArray.isEmpty).toBe(false);21 expect(circularArray.isFull).toBe(false);22 expect(Array.from(circularArray)).toEqual([1, 2]);23 circularArray.add(3);24 expect(circularArray).toHaveLength(3);25 expect(1).toEqual(circularArray.get(0));26 expect(2).toEqual(circularArray.get(1));27 expect(3).toEqual(circularArray.get(2));28 expect(circularArray.isEmpty).toBe(false);29 expect(circularArray.isFull).toBe(true);30 expect(Array.from(circularArray)).toEqual([1, 2, 3]);31 circularArray.add(4);32 expect(circularArray).toHaveLength(3);33 expect(2).toEqual(circularArray.get(0));34 expect(3).toEqual(circularArray.get(1));35 expect(4).toEqual(circularArray.get(2));36 expect(circularArray.isEmpty).toBe(false);37 expect(circularArray.isFull).toBe(true);38 expect(Array.from(circularArray)).toEqual([2, 3, 4]);39 circularArray.add(5);40 expect(circularArray).toHaveLength(3);41 expect(3).toEqual(circularArray.get(0));42 expect(4).toEqual(circularArray.get(1));43 expect(5).toEqual(circularArray.get(2));44 expect(circularArray.isEmpty).toBe(false);45 expect(circularArray.isFull).toBe(true);46 expect(Array.from(circularArray)).toEqual([3, 4, 5]);47 circularArray.add(6);48 expect(circularArray).toHaveLength(3);49 expect(4).toEqual(circularArray.get(0));50 expect(5).toEqual(circularArray.get(1));51 expect(6).toEqual(circularArray.get(2));52 expect(circularArray.isEmpty).toBe(false);53 expect(circularArray.isFull).toBe(true);54 expect(Array.from(circularArray)).toEqual([4, 5, 6]);55 expect(Array.from(circularArray.slice(0, 2))).toEqual([4, 5]);56 circularArray.add(7);57 expect(circularArray).toHaveLength(3);58 expect(5).toEqual(circularArray.get(0));59 expect(6).toEqual(circularArray.get(1));60 expect(7).toEqual(circularArray.get(2));61 expect(circularArray.isEmpty).toBe(false);62 expect(circularArray.isFull).toBe(true);63 expect(Array.from(circularArray)).toEqual([5, 6, 7]);64 expect(Array.from(circularArray.slice(1, 3))).toEqual([6, 7]);65 });66 test('capacity change', () => {67 const circularArray = new CircularArray(3);68 circularArray.add(1);69 circularArray.add(2);70 circularArray.add(3);71 expect(circularArray).toHaveLength(3);72 expect(circularArray.capacity).toEqual(3);73 expect(1).toEqual(circularArray.get(0));74 expect(2).toEqual(circularArray.get(1));75 expect(3).toEqual(circularArray.get(2));76 circularArray.capacity = 5;77 expect(circularArray).toHaveLength(3);78 expect(circularArray.capacity).toEqual(5);79 expect(1).toEqual(circularArray.get(0));80 expect(2).toEqual(circularArray.get(1));81 expect(3).toEqual(circularArray.get(2));82 circularArray.add(4);83 circularArray.add(5);84 circularArray.add(6);85 expect(circularArray).toHaveLength(5);86 expect(circularArray.capacity).toEqual(5);87 expect(Array.from(circularArray)).toEqual([2, 3, 4, 5, 6]);88 circularArray.capacity = 2;89 expect(circularArray).toHaveLength(2);90 expect(circularArray.capacity).toEqual(2);91 expect(Array.from(circularArray)).toEqual([2, 3]);92 circularArray.capacity = 4;93 expect(circularArray).toHaveLength(2);94 expect(circularArray.capacity).toEqual(4);95 expect(Array.from(circularArray)).toEqual([2, 3]);96 circularArray.add(4);97 circularArray.add(5);98 circularArray.add(6);99 circularArray.add(7);100 expect(circularArray).toHaveLength(4);101 expect(circularArray.capacity).toEqual(4);102 expect(Array.from(circularArray)).toEqual([4, 5, 6, 7]);103 circularArray.capacity = 5;104 expect(circularArray).toHaveLength(4);105 expect(circularArray.capacity).toEqual(5);106 expect(Array.from(circularArray)).toEqual([4, 5, 6, 7]);107 circularArray.capacity = 3;108 expect(circularArray).toHaveLength(3);109 expect(circularArray.capacity).toEqual(3);110 expect(Array.from(circularArray)).toEqual([4, 5, 6]);111 });112 test('shift towards start', () => {113 const circularArray: CircularArray<string> = new CircularArray(5);114 circularArray.add('v');115 circularArray.add('w');116 circularArray.add('x');117 circularArray.add('y');118 circularArray.add('z');119 expect(circularArray.isFull).toBe(true);120 let onEnter = jest.fn();121 let onExit = jest.fn();122 circularArray.shift(-3, ['s', 't', 'u'], onExit, onEnter);123 expect(circularArray.get(0)).toEqual('s');124 expect(circularArray.get(1)).toEqual('t');125 expect(circularArray.get(2)).toEqual('u');126 expect(circularArray.get(3)).toEqual('v');127 expect(circularArray.get(4)).toEqual('w');128 expect(onEnter.mock.calls).toHaveLength(3);129 expect(onExit.mock.calls).toHaveLength(3);130 expect(onEnter.mock.calls[0]).toEqual(['s', 0]);131 expect(onEnter.mock.calls[1]).toEqual(['t', 1]);132 expect(onEnter.mock.calls[2]).toEqual(['u', 2]);133 expect(onExit.mock.calls[0]).toEqual(['x', 2]);134 expect(onExit.mock.calls[1]).toEqual(['y', 3]);135 expect(onExit.mock.calls[2]).toEqual(['z', 4]);136 onEnter = jest.fn();137 onExit = jest.fn();138 circularArray.shift(139 -4,140 (function* () {141 yield 'o';142 yield 'p';143 yield 'q';144 yield 'r';145 })(),146 onExit,147 onEnter148 );149 expect(circularArray.get(0)).toEqual('o');150 expect(circularArray.get(1)).toEqual('p');151 expect(circularArray.get(2)).toEqual('q');152 expect(circularArray.get(3)).toEqual('r');153 expect(circularArray.get(4)).toEqual('s');154 expect(onEnter.mock.calls).toHaveLength(4);155 expect(onExit.mock.calls).toHaveLength(4);156 expect(onEnter.mock.calls[0]).toEqual(['o', 0]);157 expect(onEnter.mock.calls[1]).toEqual(['p', 1]);158 expect(onEnter.mock.calls[2]).toEqual(['q', 2]);159 expect(onEnter.mock.calls[3]).toEqual(['r', 3]);160 expect(onExit.mock.calls[0]).toEqual(['t', 1]);161 expect(onExit.mock.calls[1]).toEqual(['u', 2]);162 expect(onExit.mock.calls[2]).toEqual(['v', 3]);163 expect(onExit.mock.calls[3]).toEqual(['w', 4]);164 });165 test('shift towards end', () => {166 const circularArray: CircularArray<string> = new CircularArray(5);167 circularArray.add('a');168 circularArray.add('b');169 circularArray.add('c');170 circularArray.add('d');171 circularArray.add('e');172 expect(circularArray.isFull).toBe(true);173 let onEnter = jest.fn();174 let onExit = jest.fn();175 circularArray.shift(2, ['f', 'g'], onExit, onEnter);176 expect(circularArray.get(0)).toEqual('c');177 expect(circularArray.get(1)).toEqual('d');178 expect(circularArray.get(2)).toEqual('e');179 expect(circularArray.get(3)).toEqual('f');180 expect(circularArray.get(4)).toEqual('g');181 expect(onEnter.mock.calls).toHaveLength(2);182 expect(onExit.mock.calls).toHaveLength(2);183 expect(onEnter.mock.calls[0]).toEqual(['f', 3]);184 expect(onEnter.mock.calls[1]).toEqual(['g', 4]);185 expect(onExit.mock.calls[0]).toEqual(['a', 0]);186 expect(onExit.mock.calls[1]).toEqual(['b', 1]);187 onEnter = jest.fn();188 onExit = jest.fn();189 circularArray.shift(190 4,191 (function* () {192 yield 'h';193 yield 'i';194 yield 'j';195 yield 'k';196 })(),197 onExit,198 onEnter199 );200 expect(circularArray.get(0)).toEqual('g');201 expect(circularArray.get(1)).toEqual('h');202 expect(circularArray.get(2)).toEqual('i');203 expect(circularArray.get(3)).toEqual('j');204 expect(circularArray.get(4)).toEqual('k');205 expect(onEnter.mock.calls).toHaveLength(4);206 expect(onExit.mock.calls).toHaveLength(4);207 expect(onEnter.mock.calls[0]).toEqual(['h', 1]);208 expect(onEnter.mock.calls[1]).toEqual(['i', 2]);209 expect(onEnter.mock.calls[2]).toEqual(['j', 3]);210 expect(onEnter.mock.calls[3]).toEqual(['k', 4]);211 expect(onExit.mock.calls[0]).toEqual(['c', 0]);212 expect(onExit.mock.calls[1]).toEqual(['d', 1]);213 expect(onExit.mock.calls[2]).toEqual(['e', 2]);214 expect(onExit.mock.calls[3]).toEqual(['f', 3]);215 });216 test('fit', () => {217 const circularArray: CircularArray<string> = new CircularArray(5);218 let onEnter = jest.fn();219 let onExit = jest.fn();220 circularArray.fit(['a', 'b', 'c', 'd', 'e'], onExit, onEnter);221 expect(circularArray.get(0)).toEqual('a');222 expect(circularArray.get(1)).toEqual('b');223 expect(circularArray.get(2)).toEqual('c');224 expect(circularArray.get(3)).toEqual('d');225 expect(circularArray.get(4)).toEqual('e');226 expect(circularArray.isFull).toBe(true);227 expect(circularArray).toHaveLength(5);228 expect(onEnter.mock.calls).toHaveLength(5);229 expect(onExit.mock.calls).toHaveLength(0);230 expect(onEnter.mock.calls[0]).toEqual(['a', 0]);231 expect(onEnter.mock.calls[1]).toEqual(['b', 1]);232 expect(onEnter.mock.calls[2]).toEqual(['c', 2]);233 expect(onEnter.mock.calls[3]).toEqual(['d', 3]);234 expect(onEnter.mock.calls[4]).toEqual(['e', 4]);235 onEnter = jest.fn();236 onExit = jest.fn();237 circularArray.fit(238 (function* () {239 yield 'D';240 yield 'E';241 yield 'F';242 })(),243 onExit,244 onEnter245 );246 expect(circularArray.get(0)).toEqual('D');247 expect(circularArray.get(1)).toEqual('E');248 expect(circularArray.get(2)).toEqual('F');249 expect(circularArray.isFull).toBe(false);250 expect(circularArray).toHaveLength(3);251 expect(onEnter.mock.calls).toHaveLength(3);252 expect(onExit.mock.calls).toHaveLength(5);253 expect(onEnter.mock.calls[0]).toEqual(['D', 0]);254 expect(onEnter.mock.calls[1]).toEqual(['E', 1]);255 expect(onEnter.mock.calls[2]).toEqual(['F', 2]);256 expect(new Set(onExit.mock.calls.map((call) => call[0]))).toEqual(257 new Set(['a', 'b', 'c', 'd', 'e'])258 );259 const numRandomAdd = Math.floor(Math.random() * (30 - 20) + 20);260 for (let i = 0; i < numRandomAdd; i++) {261 circularArray.add('RANDOM');262 }263 expect(circularArray.isFull).toBe(true);264 expect(circularArray).toHaveLength(5);265 onEnter = jest.fn();266 onExit = jest.fn();267 const iterable = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'];268 circularArray.fit(iterable, onExit, onEnter);269 expect(circularArray.isFull).toBe(true);270 expect(circularArray).toHaveLength(10);271 expect([...circularArray.slice(2, 6)]).toEqual(['c', 'd', 'e', 'f']);272 expect(onEnter.mock.calls).toHaveLength(10);273 expect(onExit.mock.calls).toHaveLength(5);274 expect(onEnter.mock.calls.map((call) => call[0])).toEqual(iterable);275 expect(new Set(onExit.mock.calls.map((call) => call[0]))).toEqual(new Set(['RANDOM']));276 });277 test('shift towards end with a large capacity', () => {278 // initialization279 const circularArray: CircularArray<number> = new CircularArray(1000);280 circularArray.add(10);281 circularArray.add(11);282 circularArray.add(12);283 circularArray.add(13);284 expect(circularArray).toHaveLength(4);285 expect(circularArray.get(0)).toEqual(10);286 expect(circularArray.get(2)).toEqual(12);287 // shift towards end288 const onEnter = jest.fn();289 const onExit = jest.fn();290 circularArray.shift(2, [21, 22], onExit, onEnter);291 expect(circularArray).toHaveLength(4);292 expect(circularArray.get(0)).toEqual(12);293 expect(circularArray.get(1)).toEqual(13);294 expect(circularArray.get(2)).toEqual(21);295 expect(circularArray.get(3)).toEqual(22);296 expect(onEnter.mock.calls).toHaveLength(2);297 expect(onExit.mock.calls).toHaveLength(2);298 expect(onEnter.mock.calls[0]).toEqual([21, 2]);299 expect(onEnter.mock.calls[1]).toEqual([22, 3]);300 expect(onExit.mock.calls[0]).toEqual([10, 0]);301 expect(onExit.mock.calls[1]).toEqual([11, 1]);302 });303 test('shift past start with a large capacity', () => {304 // initialization305 const circularArray: CircularArray<number> = new CircularArray(1000);306 circularArray.add(10);307 circularArray.add(11);308 circularArray.add(12);309 circularArray.add(13);310 circularArray.add(14);311 expect(circularArray).toHaveLength(5);312 expect(circularArray.get(0)).toEqual(10);313 expect(circularArray.get(2)).toEqual(12);314 // shift towards start315 const onEnter = jest.fn();316 const onExit = jest.fn();317 circularArray.shift(-2, [0, 1], onExit, onEnter);318 expect(circularArray).toHaveLength(5);319 expect(circularArray.get(0)).toEqual(0);320 expect(circularArray.get(1)).toEqual(1);321 expect(circularArray.get(2)).toEqual(10);322 expect(circularArray.get(3)).toEqual(11);323 expect(circularArray.get(4)).toEqual(12);324 expect(onEnter.mock.calls).toHaveLength(2);325 expect(onExit.mock.calls).toHaveLength(2);326 expect(onEnter.mock.calls[0]).toEqual([0, 0]);327 expect(onEnter.mock.calls[1]).toEqual([1, 1]);328 expect(onExit.mock.calls[0]).toEqual([13, 3]);329 expect(onExit.mock.calls[1]).toEqual([14, 4]);330 });...
index.js
Source:index.js
1import {default as SceneBase} from './SceneBase/SceneBase'2import {onEnter as onEnterSceneBase, onExit as onExitSceneBase} from './SceneBase/transition'3import {default as SceneFlashlight} from './SceneFlashlight/SceneFlashlight'4import {onEnter as onEnterSceneFlashlight, onExit as onExitSceneFlashlight} from './SceneFlashlight/transition'5import {default as SceneGenerator} from './SceneGenerator/SceneGenerator'6import {onEnter as onEnterSceneGenerator, onExit as onExitSceneGenerator} from './SceneGenerator/transition'7import {default as SceneKinematic} from './SceneKinematic/SceneKinematic'8import {onEnter as onEnterSceneKinematic, onExit as onExitSceneKinematic} from './SceneKinematic/transition'9import {default as SceneKinematic2} from './SceneKinematic2/SceneKinematic2'10import {onEnter as onEnterSceneKinematic2, onExit as onExitSceneKinematic2} from './SceneKinematic2/transition'11import {default as SceneStairs} from './SplitScreen/SceneStairs/SceneStairs'12import {onEnter as onEnterSceneStairs, onExit as onExitSceneStairs} from './SplitScreen/SceneStairs/transition'13import {default as SceneDoor} from './SplitScreen/SceneDoor/SceneDoor'14import {onEnter as onEnterSceneDoor, onExit as onExitSceneDoor} from './SplitScreen/SceneDoor/transition'15export default {16 SCENEBASE: {17 name:"SCENEBASE",18 scene: SceneBase,19 onEnter: onEnterSceneBase,20 onExit: onExitSceneBase,21 },22 SCENEFLASHLIGHT: {23 name:"SCENEFLASHLIGHT",24 scene: SceneFlashlight,25 onEnter: onEnterSceneFlashlight,26 onExit: onExitSceneFlashlight,27 },28 SCENEGENERATOR: {29 name:"SCENEGENERATOR",30 scene: SceneGenerator,31 onEnter: onEnterSceneGenerator,32 onExit: onExitSceneGenerator,33 },34 SCENEKINEMATIC: {35 name:"SCENEKINEMATIC",36 scene: SceneKinematic,37 onEnter: onEnterSceneKinematic,38 onExit: onExitSceneKinematic,39 },40 SCENEKINEMATIC2: {41 name:"SCENEKINEMATIC2",42 scene: SceneKinematic2,43 onEnter: onEnterSceneKinematic2,44 onExit: onExitSceneKinematic2,45 },46 SCENESTAIRS: {47 name:"SCENESTAIRS",48 scene: SceneStairs,49 onEnter: onEnterSceneStairs,50 onExit: onExitSceneStairs,51 },52 SCENEDOOR: {53 name:"SCENEDOOR",54 scene: SceneDoor,55 onEnter: onEnterSceneDoor,56 onExit: onExitSceneDoor,57 },...
component.js
Source:component.js
1COMPONENT('movement', function(self) {2 var SKIP = { INPUT: 1, TEXTAREA: 1, SELECT: 1 };3 var TYPE = { '13': 'enter', '27': 'esc', '38': 'up', '40': 'down', '37': 'left', '39': 'right' };4 var ondown, onexit;5 self.singleton();6 self.readonly();7 self.nocompile();8 self.make = function() {9 $(W).on('keydown', function(e) {10 if (!SKIP[e.target.tagName]) {11 var c = e.keyCode;12 switch (c) {13 case 9: // tab14 onexit && onexit();15 onexit = null;16 ondown = null;17 break;18 case 13: // enter19 case 27: // esc20 case 38: // up21 case 40: // down22 case 37: // left23 case 39: // right24 ondown && ondown(TYPE[c + ''], e);25 break;26 }27 }28 });29 };30 self.assign = function(down, exit) {31 onexit && onexit();32 ondown = down;33 onexit = exit;34 };...
Using AI Code Generation
1var stf = require('devicefarmer-stf');2var device = stf.getDevice();3device.onExit(function() {4 console.log("Device is going to be released");5});6var stf = require('devicefarmer-stf');7var device = stf.getDevice();8device.onExit(function() {9 console.log("Device is going to be released");10});11var stf = require('devicefarmer-stf');12var device = stf.getDevice();13device.onExit(function() {14 console.log("Device is going to be released");15});16var stf = require('devicefarmer-stf');17var device = stf.getDevice();18device.onExit(function() {19 console.log("Device is going to be released");20});21var stf = require('devicefarmer-stf');22var device = stf.getDevice();23device.onExit(function() {24 console.log("Device is going to be released");25});
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!!