Ad

Test Case React With Node Js

- 1 answer

My site is using react and node . For that I created the test case using enzyme at react side and from server site I am using Mocha. Both are working correctly when I am using terminal command (npm test) its showing fail ans success result. But I want to do test case from ui interface .

Basically i want to backed (nodejs) function fronted with one command, Now my test case working on different terminal For example for react I am opened in one terminal and for node side test case I oped another terminal.

this is my node side code :-

const request = require('supertest');
const expect = require('chai').expect;
const req = 'http://localhost:3000';
 this.user_id = '';

describe('Register form', function() {
    it('User saved successfully in database', function(done) {
        request(req)
           .post('/users/register')
           .set('Accept', 'application/json')
           .set('Content-Type', 'application/json')
           .send({user:{disabled: false,
                    email: "[email protected]",
                    firstName: "test",
                    lastName: "test",
                    password: "24234234ds",
                    roles: "hr",
                    status: "verified"}})
           .expect(200)
           .expect('Content-Type', /json/)
           .expect(function(response) {
           })
           .end(done);
    }); 
});

and this is react side code:-

import React from 'react';
import { Link } from 'react-router-dom';
import { connect } from 'react-redux';
import Select from 'react-select'
import config from 'config';
import { Alert } from 'reactstrap';
import { userActions } from '../../../src/actions';

import Enzyme, {shallow,mount,render,unmount} from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import RegisterPage from '../../../src/features/RegisterPage/RegisterPage';
import { MemoryRouter } from 'react-router-dom';

Enzyme.configure({adapter: new Adapter()});

describe('In register page check react syntex', () => {
  it('Should RegisterPage shallow correctly in "debug" mode', () => {
    const component = (<RegisterPage debug />);
    expect(component).toMatchSnapshot();
  });
}); 

describe('On register page count input filed', () => {
     it('Total text filed', () => {
      expect(shallow(<RegisterPage />).find('input[type="text"]').length).toEqual(2)
     })
    it('Total email filed', () => {
      expect(shallow(<RegisterPage />).find('input[type="email"]').length).toEqual(1)
     })
    it('Total checkbox filed', () => {
      expect(shallow(<RegisterPage />).find('input[type="checkbox"]').length).toEqual(1)
     })
    it('Total radio filed', () => {
      expect(shallow(<RegisterPage />).find('input[type="radio"]').length).toEqual(2)
     })
}); 


describe('On Register page check firstname , lastname or email address vaildation', () => {  
  it('Check first name in registerpage ', () => {
   const wrapper = mount(<RegisterPage />);
   wrapper.find('input[name="firstName"]').simulate('change', {target: {name: 'firstName', value: 'dinesh'}}); 
   expect(wrapper.state().user.firstName).toEqual('dinesh');    
  })

  it('Check lastname in register page ', () => {
   const wrapper = mount(<RegisterPage />);
   wrapper.find('input[name="lastName"]').simulate('change', {target: {name: 'lastName', value: 'Sharma'}});
   expect(wrapper.state().user.lastName).toEqual('Sharma');   
  })

  it('Check email address in register page', () => {
  const wrapper = mount(<RegisterPage />);
  wrapper.find('input[name="email"]').simulate('change', {target: {name: 'email', value: '[email protected]'}});
  expect(wrapper.state().user.email).toEqual('[email protected]');   
  })

})
Ad

Answer

If you want to run front end and back end test together in one terminal you can modify your package.json scripts section to look something like this.

{
    "scripts": {
        "test": "mocha <path to node test files> && jest <path to react test files>"
    }
}

Every time you run npm test both sets of tests will run.

Ad
source: stackoverflow.com
Ad