Skip to content

Commit

Permalink
Niloofar Sadeghi / Refactor tests in the network-status.spec.tsx file (
Browse files Browse the repository at this point in the history
…#7230)

* test: writting test for network-status component

* refactor: improve namings

* refactor: improve tests

* build: remove extra packages from core

Co-authored-by: Niloofar Sadeghi <niloofar.sadeghi@firstsource.tech>
  • Loading branch information
niloofar-deriv and Niloofar Sadeghi committed Jan 9, 2023
1 parent fe8cc3f commit 773ece1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 50 deletions.
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 @@ -61,6 +61,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' }} />);
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

0 comments on commit 773ece1

Please sign in to comment.