Skip to content

Commit

Permalink
extract security mocks to a file
Browse files Browse the repository at this point in the history
  • Loading branch information
WafaaNasr committed Oct 18, 2022
1 parent 5607902 commit fe28669
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { fireEvent, render } from '@testing-library/react';
import { ruleReferences } from '../mocks/rule_references.mock';
import { Pagination } from '@elastic/eui';
import { mockGetFormattedComments } from '../mocks/comments.mock';
import { securityLinkAnchorComponent } from '../mocks/security_link_component.mock';

const onCreateExceptionListItem = jest.fn();
const onDeleteException = jest.fn();
Expand Down Expand Up @@ -133,17 +134,6 @@ describe('ExceptionsViewerItems', () => {

describe('securityLinkAnchorComponent, formattedDateComponent, exceptionsUtilityComponent and getFormattedComments', () => {
it('it should render sent securityLinkAnchorComponent', () => {
const securityLinkAnchorComponent = ({
referenceName,
referenceId,
}: {
referenceName: string;
referenceId: string;
}) => (
<div data-test-subj="securityLinkAnchorComponent">
<a href={referenceId}>{referenceName}</a>
</div>
);
const wrapper = render(
<ExceptionItems
viewerStatus={'' as ViewerStatus}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,18 @@
* Side Public License, v 1.
*/
import { render } from '@testing-library/react';
import React, { ReactElement } from 'react';
import { ReactElement } from 'react';
import { ElementType } from 'react';
import { generateLinedRulesMenuItems } from '.';
import { rules } from '../mocks/rule_references.mock';
import {
getSecurityLinkAction,
securityLinkAnchorComponent,
} from '../mocks/security_link_component.mock';

const dataTestSubj = 'generateLinedRulesMenuItemsTest';
const linkedRules = rules;
const securityLinkAnchorComponent = ({
referenceName,
referenceId,
}: {
referenceName: string;
referenceId: string;
}) => (
<div data-test-subj="securityLinkAnchorComponent">
<a href={referenceId}>{referenceName}</a>
</div>
);

describe('generateLinedRulesMenuItems', () => {
it('should not render if the linkedRules length is falsy', () => {
const result = generateLinedRulesMenuItems({
Expand Down Expand Up @@ -57,19 +51,7 @@ describe('generateLinedRulesMenuItems', () => {
});
});
it('should render the second linked rule and apply the css when the length is > 1', () => {
const result: ReactElement[] = generateLinedRulesMenuItems({
dataTestSubj,
linkedRules: [
...rules,
{
exception_lists: [],
id: '2a2b3c',
name: 'Simple Rule Query 2',
rule_id: 'rule-2',
},
],
securityLinkAnchorComponent,
}) as ReactElement[];
const result: ReactElement[] = getSecurityLinkAction(dataTestSubj);

const wrapper = render(result[1]);
expect(wrapper).toMatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { fireEvent, render } from '@testing-library/react';
import React from 'react';
import { HeaderMenu } from '.';
import { actions } from '../mocks/header.mock';
import { getSecurityLinkAction } from '../mocks/security_link_component.mock';

describe('HeaderMenu', () => {
it('should render button icon with default settings', () => {
Expand Down Expand Up @@ -91,7 +92,7 @@ describe('HeaderMenu', () => {
});

it('should render custom Actions', () => {
const customActions = [<p key={1}>test</p>];
const customActions = getSecurityLinkAction('headerMenuTest');
const wrapper = render(
<HeaderMenu disableActions={false} emptyButton actions={customActions} useCustomActions />
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import React, { ReactElement } from 'react';
import { generateLinedRulesMenuItems } from '../generate_linked_rules_menu_item';
import { rules } from './rule_references.mock';
export const securityLinkAnchorComponent = ({
referenceName,
referenceId,
}: {
referenceName: string;
referenceId: string;
}) => (
<div data-test-subj="securityLinkAnchorComponent">
<a href={referenceId}>{referenceName}</a>
</div>
);

export const getSecurityLinkAction = (dataTestSubj: string) =>
generateLinedRulesMenuItems({
dataTestSubj,
linkedRules: [
...rules,
{
exception_lists: [],
id: '2a2b3c',
name: 'Simple Rule Query 2',
rule_id: 'rule-2',
},
],
securityLinkAnchorComponent,
}) as ReactElement[];

0 comments on commit fe28669

Please sign in to comment.