Skip to content

Commit 793b983

Browse files
authored
Introduce factories for mocking test data (#4772)
- integrate fishery to provide simple and handy tools for building factories - integrate faker to provide utils for generating dynamic and realistic-looking data - create example factory for the vector search field box and update unit tests
1 parent bfce199 commit 793b983

File tree

12 files changed

+260
-285
lines changed

12 files changed

+260
-285
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@
105105
"@babel/preset-react": "^7.22.15",
106106
"@babel/preset-typescript": "^7.23.2",
107107
"@electron/rebuild": "^3.7.1",
108+
"@faker-js/faker": "^9.9.0",
108109
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.10",
109110
"@svgr/webpack": "^8.1.0",
110111
"@teamsupercell/typings-for-css-modules-loader": "^2.4.0",
@@ -178,6 +179,7 @@
178179
"eslint-plugin-react-hooks": "^4.0.8",
179180
"eslint-plugin-sonarjs": "^0.10.0",
180181
"file-loader": "^6.0.0",
182+
"fishery": "^2.3.1",
181183
"google-auth-library": "^9.0.0",
182184
"googleapis": "^125.0.0",
183185
"html-webpack-plugin": "^5.6.0",

redisinsight/ui/src/components/new-index/create-index-step/field-box/FieldBox.spec.tsx

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import React from 'react'
22
import { BoxSelectionGroup } from '@redis-ui/components'
33

4-
import { FieldTypes } from 'uiSrc/pages/browser/components/create-redisearch-index/constants'
5-
import { MOCK_VECTOR_SEARCH_BOX } from 'uiSrc/constants/mocks/mock-vector-index-search'
64
import { cleanup, fireEvent, render, screen } from 'uiSrc/utils/test-utils'
5+
import { vectorSearchBoxFactory } from 'uiSrc/mocks/factories/redisearch/VectorSearchBox.factory'
76

87
import { FieldBox, FieldBoxProps } from './FieldBox'
98
import { VectorSearchBox } from './types'
109

1110
const renderFieldBoxComponent = (props?: FieldBoxProps) => {
1211
const defaultProps: FieldBoxProps = {
13-
box: MOCK_VECTOR_SEARCH_BOX,
12+
box: vectorSearchBoxFactory.build(),
1413
}
1514

1615
return render(
@@ -27,14 +26,7 @@ describe('CreateIndexStepWrapper', () => {
2726

2827
it('should render', () => {
2928
const props: FieldBoxProps = {
30-
box: {
31-
...MOCK_VECTOR_SEARCH_BOX,
32-
value: 'id',
33-
label: 'id',
34-
text: 'Unique product identifier',
35-
tag: FieldTypes.TAG,
36-
disabled: false,
37-
},
29+
box: vectorSearchBoxFactory.build(),
3830
}
3931

4032
const { container } = renderFieldBoxComponent(props)
@@ -53,23 +45,31 @@ describe('CreateIndexStepWrapper', () => {
5345
expect(checkbox).toBeInTheDocument()
5446
})
5547

56-
it('should select the box when clicked', () => {
57-
renderFieldBoxComponent()
48+
it('should select the box when clicked', async () => {
49+
const props: FieldBoxProps = {
50+
box: vectorSearchBoxFactory.build({
51+
disabled: false,
52+
}),
53+
}
54+
55+
renderFieldBoxComponent(props)
5856

57+
// Verify that the checkbox is not checked initially
5958
const checkbox = screen.getByRole('checkbox')
6059
expect(checkbox).not.toBeChecked()
6160

62-
const box = screen.getByTestId(`field-box-${MOCK_VECTOR_SEARCH_BOX.value}`)
61+
// Click on the box to select it
62+
const box = screen.getByTestId(`field-box-${props.box.value}`)
6363
fireEvent.click(box)
6464

65+
// Wait for the checkbox to be checked
6566
expect(checkbox).toBeChecked()
6667
})
6768

6869
it('should not select the box when clicked if disabled', () => {
69-
const disabledBox: VectorSearchBox = {
70-
...MOCK_VECTOR_SEARCH_BOX,
70+
const disabledBox: VectorSearchBox = vectorSearchBoxFactory.build({
7171
disabled: true,
72-
}
72+
})
7373

7474
renderFieldBoxComponent({ box: disabledBox })
7575

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import React from 'react'
22

33
import { fireEvent, render, screen } from 'uiSrc/utils/test-utils'
4-
import { MOCK_VECTOR_SEARCH_BOX } from 'uiSrc/constants/mocks/mock-vector-index-search'
4+
import { vectorSearchBoxFactory } from 'uiSrc/mocks/factories/redisearch/VectorSearchBox.factory'
55
import { FieldBoxesGroup, FieldBoxesGroupProps } from './FieldBoxesGroup'
66

77
const renderFieldBoxesGroupComponent = (
88
props?: Partial<FieldBoxesGroupProps>,
99
) => {
1010
const defaultProps: FieldBoxesGroupProps = {
11-
boxes: [MOCK_VECTOR_SEARCH_BOX],
12-
value: [MOCK_VECTOR_SEARCH_BOX.value],
11+
boxes: vectorSearchBoxFactory.buildList(3, { disabled: false }),
12+
value: [],
1313
onChange: jest.fn(),
1414
}
1515

@@ -26,30 +26,44 @@ describe('FieldBoxesGroup', () => {
2626
expect(fieldBoxesGroup).toBeInTheDocument()
2727

2828
const fieldBoxes = screen.getAllByTestId(/field-box-/)
29-
expect(fieldBoxes).toHaveLength(1)
29+
expect(fieldBoxes).toHaveLength(3)
3030
})
3131

3232
it('should call onChange when clicking on a box to select it', () => {
33-
const onChangeMock = jest.fn()
34-
const value: string[] = []
33+
const mockVectorSearchBox = vectorSearchBoxFactory.build({
34+
disabled: false,
35+
})
3536

36-
renderFieldBoxesGroupComponent({ value, onChange: onChangeMock })
37+
const props: FieldBoxesGroupProps = {
38+
boxes: [mockVectorSearchBox],
39+
value: [],
40+
onChange: jest.fn(),
41+
}
3742

38-
const box = screen.getByTestId(`field-box-${MOCK_VECTOR_SEARCH_BOX.value}`)
43+
renderFieldBoxesGroupComponent(props)
44+
45+
const box = screen.getByTestId(`field-box-${mockVectorSearchBox.value}`)
3946

4047
fireEvent.click(box)
41-
expect(onChangeMock).toHaveBeenCalledWith([MOCK_VECTOR_SEARCH_BOX.value])
48+
expect(props.onChange).toHaveBeenCalledWith([mockVectorSearchBox.value])
4249
})
4350

4451
it('should call onChange when clicking on a box to deselect it', () => {
45-
const onChangeMock = jest.fn()
46-
const value: string[] = [MOCK_VECTOR_SEARCH_BOX.value]
52+
const mockVectorSearchBox = vectorSearchBoxFactory.build({
53+
disabled: false,
54+
})
55+
56+
const props: FieldBoxesGroupProps = {
57+
boxes: [mockVectorSearchBox],
58+
value: [mockVectorSearchBox.value],
59+
onChange: jest.fn(),
60+
}
4761

48-
renderFieldBoxesGroupComponent({ value, onChange: onChangeMock })
62+
renderFieldBoxesGroupComponent(props)
4963

50-
const box = screen.getByTestId(`field-box-${MOCK_VECTOR_SEARCH_BOX.value}`)
64+
const box = screen.getByTestId(`field-box-${mockVectorSearchBox.value}`)
5165

5266
fireEvent.click(box)
53-
expect(onChangeMock).toHaveBeenCalledWith([])
67+
expect(props.onChange).toHaveBeenCalledWith([])
5468
})
5569
})

redisinsight/ui/src/constants/mocks/mock-vector-index-search.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

redisinsight/ui/src/mocks/data/redisearch.ts

Lines changed: 0 additions & 203 deletions
This file was deleted.

0 commit comments

Comments
 (0)