|
1 | 1 | import { beforeEach, expect } from 'vitest'
|
| 2 | +import { v4 as uuidv4 } from 'uuid' |
| 3 | +import { http, HttpResponse } from 'msw' |
2 | 4 | import { renderHook, waitFor } from '@testing-library/react'
|
3 | 5 | import { createErrorHandler, toErrorList } from '@rjsf/utils'
|
| 6 | +import type { UseQueryResult } from '@tanstack/react-query' |
4 | 7 |
|
5 | 8 | import { server } from '@/__test-utils__/msw/mockServer.ts'
|
6 | 9 | 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__' |
8 | 19 | import { EntityType } from '@/api/__generated__'
|
9 | 20 | import {
|
| 21 | + deviceHandlers, |
10 | 22 | handlers as failCapabilityHandlers,
|
11 | 23 | mockAdapter_OPCUA,
|
12 | 24 | mockProtocolAdapter_OPCUA,
|
13 | 25 | } from '@/api/hooks/useProtocolAdapters/__handlers__'
|
14 | 26 | 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' |
15 | 30 |
|
16 | 31 | import { useValidateCombiner } from './useValidateCombiner'
|
17 |
| -import { http, HttpResponse } from 'msw' |
18 | 32 |
|
19 | 33 | const capabilityHandlers = [
|
20 | 34 | http.get('*/protocol-adapters/types', () => {
|
@@ -194,6 +208,98 @@ describe('useValidateCombiner', () => {
|
194 | 208 | const errors = await renderValidateHook(getFormData([]))
|
195 | 209 | expect(errors).toStrictEqual([])
|
196 | 210 | })
|
| 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 | + }) |
197 | 303 | })
|
198 | 304 |
|
199 | 305 | // TODO[TEST] Complete the test suite
|
|
0 commit comments