Skip to content
Draft
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
40 changes: 29 additions & 11 deletions webpack/InsightsCloudSync/InsightsCloudSync.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { testComponentSnapshotsWithFixtures } from '@theforeman/test';
import React from 'react';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';
import { noop } from 'foremanReact/common/helpers';
import InsightsCloudSync from './InsightsCloudSync';

// Mock webpack share scopes for module federation
Copy link
Member

Choose a reason for hiding this comment

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

@MariaAga We need to do it in the core

Copy link
Member

Choose a reason for hiding this comment

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

Probably, why was it needed for the test?

global.__webpack_share_scopes__ = { default: {} };

jest.mock('foremanReact/Root/Context/ForemanContext', () => ({
useForemanContext: () => ({
metadata: {
Expand All @@ -12,14 +17,27 @@ jest.mock('foremanReact/Root/Context/ForemanContext', () => ({
}),
}));

const fixtures = {
render: {
status: 'RESOLVED',
syncInsights: noop,
fetchInsights: noop,
query: '',
},
};
jest.mock('foremanReact/components/PF4/TableIndexPage/Table/TableHooks', () => ({
useBulkSelect: () => ({
selectedCount: 0,
selectAll: () => {},
selectNone: () => {},
selectOne: () => {},
isSelected: () => false,
selectedResults: [],
}),
}));

describe('InsightsCloudSync', () =>
testComponentSnapshotsWithFixtures(InsightsCloudSync, fixtures));
describe('InsightsCloudSync', () => {
it('should render with props', () => {
const { container } = render(
<InsightsCloudSync
status="RESOLVED"
syncInsights={noop}
fetchInsights={noop}
query=""
/>
);
expect(container.querySelector('.insights-cloud-sync')).toBeInTheDocument();
});
});
26 changes: 19 additions & 7 deletions webpack/InsightsHostDetailsTab/__tests__/InsightsTab.test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { testComponentSnapshotsWithFixtures } from '@theforeman/test';
import React from 'react';
import { render, screen } from '@testing-library/react';
import '@testing-library/jest-dom';

import InsightsTab from '../InsightsTab';
import { props } from './InsightsTab.fixtures';

const fixtures = {
'render with props': props,
};

describe('InsightsTab', () => {
describe('rendering', () =>
testComponentSnapshotsWithFixtures(InsightsTab, fixtures));
describe('rendering', () => {
it('should render with props', () => {
render(<InsightsTab {...props} />);
expect(screen.getByText('Recommendations')).toBeInTheDocument();
});

it('should render empty state when hits array is empty', () => {
render(<InsightsTab hostID={1} hits={[]} />);
expect(screen.getByText('No recommendations were found for this host!')).toBeInTheDocument();
});

it('should use default props when hits is not provided', () => {
render(<InsightsTab hostID={1} />);
expect(screen.getByText('No recommendations were found for this host!')).toBeInTheDocument();
});
});
});
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@
import { testActionSnapshotWithFixtures } from '@theforeman/test';
import { API } from 'foremanReact/redux/API';
import { fetchHits } from '../InsightsTabActions';
import { hostID, hits } from './InsightsTab.fixtures';
import {
INSIGHTS_HITS_REQUEST,
INSIGHTS_HITS_SUCCESS,
INSIGHTS_HITS_FAILURE,
} from '../InsightsTabConstants';

jest.mock('foremanReact/redux/API');
API.get.mockImplementation(async () => ({ data: { hits } }));

const fixtures = {
'should fetchHits': () => fetchHits(hostID),
'should fetchHits with error': () => {
describe('InsightsTab actions', () => {
it('should fetchHits', async () => {
API.get.mockImplementation(async () => ({ data: { hits } }));
const dispatch = jest.fn();

await fetchHits(hostID)(dispatch);

expect(dispatch).toHaveBeenCalledWith(
expect.objectContaining({
type: INSIGHTS_HITS_REQUEST,
})
);
expect(dispatch).toHaveBeenCalledWith(
expect.objectContaining({
type: INSIGHTS_HITS_SUCCESS,
payload: { hits },
})
);
});

it('should fetchHits with error', async () => {
API.get.mockImplementationOnce(() =>
Promise.reject(new Error('Network error!'))
);
return fetchHits(hostID);
},
};
const dispatch = jest.fn();

await fetchHits(hostID)(dispatch);

describe('InsightsTab actions', () => testActionSnapshotWithFixtures(fixtures));
expect(dispatch).toHaveBeenCalledWith(
expect.objectContaining({
type: INSIGHTS_HITS_REQUEST,
})
);
// Error handling dispatches a toast notification, not INSIGHTS_HITS_FAILURE
expect(dispatch).toHaveBeenCalledWith(
expect.objectContaining({
type: 'TOASTS_ADD',
})
);
});
});
70 changes: 56 additions & 14 deletions webpack/InsightsHostDetailsTab/__tests__/InsightsTabReducer.test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,68 @@
import { testReducerSnapshotWithFixtures } from '@theforeman/test';
import Immutable from 'seamless-immutable';
import reducer from '../InsightsTabReducer';
import { hits } from './InsightsTab.fixtures';
import {
INSIGHTS_HITS_REQUEST,
INSIGHTS_HITS_SUCCESS,
} from '../InsightsTabConstants';

const fixtures = {
'should return the initial state': {},
'should handle INSIGHTS_HITS_REQUEST': {
action: {
describe('AccountList reducer', () => {
it('should return the initial state', () => {
const initialState = Immutable({ hits: [] });
expect(reducer(undefined, {})).toEqual(initialState);
});

it('should handle INSIGHTS_HITS_REQUEST', () => {
const initialState = Immutable({ hits: [] });
const action = {
type: INSIGHTS_HITS_REQUEST,
payload: {},
},
},
'should handle INSIGHTS_HITS_SUCCESS': {
action: {
};
const newState = reducer(initialState, action);
expect(newState).toHaveProperty('hits');
expect(newState.hits).toEqual([]);
});

it('should handle INSIGHTS_HITS_SUCCESS', () => {
const initialState = Immutable({ hits: [] });
const action = {
type: INSIGHTS_HITS_SUCCESS,
payload: { hits },
},
},
};
};
const newState = reducer(initialState, action);
expect(newState.hits).toEqual(hits);
});

it('should handle INSIGHTS_HITS_SUCCESS with missing hits in payload', () => {
const initialState = Immutable({ hits: [] });
const action = {
type: INSIGHTS_HITS_SUCCESS,
payload: {},
};
const newState = reducer(initialState, action);
expect(newState).toHaveProperty('hits');
expect(newState.hits).toBeUndefined();
});

it('should handle INSIGHTS_HITS_SUCCESS with undefined hits', () => {
const initialState = Immutable({ hits: [] });
const action = {
type: INSIGHTS_HITS_SUCCESS,
payload: { hits: undefined },
};
const newState = reducer(initialState, action);
expect(newState).toHaveProperty('hits');
expect(newState.hits).toBeUndefined();
});

describe('AccountList reducer', () =>
testReducerSnapshotWithFixtures(reducer, fixtures));
it('should handle INSIGHTS_HITS_SUCCESS with null hits', () => {
const initialState = Immutable({ hits: [] });
const action = {
type: INSIGHTS_HITS_SUCCESS,
payload: { hits: null },
};
const newState = reducer(initialState, action);
expect(newState).toHaveProperty('hits');
expect(newState.hits).toBeNull();
});
});
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { testSelectorsSnapshotWithFixtures } from '@theforeman/test';
import { hostInsightsStateWrapper } from '../../ForemanRhCloudTestHelpers';
import { hits } from './InsightsTab.fixtures';
import { selectHits } from '../InsightsTabSelectors';

const state = hostInsightsStateWrapper({ hits });

const fixtures = {
'should return hits': () => selectHits(state),
};

describe('InsightsTab selectors', () =>
testSelectorsSnapshotWithFixtures(fixtures));
describe('InsightsTab selectors', () => {
it('should return hits', () => {
expect(selectHits(state)).toEqual(hits);
});
});

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading