Skip to content

Instantly share code, notes, and snippets.

@night-fury-rider
Last active February 6, 2024 17:04
Show Gist options
  • Save night-fury-rider/fada57f43c4a0fb31eab82a5055334a1 to your computer and use it in GitHub Desktop.
Save night-fury-rider/fada57f43c4a0fb31eab82a5055334a1 to your computer and use it in GitHub Desktop.
React - Jest with Enzyme_Shallow
import React from 'react';
import { shallow } from 'enzyme';
import Timeline from './Timeline';
let componentToTest = {
title: 'Timeline: ',
html: <Timeline />,
noOfCompanies: 4,
noOfColleges:3
};
let wrapper = shallow(componentToTest.html),
elementToSearch;
it(componentToTest.title + 'renders without crashing', () => {
shallow(componentToTest.html);
});
it(componentToTest.title + 'renders major html elements', () => {
elementToSearch = <h2 className="mb-4 rem-2">Experience</h2>;
expect(wrapper.contains(elementToSearch)).toEqual(true);
elementToSearch = <h2 className="mb-4 mobile-mt-2 rem-2">Education</h2>;
expect(wrapper.contains(elementToSearch)).toEqual(true);
elementToSearch = <p>Lannisters always pay their debt</p>;
expect(wrapper.contains(elementToSearch)).toEqual(false);
elementToSearch = 'e-Zest';
// Test case to check particular string exists on html element or not
expect(wrapper.text()).toEqual(expect.stringContaining(elementToSearch));
});
it(componentToTest.title + 'renders experience section', () => {
expect(wrapper.find('.experience').children()).toHaveLength(componentToTest.noOfCompanies);
});
it(componentToTest.title + 'renders education section', () => {
expect(wrapper.find('.education').children()).toHaveLength(componentToTest.noOfColleges);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment