Skip to content

Instantly share code, notes, and snippets.

@dejanvasic85
Last active June 3, 2020 12:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dejanvasic85/55a97b019c26bfd96652ebbfc646a1a3 to your computer and use it in GitHub Desktop.
Save dejanvasic85/55a97b019c26bfd96652ebbfc646a1a3 to your computer and use it in GitHub Desktop.
Jest Testing Examples
import React from 'react';
import { Avatar } from 'antd';
import { shallow, ShallowWrapper } from 'enzyme';
import ColourAvatar from './ColourAvatar';
describe('ColourAvatar', () => {
let wrapper: ShallowWrapper;
const defaultProps: any = {
firstName: 'Boom',
};
describe('when only first name is provided', () => {
beforeEach(() => {
wrapper = shallow(<ColourAvatar {...defaultProps} />);
});
it('should render Avatar', () => {
expect(wrapper.find(Avatar)).toHaveLength(1);
});
it('should display first initial', () => {
expect(wrapper.find(Avatar).render().text()).toEqual('B');
});
it('should have large Avatar size', () => {
expect(wrapper.find(Avatar).prop('size')).toEqual('large');
});
it('should set background colour style', () => {
expect(wrapper.find(Avatar).prop('style').backgroundColor).toHaveLength(7);
});
});
describe('when last name is provided', () => {
beforeEach(() => {
wrapper = shallow(<ColourAvatar {...defaultProps} lastName="Shaka" />);
});
it('should render both initials', () => {
expect(wrapper.find(Avatar).render().text()).toEqual('BS');
});
});
describe('when the src attribute is provided', () => {
beforeEach(() => {
wrapper = shallow(<ColourAvatar {...defaultProps} src="http://jedigova" />);
});
it('should render both initials', () => {
expect(wrapper.find(Avatar).prop('src')).toEqual('http://jedigova');
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment