Skip to content

Commit

Permalink
test(28015): fix and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vanch3d committed Nov 26, 2024
1 parent e68ceb7 commit 5d1a96b
Show file tree
Hide file tree
Showing 11 changed files with 447 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { http, HttpResponse } from 'msw'
import { RJSFSchema } from '@rjsf/utils'

import type { TagSchema } from '@/api/__generated__'
import { MockAdapterType } from '@/__test-utils__/adapters/types.ts'
import type { ProblemDetails } from '@/api/types/http-problem-details.ts'
import type { DomainTag, DomainTagList, JsonNode, PayloadSampleList, TagSchema } from '@/api/__generated__'
import {
MOCK_DEVICE_TAG_ADDRESS_MODBUS,
MOCK_DEVICE_TAG_FAKE,
MOCK_DEVICE_TAG_JSON_SCHEMA_OPCUA,
} from '@/api/hooks/useProtocolAdapters/__handlers__'
import { payloadToSchema } from '@/components/rjsf/MqttTransformation/utils/json-schema.utils.ts'
import type { MQTTSample } from '@/hooks/usePrivateMqttClient/type.ts'

Expand Down Expand Up @@ -81,20 +88,74 @@ export const GENERATE_DATA_MODELS = (short = false, title?: string): RJSFSchema
}

export const handlers = [
http.get('**/management/domain/tags', () => {
return HttpResponse.json<Array<string>>([], { status: 200 })
http.get<{ protocolId: string }>('**/management/protocol-adapters/tagschemas/:protocolId', ({ params }) => {
const { protocolId } = params

if (protocolId === MockAdapterType.ADS)
return HttpResponse.json<ProblemDetails>(
{ title: 'The schema for the tags cannot be found', status: 404 },
{ status: 404 }
)

return HttpResponse.json<TagSchema>(MOCK_DEVICE_TAG_JSON_SCHEMA_OPCUA, { status: 200 })
}),

http.get('**/management/domain/tags/schema', () => {
return HttpResponse.json<TagSchema>({ configSchema: {}, protocolId: 'protocol' }, { status: 200 })
http.get<{ tagName: string }>('**/management/protocol-adapters/tags/:tagName', ({ params }) => {
const { tagName } = params

try {
const realTag = atob(tagName)
console.log('tagName', realTag)
if (realTag === MOCK_DEVICE_TAG_FAKE)
return HttpResponse.json<ProblemDetails>({ title: 'The tag is not found', status: 404 }, { status: 404 })
return HttpResponse.json<DomainTag>(
{ tagName: realTag, protocolId: MockAdapterType.MODBUS, tagDefinition: MOCK_DEVICE_TAG_ADDRESS_MODBUS },
{ status: 200 }
)
} catch (e) {
return HttpResponse.json<ProblemDetails>({ title: 'The tag is not well formed', status: 400 }, { status: 400 })
}
}),

http.get('**/management/domain/topics', () => {
return HttpResponse.json<Array<string>>([], { status: 200 })
http.get<{ topic: string }>('**/management/sampling/topic/:topic', ({ params }) => {
const { topic } = params

if (topic === MOCK_DEVICE_TAG_FAKE)
return HttpResponse.json<ProblemDetails>(
{ title: 'The schema for the tags cannot be found', status: 404 },
{ status: 404 }
)

return HttpResponse.json<PayloadSampleList>({ items: [] }, { status: 200 })
}),

http.get('**/management/domain/topics/schema', () => {
return HttpResponse.json<TagSchema>({ configSchema: {}, protocolId: 'protocol' }, { status: 200 })
http.get<{ topic: string }>('**/management/sampling/schema/:topic', ({ params }) => {
const { topic } = params

if (topic === MOCK_DEVICE_TAG_FAKE)
return HttpResponse.json<ProblemDetails>(
{ title: 'The schema for the tags cannot be found', status: 404 },
{ status: 404 }
)

return HttpResponse.json<JsonNode>(GENERATE_DATA_MODELS(true, topic), { status: 200 })
}),

http.get('**/management/protocol-adapters/tags', () => {
return HttpResponse.json<DomainTagList>(
{
items: [
{ tagName: 'test/tag1', protocolId: MockAdapterType.MODBUS, tagDefinition: MOCK_DEVICE_TAG_ADDRESS_MODBUS },
],
},
{ status: 200 }
)
}),

http.post<{ topic: string }>('**/management/sampling/topic/:topic', ({ params }) => {
const { topic } = params

return HttpResponse.json({ topic }, { status: 200 })
}),
]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { beforeEach, expect } from 'vitest'
import { renderHook, waitFor } from '@testing-library/react'

import { server } from '@/__test-utils__/msw/mockServer.ts'
import { SimpleWrapper as wrapper } from '@/__test-utils__/hooks/SimpleWrapper.tsx'
import { useGetDomainTag } from '@/api/hooks/useDomainModel/useGetDomainTag.ts'
import { DomainTag } from '@/api/__generated__'
import { ProblemDetails } from '@/api/types/http-problem-details.ts'
import { MOCK_DEVICE_TAG_FAKE } from '@/api/hooks/useProtocolAdapters/__handlers__'

import { handlers } from './__handlers__'

describe('useGetDomainTag', () => {
beforeEach(() => {
server.use(...handlers)
})

afterEach(() => {
server.resetHandlers()
})

it('should load the data', async () => {
const mockTag = 'tag/test'
const { result } = renderHook(() => useGetDomainTag(mockTag), { wrapper })
await waitFor(() => {
expect(result.current.isLoading).toBeFalsy()
expect(result.current.isSuccess).toBeTruthy()
})
expect(result.current.data).toStrictEqual<DomainTag>({
protocolId: 'modbus',
tagDefinition: {
endIdx: 1,
startIdx: 0,
},
tagName: mockTag,
})
})

it('should return an error', async () => {
const { result } = renderHook(() => useGetDomainTag(MOCK_DEVICE_TAG_FAKE), { wrapper })
await waitFor(() => {
expect(result.current.isLoading).toBeFalsy()
})
expect(result.current.data).toStrictEqual(undefined)
expect(result.current.error?.status).toStrictEqual(404)
expect(result.current.error?.body).toStrictEqual<ProblemDetails>({ title: 'The tag is not found', status: 404 })
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { beforeEach, expect } from 'vitest'
import { renderHook, waitFor } from '@testing-library/react'

import { server } from '@/__test-utils__/msw/mockServer.ts'
import { SimpleWrapper as wrapper } from '@/__test-utils__/hooks/SimpleWrapper.tsx'
import { MockAdapterType } from '@/__test-utils__/adapters/types.ts'

import { type TagSchema } from '@/api/__generated__'
import { useGetDomainTagSchema } from '@/api/hooks/useDomainModel/useGetDomainTagSchema.ts'

import { handlers } from './__handlers__'

describe('useGetDomainTagSchema', () => {
beforeEach(() => {
server.use(...handlers)
})

afterEach(() => {
server.resetHandlers()
})

it('should load the data', async () => {
const { result } = renderHook(() => useGetDomainTagSchema(MockAdapterType.OPC_UA), { wrapper })
await waitFor(() => {
expect(result.current.isLoading).toBeFalsy()
expect(result.current.isSuccess).toBeTruthy()
})
expect(result.current.data).toStrictEqual<TagSchema>({
protocolId: 'opcua',
configSchema: expect.objectContaining({
$schema: 'https://json-schema.org/draft/2020-12/schema',
}),
})
})

it('should not load if protocolId is not given', async () => {
const { result } = renderHook(() => useGetDomainTagSchema(undefined), { wrapper })
await waitFor(() => {
expect(result.current.isLoading).toBeFalsy()
expect(result.current.isFetched).toBeFalsy()
})
expect(result.current.data).toBeUndefined()
expect(result.current.error).toBeNull()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { beforeEach, expect } from 'vitest'
import { renderHook, waitFor } from '@testing-library/react'

import { server } from '@/__test-utils__/msw/mockServer.ts'
import { SimpleWrapper as wrapper } from '@/__test-utils__/hooks/SimpleWrapper.tsx'
import { type PayloadSampleList } from '@/api/__generated__'
import { useGetSamplesForTopic } from '@/api/hooks/useDomainModel/useGetSamplesForTopic.ts'

import { handlers } from './__handlers__'

describe('useGetSamplesForTopic', () => {
beforeEach(() => {
server.use(...handlers)
})

afterEach(() => {
server.resetHandlers()
})

it('should load the data', async () => {
const { result } = renderHook(() => useGetSamplesForTopic('test/topic1', true), { wrapper })
await waitFor(() => {
expect(result.current.isLoading).toBeFalsy()
expect(result.current.isSuccess).toBeTruthy()
})
expect(result.current.data).toStrictEqual<PayloadSampleList>({
items: [],
})
})

it('should not load if not seeded', async () => {
const { result } = renderHook(() => useGetSamplesForTopic('test/topic1', false), { wrapper })
await waitFor(() => {
expect(result.current.isLoading).toBeFalsy()
expect(result.current.isFetched).toBeFalsy()
})
expect(result.current.data).toBeUndefined()
expect(result.current.error).toBeNull()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { beforeEach, expect } from 'vitest'
import { renderHook, waitFor } from '@testing-library/react'

import { server } from '@/__test-utils__/msw/mockServer.ts'
import { SimpleWrapper as wrapper } from '@/__test-utils__/hooks/SimpleWrapper.tsx'
import { type JsonNode } from '@/api/__generated__'
import { useGetSchemaForTopic } from '@/api/hooks/useDomainModel/useGetSchemaForTopic.ts'

import { handlers } from './__handlers__'

describe('useGetSchemaForTopic', () => {
beforeEach(() => {
server.use(...handlers)
})

afterEach(() => {
server.resetHandlers()
})

it('should load the data', async () => {
const { result } = renderHook(() => useGetSchemaForTopic('test/topic1', true), { wrapper })
await waitFor(() => {
expect(result.current.isLoading).toBeFalsy()
expect(result.current.isSuccess).toBeTruthy()
})
expect(result.current.data).toStrictEqual<JsonNode>(
expect.objectContaining({
title: 'dGVzdC90b3BpYzE=',
type: 'object',
})
)
})

it('should not load if not seeded', async () => {
const { result } = renderHook(() => useGetSchemaForTopic('test/topic1', false), { wrapper })
await waitFor(() => {
expect(result.current.isLoading).toBeFalsy()
expect(result.current.isFetched).toBeFalsy()
})
expect(result.current.data).toBeUndefined()
expect(result.current.error).toBeNull()
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { beforeEach, expect } from 'vitest'
import { renderHook, waitFor } from '@testing-library/react'

import { server } from '@/__test-utils__/msw/mockServer.ts'
import { SimpleWrapper as wrapper } from '@/__test-utils__/hooks/SimpleWrapper.tsx'
import { type DomainTagList } from '@/api/__generated__'
import { useListDomainTags } from '@/api/hooks/useDomainModel/useListDomainTags.ts'

import { handlers } from './__handlers__'

describe('useListDomainTags', () => {
beforeEach(() => {
server.use(...handlers)
})

afterEach(() => {
server.resetHandlers()
})

it('should load the data', async () => {
const { result } = renderHook(() => useListDomainTags(), { wrapper })
await waitFor(() => {
expect(result.current.isLoading).toBeFalsy()
expect(result.current.isSuccess).toBeTruthy()
})
expect(result.current.data).toStrictEqual<DomainTagList>({
items: [
{
protocolId: 'modbus',
tagDefinition: {
endIdx: 1,
startIdx: 0,
},
tagName: 'test/tag1',
},
],
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { beforeEach, expect } from 'vitest'
import { renderHook, waitFor } from '@testing-library/react'

import { server } from '@/__test-utils__/msw/mockServer.ts'
import { SimpleWrapper as wrapper } from '@/__test-utils__/hooks/SimpleWrapper.tsx'
import { useSamplingForTopic } from '@/api/hooks/useDomainModel/useSamplingForTopic.ts'

import { handlers } from './__handlers__'
import { type JsonNode } from '@/api/__generated__'

describe('useSamplingForTopic', () => {
beforeEach(() => {
server.use(...handlers)
})

afterEach(() => {
server.resetHandlers()
})

it('should load the data', async () => {
const mockTopic = 'topic/test'
const { result } = renderHook(() => useSamplingForTopic(mockTopic), { wrapper })
await waitFor(() => {
expect(result.current.isLoading).toBeFalsy()
expect(result.current.isSuccess).toBeTruthy()
})

expect(result.current.data).toStrictEqual<JsonNode>(
expect.objectContaining({
title: 'dG9waWMvdGVzdA==',
type: 'object',
})
)

// there is no samples so examples are the same as original
expect(result.current.schema).toStrictEqual(result.current.data)

// // TODO[NVL] Not the way to test the refetch - find better
// expect(result.current.isLoading).toBeFalsy()
// act(() => {
// result.current.refetch()
// })
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@ import { useEffect, useMemo } from 'react'
import { reducerSchemaExamples } from '@/components/rjsf/MqttTransformation/utils/json-schema.utils.ts'

export const useSamplingForTopic = (topic: string) => {
const { isSuccess, isPending, isError: isMutateError, error: mutateError, mutate } = useStartSamplingForTopic()
const {
isSuccess: isMutateSuccess,
isPending,
isError: isMutateError,
error: mutateError,
mutate,
} = useStartSamplingForTopic()
const {
data,
isLoading: isSchemaLoading,
isError: isSchemaError,
error: schemaError,
isSuccess: isSchemaSuccess,
} = useGetSchemaForTopic(topic, isSuccess)
} = useGetSchemaForTopic(topic, isMutateSuccess)
const {
data: samples,
error: sampleError,
Expand Down Expand Up @@ -41,6 +47,7 @@ export const useSamplingForTopic = (topic: string) => {
const isLoading = isSchemaLoading || isSampleLoading || isPending
const isError = isSchemaError || isSampleError || isMutateError
const error = schemaError || sampleError || mutateError
const isSuccess = isMutateSuccess || isSchemaSuccess

return { data, schema, isLoading, refetch, isError, error }
return { data, schema, isLoading, isSuccess, refetch, isError, error }
}
Loading

0 comments on commit 5d1a96b

Please sign in to comment.