Best JavaScript code snippet using mountebank
cohortEdit.js
Source:cohortEdit.js
1import { useState } from 'react';2import CloseIcon from '@mui/icons-material/Close';3import DateTimePicker from '@mui/lab/DateTimePicker';4import AdapterDateFns from '@mui/lab/AdapterLuxon';5import LocalizationProvider from '@mui/lab/LocalizationProvider';6import { Box, IconButton, AppBar, Typography, Toolbar, TextField, Stack, Button } from '@mui/material';7import { createCohort } from '../../lib/apiFacade';8import { useAppContext } from '../../lib/appState';9export default function CohortEdit({ onClose, onCohortCreated }) {10 const [state] = useAppContext();11 const emptyNewCohort = {12 autoSynchDates: true, daysInterval: 2, daysBetweenConfirmationAndEp1: 4,13 datetimeEp1: null, datetimeEp2: null, datetimeEp3: null, confirmationDeadline: null,14 warnings: [], validationErrors: {}, pristine: true15 };16 const [newCohort, setNewCohort] = useState(emptyNewCohort);17 const validateAndSaveNewCohort = async () => {18 const validationErrors = getValidationErrorsNewCohort();19 if (Object.keys(validationErrors).length === 0) {20 try {21 const result = await createCohort({22 facilitatorAddress: state.walletAddress,23 datetimeEp1: newCohort.datetimeEp1.toJSDate(),24 datetimeEp2: newCohort.datetimeEp2.toJSDate(),25 datetimeEp3: newCohort.datetimeEp3.toJSDate(),26 confirmationDeadline: newCohort.confirmationDeadline.toJSDate()27 });28 mergeNewCohortState(emptyNewCohort);29 onCohortCreated({30 _id: result._id,31 facilitatorAddress: newCohort.walletAddress,32 datetimeEp1: newCohort.datetimeEp1,33 datetimeEp2: newCohort.datetimeEp2,34 datetimeEp3: newCohort.datetimeEp3,35 confirmationDeadline: newCohort.confirmationDeadline36 });37 }38 catch (e) {39 state.setError(state, 'Error while saving. Please retry, and contact us if the problem persists.');40 }41 } else {42 mergeNewCohortState({ pristine: false, validationErrors });43 }44 };45 const mergeNewCohortState = (newState, reducer) => {46 if (reducer) {47 setNewCohort({ ...newCohort, ...(reducer(newState)) });48 } else {49 setNewCohort({ ...newCohort, ...newState });50 }51 };52 const recalcDatesFromDateEp1 = ep1Date => {53 if (ep1Date) {54 if (!ep1Date.invalid) {55 return {56 datetimeEp1: ep1Date,57 datetimeEp2: ep1Date.plus({ days: newCohort.daysInterval }),58 datetimeEp3: ep1Date.plus({ days: 2 * newCohort.daysInterval }),59 confirmationDeadline: ep1Date.plus({ days: -newCohort.daysBetweenConfirmationAndEp1 })60 };61 }62 return { datetimeEp1: ep1Date };63 }64 return { datetimeEp1: null };65 };66 const validateNewCohortIfNotPristine = newCohortState => {67 if (!newCohort.pristine) {68 const validationErrors = getValidationErrorsNewCohort();69 return { ...newCohortState, ...{ validationErrors } };70 }71 return newCohortState;72 };73 const getValidationErrorsNewCohort = () => {74 const validationErrors = {};75 if (!newCohort.datetimeEp1) {76 validationErrors.datetimeEp1 = 'Required';77 } else {78 if (newCohort.datetimeEp1.invalid) {79 validationErrors.datetimeEp1 = 'Invalid date';80 }81 }82 if (!newCohort.datetimeEp2) {83 validationErrors.datetimeEp2 = 'Required';84 } else {85 if (newCohort.datetimeEp2.invalid) {86 validationErrors.datetimeEp2 = 'Invalid date';87 }88 }89 if (!newCohort.datetimeEp3) {90 validationErrors.datetimeEp3 = 'Required';91 } else {92 if (newCohort.datetimeEp3.invalid) {93 validationErrors.datetimeEp3 = 'Invalid date';94 }95 }96 if (!newCohort.confirmationDeadline) {97 validationErrors.confirmationDeadline = 'Required';98 } else {99 if (newCohort.confirmationDeadline.invalid) {100 validationErrors.confirmationDeadline = 'Invalid date';101 }102 }103 if (!validationErrors.datetimeEp1 && !validationErrors.confirmationDeadline && newCohort.datetimeEp1.toJSDate() < newCohort.confirmationDeadline.toJSDate()) {104 validationErrors.datetimeEp1 = 'Can\'t be before confirmation deadline';105 }106 if (!validationErrors.datetimeEp2 && !validationErrors.datetimeEp1 && newCohort.datetimeEp2.toJSDate() < newCohort.datetimeEp1.toJSDate()) {107 validationErrors.datetimeEp2 = 'Can\'t be before episode 1';108 }109 if (!validationErrors.datetimeEp3 && !validationErrors.datetimeEp2 && newCohort.datetimeEp3.toJSDate() < newCohort.datetimeEp2.toJSDate()) {110 validationErrors.datetimeEp3 = 'Can\'t be before episode 2';111 }112 if (!validationErrors.confirmationDeadline && newCohort.confirmationDeadline.toJSDate() < new Date()) {113 validationErrors.confirmationDeadline = 'Can\'t be in the past';114 }115 return validationErrors;116 };117 return (<LocalizationProvider dateAdapter={AdapterDateFns}>118 <Box component="form"119 onSubmit={e => {120 e.preventDefault();121 validateAndSaveNewCohort();122 }}>123 <AppBar sx={{ position: 'relative' }}>124 <Toolbar>125 <IconButton126 edge="start"127 color="inherit"128 onClick={() => onClose()}129 aria-label="close">130 <CloseIcon />131 </IconButton>132 <Typography variant="h4" sx={{ flex: 1, justifyContent: 'center' }}>Create cohort</Typography>133 <Button type="submit" color="secondary" variant="contained">Save</Button>134 </Toolbar>135 </AppBar>136 <Stack id="newCohortForm" padding={1} spacing={1} alignItems="flex-start">137 <DateTimePicker renderInput={(props) => <TextField {...props} error={!!newCohort.validationErrors.datetimeEp1} helperText={newCohort.validationErrors.datetimeEp1} />}138 label="Episode 1"139 value={newCohort.datetimeEp1}140 onChange={(newValue) => {141 if (newCohort.autoSynchDates) {142 mergeNewCohortState(recalcDatesFromDateEp1(newValue), validateNewCohortIfNotPristine);143 } else {144 mergeNewCohortState({ datetimeEp1: newValue }, validateNewCohortIfNotPristine);145 }146 }} />147 <DateTimePicker renderInput={(props) => <TextField {...props} error={!!newCohort.validationErrors.datetimeEp2} helperText={newCohort.validationErrors.datetimeEp2} />}148 label="Episode 2"149 required150 value={newCohort.datetimeEp2}151 onChange={(newValue) => {152 mergeNewCohortState({ datetimeEp2: newValue, autoSynchDates: false }, validateNewCohortIfNotPristine);153 }} />154 <DateTimePicker renderInput={(props) => <TextField {...props} error={!!newCohort.validationErrors.datetimeEp3} helperText={newCohort.validationErrors.datetimeEp3} />}155 label="Episode 3"156 required157 value={newCohort.datetimeEp3}158 onChange={(newValue) => {159 mergeNewCohortState({ datetimeEp3: newValue, autoSynchDates: false }, validateNewCohortIfNotPristine);160 }} />161 <DateTimePicker renderInput={(props) => <TextField {...props} error={!!newCohort.validationErrors.confirmationDeadline} helperText={newCohort.validationErrors.confirmationDeadline} />}162 label="Confirmation deadline"163 required164 value={newCohort.confirmationDeadline}165 onChange={(newValue) => {166 mergeNewCohortState({ confirmationDeadline: newValue, autoSynchDates: false }, validateNewCohortIfNotPristine);167 }} />168 </Stack>169 </Box>170 </LocalizationProvider>);...
cross-field.directive.ts
Source:cross-field.directive.ts
1/* eslint-disable @angular-eslint/directive-selector */2import { AfterViewInit, Directive, DoCheck, ElementRef, forwardRef, Input } from '@angular/core';3import { AbstractControl, FormGroup, NgModel, NG_VALIDATORS, ValidationErrors, Validator, ValidatorFn } from '@angular/forms';4import { Comparator, convertValue } from './utils.class';5@Directive({6 selector: `[cross-field-validation]`,7 providers: [{ provide: NG_VALIDATORS, useExisting: CrossFieldValidator, multi: true }]8})9export class CrossFieldValidator implements Validator {10 private working = false;11 validate(control: FormGroup): ValidationErrors | null {12 if (this.working) return null;13 this.working = true;14 for (let field in control.controls) {15 control.controls[field].updateValueAndValidity();16 }17 this.working = false;18 return null;19 }20}21export function CrossInputValidation(bindNameControl: string | null | undefined, compara: Comparator<any>, property: string = 'crossInput'): ValidatorFn {22 return (control: AbstractControl): ValidationErrors | null => {23 control.valueChanges.subscribe()24 if (!control.value) { return null; }25 if (!bindNameControl)26 throw new Error('Falta el control de referencia.');27 let bindControl = control.root.get(bindNameControl);28 if (!bindControl)29 throw new Error('No encuentro el control de referencia.');30 let localValue = convertValue(control.value);31 let bindValue = convertValue(bindControl.value);32 let validationErrors: { [key: string]: any } = {}33 validationErrors[property] = { localValue: control.value, bindValue: bindControl.value, message: 'Unsurpassed comparison' }34 return compara(localValue, bindValue) ? null : validationErrors35 };36}37@Directive({38 selector: '[equalsTo][formControlName],[equalsTo][formControl],[equalsTo][ngModel]',39 providers: [{ provide: NG_VALIDATORS, useExisting: EqualsToValidator, multi: true }]40})41export class EqualsToValidator implements Validator {42 readonly propertyName = 'equalsTo'43 // eslint-disable-next-line @angular-eslint/no-input-rename44 @Input('equalsTo') bindControl: string | null | undefined;45 // eslint-disable-next-line @angular-eslint/no-input-rename46 @Input('equalsToMessage') message: string | null | undefined;47 validate(control: AbstractControl): ValidationErrors | null {48 let validationErrors = CrossInputValidation(this.bindControl, (a, b) => a === b, this.propertyName)(control)49 if (validationErrors)50 if (this.message)51 validationErrors[this.propertyName].message = this.message;52 else53 validationErrors[this.propertyName].message = `${validationErrors[this.propertyName].localValue} es distinto de ${validationErrors[this.propertyName].bindValue}`54 return validationErrors;55 }56}57@Directive({58 selector: '[distinctTo][formControlName],[distinctTo][formControl],[distinctTo][ngModel]',59 providers: [{ provide: NG_VALIDATORS, useExisting: DistinctToValidator, multi: true }]60})61export class DistinctToValidator implements Validator {62 readonly propertyName = 'distinctTo'63 // eslint-disable-next-line @angular-eslint/no-input-rename64 @Input('distinctTo') bindControl: string | null | undefined;65 // eslint-disable-next-line @angular-eslint/no-input-rename66 @Input('distinctToMessage') message: string | null | undefined;67 validate(control: AbstractControl): ValidationErrors | null {68 let validationErrors = CrossInputValidation(this.bindControl, (a, b) => a !== b, this.propertyName)(control)69 if (validationErrors)70 if (this.message)71 validationErrors[this.propertyName].message = this.message;72 else73 validationErrors[this.propertyName].message = `${validationErrors[this.propertyName].localValue} es igual a ${validationErrors[this.propertyName].bindValue}`74 return validationErrors;75 }76}77@Directive({78 selector: '[lessThan][formControlName],[lessThan][formControl],[lessThan][ngModel]',79 providers: [{ provide: NG_VALIDATORS, useExisting: LessThanValidator, multi: true }]80})81export class LessThanValidator implements Validator {82 readonly propertyName = 'lessThan'83 // eslint-disable-next-line @angular-eslint/no-input-rename84 @Input('lessThan') bindControl: string | null | undefined;85 // eslint-disable-next-line @angular-eslint/no-input-rename86 @Input('lessThanMessage') message: string | null | undefined;87 validate(control: AbstractControl): ValidationErrors | null {88 let validationErrors = CrossInputValidation(this.bindControl, (a, b) => a < b, this.propertyName)(control)89 if (validationErrors)90 if (this.message)91 validationErrors[this.propertyName].message = this.message;92 else93 validationErrors[this.propertyName].message = `${validationErrors[this.propertyName].localValue} debe ser menor que ${validationErrors[this.propertyName].bindValue}`94 return validationErrors;95 }96}97@Directive({98 selector: '[lessOrEqualsThan][formControlName],[lessOrEqualsThan][formControl],[lessOrEqualsThan][ngModel]',99 providers: [{ provide: NG_VALIDATORS, useExisting: LessOrEqualsThanValidator, multi: true }]100})101export class LessOrEqualsThanValidator implements Validator {102 readonly propertyName = 'lessOrEqualsThan'103 // eslint-disable-next-line @angular-eslint/no-input-rename104 @Input('lessOrEqualsThan') bindControl: string | null | undefined;105 // eslint-disable-next-line @angular-eslint/no-input-rename106 @Input('lessOrEqualsThanMessage') message: string | null | undefined;107 validate(control: AbstractControl): ValidationErrors | null {108 let validationErrors = CrossInputValidation(this.bindControl, (a, b) => a <= b, this.propertyName)(control)109 if (validationErrors)110 if (this.message)111 validationErrors[this.propertyName].message = this.message;112 else113 validationErrors[this.propertyName].message = `${validationErrors[this.propertyName].localValue} debe ser menor o igual a ${validationErrors[this.propertyName].bindValue}`114 return validationErrors;115 }116}117@Directive({118 selector: '[greaterThan][formControlName],[greaterThan][formControl],[greaterThan][ngModel]',119 providers: [{ provide: NG_VALIDATORS, useExisting: GreaterThanValidator, multi: true }]120})121export class GreaterThanValidator implements Validator {122 readonly propertyName = 'greaterThan'123 // eslint-disable-next-line @angular-eslint/no-input-rename124 @Input('greaterThan') bindControl: string | null | undefined;125 // eslint-disable-next-line @angular-eslint/no-input-rename126 @Input('greaterThanMessage') message: string | null | undefined;127 validate(control: AbstractControl): ValidationErrors | null {128 let validationErrors = CrossInputValidation(this.bindControl, (a, b) => a > b, this.propertyName)(control)129 if (validationErrors)130 if (this.message)131 validationErrors[this.propertyName].message = this.message;132 else133 validationErrors[this.propertyName].message = `'${validationErrors[this.propertyName].localValue}' debe ser mayor que '${validationErrors[this.propertyName].bindValue}'`134 return validationErrors;135 }136}137@Directive({138 // eslint-disable-next-line @angular-eslint/directive-selector139 selector: '[greaterOrEqualsThan][formControlName],[greaterOrEqualsThan][formControl],[greaterOrEqualsThan][ngModel]',140 providers: [{ provide: NG_VALIDATORS, useExisting: GreaterOrEqualsThanValidator, multi: true }]141})142export class GreaterOrEqualsThanValidator implements Validator {143 readonly propertyName = 'greaterOrEqualsThan'144 // eslint-disable-next-line @angular-eslint/no-input-rename145 @Input('greaterOrEqualsThan') bindControl: string | null | undefined;146 // eslint-disable-next-line @angular-eslint/no-input-rename147 @Input('greaterOrEqualsThanMessage') message: string | null | undefined;148 validate(control: AbstractControl): ValidationErrors | null {149 let validationErrors = CrossInputValidation(this.bindControl, (a, b) => a >= b, this.propertyName)(control)150 if (validationErrors)151 if (this.message)152 validationErrors[this.propertyName].message = this.message;153 else154 validationErrors[this.propertyName].message = `${validationErrors[this.propertyName].localValue} debe ser mayor o igual a ${validationErrors[this.propertyName].bindValue}`155 return validationErrors;156 }157}158export const VALIDATORS_CROSS_INPUT = [EqualsToValidator, DistinctToValidator, LessThanValidator, LessOrEqualsThanValidator,...
Using AI Code Generation
1var assert = require('assert');2var mb = require('mountebank');3var port = 2525;4var protocol = 'http';5var host = 'localhost';6mb.create({port: port, pidfile: 'mb.pid', logfile: 'mb.log', protofile: 'mb.proto'}, function (error, mbProcess) {7 assert.ifError(error);8 console.log('mb process started');9 mbProcess.deleteImposters(function (error) {10 assert.ifError(error);11 mbProcess.createImposter({protocol: 'http', port: 3000, stubs: [{responses: [{is: {statusCode: 200}}]}]}, function (error, imposter) {12 assert.ifError(error);13 console.log('imposter created');14 var request = require('request');15 request({method: 'POST', uri: url + '/imposters/3000/requests', json: {method: 'GET', path: '/test', headers: {}}}, function (error, response, body) {16 assert.ifError(error);17 console.log('request made');18 console.log('validation errors: ' + JSON.stringify(body.validationErrors, null, 2));19 mbProcess.stop(function (error) {20 assert.ifError(error);21 console.log('mb process stopped');22 });23 });24 });25 });26});27var assert = require('assert');28var mb = require('mountebank');29var port = 2525;30var protocol = 'http';31var host = 'localhost';32mb.create({port: port, pidfile: 'mb.pid', logfile: 'mb.log', protofile: 'mb.proto'}, function (error, mbProcess) {33 assert.ifError(error);34 console.log('mb process started');35 mbProcess.deleteImposters(function (error) {36 assert.ifError(error);37 mbProcess.createImposter({protocol: 'http', port: 3000, stubs: [{responses: [{is: {statusCode: 200}}]}]}, function (error, imposter) {38 assert.ifError(error);39 console.log('imposter created');40 var request = require('request');41 request({method
Using AI Code Generation
1const assert = require('assert');2const mb = require('mountebank');3const port = 2525;4 {5 {6 {7 is: {8 }9 }10 }11 }12];13mb.create({port: port, pidfile: 'mb.pid', logfile: 'mb.log'}, () => {14 mb.post('/imposters', imposters, () => {15 mb.get('/imposters/3000', (error, response) => {16 assert.equal(response.statusCode, 200);17 assert.deepEqual(response.body, {18 {19 {20 is: {21 }22 }23 }24 });25 console.log('test passed');26 mb.stop();27 });28 });29});30{ errors: [ { code: 'invalid predicate', message: 'expected object but found string' } ] }
Using AI Code Generation
1const assert = require('assert');2const mb = require('mountebank');3const request = require('request-promise-native');4mb.create({5}).then(function () {6 return mb.post('/imposters', {7 {8 {9 is: {10 headers: {11 },12 body: {13 }14 }15 }16 }17 });18}).then(function (response) {19}).then(function (response) {20 assert.deepEqual(JSON.parse(response), { status: 'ok' });21 return mb.get('/imposters/3000');22}).then(function (response) {23 assert.deepEqual(response, {24 {25 {26 is: {27 headers: {28 },29 body: {30 }31 }32 }33 }34 {35 query: {},36 headers: {37 },38 }39 metadata: {40 }41 });42 return mb.delete('/imposters/3000');43}).then(function () {44 return mb.get('/imposters/3000');45}).then(function (response) {46 assert.deepEqual(response, {47 {48 }49 });50}).then(function () {51 return mb.get('/imposters');52}).then(function (response) {53 assert.deepEqual(response, { impost
Using AI Code Generation
1const assert = require('assert');2const mb = require('mountebank');3const port = 2525;4const imposter = {5 {6 {7 is: {8 }9 }10 }11};12mb.create(url, imposter)13 .then(() => mb.get(url, imposter.port))14 .then(api => api.get('/'))15 .then(response => assert.equal(response.body, 'Hello, World!'))16 .then(() => mb.get(url, imposter.port))17 .then(api => api.validationErrors())18 .then(errors => assert.equal(errors.length, 0))19 .then(() => mb.get(url, imposter.port))20 .then(api => api.del('/'))21 .then(() => mb.get(url, imposter.port))22 .then(api => api.validationErrors())23 .then(errors => assert.equal(errors.length, 1))24 .then(() => mb.stop(url))25 .then(() => console.log('SUCCESS'))26 .catch(error => console.error(error));27{28 "dependencies": {29 }30}31{32 "dependencies": {33 "mountebank": {34 "requires": {
Using AI Code Generation
1const request = require('request');2const assert = require('assert');3const mb = require('mountebank');4const port = 2525;5 {6 {7 {8 is: {9 headers: { 'Content-Type': 'application/json' },10 body: JSON.stringify({ result: 'OK' })11 }12 }13 }14 }15];16mb.create({ port: port, pidfile: 'mb.pid', logfile: 'mb.log' }, function (err, server) {17 assert.ifError(err);18 server.post('/imposters', { imposters: imposters }, function (err, res) {19 assert.ifError(err);20 assert.strictEqual(res.statusCode, 201);21 assert.ifError(err);22 assert.strictEqual(res.statusCode, 200);23 assert.strictEqual(body, '{"result":"OK"}');24 server.get('/imposters/3000/requests', function (err, res, body) {25 assert.ifError(err);26 assert.strictEqual(res.statusCode, 200);27 assert.strictEqual(body, '[]');28 server.get('/imposters/3000/requests?replayable=true', function (err, res, body) {29 assert.ifError(err);30 assert.strictEqual(res.statusCode, 200);31 assert.strictEqual(body, '[{"protocol":"http","method":"GET","path":"/","query":{},"headers":{"host":"localhost:3000","accept":"*/*"},"body":""}]');32 server.get('/imposters/3000/requests?replayable=false', function (err, res, body) {33 assert.ifError(err);34 assert.strictEqual(res.statusCode, 200);35 assert.strictEqual(body, '[]');36 server.del('/imposters', function (err, res) {37 assert.ifError(err);38 assert.strictEqual(res.statusCode, 200);39 server.get('/imposters', function (err, res, body) {40 assert.ifError(err);41 assert.strictEqual(res.statusCode, 200);42 assert.strictEqual(body, '[]');43 server.get('/imposters/3000/requests', function (err, res, body) {
Using AI Code Generation
1var request = require('request');2var mb = require('mountebank');3var assert = require('assert');4var Q = require('q');5var port = 2525;6var imposterPort = 3000;7var protocol = 'http';8var host = 'localhost';9var deferred = Q.defer();10var stub = {11 {12 is: {13 }14 }15};16var predicate = {17 equals: {18 }19};20var imposter = {21 {22 {23 is: {24 }25 }26 {27 equals: {28 }29 }30 }31};32var imposterWithStubs = {33};34var imposterWithPredicates = {35 {36 {37 is: {38 }39 }40 }41};42var imposterWithStubsAndPredicates = {43};44var imposterWithStubsAndPredicates = {45};46var imposterWithStubsAndPredicates = {47};
Using AI Code Generation
1const mb = require("mountebank"),2assert = require("assert"),3imposter = {4{5{6is: {7}8}9}10};11mb.create(imposter, validationErrors.imposter).then(function () {12return mb.get("/imposters", validationErrors.imposters);13}).then(function (response) {14assert.equal(response.statusCode, 200);15assert.deepEqual(response.body, [imposter]);16}).then(function () {17return mb.delete("/imposters/" + port, validationErrors.imposter);18}).then(function () {19console.log("Success!");20}).catch(function (error) {21console.error(error.message);22});
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!!