Best JavaScript code snippet using jest-extended
compareSmiles.js
Source:compareSmiles.js
1"use strict";2define(['common', 'htmlHelper', 'copyOptionParametersDataAccess', 'compareSmilesFactory'], function (common, htmlHelper, dataAccess, factory) {3 var earlier = $('#earlier'),4 diff = $('#diff'),5 later = $('#later'),6 earlierTab = $('#earlierTab'),7 diffTab = $('#diffTab'),8 laterTab = $('#laterTab'),9 compareSmilesWorkspace = $("#compareSmiles"),10 archiveSetsTraderSelect = $("#archiveSetsTraderSelect"),11 archiveSetsTraderProductsSelect = $("#archiveSetsTraderProductsSelect"),12 compareSmilesError = $("#compareSmilesError"),13 archiveSetsContainer = $("#archiveSets"),14 tabsContainer = $("#tabs"),15 getTrader = function () {16 return archiveSetsTraderSelect.val();17 },18 getProduct = function () {19 return archiveSetsTraderProductsSelect.val();20 },21 clearComparisonTables = function () {22 tabsContainer.fadeOut();23 earlier.fadeOut();24 earlier.empty();25 diff.fadeOut();26 diff.empty();27 later.fadeOut();28 later.empty();29 },30 displayArchiveSets = function (response) {31 var table, createArchiveSetsTable;32 if (!response.Success) {33 if (response.Message === common.sessionTimedOutMessage) {34 return;35 }36 compareSmilesError.text(response.Message);37 return;38 }39 createArchiveSetsTable = function () {40 archiveSetsContainer.empty();41 table = factory.createArchiveSetsTable(response.Payload);42 archiveSetsContainer.append(table);43 archiveSetsContainer.fadeIn('slow');44 };45 archiveSetsContainer.fadeOut('slow', createArchiveSetsTable);46 },47 getParametersArchiveSetsForTraderByProduct = function () {48 compareSmilesError.text('');49 clearComparisonTables();50 var trader = getTrader();51 var product = getProduct();52 if (!trader || !product) {53 archiveSetsContainer.empty();54 return;55 }56 dataAccess.getParametersArchiveSetsForTraderByProduct(trader, product, displayArchiveSets);57 },58 fillTradersSelect = function (response) {59 if (!response.Success) {60 if (response.Message === common.sessionTimedOutMessage) {61 return;62 }63 compareSmilesError.text(response.Message);64 return;65 }66 htmlHelper.fillSelectFromList(archiveSetsTraderSelect, "Choose Trader", response.Payload, "< select >");67 },68 initializeTradersSelect = function () {69 compareSmilesError.text('');70 clearComparisonTables();71 archiveSetsContainer.empty();72 tabsContainer.hide();73 compareSmilesWorkspace.fadeIn('slow');74 htmlHelper.initializeSelect(archiveSetsTraderProductsSelect, "Choose Product", "< select >");75 dataAccess.getTradersHavingArchiveSetsAsOf(fillTradersSelect);76 },77 fillProductsSelect = function (response) {78 if (!response.Success) {79 if (response.Message === common.sessionTimedOutMessage) {80 return;81 }82 compareSmilesError.text(response.Message);83 return;84 }85 htmlHelper.fillSelectFromList(archiveSetsTraderProductsSelect, "Choose Product", response.Payload, "< select >");86 },87 getProductsForTrader = function () {88 compareSmilesError.text('');89 clearComparisonTables();90 archiveSetsContainer.empty();91 tabsContainer.hide();92 var trader = getTrader();93 if (!trader) {94 htmlHelper.initializeSelect(archiveSetsTraderProductsSelect, "Choose Product", "< select >");95 return;96 }97 dataAccess.getProductsForTradersHavingArchiveSetsAsOf(trader, fillProductsSelect);98 },99 getSelectedArchiveSetsInfos = function () {100 var cells, infos;101 cells = $('#archiveSetsTable td');102 infos = [];103 cells.each(function () {104 var cell = $(this);105 if (cell.hasClass("selectedArchiveCell")) {106 infos.push({ id: cell.attr("id"), text: cell.text() });107 }108 });109 return infos;110 },111 getSelectedArchiveDates = function () {112 var infos = getSelectedArchiveSetsInfos();113 if (!infos || infos.length !== 2) {114 return { earlierDate: '', laterDate: '' };115 }116 if (Number(infos[0].id) < Number(infos[1].id)) {117 return { earlierDate: infos[0].text, laterDate: infos[1].text };118 }119 return { earlierDate: infos[1].text, laterDate: infos[0].text };120 },121 selectEarlierTab = function () {122 diff.fadeOut();123 later.fadeOut();124 earlierTab.css({125 "background-color": "rgb(233, 231, 231)",126 "color": "black",127 "font-weight": "bold"128 });129 diffTab.css({130 "background-color": "transparent",131 "color": "dimgray",132 "font-weight": "normal"133 });134 laterTab.css({135 "background-color": "transparent",136 "color": "dimgray",137 "font-weight": "normal"138 });139 earlier.fadeIn('slow');140 },141 selectDiffTab = function () {142 earlier.fadeOut();143 later.fadeOut();144 diffTab.css({145 "background-color": "rgb(233, 231, 231)",146 "color": "black",147 "font-weight": "bold"148 });149 earlierTab.css({150 "background-color": "transparent",151 "color": "dimgray",152 "font-weight": "normal"153 });154 laterTab.css({155 "background-color": "transparent",156 "color": "dimgray",157 "font-weight": "normal"158 });159 diff.fadeIn('slow');160 },161 selectLaterTab = function () {162 earlier.fadeOut();163 diff.fadeOut();164 laterTab.css({165 "background-color": "rgb(233, 231, 231)",166 "color": "black",167 "font-weight": "bold"168 });169 earlierTab.css({170 "background-color": "transparent",171 "color": "dimgray",172 "font-weight": "normal"173 });174 diffTab.css({175 "background-color": "transparent",176 "color": "dimgray",177 "font-weight": "normal"178 });179 later.fadeIn('slow');180 },181 getNonZeroCellIndices = function (diffTable) {182 var rowColArray = [];183 var diffTableRows = diffTable.find('tr');184 for (var i = 1; i < diffTableRows.length; i++) {185 var cells = $(diffTableRows[i]).find('td');186 for (var j = 2; j < cells.length; j++) {187 var cell = $(cells[j]);188 var cellValue = Number(cell.text());189 if (cellValue < 0 || cellValue > 0) {190 rowColArray.push({ row: i, col: j });191 }192 }193 }194 return rowColArray;195 },196 highlightDeltas = function (rowColArray, tableRows) {197 rowColArray.forEach(function (rowCol) {198 var row = $(tableRows[rowCol.row]);199 var cells = row.find('td');200 var cell = $(cells[rowCol.col]);201 cell.addClass('hasNonZeroDelta');202 });203 },204 applyColor = function (earlierTable, diffTable, laterTable) {205 var rowColArray = getNonZeroCellIndices(diffTable);206 highlightDeltas(rowColArray, earlierTable.find('tr'));207 highlightDeltas(rowColArray, diffTable.find('tr'));208 highlightDeltas(rowColArray, laterTable.find('tr'));209 },210 archiveSetsSelected = function (earlierArchiveID, laterArchiveID) {211 var product, callback, selectedArchiveDates, earlierTable, diffTable, laterTable;212 callback = function (response) {213 if (!response.Success) {214 if (response.Message === common.sessionTimedOutMessage) {215 return;216 }217 compareSmilesError.text(response.Message);218 return;219 }220 clearComparisonTables();221 earlierTable = factory.createOptionContractParametersTable(response.Payload.Earlier, "earlierTable");222 diffTable = factory.createOptionContractParametersTable(response.Payload.Diff, "diffTable");223 laterTable = factory.createOptionContractParametersTable(response.Payload.Later, "laterTable");224 applyColor(earlierTable, diffTable, laterTable);225 earlier.append(earlierTable);226 diff.append(diffTable);227 later.append(laterTable);228 selectedArchiveDates = getSelectedArchiveDates();229 tabsContainer.fadeIn("slow");230 earlierTab.text(selectedArchiveDates.earlierDate);231 laterTab.text(selectedArchiveDates.laterDate);232 selectEarlierTab();233 };234 product = getProduct();235 if (!product) {236 return;237 }238 dataAccess.getOptionParametersArchiveSetComparisons(earlierArchiveID, laterArchiveID, product, callback);239 },240 assignEventHandlers = function () {241 earlierTab.click(selectEarlierTab);242 diffTab.click(selectDiffTab);243 laterTab.click(selectLaterTab);244 archiveSetsTraderSelect.change(getProductsForTrader);245 archiveSetsTraderProductsSelect.change(getParametersArchiveSetsForTraderByProduct);246 };247 return {248 initialize: function () {249 assignEventHandlers();250 factory.addListener("archiveSetsSelected", archiveSetsSelected);251 initializeTradersSelect();252 }253 };...
UserService.js
Source:UserService.js
1const { scrape } = require("./scraper");2const Search = require("./Search");3async function startASubscription(user, searchWord) {4 let response = {};5 if (searchWord.length < 3) {6 const response = {7 typeOfAlert: "alert alert-danger alert-dismissible fade show",8 text: "Vänligen skriv in ett ord med minst tre bokstäver",9 earlierSearches: [],10 };11 return response;12 }13 const earlierSearches = await getEarlierSearchWords(user);14 //hämtar användarens olika sökningar inkl resultat15 const searchArray = user.searches;16 let searchDoneBefore = false;17 //Ittererar över användarens tidigare sökningar och ser om samma sökning gjorts18 searchArray.forEach((ad) => {19 if (ad.searchWord === searchWord) {20 searchDoneBefore = true;21 }22 });23 //om searchDoneBefore är false skapas en ny sökning för given användare24 if (!searchDoneBefore) {25 //skrapar data26 const data = await scrape(27 "https://www.bortskankes.se/index.php?lan=&kat=&searchtxt=&page=0&showclosed=n",28 searchWord29 );30 response = {31 typeOfAlert: "alert alert-primary alert-dismissible fade show",32 text: "Prenumeration skapad!",33 earlierSearches: earlierSearches,34 };35 //lägger till den nya datan i arrayen som plockades ur från användaren36 searchArray.push({ searchWord: searchWord, adArray: data });37 //sparar användarens nya objekt38 user.searches = searchArray;39 await user.save();40 } else {41 response = {42 typeOfAlert: "alert alert-danger alert-dismissible fade show",43 text: "Du prenumererar redan på det valda sökordet",44 earlierSearches: earlierSearches,45 };46 }47 return response;48}49const removeSearch = async (user, searchWordToDelete) => {50 userSearches = user.searches;51 const updatedSearches = userSearches.filter(52 (obj) => obj.searchWord !== searchWordToDelete53 );54 //lista med enbart sökord för response55 const earlierSearches = [];56 updatedSearches.forEach((search) => {57 earlierSearches.push(search.searchWord);58 });59 user.searches = updatedSearches;60 //sparar användarens nya objekt61 await user.save();62 console.log("Sökordet borttaget!");63 const response = {64 typeOfAlert: "alert alert-danger alert-dismissible fade show",65 text: "Sökordet borttaget!",66 earlierSearches: [],67 earlierSearches: earlierSearches,68 };69 return response;70};71const getEarlierSearchWords = async (user) => {72 const earlierSearches = [];73 const userSearchObj = user.searches;74 userSearchObj.forEach((search) => {75 earlierSearches.push(search.searchWord);76 });77 const response = {78 typeOfAlert: "alert alert-danger alert-dismissible fade show",79 text: "Inga uppsatta prenumerationer",80 earlierSearches: earlierSearches,81 };82 return response;83};84module.exports = {85 startASubscription,86 removeSearch,87 getEarlierSearchWords,...
date-helpers.spec.ts
Source:date-helpers.spec.ts
1import { dateMax, dateMin, dateOffset, sortByEndDate } from './date-helpers';2jest.mock('fs');3describe('date-helpers', () => {4 describe('dateOffset', () => {5 it('should return the current date plus the number of seconds', () => {6 const now = Date.now();7 const allowance = 500; //+-0.5s allowance for test variation.8 expect(Math.abs(dateOffset(0).getTime() - now)).toBeLessThan(allowance);9 expect(Math.abs(dateOffset(90).getTime() - (now + 90 * 1000))).toBeLessThan(allowance);10 expect(Math.abs(dateOffset(23473).getTime() - (now + 23473 * 1000))).toBeLessThan(allowance);11 expect(Math.abs(dateOffset(-123).getTime() - (now - 123 * 1000))).toBeLessThan(allowance);12 });13 });14 describe('dateMax', () => {15 it('should return the later of two dates', () => {16 const earlier = new Date('2022-01-07T15:31:47.337Z');17 const later = new Date('2022-01-07T15:32:47.337Z');18 expect(dateMax(earlier, later)).toEqual(later);19 expect(dateMax(later, earlier)).toEqual(later);20 expect(dateMax(later, later)).toEqual(later);21 });22 });23 describe('dateMin', () => {24 it('should return the earlier of two dates', () => {25 const earlier = new Date('2022-01-07T15:31:47.337Z');26 const later = new Date('2022-01-07T15:32:47.337Z');27 expect(dateMin(earlier, later)).toEqual(earlier);28 expect(dateMin(later, earlier)).toEqual(earlier);29 expect(dateMin(earlier, earlier)).toEqual(earlier);30 });31 });32 describe('sortByEndDate', () => {33 it('should return the dates ordered by end date in ascending order', () => {34 const start = new Date('2022-01-07T15:30:47.337Z').toISOString();35 const earlier = { start, end: new Date('2022-01-07T15:31:47.337Z').toISOString() };36 const later = { start, end: new Date('2022-01-07T15:32:47.337Z').toISOString() };37 const latest = { start, end: new Date('2022-01-07T15:33:47.337Z').toISOString() };38 expect(sortByEndDate([earlier])).toEqual([earlier]);39 expect(sortByEndDate([later, earlier])).toEqual([earlier, later]);40 expect(sortByEndDate([earlier, later])).toEqual([earlier, later]);41 expect(sortByEndDate([earlier, later, latest])).toEqual([earlier, later, latest]);42 expect(sortByEndDate([later, latest, earlier])).toEqual([earlier, later, latest]);43 expect(sortByEndDate([latest, later, earlier])).toEqual([earlier, later, latest]);44 expect(sortByEndDate([earlier, latest, later])).toEqual([earlier, later, latest]);45 expect(sortByEndDate([later, earlier, latest])).toEqual([earlier, later, latest]);46 expect(sortByEndDate([latest, earlier, later])).toEqual([earlier, later, latest]);47 });48 it('should return an empty array when given one', () => {49 expect(sortByEndDate([])).toEqual([]);50 });51 });...
Using AI Code Generation
1const jestExtended = require('jest-extended');2expect.extend(jestExtended);3const { toBeDeepCloseTo } = require('jest-matcher-deep-close-to');4expect.extend({ toBeDeepCloseTo });5const { toBeDeepCloseTo } = require('jest-matcher-deep-close-to');6expect.extend({ toBeDeepCloseTo });7const { toBeDeepCloseTo } = require('jest-matcher-deep-close-to');8expect.extend({ toBeDeepCloseTo });9const { toBeDeepCloseTo } = require('jest-matcher-deep-close-to');10expect.extend({ toBeDeepCloseTo });11const { toBeDeepCloseTo } = require('jest-matcher-deep-close-to');12expect.extend({ toBeDeepCloseTo });13const { toBeDeepCloseTo } = require('jest-matcher-deep-close-to');14expect.extend({ toBeDeepCloseTo });15const { toBeDeepCloseTo } = require('jest-matcher-deep-close-to');16expect.extend({ toBeDeepCloseTo });17const { toBeDeepCloseTo } = require('jest-matcher-deep-close-to');18expect.extend({ toBeDeepCloseTo });19const { toBeDeepCloseTo } = require('jest-matcher-deep-close-to');20expect.extend({ toBeDeepCloseTo });21const { toBeDeepCloseTo } = require('jest-matcher-deep-close-to');22expect.extend({ toBeDeepCloseTo });23const { toBeDeepCloseTo } = require('jest-matcher-deep-close-to');24expect.extend({ toBeDeepCloseTo });25const { toBeDeepCloseTo } = require('jest-matcher-deep-close-to');26expect.extend({ toBeDeepCloseTo });
Using AI Code Generation
1import 'jest-extended';2import 'jest-extended/dist/extend-expect';3import 'jest-extended/dist/matchers';4import 'jest-extended/dist/matchers/toBeAfter';5import 'jest-extended/dist/matchers/toBeAfter/toBeAfter';6import 'jest-extended/dist/matchers/toBeAfter/toBeAfterOrEqual';7import 'jest-extended/dist/matchers/toBeAfter/toBeBefore';8import 'jest-extended/dist/matchers/toBeAfter/toBeBeforeOrEqual';9import 'jest-extended/dist/matchers/toBeAfter/toBeBetween';10import 'jest-extended/dist/matchers/toBeAfter/toBeDate';11import 'jest-extended/dist/matchers/toBeAfter/toBeDateString';12import 'jest-extended/dist/matchers/toBeAfter/toBeIso8601';13import 'jest-extended/dist/matchers/toBeAfter/toBeSameHour';14import 'jest-extended/dist/matchers/toBeAfter/toBeSameMinute';15import 'jest-extended/dist/matchers/toBeAfter/toBeSameMonth';16import 'jest-extended/dist/matchers/toBeAfter/toBeSameSecond';17import 'jest-extended/dist/matchers/toBeAfter/toBeSameYear';18import 'jest-extended/dist/matchers/toBeAfter/toBeToday';19import 'jest-extended/dist/match
Using AI Code Generation
1expect.extend({ toBeOneOf: toBeOneOf });2expect(1).toBeOneOf([1, 2, 3]);3expect(1).toBeOneOf([1, 2, 3]);4import { toBeOneOf } from 'jest-extended';5expect(1).toBeOneOf([1, 2, 3]);6import * as matchers from 'jest-extended';7expect.extend(matchers);8expect(1).toBeOneOf([1, 2, 3]);9import { toBeOneOf } from 'jest-extended';10expect.extend({ toBeOneOf });11expect(1).toBeOneOf([1, 2, 3]);12import { toBeOneOf } from 'jest-extended';13expect.extend({ toBeOneOf: toBeOneOf });14expect(1).toBeOneOf([1, 2, 3]);15import { toBeOneOf } from 'jest-extended';16expect.extend({ toBeOneOf: toBeOneOf });17expect(1).toBeOneOf([1, 2, 3]);18import { toBeOneOf } from 'jest-extended';19expect.extend({ toBeOneOf: toBeOneOf });20expect(1).toBeOneOf([1, 2, 3]);21import { toBeOneOf } from 'jest-extended';22expect.extend({ toBeOneOf: toBeOneOf });23expect(1).toBeOneOf([1, 2, 3]);24import { toBeOneOf } from 'jest-extended';25expect.extend({ toBeOneOf: toBeOneOf });26expect(1).toBeOneOf([1, 2, 3]);27import { toBeOneOf } from 'jest-extended';28expect.extend({ toBeOneOf: toBeOneOf });29expect(1).toBeOneOf([1, 2, 3]);30import { toBeOneOf } from 'jest-extended';31expect.extend({ toBeOneOf: toBeOneOf });32expect(1).toBeOne
Using AI Code Generation
1const { toBeWithinRange } = require('jest-matcher-utils');2expect.extend({ toBeWithinRange });3expect(5).toBeWithinRange(1, 10);4const { toBeWithinRange } = require('jest-extended');5expect.extend({ toBeWithinRange });6expect(5).toBeWithinRange(1, 10);7const { toBeWithinRange } = require('jest-matcher-utils');8expect.extend({ toBeWithinRange });9expect(5).toBeWithinRange(1, 10);10const { toBeWithinRange } = require('jest-extended');11expect.extend({ toBeWithinRange });12expect(5).toBeWithinRange(1, 10);13const { toBeWithinRange } = require('jest-matcher-utils');14expect.extend({ toBeWithinRange });15expect(5).toBeWithinRange(1, 10);16const { toBeWithinRange } = require('jest-extended');17expect.extend({ toBeWithinRange });18expect(5).toBeWithinRange(1, 10);19const { toBeWithinRange } = require('jest-matcher-utils');20expect.extend({ toBeWithinRange });21expect(5).toBeWithinRange(1, 10);22const { toBeWithinRange } = require('jest-extended');23expect.extend({ toBeWithinRange });24expect(5).toBeWithinRange(1, 10);25const { toBeWithinRange } = require('jest-matcher-utils');26expect.extend({ toBeWithinRange });27expect(5).toBeWithinRange(1, 10);28const { toBeWithinRange } = require('jest-extended');29expect.extend({ toBeWithinRange });30expect(5).toBeWithinRange(1, 10);
Using AI Code Generation
1import 'jest-extended/dist/extend-expect';2import 'jest-extended';3import 'jest-extended/dist/extend-expect';4import 'jest-extended/dist/extend-expect';5import 'jest-extended';6import 'jest-extended';7import 'jest-extended/dist/extend-expect';8import 'jest-extended/dist/extend-expect';9import 'jest-extended';10import 'jest-extended';11import 'jest-extended/dist/extend-expect';12import 'jest-extended/dist/extend-expect';13import 'jest-extended';14import 'jest-extended';15import 'jest-extended/dist/extend-expect';16import 'jest-extended/dist/extend-expect';17import 'jest-extended';18import 'jest-extended';19import 'jest-extended/dist/extend-expect';
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!!