Best JavaScript code snippet using jest
request.js
Source:request.js
...224 if(!validateUrlPath(path)) {225 throw new Error('Invalid resource');226 }227 for(let param of fetchParamNames) {228 if(param === 'body' && hasDefinedKey(options, param)) {229 fetchConfig[param] = JSON.stringify(options[param]);230 } else {231 fetchConfig[param] = options[param]; 232 }233 }234 // build pre-upload (authorisation) request body based on the file provided235 if(hasDefinedKey(options, 'body') && hasDefinedKey(options, 'file')) {236 throw new Error('Cannot use both "file" and "body" in a single request.');237 }238 // process the request for file upload authorisation request239 if(hasDefinedKey(options, 'file') && hasDefinedKey(options, 'fileName')) {240 let fileName = options.fileName;241 let md5sum = SparkMD5.ArrayBuffer.hash(options.file);242 let filesize = options.file.byteLength;243 let mtime = options.mtime || Date.now();244 fetchConfig['body'] = `md5=${md5sum}&filename=${fileName}&filesize=${filesize}&mtime=${mtime}`;245 }246 if(options.uploadRegisterOnly === true) {247 const { fileName, fileSize, md5sum, mtime } = options;248 fetchConfig['body'] = `md5=${md5sum}&filename=${fileName}&filesize=${fileSize}&mtime=${mtime}`; 249 }250 // checking against access-control-allow-methods seems to be case sensitive251 fetchConfig.method = fetchConfig.method.toUpperCase();252 fetchConfig.headers = headers;253 options.retryCount = 0;254 if(options.pretend) {255 const response = new PretendResponse({ url, fetchConfig }, options);256 return { response, ...config, source: 'request' };257 }258 let rawResponse = await fetch(url, fetchConfig);259 if(isTransientFailure(rawResponse) && options.retry > 0) {260 let retriesCounter = options.retry;261 let nextRetryDelay = typeof(options.retryDelay) === 'number' ? options.retryDelay : 1;262 while(retriesCounter > 0) {263 await sleep(nextRetryDelay);264 options.retryCount++;265 rawResponse = await fetch(url, fetchConfig);266 if(!isTransientFailure(rawResponse)) {267 break;268 }269 if(typeof(options.retryDelay) !== 'number') {270 nextRetryDelay *= 2;271 }272 retriesCounter--;273 }274 }275 if((hasDefinedKey(options, 'file') && hasDefinedKey(options, 'fileName')) || options.uploadRegisterOnly === true) {276 if(rawResponse.ok) {277 let authData = await rawResponse.json();278 if('exists' in authData && authData.exists) {279 response = new FileUploadResponse(authData, options, rawResponse);280 } else {281 if(options.uploadRegisterOnly === true) {282 throw new ErrorResponse(283 'API did not recognize provided file meta.',284 'Attempted to register existing file, but API did not recognize provided file meta.',285 rawResponse, options286 );287 }288 let prefix = new Uint8ClampedArray(authData.prefix.split('').map(e => e.charCodeAt(0)));289 let suffix = new Uint8ClampedArray(authData.suffix.split('').map(e => e.charCodeAt(0)));...
jasmineUtils.js
Source:jasmineUtils.js
...155 symbol => Object.getOwnPropertyDescriptor(obj, symbol).enumerable156 )157 );158}159function hasDefinedKey(obj, key) {160 return hasKey(obj, key) && obj[key] !== undefined;161}162function hasKey(obj, key) {163 return Object.prototype.hasOwnProperty.call(obj, key);164}165function isA(typeName, value) {166 return Object.prototype.toString.apply(value) === '[object ' + typeName + ']';167}168function isDomNode(obj) {169 return (170 obj !== null &&171 typeof obj === 'object' &&172 typeof obj.nodeType === 'number' &&173 typeof obj.nodeName === 'string' &&...
ThermodynamicsPlot.js
Source:ThermodynamicsPlot.js
...36 xkey,ykey,xlabel,ylabel,37 dataPointsGenerator38}) =>{39 for (var i=0; i<steps.length; i++){40 if (hasDefinedKey(steps[i],'staticEntropy')){41 steps[i].entropy = steps[i].staticEntropy42 }43 }44 const [canvasDraggable,setCanvasDraggable] = React.useState(false)45 const [mouseLoc, setMouseLoc] = React.useState({x: null, y: null})46 const [minX, setMinX] = React.useState(0)47 const [maxX, setMaxX] = React.useState(0.1)48 const [maxY, setMaxY] = React.useState(500000)49 const posSetter = (index) => {50 return (x,y)=>{51 var update = {}52 if (xkey === 'entropy'){53 if (hasDefinedKey(steps[index],'staticEntropy')){54 update.staticEntropy = x55 }56 }57 update[xkey] = x58 update[ykey] = y59 var dragPointGroup = {index: index, xkey: xkey}60 steps_updateProperties(index,update,dragPointGroup)61 }62 }63 64 var dataLines = dataPointsGenerator(steps,system)65 var dataPoints = steps.map((step,index)=>{66 return {67 x:step[xkey],...
Thermodynamics.js
Source:Thermodynamics.js
...86 return [point1, point2]87}88const solveEntropyChange = (stepConstraints,system) => {89 const step = Object.assign({},stepConstraints)90 const hasDeltaS = hasDefinedKey(step,'entropyChange')91 const hasP1 = hasDefinedKey(step,'pressure_1')92 const hasV1 = hasDefinedKey(step,'volume_1')93 const hasT1 = hasDefinedKey(step,'temperature_1')94 const hasP2 = hasDefinedKey(step,'pressure_2')95 const hasV2 = hasDefinedKey(step,'volume_2')96 const hasT2 = hasDefinedKey(step,'temperature_2')97 const numberOfConstraints = hasDeltaS + hasP1 + hasV1 + hasT1 + hasP2 + hasV2 + hasT298 if (numberOfConstraints < 4){99 throw new InsufficientConstraintsError('Must specify atleast 4 constraints')100 }101 var [point1, point2] = _extractPointsFromStep(step)102 var A, B, X, hasA, hasB103 hasA = hasB = false104 105 point1 = _computeEntropyCalculationComponent(point1,system)106 if ('entropyCalculationComponent' in point1){107 B = point1.entropyCalculationComponent108 hasB = true109 }110 point2 = _computeEntropyCalculationComponent(point2,system)...
StepListItem.js
Source:StepListItem.js
...24 )25}26const StepListItem = ({step,index,setStep,deleteStep,deleteDisabled}) => {27 var entropyReadOnly = true28 if (hasDefinedKey(step,'staticEntropy')){29 step.entropy = step.staticEntropy30 entropyReadOnly = false31 }32 if (!hasDefinedKey(step,'entropy')){33 step.entropy = ''34 }35 if (!hasDefinedKey(step,'entropyChange')){36 step.entropyChange = ''37 }38 const stepUpdateFunction = (param) =>{39 setStep(index,param)40 }41 var deleteButtonParams = {42 size: 'small',43 icon: 'trash',44 onClick: ()=>{deleteStep(index)},45 className: 'no-drag',46 }47 if (deleteDisabled){48 deleteButtonParams.disabled = true49 }else{...
generatePlotLineData.js
Source:generatePlotLineData.js
...18const _getXYPV = (step) => {19 return {x: step.volume, y: step.pressure}20}21const getEntropy = (step) => {22 if (hasDefinedKey(step,'staticEntropy')){23 return step.staticEntropy24 }25 return step.entropy26}27const _getXYST = (step) => {28 return {x: getEntropy(step), y: step.temperature}29}30function _getLinePointsST(steps,index,indexNext,system) {31 if (steps[index].type === 'isothermal' || steps[index].type === 'isentropic'){32 return[33 _getXYST(steps[index]), _getXYST(steps[indexNext])34 ]35 }36 const entropies = linspace(getEntropy(steps[index]),getEntropy(steps[indexNext]),100)...
index.js
Source:index.js
...7import { setPreset } from './actions/setPreset.js';8import undoable, { ActionCreators as UndoActionCreators } from 'redux-undo';9import { hasDefinedKey } from './Utils.js';10const store = createStore(undoable(thermodynamicSystemReducer,{groupBy: (action,currentState,previousHistory)=>{11 if (hasDefinedKey(action,'groupBy')){12 if (action.groupBy === null){return null}13 return `${action.groupBy.index}-${action.groupBy.xkey}`14 }15 return null16}}))17store.dispatch(setPreset('carnotCycle'))18store.dispatch(UndoActionCreators.clearHistory())19ReactDOM.render(20 <Provider store={store}>21 <App />22 </Provider>23 ,24 document.getElementById('root')25);
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!!