How to use checkIsError method in Jest

Best JavaScript code snippet using jest

index.js

Source:index.js Github

copy

Full Screen

...206 }207 </div>208 <TextField209 error={210 checkIsError(errors, touched, index, true)211 }212 variant="standard"213 margin="dense"214 fullWidth215 onChange={handleChange}216 value={actor.name}217 id={`actors.${index}.name`}218 label="Name"219 name={`actors.${index}.name`}220 helperText={221 checkIsError(errors, touched, index, true)222 }223 required224 color='secondary'225 />226 <TextField227 error={228 checkIsError(errors, touched, index)229 }230 variant="standard"231 margin="dense"232 fullWidth233 onChange={handleChange}234 value={actor.surname}235 id={`actors.${index}.surname`}236 label="Surname"237 name={`actors.${index}.surname`}238 helperText={239 checkIsError(errors, touched, index)240 }241 required242 color='secondary'243 />244 </div>245 ))246 }247 <span className={classes.plusContainer}>248 <AddCircle className={classes.plusIcon} onClick={() => push({ name: '', surname: '' })} />249 </span>250 </div>251 )}252 </FieldArray>253 <p className={classes.error}>{error}</p>...

Full Screen

Full Screen

script.js

Source:script.js Github

copy

Full Screen

1//Function for adding author via fetch request2$('#add_author').on('submit', async function (e) {3 e.preventDefault()4 const res = await requestToBackend($(this).attr('action'), 'POST', new FormData(this))5 if (!checkIsError(res)) {6 alertShow($('#successAlert'))7 $('.body').append(createAuthorCard(res.data))8 }9})10//Function for adding a book through a fetch request11$('#add_book').on('submit', async function (e) {12 e.preventDefault()13//Create an array for storing author IDs and put data from the form into it14 let authorsId = createAuthorIdArray($("span[name='author']"))15//Create form data variables16 let data = new FormData(this)17 data.append('authorsId', authorsId)18 console.log(data.image)19 const res = await requestToBackend($(this).attr('action'), 'POST', data)20 if(!checkIsError(res)) {21 alertShow($('#successAlert'))22 showBookCard(res.data)23 }24})25//Function to open the author edit window26$("[name='authorForm']").on('submit', function (e) {27 e.preventDefault()28 let authorNameBox = $(this).children(".authorName")29 $("input[name='authorId']").val(authorNameBox.children("input[name='id']").val())30 $("input[name='lastname']").val(authorNameBox.children("p[name='lastname']").text())31 $("input[name='name']").val(authorNameBox.children("p[name='name']").text())32 $("input[name='patronymic']").val(authorNameBox.children("p[name='patronymic']").text())33})34//Function for sending a request to edit an author35$('#edit_author').on('submit',async function (e) {36 e.preventDefault()37 const res = await requestToBackend($(this).attr('action'), 'PATCH', new FormData(this))38 if (!checkIsError(res)) {39 alertShow($('.alert-success'))40 editAuthorCard(res.data)41 }42})43//Function for sending a request to delete an author44async function deleteAuthor(btn) {45 const res = await requestToBackend($(btn).data('link'), 'DELETE')46 if(!checkIsError(res)) {47 alert('Автор удален')48 $('.author-' + res.data).remove()49 }50}51//Function of sending a request to delete a book52async function deleteBook(btn) {53 const res = await requestToBackend($(btn).data('link'), 'DELETE')54 const book = res.data55 if(!checkIsError(res)) {56 alert('Книга ' + book.title + 'удалена' )57 $(`#book-${book.title}`).remove()58 }59}60//Function for sorting authors/books61//The function takes the pressed button as a parameter62async function sort(btn) {63 const res = await requestToBackend($(btn).data('url'), 'GET')64 if (!checkIsError(res)) {65 refreshList(res.data, $(btn).data('flag'))66 }67}68//Search function for authors/books69async function search(btn) {70//Create variable request addresses71 let url = $(btn).data('url') + $('#search').val()72 const res = await requestToBackend(url, 'GET')73 if (!checkIsError(res)) {74 refreshList(res.data, $(btn).data('flag'))75 }76}77//Function on / off the search button depending on the presence of text in the search input field78$('#search').on('input', function () {79 if($(this).val()){80 $('#searchBtn').removeAttr('disabled')81 }else{82 $('#searchBtn').attr('disabled', 'disabled')83 }84})85//The function of adding an author to the list of authors when creating a new book86function addToList(select){87 let authorName = select.options[select.selectedIndex].text88 $('#authorsList').append($('<span>', {89 class: 'badge badge-light',90 text: authorName,91 value: select.value,92 name: 'author'93 }))94}95//Author card creation function96function createAuthorCard(author) {97 return $("<div class=\"card w-100 author-" + author._id + "\">\n" +98 "<div class=\"card-body\">\n" +99 "<form name=\"authorForm\">\n" +100 "<div class=\"authorCard authorName author-" + author._id + "\">\n" +101 "<p class=\"name\" id=\"lastname\" onformdata=\"lastname\">" + author.lastname + "</p>\n" +102 "<p class=\"name\" id=\"name\">" + author.name + "</p>\n" +103 "<p class=\"name\" id=\"patronymic\">" + author.patronymic +"</p>\n" +104 "<input class=\"name\" name=\"id\" readOnly type=\"hidden\" value=\"" + author._id + "\">\n" +105 "</div>\n" +106 "<div class=\"btn-group btn-card authorCard\">\n" +107 "<button type=\"submit\" class=\"btn btn-info\" data-toggle=\"modal\" data-target=\"#editModalWindow\">\n" +108 "Edit\n" +109 "</button>\n" +110 "<button type=\"button\" class=\"btn btn-danger\" onClick=\"deleteAuthor(this)\" data-link=\"/api/authors/"+ author._id +"\">\n" +111 "Delete\n" +112 "</button>\n" +113 "</div>\n" +114 "</form>\n" +115 "</div>\n" +116 "</div>")117}118//Book card creation function119function createBookCard(book) {120 return $(121 "<div id=\"book-"+ book.title +"\" class=\"card mb-8\" style=\"max-width: 740px;\">\n" +122 "<div class=\"row g-0\">\n" +123 "<div class=\"col-md-2\">\n" +124 "<img class=\"book-img\" src=\"/uploads/" + book.image + "\">\n" +125 "</div>\n" +126 "<div class=\"col-md-8\">\n" +127 "<div class=\"card-body " + book.title + "\">\n" +128 "<h5 class=\"card-title\">" + book.title + "</h5>\n" +129 "<p class=\"card-text\">" + book.description + "</p>\n" +130 "<label>Авторы</label>\n" +131 "</div>\n" +132 "</div>\n" +133 "<div class=\"col-md-2\">\n" +134 "<div class=\"card-body\">\n" +135 "<div class=\"btn-group btn-card\">\n" +136 "<button type=\"button\" class=\"btn btn-danger\" onclick=\"deleteBook(this)\" data-link=\"/api/books/"+ book._id +"\">\n" +137 "Delete\n" +138 "</button>\n" +139 "</div>\n" +140 "</div>\n" +141 "</div>\n" +142 "</div>\n" +143 "</div>\n")144}145//The function of displaying authors in the book card when adding a new book146function showAuthor(authors, title) {147 return authors.forEach( (author) => {148 let authorName = '' + author.name + ' ' + author.lastname + ' ' + author.patronymic149 $(`.${title}`).append($('<span>', {150 class: 'badge badge-light',151 text: authorName152 }))153 })154}155//Author card editing function156function editAuthorCard(author) {157 $(`.author-${author._id}`).children("p[name='lastname']").text(author.lastname)158 $(`.author-${author._id}`).children("p[name='name']").text(author.name)159 $(`.author-${author._id}`).children("p[name='patronymic']").text(author.patronymic)160}161//function of updating the list of authors / books162function refreshList(array, flag) {163 164 switch (flag) {165 case 'authors' :166 $('.card').remove()167 array.forEach((author) => {168 $('.body').append(createAuthorCard(author))169 })170 break171 case 'books' :172 $('.card').remove()173 array.forEach((book) => {174 $('.body').append(createBookCard(book))175 $('.authors').append(showAuthor(book.authors, book.title))176 })177 break178 }179}180//Info message display function181function alertShow(alert) {182 alert.css('display', 'block')183 setTimeout(function(){184 alert.css('display', 'none')185 }, 3000)186}187//Function to create an array of author IDs188function createAuthorIdArray(span) {189 const authorsId = []190 span.each(function (index){191 authorsId.push($(this).attr('value'))192 })193 return authorsId194}195//The function of creating a book card196//when a response from the server about the successful addition of a book to the database197function showBookCard(book) {198 $('.body').append(createBookCard(book))199 $('.authors').append(showAuthor(book.authors, book.title))200}201//Function for request to backend202async function requestToBackend(url, method, body = null) {203 const res = {}204 await fetch(url, {205 method: method,206 body: body207 })208 .then(response => response.json())209 .then(data => res.data = data)210 .catch(e => res.e = e)211 return res212}213//Function checking for an error in response214function checkIsError(res) {215 if (res.e) {216 console.log(e)217 alertShow($('#errorAlert'))218 return true219 }...

