Skip to content

Commit c920db1

Browse files
committed
test(30977): add tests
1 parent 718c57f commit c920db1

File tree

1 file changed

+108
-2
lines changed

1 file changed

+108
-2
lines changed

hivemq-edge/src/frontend/src/modules/Mappings/hooks/useValidateCombiner.spec.ts

+108-2
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,34 @@
11
import { beforeEach, expect } from 'vitest'
2+
import { v4 as uuidv4 } from 'uuid'
3+
import { http, HttpResponse } from 'msw'
24
import { renderHook, waitFor } from '@testing-library/react'
35
import { createErrorHandler, toErrorList } from '@rjsf/utils'
6+
import type { UseQueryResult } from '@tanstack/react-query'
47

58
import { server } from '@/__test-utils__/msw/mockServer.ts'
69
import { SimpleWrapper as wrapper } from '@/__test-utils__/hooks/SimpleWrapper.tsx'
7-
import type { AdaptersList, Combiner, DataCombining, ProtocolAdaptersList } from '@/api/__generated__'
10+
import type {
11+
AdaptersList,
12+
Combiner,
13+
DataCombining,
14+
DomainTagList,
15+
EntityReference,
16+
ProtocolAdaptersList,
17+
TopicFilterList,
18+
} from '@/api/__generated__'
819
import { EntityType } from '@/api/__generated__'
920
import {
21+
deviceHandlers,
1022
handlers as failCapabilityHandlers,
1123
mockAdapter_OPCUA,
1224
mockProtocolAdapter_OPCUA,
1325
} from '@/api/hooks/useProtocolAdapters/__handlers__'
1426
import { mockCombinerId, mockEmptyCombiner } from '@/api/hooks/useCombiners/__handlers__'
27+
import { handlers as topicFilterHandlers } from '@/api/hooks/useTopicFilters/__handlers__'
28+
import { mappingHandlers } from '@/api/hooks/useProtocolAdapters/__handlers__/mapping.mocks'
29+
import { useGetCombinedEntities } from '@/api/hooks/useDomainModel/useGetCombinedEntities'
1530

1631
import { useValidateCombiner } from './useValidateCombiner'
17-
import { http, HttpResponse } from 'msw'
1832

1933
const capabilityHandlers = [
2034
http.get('*/protocol-adapters/types', () => {
@@ -194,6 +208,98 @@ describe('useValidateCombiner', () => {
194208
const errors = await renderValidateHook(getFormData([]))
195209
expect(errors).toStrictEqual([])
196210
})
211+
212+
it('should not validate an empty list of mappings', async () => {
213+
const errors = await renderValidateHook(
214+
getFormData([
215+
{
216+
id: uuidv4(),
217+
sources: {
218+
tags: [],
219+
topicFilters: [],
220+
// @ts-ignore TODO[NVL] Needs to be nullable
221+
primary: {},
222+
},
223+
destination: {},
224+
instructions: [],
225+
},
226+
])
227+
)
228+
expect(errors).toStrictEqual([
229+
expect.objectContaining({
230+
message: 'At least one schema should be available',
231+
}),
232+
])
233+
})
234+
235+
it('should not validate a tag not belonging to a source', async () => {
236+
server.use(...topicFilterHandlers, ...deviceHandlers)
237+
const { result } = renderHook(() => useGetCombinedEntities(sources), { wrapper })
238+
239+
expect(result.current).toHaveLength(2)
240+
await waitFor(() => {
241+
expect(result.current[0].isSuccess).toBeTruthy()
242+
expect(result.current[1].isSuccess).toBeTruthy()
243+
})
244+
245+
const errors = await renderValidateHook(
246+
getFormData([
247+
{
248+
id: uuidv4(),
249+
sources: {
250+
tags: ['test/tag1'],
251+
topicFilters: [],
252+
// @ts-ignore TODO[NVL] Needs to be nullable
253+
primary: {},
254+
},
255+
destination: {},
256+
instructions: [],
257+
},
258+
]),
259+
[],
260+
sources
261+
)
262+
expect(errors).toStrictEqual([
263+
expect.objectContaining({
264+
message: 'At least one schema should be available',
265+
}),
266+
expect.objectContaining({
267+
message: "The tag test/tag1 is not defined in any of the combiner's sources",
268+
}),
269+
])
270+
})
271+
272+
it('should validate a tag belonging to a source', async () => {
273+
server.use(...topicFilterHandlers, ...deviceHandlers, ...mappingHandlers)
274+
275+
const { result } = renderHook(() => useGetCombinedEntities(sources), { wrapper })
276+
277+
expect(result.current).toHaveLength(2)
278+
await waitFor(() => {
279+
expect(result.current[0].isSuccess).toBeTruthy()
280+
expect(result.current[1].isSuccess).toBeTruthy()
281+
})
282+
console.log('XXXX', result.current[1].data)
283+
284+
const errors = await renderValidateHook(
285+
getFormData([
286+
{
287+
id: uuidv4(),
288+
sources: {
289+
tags: ['opcua-1/log/event'],
290+
topicFilters: [],
291+
// @ts-ignore TODO[NVL] Needs to be nullable
292+
primary: {},
293+
},
294+
destination: {},
295+
instructions: [],
296+
},
297+
]),
298+
result.current,
299+
sources
300+
)
301+
expect(errors).toStrictEqual([])
302+
})
197303
})
198304

199305
// TODO[TEST] Complete the test suite

0 commit comments

Comments
 (0)