Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Niloofar Sadeghi / Refactor tests in the network-status.spec.tsx file #7230

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/src/components/popover/popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const Popover = ({
ref={hover_ref as RefObject<HTMLDivElement>}
className={classNames({ 'dc-popover__wrapper': relative_render })}
onClick={onClick}
data-testid='dt_popover_wrapper'
>
{relative_render && (
<div className='dc-popover__container' style={{ zIndex }}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,56 +1,48 @@
// TODO refactor old tests in this component
import React from 'react';
import { Popover } from '@deriv/components';
import { NetworkStatus } from '../network-status.jsx';
import userEvent from '@testing-library/user-event';
import { render, screen } from '@testing-library/react';
import { NetworkStatus } from '../network-status.jsx';

const MockNetworkStatus = ({ is_mobile = true }) => (
<NetworkStatus status={{ class: 'offline' }} is_mobile={is_mobile} />
);

describe('network-status component', () => {
it('should has "network-status__wrapper--is-mobile" class when the "is_mobile" property is true', () => {
render(<MockNetworkStatus />);
const div_element = screen.getByTestId('dt_network_status');
expect(div_element).toHaveClass('network-status__wrapper--is-mobile');
});

const status = {
class: 'online',
tooltip: 'Online',
};
it('should has correct class based on class passed in the "status" property', () => {
const { rerender } = render(<NetworkStatus status={{ class: 'offline' }} />);
expect(screen.getByTestId('dt_network_status_element')).toHaveClass('network-status__circle--offline');

describe('NetworkStatus Component', () => {
it('should has "network-status__wrapper--is-mobile" class when the "is_mobile" is true', () => {
render(<NetworkStatus status={status} is_mobile />);
const divElement = screen.getByTestId('dt_network_status_id');
expect(divElement).toHaveClass('network-status__wrapper--is-mobile');
rerender(<NetworkStatus status={{ class: 'online' }} />);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do you need to run rerender on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank u @ali-hosseini-fs for your revision
I'm passing different props in each render because of that I rerender the component

expect(screen.getByTestId('dt_network_status_element')).toHaveClass('network-status__circle--online');

rerender(<NetworkStatus status={{ class: 'blinker' }} />);
expect(screen.getByTestId('dt_network_status_element')).toHaveClass('network-status__circle--blinker');
});

it('should contain "Popover" with default message when "status.tooltip" is empty', () => {
render(<MockNetworkStatus is_mobile={false} />);
const popover_wrapper = screen.getByTestId('dt_popover_wrapper');
userEvent.hover(popover_wrapper);
const network_status = screen.getByText(/connecting to server/i);
expect(network_status).toBeInTheDocument();
});

// it('should render one <NetworkStatus /> component', () => {
// const wrapper = shallow(<NetworkStatus status={status} />);
// expect(wrapper).toHaveLength(1);
// });
// it('should have correct class based on class passed in status', () => {
// const wrapper = shallow(<NetworkStatus status={status} />);
// expect(wrapper.find('.network-status__circle--online').exists()).toBe(true);
// wrapper.setProps({ status: { class: 'offline' } });
// expect(wrapper.find('.network-status__circle--offline').exists()).toBe(true);
// wrapper.setProps({ status: { class: 'blinker' } });
// expect(wrapper.find('.network-status__circle--blinker').exists()).toBe(true);
// });
// it('should contain Tooltip message passed in status', () => {
// const wrapper = shallow(<NetworkStatus status={status} />);
// expect(
// wrapper.contains(
// <Popover classNameBubble='network-status__tooltip' alignment='top' message='Network status: Online'>
// <div className='network-status__circle network-status__circle--online' />
// </Popover>
// )
// ).toBe(true);
// });
// it('should contain Popover with default message and div with only default class if status does not contain them', () => {
// status = {};
// const wrapper = shallow(<NetworkStatus status={status} />);
// expect(
// wrapper.contains(
// <Popover
// classNameBubble='network-status__tooltip'
// alignment='top'
// message='Network status: Connecting to server'
// >
// <div className='network-status__circle' />
// </Popover>
// )
// ).toBe(true);
// });
it('should contain "Tooltip" message passed in the status property', () => {
const status = {
class: 'online',
tooltip: 'Online',
};

render(<NetworkStatus status={status} />);
const popover_wrapper = screen.getByTestId('dt_popover_wrapper');
userEvent.hover(popover_wrapper);
const network_status = screen.getByText(/online/i);
expect(network_status).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { connect } from 'Stores/connect';
const NetworkStatus = ({ is_mobile, status }) => {
const network_status_element = (
<div
data-testid='dt_network_status_element'
className={classNames('network-status__circle', {
'network-status__circle--offline': status.class === 'offline',
'network-status__circle--online': status.class === 'online',
Expand All @@ -17,10 +18,10 @@ const NetworkStatus = ({ is_mobile, status }) => {
);
return (
<div
data-testid='dt_network_status'
className={classNames('network-status__wrapper', {
'network-status__wrapper--is-mobile': is_mobile,
})}
data-testid='dt_network_status_id'
>
{is_mobile ? (
network_status_element
Expand Down