Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nchaulet committed Jun 30, 2022
1 parent dcff25a commit 52624a7
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { useRouteMatch } from 'react-router-dom';

import { createFleetTestRendererMock } from '../../../../../../mock';

import { usePackagePolicyEditorPageUrl } from './datastream_hooks';

const mockedUseRouteMatch = useRouteMatch as jest.MockedFunction<typeof useRouteMatch>;

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useRouteMatch: jest.fn(),
}));

describe('usePackagePolicyEditorPageUrl', () => {
it('should render an integration url if no policy id is provided', () => {
const renderer = createFleetTestRendererMock();
mockedUseRouteMatch.mockReturnValue({
params: { packagePolicyId: 'test-package-policy-id' },
} as any);
const { result } = renderer.renderHook(() => usePackagePolicyEditorPageUrl());
expect(result.current).toBe('/mock/app/integrations/edit-integration/test-package-policy-id');
});

it('should render a fleet url if a policy id is provided', () => {
const renderer = createFleetTestRendererMock();
mockedUseRouteMatch.mockReturnValue({
params: { policyId: 'policy1', packagePolicyId: 'test-package-policy-id' },
} as any);
const { result } = renderer.renderHook(() => usePackagePolicyEditorPageUrl());
expect(result.current).toBe(
'/mock/app/fleet/policies/policy1/edit-integration/test-package-policy-id'
);
});

it('should add datastream Id if provided', () => {
const renderer = createFleetTestRendererMock();
mockedUseRouteMatch.mockReturnValue({
params: { policyId: 'policy1', packagePolicyId: 'test-package-policy-id' },
} as any);
const { result } = renderer.renderHook(() =>
usePackagePolicyEditorPageUrl('test-datastream-id')
);
expect(result.current).toBe(
'/mock/app/fleet/policies/policy1/edit-integration/test-package-policy-id?datastreamId=test-datastream-id'
);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { shouldShowStreamsByDefault } from './package_policy_input_panel';

describe('shouldShowStreamsByDefault', () => {
it('should return true if a datastreamId is provided and contained in the input', () => {
const res = shouldShowStreamsByDefault(
{} as any,
[],
{
enabled: true,
streams: [
{
id: 'datastream-id',
},
],
} as any,
'datastream-id'
);
expect(res).toBeTruthy();
});

it('should return false if a datastreamId is provided but not contained in the input', () => {
const res = shouldShowStreamsByDefault(
{} as any,
[],
{
enabled: true,
streams: [
{
id: 'datastream-1',
},
],
} as any,
'datastream-id'
);
expect(res).toBeFalsy();
});

it('should return false if a datastreamId is provided but the input is disabled', () => {
const res = shouldShowStreamsByDefault(
{} as any,
[],
{
enabled: false,
streams: [
{
id: 'datastream-id',
},
],
} as any,
'datastream-id'
);
expect(res).toBeFalsy();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,28 @@ const ShortenedHorizontalRule = styled(EuiHorizontalRule)`
}
`;

const shouldShowStreamsByDefault = (
export const shouldShowStreamsByDefault = (
packageInput: RegistryInput,
packageInputStreams: Array<RegistryStream & { data_stream: { dataset: string; type: string } }>,
packagePolicyInput: NewPackagePolicyInput,
defaultDataStreamId?: string
): boolean => {
if (!packagePolicyInput.enabled) {
return false;
}

return (
(packagePolicyInput.enabled &&
(hasInvalidButRequiredVar(packageInput.vars, packagePolicyInput.vars) ||
packageInputStreams.some(
(stream) =>
stream.enabled &&
hasInvalidButRequiredVar(
stream.vars,
packagePolicyInput.streams.find(
(pkgStream) => stream.data_stream.dataset === pkgStream.data_stream.dataset
)?.vars
)
))) ||
hasInvalidButRequiredVar(packageInput.vars, packagePolicyInput.vars) ||
packageInputStreams.some(
(stream) =>
stream.enabled &&
hasInvalidButRequiredVar(
stream.vars,
packagePolicyInput.streams.find(
(pkgStream) => stream.data_stream.dataset === pkgStream.data_stream.dataset
)?.vars
)
) ||
packagePolicyInput.streams.some((stream) => {
return defaultDataStreamId && stream.id && stream.id === defaultDataStreamId;
})
Expand Down

0 comments on commit 52624a7

Please sign in to comment.