Skip to content

Commit

Permalink
Fix unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed Mar 17, 2022
1 parent 707a013 commit 8189ca8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ jest.mock('../../../hooks', () => {
return {
...jest.requireActual('../../../hooks'),
useGetAgentPolicies: jest.fn(),
useGetOutputs: jest.fn().mockResolvedValue({
data: [],
isLoading: false,
}),
sendGetOneAgentPolicy: jest.fn().mockResolvedValue({
data: { item: { id: 'policy-1' } },
}),
useFleetStatus: jest.fn().mockReturnValue({ isReady: true } as any),
sendGetFleetStatus: jest
.fn()
Expand All @@ -34,19 +41,21 @@ describe('step select agent policy', () => {
let testRenderer: TestRenderer;
let renderResult: ReturnType<typeof testRenderer.render>;
const mockSetHasAgentPolicyError = jest.fn();
const updateAgentPolicyMock = jest.fn();
const render = () =>
(renderResult = testRenderer.render(
<StepSelectAgentPolicy
packageInfo={{ name: 'apache' } as any}
agentPolicy={undefined}
updateAgentPolicy={jest.fn()}
updateAgentPolicy={updateAgentPolicyMock}
setHasAgentPolicyError={mockSetHasAgentPolicyError}
selectedAgentPolicyId={undefined}
/>
));

beforeEach(() => {
testRenderer = createFleetTestRendererMock();
updateAgentPolicyMock.mockReset();
});

test('should not select agent policy by default if multiple exists', async () => {
Expand All @@ -68,7 +77,6 @@ describe('step select agent policy', () => {
const select = renderResult.container.querySelector('[data-test-subj="agentPolicySelect"]');
expect((select as any)?.value).toEqual('');

expect(renderResult.getAllByRole('option').length).toBe(2);
expect(renderResult.getByText('An agent policy is required.')).toBeVisible();
});
});
Expand All @@ -82,10 +90,10 @@ describe('step select agent policy', () => {
} as any);

render();

await act(async () => {}); // Needed as updateAgentPolicy is called after multiple useEffect
await act(async () => {
const select = renderResult.container.querySelector('[data-test-subj="agentPolicySelect"]');
expect((select as any)?.value).toEqual('policy-1');
expect(updateAgentPolicyMock).toBeCalled();
expect(updateAgentPolicyMock).toBeCalledWith({ id: 'policy-1' });
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ function useAgentPoliciesOptions(packageInfo?: PackageInfo) {
const isAPMPackageAndDataOutputIsLogstash =
packageInfo.name === FLEET_APM_PACKAGE &&
getDataOutputForPolicy(agentConf)?.type === outputType.Logstash;

return {
inputDisplay: (
<>
Expand Down Expand Up @@ -204,7 +205,10 @@ export const StepSelectAgentPolicy: React.FunctionComponent<{
} else setHasAgentPolicyError(true);
}, [selectedAgentPolicyError, selectedPolicyId, setHasAgentPolicyError]);

const onChange = useCallback((newValue: string) => setSelectedPolicyId(newValue), []);
const onChange = useCallback(
(newValue: string) => setSelectedPolicyId(newValue === '' ? undefined : newValue),
[]
);

// Display agent policies list error if there is one
if (agentPoliciesError) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ jest.mock('../../../hooks', () => {
return {
...jest.requireActual('../../../hooks'),
useGetAgentPolicies: jest.fn(),
useGetOutputs: jest.fn().mockResolvedValue({
data: [],
isLoading: false,
}),
sendGetOneAgentPolicy: jest.fn().mockResolvedValue({
data: { item: { id: 'policy-1', name: 'Agent policy 1' } },
}),
};
});

Expand Down Expand Up @@ -110,7 +117,7 @@ describe('StepSelectHosts', () => {
);
});

it('should display dropdown with agent policy selected when Existing hosts selected', () => {
it('should display dropdown with agent policy selected when Existing hosts selected', async () => {
(useGetAgentPolicies as jest.MockedFunction<any>).mockReturnValue({
data: {
items: [{ id: 'agent-policy-1', name: 'Agent policy 1', namespace: 'default' }],
Expand All @@ -126,8 +133,9 @@ describe('StepSelectHosts', () => {
fireEvent.click(renderResult.getByText('Existing hosts').closest('button')!);
});

expect(renderResult.getAllByRole('option').length).toEqual(1);
expect(renderResult.getByText('Agent policy 1').closest('select')).toBeInTheDocument();
expect(
renderResult.container.querySelector('[data-test-subj="agentPolicySelect"]')?.textContent
).toEqual('Agent policy 1');
});

it('should display dropdown without preselected value when Existing hosts selected with mulitple agent policies', () => {
Expand All @@ -149,7 +157,6 @@ describe('StepSelectHosts', () => {
fireEvent.click(renderResult.getByText('Existing hosts').closest('button')!);
});

expect(renderResult.getAllByRole('option').length).toEqual(2);
waitFor(() => {
expect(renderResult.getByText('An agent policy is required.')).toBeInTheDocument();
});
Expand Down

0 comments on commit 8189ca8

Please sign in to comment.