Skip to content

Commit

Permalink
fix: remove enzyme for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnvente committed Jan 22, 2024
1 parent 3eebebb commit 9cd8d8b
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`BlockLink Component snapshot 1`] = `
<div>
<div
class="link-container d-flex row p-4 rounded border border-gray-400 mx-4 mt-3"
>
<div
class="col-10"
>
<p
class="text-left"
/>
<p
class="h2 w-20 title"
>
Some Path
</p>
</div>
<div
class="col-2"
>
<button
class="d-flex justify-content-center align-items-center"
data-testid="close-link-button"
variant="tertiary"
>
 
</button>
</div>
</div>
</div>
`;
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
import React from 'react';
import Enzyme, { shallow } from 'enzyme';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
import { render, fireEvent, screen } from '@testing-library/react';
import '@testing-library/jest-dom';

import { formatBlockPath } from '../utils';
import BlockLink from './index';

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

describe('BlockLink Component', () => {
const defaultProps = {
path: 'Some Path',
onCloseLink: jest.fn(),
};

const renderComponent = (overrideProps = {}) => render(
<BlockLink {...defaultProps} {...overrideProps} />,
);

test('renders with default props', () => {
const wrapper = shallow(<BlockLink {...defaultProps} />);
expect(wrapper.text()).toContain('Some Path');
renderComponent();
expect(screen.getByText('Some Path')).toBeInTheDocument();
});

test('snapshot', () => {
const { container } = renderComponent();
expect(container).toMatchSnapshot();
});

test('renders correctly with custom path', () => {
const customProps = {
...defaultProps,
path: 'Custom Path',
};
const wrapper = shallow(<BlockLink {...customProps} />);
expect(wrapper.text()).toContain('Custom Path');
renderComponent(customProps);
expect(screen.getByText('Custom Path')).toBeInTheDocument();
});

test('calls onCloseLink when the button is clicked', () => {
const wrapper = shallow(<BlockLink {...defaultProps} />);
wrapper.find({ 'data-testid': 'close-link-button' }).simulate('click');
renderComponent();
fireEvent.click(screen.getByTestId('close-link-button'));
expect(defaultProps.onCloseLink).toHaveBeenCalledTimes(1);
});

Expand All @@ -37,10 +45,11 @@ describe('BlockLink Component', () => {
path: 'Root Section / Child 1',
onCloseLink: jest.fn(),
};
const wrapper = shallow(<BlockLink {...customProps} />);

renderComponent(customProps);
const { title, subTitle } = formatBlockPath(customProps.path);

expect(wrapper.text()).toContain(title);
expect(wrapper.text()).toContain(subTitle);
expect(screen.getByText(title)).toBeInTheDocument();
expect(screen.getByText(subTitle)).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
@@ -1,42 +1,22 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`FilteredBlock Component snapshot 1`] = `
<FilteredBlock
block={
Object {
"blockId": "edx_block-1",
"children": Array [
"block-children-1",
"block-children-2",
],
"displayName": "Any display name",
"id": "block-key",
"legacyWebUrl": "http://localhost/legacy",
"lmsWebUrl": "http://localhost/weburl",
"path": "Path / To / Block 1",
"studentViewUrl": "http://localhost/studentview",
"type": "sequential",
}
}
onBlockFilterClick={[MockFunction]}
>
<Button
className="d-flex flex-column w-100 align-items-start p-3"
<div>
<button
class="d-flex flex-column w-100 align-items-start p-3 btn btn-tertiary"
data-testid="filter-block-item"
key="filtered_block_block-key"
onClick={[Function]}
variant="tertiary"
type="button"
>
<span
className="h5 text-left"
class="h5 text-left"
>
Path / To
</span>
<span
className="h3"
class="h3"
>
Block 1
</span>
</Button>
</FilteredBlock>
</button>
</div>
`;
Original file line number Diff line number Diff line change
@@ -1,51 +1,56 @@
import React from 'react';
import Enzyme, { mount } from 'enzyme';
import Adapter from '@wojtekmaj/enzyme-adapter-react-17';
import { render, fireEvent, screen } from '@testing-library/react';
import '@testing-library/jest-dom';

import FilterBlock from '.';

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

const mockBlock = {
id: 'block-key',
blockId: 'edx_block-1',
lmsWebUrl: 'http://localhost/weburl',
legacyWebUrl: 'http://localhost/legacy',
studentViewUrl: 'http://localhost/studentview',
type: 'sequential',
displayName: 'Any display name',
path: 'Path / To / Block 1',
children: ['block-children-1', 'block-children-2'],
};
jest.unmock('@edx/frontend-platform/i18n');
jest.unmock('@edx/paragon');
jest.unmock('@edx/paragon/icons');

describe('FilteredBlock Component', () => {
const mockOnBlockFilterClick = jest.fn();

const setup = (
block = mockBlock,
onBlockFilterClick = mockOnBlockFilterClick,
) => mount(<FilterBlock block={block} onBlockFilterClick={onBlockFilterClick} />);
const mockBlock = {
id: 'block-key',
blockId: 'edx_block-1',
lmsWebUrl: 'http://localhost/weburl',
legacyWebUrl: 'http://localhost/legacy',
studentViewUrl: 'http://localhost/studentview',
type: 'sequential',
displayName: 'Any display name',
path: 'Path / To / Block 1',
children: ['block-children-1', 'block-children-2'],
};

const renderComponent = (overrideProps = {}) => render(
<FilterBlock
block={mockBlock}
onBlockFilterClick={mockOnBlockFilterClick}
{...overrideProps}
/>,
);

test('renders without crashing', () => {
const wrapper = setup();
expect(wrapper.exists()).toBeTruthy();
const { container } = renderComponent();
expect(container).toBeTruthy();
});

test('snapshot', () => {
const wrapper = setup();
expect(wrapper).toMatchSnapshot();
const { container } = renderComponent();
expect(container).toMatchSnapshot();
});

test('calls onBlockFilterClick when the button is clicked', () => {
const wrapper = setup();
const button = wrapper.find('Button[data-testid="filter-block-item"]');
button.simulate('click');
const { getByTestId } = renderComponent();
const button = getByTestId('filter-block-item');
fireEvent.click(button);
expect(mockOnBlockFilterClick).toHaveBeenCalledWith(mockBlock);
});

test('displays the block title and subtitle', () => {
const wrapper = setup();
expect(wrapper.find('.h5').text()).toContain('Path / To');
expect(wrapper.find('.h3').text()).toContain('Block 1');
renderComponent();
expect(screen.getByText('Path / To')).toBeInTheDocument();
expect(screen.getByText('Block 1')).toBeInTheDocument();
});
});

0 comments on commit 9cd8d8b

Please sign in to comment.