Skip to content

Commit

Permalink
migrate testing to memory router
Browse files Browse the repository at this point in the history
  • Loading branch information
daniele-mng authored and bjoernricks committed Feb 18, 2025
1 parent 8b54854 commit 99aa85d
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 175 deletions.
79 changes: 48 additions & 31 deletions src/web/components/link/__tests__/certlink.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@
*/

import {describe, test, expect} from '@gsa/testing';
import {beforeEach} from 'vitest';
import {fireEvent, rendererWith} from 'web/utils/testing';
import {fireEvent, rendererWith, screen} from 'web/utils/testing';

import CertLink from '../certlink';

describe('CertLink tests', () => {
beforeEach(() => {
window.history.pushState({}, 'Test page', '/');
});
test('should render CertLink', () => {
const {render} = rendererWith({capabilities: true, router: true});
const {element} = render(<CertLink id="foo" type="CERT-Bund" />);
render(<CertLink id="foo" type="CERT-Bund" />);

expect(element).toHaveTextContent('foo');
const element = screen.getByText('foo');
expect(element).toHaveAttribute(
'title',
'View details of CERT-Bund Advisory foo',
Expand All @@ -26,53 +22,74 @@ describe('CertLink tests', () => {

test('should render unknown type', () => {
const {render} = rendererWith({capabilities: true, router: true});
const {element} = render(<CertLink id="foo" type="foo" />);
render(<CertLink id="foo" type="foo" />);

expect(element.querySelector('b')).toHaveTextContent('?');
const element = screen.getByText('foo');
expect(screen.getByText('?')).toBeInTheDocument();
expect(element).toHaveTextContent('foo');
});

test('should route to certbund details', () => {
const {render} = rendererWith({capabilities: true, router: true});
const {element} = render(<CertLink id="foo" type="CERT-Bund" />);
const {render} = rendererWith({
capabilities: true,
router: true,
showLocation: true,
});
render(<CertLink id="foo" type="CERT-Bund" />);

expect(window.location.pathname).toEqual('/');
const locationPathname = screen.getByTestId('location-pathname');
expect(locationPathname).toHaveTextContent('/');

fireEvent.click(element);
fireEvent.click(screen.getByText('foo'));

expect(window.location.pathname).toEqual('/certbund/foo');
expect(locationPathname).toHaveTextContent('/certbund/foo');
});

test('should route to dfncert details', () => {
const {render} = rendererWith({capabilities: true, router: true});
const {element} = render(<CertLink id="foo" type="DFN-CERT" />);
const {render} = rendererWith({
capabilities: true,
router: true,
showLocation: true,
});
render(<CertLink id="foo" type="DFN-CERT" />);

const locationPathname = screen.getByTestId('location-pathname');
expect(locationPathname).toHaveTextContent('/');

expect(window.location.pathname).toEqual('/');
fireEvent.click(screen.getByText('foo'));

fireEvent.click(element);
expect(window.location.pathname).toEqual('/dfncert/foo');
expect(locationPathname).toHaveTextContent('/dfncert/foo');
});

test('should not route to unknown type', () => {
const {render} = rendererWith({capabilities: true, router: true});
const {element} = render(<CertLink id="foo" type="foo" />);
expect(window.location.pathname).toEqual('/');
const {render} = rendererWith({
capabilities: true,
router: true,
showLocation: true,
});
render(<CertLink id="foo" type="foo" />);

const locationPathname = screen.getByTestId('location-pathname');
expect(locationPathname).toHaveTextContent('/');

fireEvent.click(element);
fireEvent.click(screen.getByText('foo'));

expect(window.location.pathname).toEqual('/');
expect(locationPathname).toHaveTextContent('/');
});

test('should not route in text mode', () => {
const {render} = rendererWith({capabilities: true, router: true});
const {element} = render(
<CertLink id="foo" textOnly={true} type="DFN-CERT" />,
);
const {render} = rendererWith({
capabilities: true,
router: true,
showLocation: true,
});
render(<CertLink id="foo" textOnly={true} type="DFN-CERT" />);

expect(window.location.pathname).toEqual('/');
const locationPathname = screen.getByTestId('location-pathname');
expect(locationPathname).toHaveTextContent('/');

fireEvent.click(element);
fireEvent.click(screen.getByText('foo'));

expect(window.location.pathname).toEqual('/');
expect(locationPathname).toHaveTextContent('/');
});
});
43 changes: 25 additions & 18 deletions src/web/components/link/__tests__/cvelink.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,58 @@
*/

import {describe, test, expect} from '@gsa/testing';
import {fireEvent, rendererWith} from 'web/utils/testing';
import {fireEvent, rendererWith, screen} from 'web/utils/testing';

import CveLink from '../cvelink';

describe('CveLink tests', () => {
beforeEach(() => {
window.history.pushState({}, 'Test page', '/');
});
test('should render CveLink', () => {
const {render} = rendererWith({capabilities: true, router: true});
const {element} = render(<CveLink id="foo" title="Foo" />);
render(<CveLink id="foo" title="Foo" />);

expect(element).toHaveTextContent('foo');
const element = screen.getByText('foo');
expect(element).toHaveAttribute('title', 'Foo');
expect(element).toHaveAttribute('href', '/cve/foo');
});

test('should not override type', () => {
const {render} = rendererWith({capabilities: true, router: true});
const {element} = render(<CveLink id="foo" title="Foo" type="bar" />);
render(<CveLink id="foo" title="Foo" type="bar" />);

expect(element).toHaveTextContent('foo');
const element = screen.getByText('foo');
expect(element).toHaveAttribute('title', 'Foo');
expect(element).toHaveAttribute('href', '/cve/foo');
});

test('should route to details', () => {
const {render} = rendererWith({capabilities: true, router: true});
const {element} = render(<CveLink id="foo" title="Foo" />);
const {render} = rendererWith({
capabilities: true,
router: true,
showLocation: true,
});
render(<CveLink id="foo" title="Foo" />);

expect(window.location.pathname).toEqual('/');
const locationPathname = screen.getByTestId('location-pathname');
expect(locationPathname).toHaveTextContent('/');

fireEvent.click(element);
fireEvent.click(screen.getByTestId('details-link'));

expect(window.location.pathname).toEqual('/cve/foo');
expect(locationPathname).toHaveTextContent('/cve/foo');
});

test('should not route to details in text mode', () => {
const {render} = rendererWith({capabilities: true, router: true});
const {element} = render(<CveLink id="foo" textOnly={true} title="Foo" />);
const {render} = rendererWith({
capabilities: true,
router: true,
showLocation: true,
});
render(<CveLink id="foo" textOnly={true} title="Foo" />);

expect(window.location.pathname).toEqual('/');
const locationPathname = screen.getByTestId('location-pathname');
expect(locationPathname).toHaveTextContent('/');

fireEvent.click(element);
fireEvent.click(screen.getByText('foo'));

expect(window.location.pathname).toEqual('/');
expect(locationPathname).toHaveTextContent('/');
});
});
71 changes: 44 additions & 27 deletions src/web/components/link/__tests__/detailslink.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,90 +5,107 @@

import {describe, test, expect} from '@gsa/testing';
import Capabilities from 'gmp/capabilities/capabilities';
import {fireEvent, rendererWith} from 'web/utils/testing';
import {fireEvent, rendererWith, screen} from 'web/utils/testing';

import DetailsLink from '../detailslink';

describe('DetailsLink tests', () => {
beforeEach(() => {
window.history.pushState({}, 'Test page', '/');
});

test('should render DetailsLink', () => {
const {render} = rendererWith({capabilities: true, router: true});
const {element} = render(
render(
<DetailsLink id="bar" title="Foo" type="foo">
Foo
</DetailsLink>,
);

expect(element).toHaveTextContent('Foo');
const element = screen.getByText('Foo');
expect(element).toHaveAttribute('title', 'Foo');
});

test('should route to url', () => {
const {render} = rendererWith({capabilities: true, router: true});

const {element} = render(
const {render} = rendererWith({
capabilities: true,
router: true,
showLocation: true,
});
render(
<DetailsLink id="1" title="Foo" type="foo">
Foo
</DetailsLink>,
);

expect(window.location.pathname).toEqual('/');
const locationPathname = screen.getByTestId('location-pathname');
expect(locationPathname).toHaveTextContent('/');

fireEvent.click(element);
fireEvent.click(screen.getByText('Foo'));

expect(window.location.pathname).toEqual('/foo/1');
expect(locationPathname).toHaveTextContent('/foo/1');
});

test('should url encode id', () => {
const {render} = rendererWith({capabilities: true, router: true});

const {element} = render(
const {render} = rendererWith({
capabilities: true,
router: true,
showLocation: true,
});
render(
<DetailsLink id="cpe:/a:jenkins:jenkins:2.141" title="Foo" type="foo">
Foo
</DetailsLink>,
);

expect(window.location.pathname).toEqual('/');
const locationPathname = screen.getByTestId('location-pathname');
expect(locationPathname).toHaveTextContent('/');

fireEvent.click(element);
fireEvent.click(screen.getByText('Foo'));

expect(window.location.pathname).toEqual(
expect(locationPathname).toHaveTextContent(
'/foo/cpe%3A%2Fa%3Ajenkins%3Ajenkins%3A2.141',
);
});

test('should not route to url in text mode', () => {
const {render} = rendererWith({capabilities: true, router: true});

const {element} = render(
const {render} = rendererWith({
capabilities: true,
router: true,
showLocation: true,
});
render(
<DetailsLink id="1" textOnly={true} title="Foo" type="foo">
Foo
</DetailsLink>,
);

expect(window.location.pathname).toEqual('/');
const locationPathname = screen.getByTestId('location-pathname');
expect(locationPathname).toHaveTextContent('/');

fireEvent.click(element);
fireEvent.click(screen.getByText('Foo'));

expect(window.location.pathname).toEqual('/');
expect(locationPathname).toHaveTextContent('/');
});

test('should not route to url without capabilities', () => {
const capabilities = new Capabilities();
const {render} = rendererWith({capabilities, router: true});

const {element} = render(
const {render} = rendererWith({
capabilities,
router: true,
showLocation: true,
});
render(
<DetailsLink id="1" title="Foo" type="foo">
Foo
</DetailsLink>,
);

expect(window.location.pathname).toEqual('/');
const locationPathname = screen.getByTestId('location-pathname');
expect(locationPathname).toHaveTextContent('/');

fireEvent.click(element);
fireEvent.click(screen.getByText('Foo'));

expect(window.location.pathname).toEqual('/');
expect(locationPathname).toHaveTextContent('/');
});
});
Loading

0 comments on commit 99aa85d

Please sign in to comment.