lundi 12 avril 2021

how to get value of react props from a component when unit testing using enzyme &jestjs

I am trying here to write a test for my React component Component : App.tsx Test: using enzyme jestjs Error: I am trying to access the props.ag value and check if its not NULL. I have this.

App.tsx

type Props = {};
type PrismaProps = MapFunctionToProps<typeof prismaConnector>;

const App = (props: Props & PrismaProps) => {

  return (
    <MainAgentProv ag={props.ag}>
      <EventProv>
        <Base
          agent={props.ag}                         //Want to Check if props.ag is not null
          nextText={props.nextText}
          sidebar={<Toolbar agent={props.ag} />}
        />
      </EventProv>
    </MainAgentProv>
  );
};

function prismaConnector(ag: MainAg) {
  return {
    ag: ag,
    htmlInit: ag.htmlInit.bind(agent),
    nextText: ag.property(NextLabel).value()
  };
}

export default connect<Props, PrismaProps>(prismaConnector)(App);

App.test.js

import dscontrol = require('test/js/DSControl');

//React Application
import App   from "UniversalDataSelection/html/reactui/App";

//Enzyme
import * as React from "react";
import { configure, shallow, mount, render } from 'enzyme';
var Adapter = require('test/enzyme-adapter-react');

configure({ adapter: new Adapter });
var expect = chai.expect;

//Test React Component using Enzyme
var dscontrolInstance = dscontrol.getInstance();

describe('The application is running', () => {

  it('agent shall not be null', () => {
    const wrapper = shallow(<App ag={dscontrolInstance.getMainAg()} />);
    expect(wrapper.props('ag').toBe(!NULL));               //But here it gives me error
  });
});

Aucun commentaire:

Enregistrer un commentaire