Skip to content

Commit

Permalink
test: introduce reusable functions for validating agent API requests …
Browse files Browse the repository at this point in the history
…and endpoint rendering in various system collector tables
  • Loading branch information
guidomodarelli committed Nov 4, 2024
1 parent 2f357df commit 924e8e6
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 221 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import React from 'react';
import { render } from '@testing-library/react';

const AGENT_000 = '000';
const AGENT_001 = '001';

export function shouldRenderTableWithCorrectEndpointForAgent(
mocked: jest.Mock,
Component: React.ComponentType<{
agent: { id: string };
soPlatform?: string;
}>,
endpoint: string,
soPlatform: string = 'linux',
) {
const { rerender } = render(
<Component agent={{ id: AGENT_000 }} soPlatform={soPlatform} />,
);

expect(mocked.mock.calls[0][0].endpoint).toContain(
`/syscollector/${AGENT_000}/${endpoint}`,
);

mocked.mockClear();

rerender(<Component agent={{ id: AGENT_001 }} soPlatform={soPlatform} />);

expect(mocked.mock.calls[0][0].endpoint).toContain(
`/syscollector/${AGENT_001}/${endpoint}`,
);
}

export function shouldFetchDataForGivenAgentId(
mocked: jest.Mock,
Component: React.ComponentType<{
agent: { id: string };
soPlatform?: string;
}>,
endpoint: string,
soPlatform: string = 'linux',
) {
const { rerender } = render(
<Component agent={{ id: AGENT_000 }} soPlatform={soPlatform} />,
);

expect(mocked.mock.calls[0][0]).toEqual('GET');
expect(mocked.mock.calls[0][1]).toEqual(
`/syscollector/${AGENT_000}/${endpoint}`,
);

mocked.mockClear();

rerender(<Component agent={{ id: AGENT_001 }} soPlatform={soPlatform} />);

expect(mocked.mock.calls[0][0]).toEqual('GET');
expect(mocked.mock.calls[0][1]).toEqual(
`/syscollector/${AGENT_001}/${endpoint}`,
);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react';
import { render } from '@testing-library/react';
import { TableWzAPI } from '../../../common/tables';
import { WzRequest } from '../../../../react-services';
import { NetworkInterfacesTable } from './network-interfaces-table';
import {
shouldFetchDataForGivenAgentId,
shouldRenderTableWithCorrectEndpointForAgent,
} from './check-endpoint-for-given-agent-id';

const AGENT_000 = '000';
const AGENT_001 = '001';

let TableWzAPIMock = TableWzAPI as jest.Mock;
let TableWzAPIMock = TableWzAPI as unknown as jest.Mock;
let apiReqMock = WzRequest.apiReq as jest.Mock;

jest.mock('../../../common/tables', () => ({
Expand All @@ -30,41 +30,19 @@ jest.mock('../../../../react-services', () => ({
}));

describe('NetworkInterfacesTable', () => {
it('should render table with correct netiface endpoint for agent either when changing agent or not', async () => {
const { rerender } = render(
<NetworkInterfacesTable agent={{ id: AGENT_000 }} />,
);

expect(TableWzAPIMock.mock.calls[0][0].endpoint).toContain(
`/syscollector/${AGENT_000}/netiface`,
);

TableWzAPIMock.mockClear();

rerender(<NetworkInterfacesTable agent={{ id: AGENT_001 }} />);

expect(TableWzAPIMock.mock.calls[0][0].endpoint).toContain(
`/syscollector/${AGENT_001}/netiface`,
it('should render table with correct netiface endpoint for agent either when changing agent or not', () => {
shouldRenderTableWithCorrectEndpointForAgent(
TableWzAPIMock,
NetworkInterfacesTable,
'netiface',
);
});

it('should fetch netiface data for given agent id either when changing agent or not', async () => {
const { rerender } = render(
<NetworkInterfacesTable agent={{ id: AGENT_000 }} />,
);

expect(apiReqMock.mock.calls[0][0]).toEqual('GET');
expect(apiReqMock.mock.calls[0][1]).toEqual(
`/syscollector/${AGENT_000}/netiface`,
);

apiReqMock.mockClear();

rerender(<NetworkInterfacesTable agent={{ id: AGENT_001 }} />);

expect(apiReqMock.mock.calls[0][0]).toEqual('GET');
expect(apiReqMock.mock.calls[0][1]).toEqual(
`/syscollector/${AGENT_001}/netiface`,
it('should fetch netiface data for given agent id either when changing agent or not', () => {
shouldFetchDataForGivenAgentId(
apiReqMock,
NetworkInterfacesTable,
'netiface',
);
});
});
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react';
import { render } from '@testing-library/react';
import { TableWzAPI } from '../../../common/tables';
import { WzRequest } from '../../../../react-services';
import { NetworkPortsTable } from './network-ports-table';
import {
shouldFetchDataForGivenAgentId,
shouldRenderTableWithCorrectEndpointForAgent,
} from './check-endpoint-for-given-agent-id';

const AGENT_000 = '000';
const AGENT_001 = '001';

let TableWzAPIMock = TableWzAPI as jest.Mock;
let TableWzAPIMock = TableWzAPI as unknown as jest.Mock;
let apiReqMock = WzRequest.apiReq as jest.Mock;

jest.mock('../../../common/tables', () => ({
Expand All @@ -34,41 +34,15 @@ jest.mock('./with-so-platform-guard', () => ({
}));

describe('NetworkPortsTable', () => {
it('should render table with correct ports endpoint for agent either when changing agent or not', async () => {
const { rerender } = render(
<NetworkPortsTable agent={{ id: AGENT_000 }} />,
);

expect(TableWzAPIMock.mock.calls[0][0].endpoint).toContain(
`/syscollector/${AGENT_000}/ports`,
);

TableWzAPIMock.mockClear();

rerender(<NetworkPortsTable agent={{ id: AGENT_001 }} />);

expect(TableWzAPIMock.mock.calls[0][0].endpoint).toContain(
`/syscollector/${AGENT_001}/ports`,
it('should render table with correct ports endpoint for agent either when changing agent or not', () => {
shouldRenderTableWithCorrectEndpointForAgent(
TableWzAPIMock,
NetworkPortsTable,
'ports',
);
});

it('should fetch ports data for given agent id either when changing agent or not', async () => {
const { rerender } = render(
<NetworkPortsTable agent={{ id: AGENT_000 }} />,
);

expect(apiReqMock.mock.calls[0][0]).toEqual('GET');
expect(apiReqMock.mock.calls[0][1]).toEqual(
`/syscollector/${AGENT_000}/ports`,
);

apiReqMock.mockClear();

rerender(<NetworkPortsTable agent={{ id: AGENT_001 }} />);

expect(apiReqMock.mock.calls[0][0]).toEqual('GET');
expect(apiReqMock.mock.calls[0][1]).toEqual(
`/syscollector/${AGENT_001}/ports`,
);
it('should fetch ports data for given agent id either when changing agent or not', () => {
shouldFetchDataForGivenAgentId(apiReqMock, NetworkPortsTable, 'ports');
});
});
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react';
import { render } from '@testing-library/react';
import { TableWzAPI } from '../../../common/tables';
import { WzRequest } from '../../../../react-services';
import { NetworkSettingsTable } from './network-settings-table';
import {
shouldFetchDataForGivenAgentId,
shouldRenderTableWithCorrectEndpointForAgent,
} from './check-endpoint-for-given-agent-id';

const AGENT_000 = '000';
const AGENT_001 = '001';

let TableWzAPIMock = TableWzAPI as jest.Mock;
let TableWzAPIMock = TableWzAPI as unknown as jest.Mock;
let apiReqMock = WzRequest.apiReq as jest.Mock;

jest.mock('../../../common/tables', () => ({
Expand All @@ -30,41 +30,15 @@ jest.mock('../../../../react-services', () => ({
}));

describe('NetworkSettingsTable', () => {
it('should render table with correct netaddr endpoint for agent either when changing agent or not', async () => {
const { rerender } = render(
<NetworkSettingsTable agent={{ id: AGENT_000 }} />,
);

expect(TableWzAPIMock.mock.calls[0][0].endpoint).toContain(
`/syscollector/${AGENT_000}/netaddr`,
);

TableWzAPIMock.mockClear();

rerender(<NetworkSettingsTable agent={{ id: AGENT_001 }} />);

expect(TableWzAPIMock.mock.calls[0][0].endpoint).toContain(
`/syscollector/${AGENT_001}/netaddr`,
it('should render table with correct netaddr endpoint for agent either when changing agent or not', () => {
shouldRenderTableWithCorrectEndpointForAgent(
TableWzAPIMock,
NetworkSettingsTable,
'netaddr',
);
});

it('should fetch netaddr data for given agent id either when changing agent or not', async () => {
const { rerender } = render(
<NetworkSettingsTable agent={{ id: AGENT_000 }} />,
);

expect(apiReqMock.mock.calls[0][0]).toEqual('GET');
expect(apiReqMock.mock.calls[0][1]).toEqual(
`/syscollector/${AGENT_000}/netaddr`,
);

apiReqMock.mockClear();

rerender(<NetworkSettingsTable agent={{ id: AGENT_001 }} />);

expect(apiReqMock.mock.calls[0][0]).toEqual('GET');
expect(apiReqMock.mock.calls[0][1]).toEqual(
`/syscollector/${AGENT_001}/netaddr`,
);
it('should fetch netaddr data for given agent id either when changing agent or not', () => {
shouldFetchDataForGivenAgentId(apiReqMock, NetworkSettingsTable, 'netaddr');
});
});
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react';
import { render } from '@testing-library/react';
import { PackagesTable } from './packages-table';
import { TableWzAPI } from '../../../common/tables';
import { WzRequest } from '../../../../react-services';
import { PackagesTable } from './packages-table';
import {
shouldFetchDataForGivenAgentId,
shouldRenderTableWithCorrectEndpointForAgent,
} from './check-endpoint-for-given-agent-id';

const AGENT_000 = '000';
const AGENT_001 = '001';

let TableWzAPIMock = TableWzAPI as jest.Mock;
let TableWzAPIMock = TableWzAPI as unknown as jest.Mock;
let apiReqMock = WzRequest.apiReq as jest.Mock;

jest.mock('../../../common/tables', () => ({
Expand All @@ -34,37 +34,15 @@ jest.mock('./with-so-platform-guard', () => ({
}));

describe('PackagesTable', () => {
it('should render table with correct packages endpoint for agent either when changing agent or not', async () => {
const { rerender } = render(<PackagesTable agent={{ id: AGENT_000 }} />);

expect(TableWzAPIMock.mock.calls[0][0].endpoint).toContain(
`/syscollector/${AGENT_000}/packages`,
);

TableWzAPIMock.mockClear();

rerender(<PackagesTable agent={{ id: AGENT_001 }} />);

expect(TableWzAPIMock.mock.calls[0][0].endpoint).toContain(
`/syscollector/${AGENT_001}/packages`,
it('should render table with correct packages endpoint for agent either when changing agent or not', () => {
shouldRenderTableWithCorrectEndpointForAgent(
TableWzAPIMock,
PackagesTable,
'packages',
);
});

it('should fetch packages data for given agent id either when changing agent or not', async () => {
const { rerender } = render(<PackagesTable agent={{ id: AGENT_000 }} />);

expect(apiReqMock.mock.calls[0][0]).toEqual('GET');
expect(apiReqMock.mock.calls[0][1]).toEqual(
`/syscollector/${AGENT_000}/packages`,
);

apiReqMock.mockClear();

rerender(<PackagesTable agent={{ id: AGENT_001 }} />);

expect(apiReqMock.mock.calls[0][0]).toEqual('GET');
expect(apiReqMock.mock.calls[0][1]).toEqual(
`/syscollector/${AGENT_001}/packages`,
);
it('should fetch packages data for given agent id either when changing agent or not', () => {
shouldFetchDataForGivenAgentId(apiReqMock, PackagesTable, 'packages');
});
});
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react';
import { render } from '@testing-library/react';
import { TableWzAPI } from '../../../common/tables';
import { WzRequest } from '../../../../react-services';
import { ProcessesTable } from './processes-table';
import {
shouldFetchDataForGivenAgentId,
shouldRenderTableWithCorrectEndpointForAgent,
} from './check-endpoint-for-given-agent-id';

const AGENT_000 = '000';
const AGENT_001 = '001';

let TableWzAPIMock = TableWzAPI as jest.Mock;
let TableWzAPIMock = TableWzAPI as unknown as jest.Mock;
let apiReqMock = WzRequest.apiReq as jest.Mock;

jest.mock('../../../common/tables', () => ({
Expand All @@ -34,37 +34,15 @@ jest.mock('./with-so-platform-guard', () => ({
}));

describe('ProcessesTable', () => {
it('should render table with correct processes endpoint for agent either when changing agent or not', async () => {
const { rerender } = render(<ProcessesTable agent={{ id: AGENT_000 }} />);

expect(TableWzAPIMock.mock.calls[0][0].endpoint).toContain(
`/syscollector/${AGENT_000}/processes`,
);

TableWzAPIMock.mockClear();

rerender(<ProcessesTable agent={{ id: AGENT_001 }} />);

expect(TableWzAPIMock.mock.calls[0][0].endpoint).toContain(
`/syscollector/${AGENT_001}/processes`,
it('should render table with correct processes endpoint for agent either when changing agent or not', () => {
shouldRenderTableWithCorrectEndpointForAgent(
TableWzAPIMock,
ProcessesTable,
'processes',
);
});

it('should fetch processes data for given agent id either when changing agent or not', async () => {
const { rerender } = render(<ProcessesTable agent={{ id: AGENT_000 }} />);

expect(apiReqMock.mock.calls[0][0]).toEqual('GET');
expect(apiReqMock.mock.calls[0][1]).toEqual(
`/syscollector/${AGENT_000}/processes`,
);

apiReqMock.mockClear();

rerender(<ProcessesTable agent={{ id: AGENT_001 }} />);

expect(apiReqMock.mock.calls[0][0]).toEqual('GET');
expect(apiReqMock.mock.calls[0][1]).toEqual(
`/syscollector/${AGENT_001}/processes`,
);
it('should fetch processes data for given agent id either when changing agent or not', () => {
shouldFetchDataForGivenAgentId(apiReqMock, ProcessesTable, 'processes');
});
});
Loading

0 comments on commit 924e8e6

Please sign in to comment.