Best JavaScript code snippet using jest
frontend.js
Source:frontend.js
1/* eslint-disable no-alert */2/* eslint-disable no-unused-vars */3const4 inputDown = document.getElementById('down_delay');5const inputRele = document.getElementById('release_delay');6const inputHotk = document.getElementById('hotkey');7const inputCores = document.getElementById('useCores');8const oneThread = document.getElementById('one');9const twoThread = document.getElementById('two');10const customThread = document.getElementById('custom');11const saveButton = document.getElementById('btn_save');12const btnMacro = document.getElementById('btn_macro');13const radioGodMode = document.getElementById('god_mode');14const BTN_NAME = ['Saved!', 'Save configs'];15// BUTTON STATES16const BTN_NORMAL = 1;17const BTN_CHANGED = 0;18window.onload = function loadSavedUserConfigs() {19 // load configs20 window.readConfigs = JSON.parse(window.config.load());21 inputDown.value = window.readConfigs.hold_delay;22 inputRele.value = window.readConfigs.release_delay;23 inputHotk.value = window.readConfigs.hotkey.toUpperCase();24 radioGodMode.checked = window.readConfigs.god_mode;25 const checkRatio = (elem) => elem.setAttribute('checked', 'checked');26 switch (window.readConfigs.cores_to_use) {27 case 1: checkRatio(oneThread); break;28 case 2: checkRatio(twoThread); break;29 default:30 checkRatio(customThread);31 inputCores.value = window.readConfigs.cores_to_use;32 break;33 }34};35// save configs36function saveCnfs() {37 let nCoresToUse = 1;38 // verify processor cores, if custom is marked39 if (customThread.checked) {40 try {41 nCoresToUse = parseInt(inputCores.value, 10);42 const avaliableCores = window.cpu.getCores();43 const cpuModel = window.cpu.getName();44 if (nCoresToUse > avaliableCores) {45 alert(`Sir, your ${cpuModel} has only ${avaliableCores} cores.`);46 return;47 }48 } catch (err) {49 alert('Use integer values only in custom cores.');50 }51 } else if (twoThread.checked) nCoresToUse = 2;52 // verify all inputs53 if (54 inputDown.value === '' || inputRele.value === '' || inputHotk.value === ''55 ) {56 alert('Fill all inputs!');57 } else {58 saveButton.innerHTML = 'Saving...';59 const objectConfig = {60 hold_delay: inputDown.value,61 release_delay: inputRele.value,62 hotkey: inputHotk.value.toLowerCase(),63 cores_to_use: nCoresToUse,64 god_mode: radioGodMode.checked,65 };66 window.config.save(objectConfig);67 setTimeout(() => {68 saveButton.innerHTML = 'Saved!';69 alert('Restart app to apply new configs!');70 }, 3);71 }72}73function changeMacroState() {74 const arrayInput = document.querySelectorAll('input');75 const enable = (btnMacro.textContent === 'START');76 if (window.cpu.getCores() >= inputCores.value) {77 btnMacro.innerHTML = (enable) ? 'STOP' : 'START';78 arrayInput.forEach((input) => {79 const inputVar = input;80 inputVar.disabled = enable;81 inputVar.contentEditable = !enable;82 });83 window.macro.changeState();84 } else {85 alert(`Select a number of cores less or equal than ${window.cpu.getCores()}`);86 }87}88// LAYOUT FUNCTIONS89function changeState() {90 if (saveButton.textContent === BTN_NAME[BTN_CHANGED]) {91 saveButton.innerText = BTN_NAME[BTN_NORMAL];92 }93}94function toUpper() {95 inputHotk.value = inputHotk.value.toUpperCase();...
CommonBean.js
Source:CommonBean.js
...22var writeDownloadInfo = function() {23 fs.writeFileSync(downloadInfoPath, JSON.stringify(downloadInfo));24}25//é
置信æ¯26var configs = readConfigs();27/**28 * ä¸è½½ç¸å
³ä¿¡æ¯29 * ç³»ç»å¯å¨æ¶ä»æ°æ®æ件åºåå°å
å30 * 该对象信æ¯åæ´åæ¥æ´æ°æ°æ®æ件31 * */32var downloadInfo = readDownloadInfo();33//æ ¡éªæ°æ®ææ éå¤34var checkRepeat = function(obj) {35 downloadInfo.forEach(function(item) {36 if(obj.fileName == item.fileName) {37 return "åå¨ç¸åçæ件å称ï¼æ¯å¦ç»§ç»ï¼";38 }39 if(obj.downloadPath == item.downloadPath) {40 return "åå¨ç¸åçä¸è½½ä»»å¡ï¼æ¯å¦ç»§ç»ï¼";...
validRequest.js
Source:validRequest.js
1const readConfigs = require("../configs/readConfigs");2const ReadConfigs = new readConfigs();3ReadConfigs.getAllServicesPingEndpoints()4const validateRequest = (req, res, next) => {5 const url = req.path;6 const method = req.method;7 const contextRoot = "/" + url.split("/")[1];8 const path = url.replace(contextRoot, "");9 const destinationService = ReadConfigs.getDestinationService(contextRoot, path, method);10 if(!destinationService.isValid) {11 return res.status(400).send({12 error: 'Your request is not valid !!'13 })14 }15 req.destinationService = destinationService16 req.destinationPath = path...
env.js
Source:env.js
...14 .read(path.resolve(path.join(__dirname, '..', '..', 'configs/', filename+'.json')));15};16module.exports = {17 readConfigs: readConfigs,18 current: readConfigs(env)...
index.js
Source:index.js
1'use strict';2let fs = require('../helpers/fs');3let ReadConfigs = require('../helpers/readConfigs');4class Config {5 constructor(env) {6 this.env = env;7 this.configs = fs.requireRecursivly(__dirname);8 }9 get(key) {10 if (key == 'all') return this.configs;11 try {12 return this.configs[key];13 }14 catch(e) {15 console.error(e);16 return e;17 }18 }19}...
readConfigs.js
Source:readConfigs.js
1'use strict';2let readDirFiles = require('read-dir-files');3let winston = require('winston');4class ReadConfigs {5 constructor(path) {6 this.path = path;7 this.read = readDirFiles.read;8 }9 readDir() {10 this.read(this.path, (err, files) => {11 if (err) return console.dir(err);12 return files;13 });14 }15}...
readConfigs.test.js
Source:readConfigs.test.js
1// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.2import {readConfigs} from '../index';3test('readConfigs() throws when called without project paths', () => {4 expect(() => {5 readConfigs(null /* argv */, [] /* projectPaths */);6 }).toThrowError('jest: No configuration found for any project.');...
configs.js
Source:configs.js
...4function readConfigs (filename) {5 return eson()6 .read(path.join(__dirname, '/configs/', filename+'.json'));7}8module.exports = readConfigs(env);...
LambdaTest’s Jest Testing Tutorial covers step-by-step guides around Jest with code examples to help you be proficient with the Jest framework. The Jest tutorial has chapters to help you learn right from the basics of Jest framework to code-based tutorials around testing react apps with Jest, perform snapshot testing, import ES modules and more.
|<p>it('check_object_of_Car', () => {</p><p>
expect(newCar()).toBeInstanceOf(Car);</p><p>
});</p>|
| :- |
Get 100 minutes of automation test minutes FREE!!