Full Screen

Full Screen

jasmineAsyncInstall.js

Source:jasmineAsyncInstall.js Github

copy

Full Screen

1'use strict';2Object.defineProperty(exports, '__esModule', {3 value: true4});5exports.default = jasmineAsyncInstall;6var _co = _interopRequireDefault(require('co'));7var _isGeneratorFn = _interopRequireDefault(require('is-generator-fn'));8var _throat = _interopRequireDefault(require('throat'));9var _isError3 = _interopRequireDefault(require('./isError'));10function _interopRequireDefault(obj) {11 return obj && obj.__esModule ? obj : {default: obj};12}13var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;14var Symbol = global['jest-symbol-do-not-touch'] || global.Symbol;15var Promise = global[Symbol.for('jest-native-promise')] || global.Promise;16function isPromise(obj) {17 return obj && typeof obj.then === 'function';18}19function promisifyLifeCycleFunction(originalFn, env) {20 return function(fn, timeout) {21 if (!fn) {22 return originalFn.call(env);23 }24 const hasDoneCallback = typeof fn === 'function' && fn.length > 0;25 if (hasDoneCallback) {26 // Jasmine will handle it27 return originalFn.call(env, fn, timeout);28 }29 const extraError = new Error(); // Without this line v8 stores references to all closures30 // in the stack in the Error object. This line stringifies the stack31 // property to allow garbage-collecting objects on the stack32 // https://crbug.com/v8/714233 extraError.stack = extraError.stack; // We make *all* functions async and run `done` right away if they34 // didn't return a promise.35 const asyncJestLifecycle = function asyncJestLifecycle(done) {36 const wrappedFn = (0, _isGeneratorFn.default)(fn)37 ? _co.default.wrap(fn)38 : fn;39 const returnValue = wrappedFn.call({});40 if (isPromise(returnValue)) {41 returnValue.then(done.bind(null, null), error => {42 const _isError = (0, _isError3.default)(error),43 checkIsError = _isError.isError,44 message = _isError.message;45 if (message) {46 extraError.message = message;47 }48 done.fail(checkIsError ? error : extraError);49 });50 } else {51 done();52 }53 };54 return originalFn.call(env, asyncJestLifecycle, timeout);55 };56} // Similar to promisifyLifeCycleFunction but throws an error57// when the return value is neither a Promise nor `undefined`58function promisifyIt(originalFn, env, jasmine) {59 return function(specName, fn, timeout) {60 if (!fn) {61 const spec = originalFn.call(env, specName);62 spec.pend('not implemented');63 return spec;64 }65 const hasDoneCallback = fn.length > 0;66 if (hasDoneCallback) {67 return originalFn.call(env, specName, fn, timeout);68 }69 const extraError = new Error(); // Without this line v8 stores references to all closures70 // in the stack in the Error object. This line stringifies the stack71 // property to allow garbage-collecting objects on the stack72 // https://crbug.com/v8/714273 extraError.stack = extraError.stack;74 const asyncJestTest = function asyncJestTest(done) {75 const wrappedFn = (0, _isGeneratorFn.default)(fn)76 ? _co.default.wrap(fn)77 : fn;78 const returnValue = wrappedFn.call({});79 if (isPromise(returnValue)) {80 returnValue.then(done.bind(null, null), error => {81 const _isError2 = (0, _isError3.default)(error),82 checkIsError = _isError2.isError,83 message = _isError2.message;84 if (message) {85 extraError.message = message;86 }87 if (jasmine.Spec.isPendingSpecException(error)) {88 env.pending(message);89 done();90 } else {91 done.fail(checkIsError ? error : extraError);92 }93 });94 } else if (returnValue === undefined) {95 done();96 } else {97 done.fail(98 new Error(99 'Jest: `it` and `test` must return either a Promise or undefined.'100 )101 );102 }103 };104 return originalFn.call(env, specName, asyncJestTest, timeout);105 };106}107function makeConcurrent(originalFn, env, mutex) {108 return function(specName, fn, timeout) {109 if (110 env != null &&111 !env.specFilter({112 getFullName: () => specName || ''113 })114 ) {115 return originalFn.call(env, specName, () => Promise.resolve(), timeout);116 }117 let promise;118 try {119 promise = mutex(() => {120 const promise = fn();121 if (isPromise(promise)) {122 return promise;123 }124 throw new Error(125 `Jest: concurrent test "${specName}" must return a Promise.`126 );127 });128 } catch (error) {129 return originalFn.call(env, specName, () => Promise.reject(error));130 }131 return originalFn.call(env, specName, () => promise, timeout);132 };133}134function jasmineAsyncInstall(globalConfig, global) {135 const jasmine = global.jasmine;136 const mutex = (0, _throat.default)(globalConfig.maxConcurrency);137 const env = jasmine.getEnv();138 env.it = promisifyIt(env.it, env, jasmine);139 env.fit = promisifyIt(env.fit, env, jasmine);140 global.it.concurrent = (env => {141 const concurrent = makeConcurrent(env.it, env, mutex);142 concurrent.only = makeConcurrent(env.fit, env, mutex);143 concurrent.skip = makeConcurrent(env.xit, env, mutex);144 return concurrent;145 })(env);146 global.fit.concurrent = makeConcurrent(env.fit, env, mutex);147 env.afterAll = promisifyLifeCycleFunction(env.afterAll, env);148 env.afterEach = promisifyLifeCycleFunction(env.afterEach, env);149 env.beforeAll = promisifyLifeCycleFunction(env.beforeAll, env);150 env.beforeEach = promisifyLifeCycleFunction(env.beforeEach, env);...

Full Screen

Full Screen

SemForm.js

Source:SemForm.js Github

copy

Full Screen

1import React from 'react'2import validation from 'react-validation-mixin'3import strategy from 'react-validatorjs-strategy'4import { Form } from 'semantic-ui-react'5import PropTypes from 'prop-types'6import _ from 'lodash'7class SemForm extends React.Component {8 state = {9 submitted: false,10 }11 static propTypes = {12 source: PropTypes.object.isRequired,13 submitOnEnter: PropTypes.bool,14 onTrySubmit: PropTypes.func, // onTrySubmit when defined will cancel submission of form when false is returned15 onError: PropTypes.func,16 onSuccess: PropTypes.func.isRequired,17 schema: PropTypes.object.isRequired,18 customMessage: PropTypes.object,19 attributeNames: PropTypes.object,20 validationHandler: PropTypes.func,21 onAfterMountForm: PropTypes.func,22 }23 static defaultProps = {24 horizontal: false,25 customMessage: {},26 attributeNames: {},27 submitOnEnter: true,28 }29 static childContextTypes = {30 getErrorText: PropTypes.func,31 checkIsError: PropTypes.func,32 horizontal: PropTypes.bool,33 schema: PropTypes.object,34 }35 getChildContext() {36 return {37 getErrorText: this.getErrorText,38 checkIsError: this.checkIsError,39 horizontal: this.props.horizontal,40 schema: this.props.schema,41 }42 }43 // this is the object validator will look up to when validating44 // in this module, this is accessed from the store45 getValidatorData = () => {46 return this.props.source47 }48 // shows the error message on the HelpBlock49 getErrorText = field => {50 var error = this.props.errors[field]51 var forReturn = ''52 if (_.isArray(error)) {53 _.map(error, err => {54 if (forReturn !== '') {55 forReturn += '; '56 }57 forReturn += err58 })59 } else {60 forReturn = error61 }62 return forReturn63 // if (!error)64 // return null;65 // if (Array.isArray(error)) {66 // var message = [];67 // message = error.map((item, i) => {68 // return (69 // <span key={i}>70 // {item}71 // <br />72 // </span>73 // )74 // });75 // return message;76 // }77 // else78 // return (<span>{error || ''}</span>);79 }80 checkIsError = field => {81 if (this.state.submitted) {82 return !this.props.isValid(field)83 } else {84 return false85 }86 }87 onFormSubmit = event => {88 event.preventDefault() // will cancel default formsubmit mechanism we will use ajax89 this.setState(90 {91 submitted: true,92 },93 () => {94 if (this.props.onTrySubmit) {95 if (!this.props.onTrySubmit()) return96 }97 this.props.validate(this.onValidate)98 }99 )100 }101 resetFields = () => {102 this.setState({103 submitted: false,104 })105 }106 onValidate = error => {107 if (error) {108 if (this.props.onError) this.props.onError(error)109 } else {110 if (this.props.onSuccess) this.props.onSuccess()111 }112 }113 componentDidMount() {114 if (this.props.onAfterMountForm) {115 this.props.onAfterMountForm(this)116 }117 }118 submitForm = () => {119 this.refs.hiddenBtn.click()120 }121 checkEnter = e => {122 var txtArea = /textarea/i.test((e.target || e.srcElement).tagName)123 if (txtArea || (e.keyCode || e.which || e.charCode || 0) !== 13) {124 return true125 } else {126 e.preventDefault()127 return false128 }129 }130 render() {131 this.validatorTypes = strategy.createSchema(132 this.props.schema,133 {134 required: 'The field :attribute is required!',135 numeric: 'The field :attribute should be a number!',136 email: 'The field :attribute should be a valid email!',137 },138 validation => {139 validation.setAttributeNames(this.props.attributeNames)140 if (this.props.validationHandler) this.props.validationHandler(validation)141 }142 )143 return (144 <Form145 onKeyPress={!this.props.submitOnEnter ? this.checkEnter : null}146 onSubmit={this.onFormSubmit}147 style={this.props.style}148 >149 {this.props.children}150 <button style={{ display: 'none' }} type="submit" ref="hiddenBtn" />151 </Form>152 )153 }154}...

Full Screen

Full Screen

authors.js

Source:authors.js Github

copy

Full Screen

1async function getAllAuthors() {2 const res = await requestToBackend("/api/author", 'GET')3 if (!checkIsError(res)) {4 res.data.forEach(author => {5 $('.body').append(createAuthorCard(author));6 });7 }8}9getAllAuthors()10//Function for adding author via fetch request11$('#add_author').on('submit', async function (e) {12 e.preventDefault()13 const res = await requestToBackend($(this).attr('action'), 'POST', new FormData(this))14 if (!checkIsError(res)) {15 alertShow($('#successAlert'))16 $('.body').append(createAuthorCard(res.data))17 }18})19$('#edit_author').on('submit',async function (e) {20 e.preventDefault()21 const res = await requestToBackend($(this).attr('action'), $(this).attr('method'), new FormData(this))22 if (!checkIsError(res)) {23 alertShow($('.alert-success'))24 editAuthorCard(res.data)25 }26});27//Author card editing function28function editAuthorCard(author) {29 $(`.author-${author.id}`).children("p[id='lastname']").text(author.lastname)30 $(`.author-${author.id}`).children("p[id='name']").text(author.name)31 $(`.author-${author.id}`).children("p[id='patronymic']").text(author.patronymic)32}33//Function to open the author edit window34function openAuthorForm(btn) {35 let authorForm = $(btn).parent().parent()36 let authorNameBox = authorForm.children(".authorName")37 console.log(authorNameBox.children("input[name='id']").val())38 $("input[name='id']").val(authorNameBox.children("input[name='id']").val())39 $("input[name='lastname']").val(authorNameBox.children("p[id='lastname']").text())40 $("input[name='name']").val(authorNameBox.children("p[id='name']").text())41 $("input[name='patronymic']").val(authorNameBox.children("p[id='patronymic']").text())42}43//Function for sending a request to delete an author44async function deleteAuthor(btn) {45 const res = await requestToBackend($(btn).data('link'), 'DELETE')46 if(!checkIsError(res)) {47 alert('Автор удален')48 $('.author-' + res.data).remove()49 }50}51//Author card creation function52function createAuthorCard(author) {53 return $("<div class=\"card w-100 author-" + author.id + "\">\n" +54 "<div class=\"card-body\">\n" +55 "<form name=\"authorForm\">\n" +56 "<div class=\"authorCard authorName author-" + author.id + "\">\n" +57 "<p class=\"name\" id=\"lastname\" onformdata=\"lastname\">" + author.lastname + "</p>\n" +58 "<p class=\"name\" id=\"name\">" + author.name + "</p>\n" +59 "<p class=\"name\" id=\"patronymic\">" + author.patronymic +"</p>\n" +60 "<input class=\"name\" name=\"id\" readOnly type=\"hidden\" value=\"" + author.id + "\">\n" +...

Full Screen

Full Screen

Jest Testing Tutorial

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.

Chapters

  1. What is Jest Framework
  2. Advantages of Jest - Jest has 3,898,000 GitHub repositories, as mentioned on its official website. Learn what makes Jest special and why Jest has gained popularity among the testing and developer community.
  3. Jest Installation - All the prerequisites and set up steps needed to help you start Jest automation testing.
  4. Using Jest with NodeJS Project - Learn how to leverage Jest framework to automate testing using a NodeJS Project.
  5. Writing First Test for Jest Framework - Get started with code-based tutorial to help you write and execute your first Jest framework testing script.
  6. Jest Vocabulary - Learn the industry renowned and official jargons of the Jest framework by digging deep into the Jest vocabulary.
  7. Unit Testing with Jest - Step-by-step tutorial to help you execute unit testing with Jest framework.
  8. Jest Basics - Learn about the most pivotal and basic features which makes Jest special.
  9. Jest Parameterized Tests - Avoid code duplication and fasten automation testing with Jest using parameterized tests. Parameterization allows you to trigger the same test scenario over different test configurations by incorporating parameters.
  10. Jest Matchers - Enforce assertions better with the help of matchers. Matchers help you compare the actual output with the expected one. Here is an example to see if the object is acquired from the correct class or not. -

|<p>it('check_object_of_Car', () => {</p><p> expect(newCar()).toBeInstanceOf(Car);</p><p> });</p>| | :- |

  1. Jest Hooks: Setup and Teardown - Learn how to set up conditions which needs to be followed by the test execution and incorporate a tear down function to free resources after the execution is complete.
  2. Jest Code Coverage - Unsure there is no code left unchecked in your application. Jest gives a specific flag called --coverage to help you generate code coverage.
  3. HTML Report Generation - Learn how to create a comprehensive HTML report based on your Jest test execution.
  4. Testing React app using Jest Framework - Learn how to test your react web-application with Jest framework in this detailed Jest tutorial.
  5. Test using LambdaTest cloud Selenium Grid - Run your Jest testing script over LambdaTest cloud-based platform and leverage parallel testing to help trim down your test execution time.
  6. Snapshot Testing for React Front Ends - Capture screenshots of your react based web-application and compare them automatically for visual anomalies with the help of Jest tutorial.
  7. Bonus: Import ES modules with Jest - ES modules are also known as ECMAScript modules. Learn how to best use them by importing in your Jest testing scripts.
  8. Jest vs Mocha vs Jasmine - Learn the key differences between the most popular JavaScript-based testing frameworks i.e. Jest, Mocha, and Jasmine.
  9. Jest FAQs(Frequently Asked Questions) - Explore the most commonly asked questions around Jest framework, with their answers.

Run Jest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful