Skip to content

Commit 30be49f

Browse files
#RI-5156 - fix tests
1 parent b6c1954 commit 30be49f

File tree

11 files changed

+300
-229
lines changed

11 files changed

+300
-229
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react'
2+
import { instance, mock } from 'ts-mockito'
3+
import { render } from 'uiSrc/utils/test-utils'
4+
import BadgeIcon, { Props } from './BadgeIcon'
5+
6+
const mockedProps = mock<Props>()
7+
8+
const icon = <div />
9+
10+
describe('BadgeIcon', () => {
11+
it('should render', () => {
12+
expect(render(<BadgeIcon {...instance(mockedProps)} icon={icon} />)).toBeTruthy()
13+
})
14+
})

redisinsight/ui/src/components/recommendation/badge-icon/BadgeIcon.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ import React from 'react'
22
import { EuiToolTip, EuiFlexItem } from '@elastic/eui'
33
import styles from '../styles.module.scss'
44

5-
interface Props {
5+
export interface Props {
66
id: string
77
icon: React.ReactElement
88
name: string
99
}
1010
const BadgeIcon = ({ id, icon, name }: Props) => (
11-
<EuiFlexItem key={id} className={styles.badge} grow={false}>
11+
<EuiFlexItem key={id} className={styles.badge} grow={false} data-testid={`recommendation-badge-${id}`}>
1212
<div data-testid={id} className={styles.badgeWrapper}>
1313
<EuiToolTip content={name} position="top" display="inlineBlock" anchorClassName="flex-row">
1414
{icon}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
import React from 'react'
2+
import { instance, mock } from 'ts-mockito'
3+
import { fireEvent, render, screen } from 'uiSrc/utils/test-utils'
4+
import ContentElement, { Props } from './ContentElement'
5+
6+
const mockedProps = mock<Props>()
7+
8+
const mockTelemetryName = 'name'
9+
10+
describe('ContentElement', () => {
11+
it('should render', () => {
12+
expect(render(<ContentElement {...instance(mockedProps)} />)).toBeTruthy()
13+
})
14+
15+
it('should render paragraph', () => {
16+
const mockContent = {
17+
type: 'paragraph',
18+
value: 'paragraph',
19+
}
20+
render(<ContentElement content={mockContent} telemetryName={mockTelemetryName} idx={0} />)
21+
22+
expect(screen.queryByTestId(`paragraph-${mockTelemetryName}-0`)).toBeInTheDocument()
23+
})
24+
25+
it('should render span', () => {
26+
const mockContent = {
27+
type: 'span',
28+
value: 'span',
29+
}
30+
render(<ContentElement content={mockContent} telemetryName={mockTelemetryName} idx={0} />)
31+
32+
expect(screen.queryByTestId(`span-${mockTelemetryName}-0`)).toBeInTheDocument()
33+
})
34+
35+
it('should render code', () => {
36+
const mockContent = {
37+
type: 'code',
38+
value: 'code',
39+
}
40+
render(<ContentElement content={mockContent} telemetryName={mockTelemetryName} idx={0} />)
41+
42+
expect(screen.queryByTestId(`code-${mockTelemetryName}-0`)).toBeInTheDocument()
43+
})
44+
45+
it('should render spacer', () => {
46+
const mockContent = {
47+
type: 'spacer',
48+
value: 'l',
49+
}
50+
render(<ContentElement content={mockContent} telemetryName={mockTelemetryName} idx={0} />)
51+
52+
expect(screen.queryByTestId(`spacer-${mockTelemetryName}-0`)).toBeInTheDocument()
53+
})
54+
55+
it('should render link', () => {
56+
const mockContent = {
57+
type: 'link',
58+
value: 'link',
59+
}
60+
render(<ContentElement content={mockContent} telemetryName={mockTelemetryName} idx={0} />)
61+
62+
expect(screen.queryByTestId(`link-${mockTelemetryName}-0`)).toBeInTheDocument()
63+
})
64+
65+
it('should render code link', () => {
66+
const mockContent = {
67+
type: 'code-link',
68+
value: 'link',
69+
}
70+
render(<ContentElement content={mockContent} telemetryName={mockTelemetryName} idx={0} />)
71+
72+
expect(screen.queryByTestId(`code-link-${mockTelemetryName}-0`)).toBeInTheDocument()
73+
})
74+
75+
it('should render link-sso', () => {
76+
const mockContent = {
77+
type: 'link-sso',
78+
value: 'link-sso',
79+
}
80+
render(<ContentElement content={mockContent} telemetryName={mockTelemetryName} idx={0} />)
81+
82+
expect(screen.queryByTestId(`link-sso-${mockTelemetryName}-0`)).toBeInTheDocument()
83+
})
84+
85+
it('should render internal-link', () => {
86+
const mockContent = {
87+
type: 'internal-link',
88+
value: {
89+
path: '/some-path',
90+
name: 'name',
91+
},
92+
}
93+
render(<ContentElement content={mockContent} telemetryName={mockTelemetryName} idx={0} />)
94+
95+
expect(screen.queryByTestId(`internal-link-${mockTelemetryName}-0`)).toBeInTheDocument()
96+
})
97+
98+
it('should render list', () => {
99+
const mockContent = {
100+
type: 'list',
101+
value: [[]],
102+
}
103+
render(<ContentElement content={mockContent} telemetryName={mockTelemetryName} idx={0} />)
104+
105+
expect(screen.queryByTestId(`list-${mockTelemetryName}-0`)).toBeInTheDocument()
106+
})
107+
108+
it('should render unknown', () => {
109+
const mockContent = {
110+
type: 'unknown',
111+
value: 'unknown',
112+
}
113+
render(<ContentElement content={mockContent} telemetryName={mockTelemetryName} idx={0} />)
114+
115+
expect(screen.getByText('unknown')).toBeInTheDocument()
116+
})
117+
118+
it('click on link should call onClick', () => {
119+
const onClickMock = jest.fn()
120+
const mockContent = {
121+
type: 'link',
122+
value: 'link',
123+
}
124+
125+
const { queryByTestId } = render(
126+
<ContentElement onLinkClick={onClickMock} content={mockContent} telemetryName={mockTelemetryName} idx={0} />
127+
)
128+
129+
fireEvent.click(queryByTestId(`link-${mockTelemetryName}-0`) as HTMLElement)
130+
131+
expect(onClickMock).toBeCalled()
132+
})
133+
})

redisinsight/ui/src/components/recommendation/content-element/ContentElement.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { utmMedium } from '../constants'
1414

1515
import styles from '../styles.module.scss'
1616

17-
interface Props {
17+
export interface Props {
1818
content: IRecommendationContent
1919
telemetryName: string
2020
params?: any
@@ -24,7 +24,7 @@ interface Props {
2424
}
2525

2626
const ContentElement = (props: Props) => {
27-
const { content, params, onLinkClick, telemetryName, insights, idx } = props
27+
const { content = {}, params, onLinkClick, telemetryName, insights, idx } = props
2828
const { type, value, parameter } = content
2929

3030
const replacedValue = replaceVariables(value, parameter, params)
@@ -142,8 +142,8 @@ const ContentElement = (props: Props) => {
142142
{isArray(value) && value.map((listElement: IRecommendationContent[], idx: number) => (
143143
<li
144144
className={cx(styles.listItem, { [styles.insights]: insights })}
145-
// eslint-disable-next-line react/no-array-index-key
146-
key={`list-item-${listElement[0]}-${idx}`}
145+
// eslint-disable-next-line react/no-array-index-key
146+
key={`list-item-${idx}`}
147147
>
148148
<RecommendationBody
149149
elements={listElement}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react'
2+
import { render } from 'uiSrc/utils/test-utils'
3+
import RecommendationBadgesLegend from './RecommendationBadgesLegend'
4+
5+
describe('RecommendationBadgesLegend', () => {
6+
it('should render', () => {
7+
expect(render(<RecommendationBadgesLegend />)).toBeTruthy()
8+
})
9+
})
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from 'react'
2+
import { instance, mock } from 'ts-mockito'
3+
import { render, screen } from 'uiSrc/utils/test-utils'
4+
import RecommendationBadges, { Props } from './RecommendationBadges'
5+
6+
const mockedProps = mock<Props>()
7+
8+
describe('RecommendationBadges', () => {
9+
it('should render', () => {
10+
expect(render(<RecommendationBadges {...instance(mockedProps)} />)).toBeTruthy()
11+
})
12+
13+
it('should render code changes badge', () => {
14+
expect(render(<RecommendationBadges badges={['code_changes']} />)).toBeTruthy()
15+
16+
expect(screen.queryByTestId('recommendation-badge-code_changes')).toBeInTheDocument()
17+
expect(screen.queryByTestId('recommendation-badge-upgrade')).not.toBeInTheDocument()
18+
expect(screen.queryByTestId('recommendation-badge-configuration_changes')).not.toBeInTheDocument()
19+
})
20+
21+
it('should render upgrade and configuration changes badges', () => {
22+
expect(render(<RecommendationBadges badges={['upgrade', 'configuration_changes']} />)).toBeTruthy()
23+
24+
expect(screen.queryByTestId('recommendation-badge-code_changes')).not.toBeInTheDocument()
25+
expect(screen.queryByTestId('recommendation-badge-upgrade')).toBeInTheDocument()
26+
expect(screen.queryByTestId('recommendation-badge-configuration_changes')).toBeInTheDocument()
27+
})
28+
})

redisinsight/ui/src/components/recommendation/recommendation-badges/RecommendationBadges.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import BadgeIcon from '../badge-icon'
55
import { badgesContent } from '../constants'
66
import styles from '../styles.module.scss'
77

8-
interface Props {
8+
export interface Props {
99
badges?: string[]
1010
}
1111

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import React from 'react'
2+
import { instance, mock } from 'ts-mockito'
3+
import { render, screen } from 'uiSrc/utils/test-utils'
4+
import { IRecommendationContent } from 'uiSrc/slices/interfaces/recommendations'
5+
import RecommendationBody, { Props } from './RecommendationBody'
6+
7+
const mockedProps = mock<Props>()
8+
9+
const mockContent: IRecommendationContent[] = [
10+
{
11+
type: 'unknown',
12+
value: 'unknown',
13+
},
14+
]
15+
16+
const mockTelemetryName = 'name'
17+
18+
describe('RecommendationBody', () => {
19+
it('should render', () => {
20+
expect(render(<RecommendationBody {...instance(mockedProps)} />)).toBeTruthy()
21+
})
22+
23+
it('should render element', () => {
24+
render(<RecommendationBody {...instance(mockedProps)} elements={mockContent} telemetryName={mockTelemetryName} />)
25+
26+
expect(screen.getByText('unknown')).toBeInTheDocument()
27+
})
28+
})

redisinsight/ui/src/components/recommendation/recommendation-body/RecommendationBody.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react'
22
import { IRecommendationContent } from 'uiSrc/slices/interfaces/recommendations'
33
import ContentElement from '../content-element'
44

5-
interface Props {
5+
export interface Props {
66
elements: IRecommendationContent[]
77
telemetryName: string
88
params?: any
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { MOCK_RECOMMENDATIONS } from 'uiSrc/constants/mocks/mock-recommendations'
2+
import { sortRecommendations, replaceVariables } from '../../recommendation'
3+
4+
const sortRecommendationsTests = [
5+
{
6+
input: [],
7+
expected: []
8+
},
9+
{
10+
input: [
11+
{ name: 'luaScript' },
12+
{ name: 'bigSets' },
13+
{ name: 'searchIndexes' },
14+
],
15+
expected: [
16+
{ name: 'searchIndexes' },
17+
{ name: 'bigSets' },
18+
{ name: 'luaScript' },
19+
]
20+
},
21+
{
22+
input: [
23+
{ name: 'luaScript' },
24+
{ name: 'bigSets' },
25+
{ name: 'searchJSON' },
26+
],
27+
expected: [
28+
{ name: 'searchJSON' },
29+
{ name: 'bigSets' },
30+
{ name: 'luaScript' },
31+
]
32+
},
33+
{
34+
input: [
35+
{ name: 'luaScript' },
36+
{ name: 'bigSets' },
37+
{ name: 'searchIndexes' },
38+
{ name: 'searchJSON' },
39+
{ name: 'useSmallerKeys' },
40+
{ name: 'RTS' },
41+
],
42+
expected: [
43+
{ name: 'searchJSON' },
44+
{ name: 'searchIndexes' },
45+
{ name: 'RTS' },
46+
{ name: 'bigSets' },
47+
{ name: 'luaScript' },
48+
{ name: 'useSmallerKeys' },
49+
]
50+
},
51+
]
52+
53+
const replaceVariablesTests = [
54+
{ input: ['value'], expected: 'value' },
55+
// eslint-disable-next-line no-template-curly-in-string
56+
{ input: ['some ${0} text ${1}', ['foo', 'bar'], { foo: '7', bar: 'bar' }], expected: 'some 7 text bar' },
57+
{ input: ['value'], expected: 'value' },
58+
{ input: ['value'], expected: 'value' },
59+
]
60+
61+
describe('sortRecommendations', () => {
62+
test.each(sortRecommendationsTests)(
63+
'%j',
64+
({ input, expected }) => {
65+
const result = sortRecommendations(input, MOCK_RECOMMENDATIONS)
66+
expect(result).toEqual(expected)
67+
}
68+
)
69+
})
70+
71+
describe('replaceVariables', () => {
72+
test.each(replaceVariablesTests)(
73+
'%j',
74+
({ input, expected }) => {
75+
// @ts-ignore
76+
const result = replaceVariables(...input)
77+
expect(result).toEqual(expected)
78+
}
79+
)
80+
})

0 commit comments

Comments
 (0)