Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,82 +1,83 @@
import React from 'react';
import { shallow } from '@theforeman/test';
import { render, screen } from '@testing-library/react';
import { MemoryRouter } from 'react-router-dom';
import '@testing-library/jest-dom';
import Toast from '../Toast';

jest.mock('foremanReact/common/I18n', () => ({
translate: jest.fn(str => str),
}));

const renderToast = (props = {}) =>
render(
<MemoryRouter>
<Toast syncHosts={5} disconnectHosts={3} {...props} />
</MemoryRouter>
);

const omittedText =
'Excluded from upload to console.redhat.com Inventory service because host_registration_insights_inventory parameter value is false:';

describe('Toast', () => {
it('renders with all three status counts including user_omitted', () => {
const wrapper = shallow(
<Toast syncHosts={5} disconnectHosts={3} userOmittedHosts={2} />
);

const links = wrapper.find('HostsWithStatusLink');
expect(links).toHaveLength(3);
renderToast({ userOmittedHosts: 2 });

// Check the children (numbers) of each link
expect(
links
.at(0)
.children()
.text()
).toBe('5');
expect(
links
.at(1)
.children()
.text()
).toBe('3');
expect(
links
.at(2)
.children()
.text()
).toBe('2');
expect(screen.getByRole('link', { name: '10' })).toHaveAttribute(
'href',
'/new/hosts?search=set%3F+subscription_uuid&page=1'
);
expect(screen.getByRole('link', { name: '5' })).toHaveAttribute(
'href',
'/new/hosts?search=insights_inventory_sync_status+%3D+sync&page=1'
);
expect(screen.getByRole('link', { name: '3' })).toHaveAttribute(
'href',
'/new/hosts?search=insights_inventory_sync_status+%3D+disconnect&page=1'
);
expect(screen.getByRole('link', { name: '2' })).toHaveAttribute(
'href',
'/new/hosts?search=insights_inventory_sync_status+%3D+user_omitted&page=1'
);
expect(screen.getByText(omittedText, { exact: false })).toBeInTheDocument();
});

it('does not render user_omitted section when count is 0', () => {
const wrapper = shallow(
<Toast syncHosts={5} disconnectHosts={3} userOmittedHosts={0} />
);

// Should have only 2 HostsWithStatusLink components (sync and disconnect)
const links = wrapper.find('HostsWithStatusLink');
expect(links).toHaveLength(2);
renderToast({ userOmittedHosts: 0 });

// Should not contain the user_omitted explanation text
expect(wrapper.text()).not.toContain(
'host_registration_insights_inventory parameter value is false'
);
expect(screen.getByRole('link', { name: '8' })).toBeInTheDocument();
expect(screen.getByRole('link', { name: '5' })).toBeInTheDocument();
expect(screen.getByRole('link', { name: '3' })).toBeInTheDocument();
expect(screen.queryByRole('link', { name: '2' })).not.toBeInTheDocument();
expect(
screen.queryByText(omittedText, { exact: false })
).not.toBeInTheDocument();
});

it('renders without crashing when userOmittedHosts is not provided (default)', () => {
const wrapper = shallow(<Toast syncHosts={5} disconnectHosts={3} />);

// Should use default value of 0, so only 2 links
const links = wrapper.find('HostsWithStatusLink');
expect(links).toHaveLength(2);
renderToast();

// Verify the count values
expect(screen.getByRole('link', { name: '8' })).toBeInTheDocument();
expect(screen.getByRole('link', { name: '5' })).toBeInTheDocument();
expect(screen.getByRole('link', { name: '3' })).toBeInTheDocument();
expect(
links
.at(0)
.children()
.text()
).toBe('5');
expect(
links
.at(1)
.children()
.text()
).toBe('3');
screen.queryByText(omittedText, { exact: false })
).not.toBeInTheDocument();
});

it('renders correct status links for each category', () => {
const wrapper = shallow(
<Toast syncHosts={5} disconnectHosts={3} userOmittedHosts={2} />
);
renderToast({ userOmittedHosts: 2 });

const links = wrapper.find('HostsWithStatusLink');
expect(links.at(0).prop('statusName')).toBe('sync');
expect(links.at(1).prop('statusName')).toBe('disconnect');
expect(links.at(2).prop('statusName')).toBe('user_omitted');
expect(screen.getByRole('link', { name: '5' })).toHaveAttribute(
'href',
expect.stringContaining('sync')
);
expect(screen.getByRole('link', { name: '3' })).toHaveAttribute(
'href',
expect.stringContaining('disconnect')
);
expect(screen.getByRole('link', { name: '2' })).toHaveAttribute(
'href',
expect.stringContaining('user_omitted')
);
});
});
Loading