Best JavaScript code snippet using best
schemaManager.spec.ts
Source:schemaManager.spec.ts
...17 const sm = new SchemaManager();18 it('schema error wrong type', () => {19 // @ts-ignore20 sm.InitSchema('ddd')21 expect(hasError(sm, err_schema)).toEqual(true);22 })23 it('schema error no type property', () => {24 // @ts-ignore25 sm.InitSchema({ label: 'dada'})26 expect(hasError(sm, err_schema)).toEqual(true);27 })28 it('schema ok', () => {29 sm.InitSchema({ type: 'label', label: 'dada'})30 expect(sm.SchemaErrors.length).toEqual(0);31 })32})33describe('Component has wrong properties', () => {34 const sm = new SchemaManager();35 sm.InitSchema(schemaErr)36 sm.CheckSchema()37 it('component no type', () => {38 expect(hasError(sm, err_notype, 'notype')).toEqual(true);39 })40 it('component wrong type', () => {41 expect(hasError(sm, err_typewrong, 'wrongtype')).toEqual(true);42 })43 it('container with no child', () => {44 expect(hasError(sm, err_noChild, 'noChild')).toEqual(true);45 expect(hasError(sm, err_noChild, 'noChild1')).toEqual(false);46 expect(hasError(sm, err_zeroChild, 'noChild1')).toEqual(true);47 })48 it('input has no field', () => {49 expect(hasError(sm, err_noField, 'nofield')).toEqual(true);50 expect(hasError(sm, err_noField, 'hasfield')).toEqual(false);51 expect(hasError(sm, err_noField, 'noLabelnoField')).toEqual(true);52 })53 // it('input has no label', () => {54 // expect(hasError(sm, err_noLabel, 'noLabelnoField')).toEqual(true);55 // })56 it('input has no field', () => {57 // expect(hasError(sm, err_noLabel, 'noLabelnoField')).toEqual(true);58 expect(hasError(sm, err_noField, 'noLabelnoField')).toEqual(true);59 })60 it('double fields', () => {61 expect(hasError(sm, err_doubleField, '', 'doublefield')).toEqual(true);62 expect(hasError(sm, err_doubleField, 'hasfield')).toEqual(false);63 })64 65 it('double names', () => {66 expect(hasError(sm, err_doubleName, 'doublename')).toEqual(true);67 expect(hasError(sm, err_doubleField, 'hasfield')).toEqual(false);68 })69 it('select has no options', () => {70 expect(hasError(sm, err_noOptions, '', 'selectnoptions')).toEqual(true);71 expect(hasError(sm, err_noOptions, '', 'selecthasptions')).toEqual(false);72 })73 it('radiogroup has no options', () => {74 expect(hasError(sm, err_noOptions, '', 'radiogroupnooptions')).toEqual(true);75 expect(hasError(sm, err_noOptions, '', 'radiogrouphasptions')).toEqual(false);76 })77 it('select has zero options', () => {78 expect(hasError(sm, err_zeroOptions, '', 'selectzerooptions')).toEqual(true);79 })80 it('select has wrong options', () => {81 expect(hasError(sm, err_wrongOptions, '', 'selectwrongoptions')).toEqual(true);82 expect(hasError(sm, err_wrongOptions, '', 'selectwrongoptions2')).toEqual(true);83 expect(hasError(sm, err_wrongOptions, '', 'selectwrongoptions3')).toEqual(true);84 expect(hasError(sm, err_wrongOptions, '', 'selectwrongoptions4')).toEqual(true);85 })86 it('select has duplicate values in options', () => {87 expect(hasError(sm, err_OptionsDoubleValues, '', 'selectoptionsDouble1')).toEqual(true);88 expect(hasError(sm, err_OptionsDoubleValues, '', 'selectoptionsDouble2')).toEqual(true);89 expect(hasError(sm, err_OptionsDoubleValues, '', 'selectoptionsok1')).toEqual(false);90 })91 it('select has ok options', () => {92 expect(hasError(sm, err_wrongOptions, '', 'selectoptionsok1')).toEqual(false);93 expect(hasError(sm, err_wrongOptions, '', 'selectoptionsok2')).toEqual(false);94 expect(hasError(sm, err_wrongOptions, '', 'selectoptionsok3')).toEqual(false);95 })96 it('unnecessary option', () => {97 expect(hasError(sm, err_unn('label1'), 'btnunn')).toEqual(true);98 expect(hasError(sm, err_noIcon, 'iconmissing')).toEqual(true);99 })100 it('datatable in datatable not ok', () => {101 expect(hasError(sm, err_noDataTableInDataTable, 'datatableIndatatable')).toEqual(true);102 // expect(hasError(sm, err_noDataTableInDataTable, 'datatableNoFields')).toEqual(false);103 })104 it('datatable with no fields', () => {105 expect(hasError(sm, err_noDataTableNoField, 'datatable2')).toEqual(false);106 })107})108describe('SchemaManager Value Type', () => {109 const sm = new SchemaManager();110 it('test checkValueType', () => {111 let res;112 let x = undefined;113 res = sm.checkValueType(x);114 expect(res).toEqual(IValueType.undefined);115 x = null;116 res = sm.checkValueType(x);117 expect(res).toEqual(IValueType.null);118 x = 'aa';119 res = sm.checkValueType(x);...
formUtils.js
Source:formUtils.js
1 2 3 export const initialState = {4 name: { value: "", touched: false, hasError: true, error: "" },5 email: { value: "", touched: false, hasError: true, error: "" },6 password: { value: "", touched: false, hasError: true, error: "" },7 confirm: { value: "", touched: false, hasError: true, error: "" },8 isFormValid: false,9 }10 export const initialLoginStateForget = {11 email: { value: "", touched: false, hasError: true, error: "" },12 isFormValid: false,13 }14 export const initialLoginState = {15 email: { value: "", touched: false, hasError: true, error: "" },16 password: { value: "", touched: false, hasError: true, error: "" },17 isFormValid: false,18 }19 export const reducer = (state, action) => {20 switch (action.type) {21 case 'UPDATE_FORM':22 const { name, value, hasError, error, touched, isFormValid } = action.data23 return {24 ...state,25 // update the state of the particular field,26 // by retaining the state of other fields27 [name]: { ...state[name], value, hasError, error, touched },28 isFormValid,29 }30 default:31 return state32 }33}34 export const validateInput = (name, value) => {35 let hasError = false,36 error = ""37 switch (name) {38 case "name":39 if (value.trim() === "") {40 hasError = true41 error = "Name cannot be empty"42 } else if (!/^[a-zA-Z ]+$/.test(value)) {43 hasError = true44 error = "Invalid Name. Avoid Special characters"45 } else {46 hasError = false47 error = ""48 }49 break50 case "email":51 if (value.trim() === "") {52 hasError = true53 error = "Email cannot be empty"54 } else if (55 !/^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(56 value57 )58 ) {59 hasError = true60 error = "Invalid Email"61 } else {62 hasError = false63 error = ""64 }65 break66 case "password":67 if (value.trim() === "") {68 hasError = true69 error = "Password cannot be empty"70 } else if (value.trim().length < 4) {71 hasError = true72 error = "Password must have at least 4 characters"73 } else {74 hasError = false75 error = ""76 }77 break78 case "confirm":79 if (value.trim() === "") {80 hasError = true81 error = "Password cannot be empty"82 } else if (value.trim().length < 4) {83 hasError = true84 error = "Password must have at least 4 characters"85 } else {86 hasError = false87 error = ""88 }89 break90 default:91 break92 }93 return { hasError, error }94 }95 export const onInputChange = (name, value, dispatch, formState) => {96 const { hasError, error } = validateInput(name, value)97 let isFormValid = true98 99 for (const key in formState) {100 const item = formState[key]101 // Check if the current field has error102 if (key === name && hasError) {103 isFormValid = false104 break105 } else if (key !== name && item.hasError) {106 // Check if any other field has error107 isFormValid = false108 break109 }110 }111 112 dispatch({113 type: 'UPDATE_FORM',114 data: { name, value, hasError, error, touched: false, isFormValid },115 })116 }117 export const onFocusOut = (name, value, dispatch, formState) => {118 const { hasError, error } = validateInput(name, value)119 let isFormValid = true120 for (const key in formState) {121 const item = formState[key]122 if (key === name && hasError) {123 isFormValid = false124 break125 } else if (key !== name && item.hasError) {126 isFormValid = false127 break128 }129 }130 131 dispatch({132 type: 'UPDATE_FORM',133 data: { name, value, hasError, error, touched: true, isFormValid },134 })135 }...
errors.component.ts
Source:errors.component.ts
...12 this.formGroup.get(this.formcontrolname);13 }14 getErrorMessage(): string {15 const control = this.formGroup.get(this.formcontrolname);16 return control.hasError('passwordsNotMatching') ? 'Senhas não são iguais' :17 control.hasError('email') ? 'Email inválido' :18 control.hasError('maxlength') ? 'Número de caracteres ultrapassados' :19 control.hasError('minlength') ? 'Número de caracteres não atingido' :20 control.hasError('max') ? 'Valor maior que o permitido' :21 control.hasError('min') ? 'Valor menor que o permitido' :22 control.hasError('pattern') ? 'Campo inválido' :23 control.hasError('invalid') ? 'Campo inválido' :24 control.hasError('cnpj') ? 'CNPJ inválido' :25 control.hasError('cpf') ? 'CPF inválido' :26 control.hasError('blank') ? 'Existe um espaço no Ãnicio do campo' :27 control.hasError('requiredFileType') ? 'Arquivo inválido' :28 control.hasError('nonzero') ? 'Valor 0 não é aceito' :29 control.hasError('document') ? 'CPF/CNPJ Inválido' :30 control.hasError('cnh') ? 'Número da CNH Inválida' :31 control.hasError('birthDate') ? 'Data de Nascimento Inválida' :32 control.hasError('DTPRODUC') ? 'Data de Produção Inválida' :33 control.hasError('DTENTREG') ? 'Data de Entrega Inválida' :34 control.hasError('QTVLLBPR') ? 'Volume a ser liberado deve ser positivo e menor ou igual ao Saldo pedido' :35 control.hasError('dateValidity') ? 'CNH está vencida! Verificar Data de Validade!' :36 control.hasError('integrationDate') ? 'Data de Integração inválida' :37 control.hasError('matDatepickerParse') ? 'Data Informada Inválida' :38 control.hasError('zipCode') ? 'CEP Inválido' :39 control.hasError('Mask error') ? 'Valor Informado Inválido' :40 control.hasError('required') ? 'Dados obrigatórios não informados' :41 '';42 }...
Using AI Code Generation
1const { BestPractice } = require('eslint-plugin-best-practices');2const { RuleTester } = require('eslint');3const ruleTester = new RuleTester();4ruleTester.run('best-practices', BestPractice, {5 {6 const a = 1;7 const b = 2;8 const c = a + b;9 },10 {11 const a = 1;12 const b = 2;13 const c = a + b;14 {15 message: 'You should use the spread operator instead of .apply()',16 },17 },18});19const { BestPractice } = require('eslint-plugin-best-practices');20const { RuleTester } = require('eslint');21const ruleTester = new RuleTester();22ruleTester.run('best-practices', BestPractice, {23 {24 const a = 1;25 const b = 2;26 const c = a + b;27 },28 {29 const a = 1;30 const b = 2;31 const c = a + b;32 {33 message: 'You should use the spread operator instead of .apply()',34 },35 },36});37const { BestPractice } = require('eslint-plugin-best-practices');38const { RuleTester } = require('eslint');39const ruleTester = new RuleTester();40ruleTester.run('best-practices', BestPractice, {41 {42 const a = 1;43 const b = 2;44 const c = a + b;45 },46 {47 const a = 1;48 const b = 2;49 const c = a + b;50 {51 message: 'You should use the spread operator instead of .apply()',52 },53 },54});
Using AI Code Generation
1const { BestValidator } = require('best-validator');2const validator = new BestValidator();3const schema = {4 name: {5 messages: {6 }7 },8 age: {9 messages: {10 }11 }12}13const data = {14}15validator.validate(data, schema);16if (validator.hasError()) {17 console.log(validator.getErrors());18}19const { BestValidator } = require('best-validator');20const validator = new BestValidator();21const schema = {22 name: {23 messages: {24 }25 },26 age: {27 messages: {28 }29 }30}31const data = {32}33validator.validate(data, schema);34if (validator.hasError()) {
Using AI Code Generation
1const BestPractices = require("./BestPractices.js");2const fs = require("fs");3const path = require("path");4const code = fs.readFileSync(path.resolve(__dirname, "sample.js"), "utf-8");5const code1 = fs.readFileSync(path.resolve(__dirname, "sample1.js"), "utf-8");6const code2 = fs.readFileSync(path.resolve(__dirname, "sample2.js"), "utf-8");7const bestPractices = new BestPractices(code);8const bestPractices1 = new BestPractices(code1);9const bestPractices2 = new BestPractices(code2);10const result = bestPractices.hasErrors();11const result1 = bestPractices1.hasErrors();12const result2 = bestPractices2.hasErrors();13console.log(result);14console.log(result1);15console.log(result2);
Using AI Code Generation
1const BestMatch = require('./bestMatch');2var bm = new BestMatch();3var testString = "test";4var testString2 = "test1";5var testString3 = "test2";6var testString4 = "test3";7console.log(bm.hasError(testString));8console.log(bm.hasError(testString2));9console.log(bm.hasError(testString3));10console.log(bm.hasError(testString4));
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!!