Skip to content

Commit

Permalink
MNT Use React Testing Library
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Apr 18, 2023
1 parent ead5813 commit 21a23fa
Show file tree
Hide file tree
Showing 70 changed files with 4,516 additions and 6,039 deletions.
40 changes: 11 additions & 29 deletions client/src/components/Accordion/tests/Accordion-test.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,15 @@
/* global jest, describe, it, expect */
/* global jest, test, describe, it, expect */

import React from 'react';
import Accordion from '../Accordion';
import Enzyme, { mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16/build/index';

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

const errorSpy = jest.spyOn(global.console, 'error');
const warnSpy = jest.spyOn(global.console, 'warn');

describe('Accordion', () => {
describe('render()', () => {
beforeEach(() => {
errorSpy.mockClear();
warnSpy.mockClear();
});

it('renders with children', () => {
const reactWrapper = mount(
<Accordion>
<p>lorem</p>
<p>ipsum</p>
</Accordion>
);

expect(reactWrapper.find('div')).toHaveLength(1);
expect(errorSpy).not.toHaveBeenCalled();
expect(warnSpy).not.toHaveBeenCalled();
});
});
import { render } from '@testing-library/react';

test('Accordion render() renders with children', () => {
const container = render(
<Accordion>
<p>lorem</p>
<p>ipsum</p>
</Accordion>
).container;
expect(container.querySelectorAll('div')).toHaveLength(1);
});
47 changes: 14 additions & 33 deletions client/src/components/Accordion/tests/AccordionBlock-test.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,19 @@
/* global jest, describe, it, expect */
/* global jest, test, describe, it, expect */

import React from 'react';
import AccordionBlock from '../AccordionBlock';
import Enzyme, { mount } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16/build/index';
import { render } from '@testing-library/react';

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

const errorSpy = jest.spyOn(global.console, 'error');
const warnSpy = jest.spyOn(global.console, 'warn');

const props = {
groupid: 123,
title: 'My title'
};

describe('AccordionBlock', () => {
describe('render()', () => {
beforeEach(() => {
errorSpy.mockClear();
warnSpy.mockClear();
});

it('renders with children', () => {
const reactWrapper = mount(
<AccordionBlock {...props}>
<p>lorem</p>
<p>ipsum</p>
</AccordionBlock>
);

expect(reactWrapper.find('div.accordion__block')).toHaveLength(1);
expect(errorSpy).not.toHaveBeenCalled();
expect(warnSpy).not.toHaveBeenCalled();
});
});
test('AccordionBlock render() renders with children', () => {
const container = render(
<AccordionBlock {...{
groupid: 123,
title: 'My title'
}}
>
<p>lorem</p>
<p>ipsum</p>
</AccordionBlock>
).container;
expect(container.querySelectorAll('div.accordion__block')).toHaveLength(1);
});
58 changes: 26 additions & 32 deletions client/src/components/Badge/tests/Badge-test.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,32 @@
/* global jest, describe, it, expect */
/* global jest, test, describe, it, expect */

import React from 'react';
import ReactTestUtils from 'react-dom/test-utils';
import Badge from '../Badge';
import { render } from '@testing-library/react';

describe('Badge', () => {
describe('render()', () => {
let badge = null;

it('should return null if status is empty', () => {
badge = ReactTestUtils.renderIntoDocument(
<Badge
status={null}
message=""
className=""
/>
);

expect(badge.render()).toBeNull();
});

it('should return a Bootstrap style badge when valid', () => {
badge = ReactTestUtils.renderIntoDocument(
<Badge
status="success"
message="Hello world"
className="customclass"
/>
);
test('Badge render() should return null if status is empty', () => {
const container = render(
<Badge {...{
status: null,
message: '',
className: ''
}}
/>
).container;
expect(container.querySelectorAll('.badge').length).toBe(0);
});

const result = badge.render();
expect(result.props.className).toContain('customclass');
expect(result.props.className).toContain('badge-success');
expect(result.props.children).toContain('Hello world');
});
});
test('Badge render() should return a Bootstrap style badge when valid', () => {
const container = render(
<Badge {...{
status: 'success',
message: 'Hello world',
className: 'customclass'
}}
/>
).container;
expect(container.querySelectorAll('.badge').length).toBe(1);
expect(container.querySelectorAll('.badge-success').length).toBe(1);
expect(container.querySelectorAll('.customclass').length).toBe(1);
expect(container.innerHTML).toContain('Hello world');
});
55 changes: 55 additions & 0 deletions client/src/components/Breadcrumb/tests/Breadcrumb-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* global jest, test, describe, beforeEach, it, expect */

import React from 'react';
import { Component as Breadcrumb } from '../Breadcrumb';
import { render } from '@testing-library/react';

test('BreadcrumbsComponent renderBreadcrumbs() should convert the props.crumbs array into jsx to be rendered', () => {
const container = render(
<Breadcrumb {...{
crumbs: [
{ text: 'breadcrumb1', href: 'href1' },
{ text: 'breadcrumb2', href: 'href2' },
{ text: 'breadcrumb3',
href: 'href3',
icon: {
className: 'breadcrumb3icon',
onClick: jest.fn(),
},
},
]
}}
/>
).container;
const links = container.querySelectorAll('.breadcrumb__item-title');
expect(links[0].textContent).toBe('breadcrumb1');
expect(links[1].textContent).toBe('breadcrumb2');
expect(links[2].textContent).toBe('breadcrumb3');
expect(links[2].parentElement.classList).toContain('breadcrumb__item--last');
expect(container.querySelectorAll('.breadcrumb3icon')).toHaveLength(1);
});

test('BreadcrumbsComponent renderBreadcrumbs() should convert the props.crumbs array into jsx to be rendered', () => {
const container = render(<Breadcrumb/>).container;
expect(container.querySelectorAll('.breadcrumb__item')).toHaveLength(0);
});

test('BreadcrumbsComponent renderBreadcrumbs() can have multiple icons for the last crumb', () => {
const container = render(
<Breadcrumb {...{
crumbs: [
{ text: 'breadcrumb1', href: 'href1' },
{ text: 'breadcrumb2',
href: 'href2',
icons: [
{ className: 'breadcrumb2iconA', onClick: jest.fn() },
{ className: 'breadcrumb2iconB', onClick: jest.fn() }
]
},
]
}}
/>
).container;
expect(container.querySelectorAll('.breadcrumb2iconA')).toHaveLength(1);
expect(container.querySelectorAll('.breadcrumb2iconB')).toHaveLength(1);
});
76 changes: 0 additions & 76 deletions client/src/components/Breadcrumb/tests/breadcrumb-test.js

This file was deleted.

Loading

0 comments on commit 21a23fa

Please sign in to comment.