Skip to content

Commit

Permalink
test(139): fix test for topic rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
vanch3d committed Oct 12, 2023
1 parent aa95b81 commit 5e43b79
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import { MOCK_TOPIC_REF1 } from '@/__test-utils__/react-flow/topics.ts'

import { formatTopicString } from '@/components/MQTT/topic-utils.ts'

import Topic from './Topic.tsx'

describe('Topic', () => {
Expand All @@ -12,7 +14,7 @@ describe('Topic', () => {
it('should render', () => {
cy.mountWithProviders(<Topic topic={MOCK_TOPIC_REF1} />)

cy.getByTestId('topic-wrapper').should('contain.text', 'root/topic/ref/1')
cy.getByTestId('topic-wrapper').should('contain.text', formatTopicString('root/topic/ref/1'))
})

it('should be accessible', () => {
Expand Down
3 changes: 2 additions & 1 deletion hivemq-edge/src/frontend/src/components/MQTT/Topic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { FC, ReactNode } from 'react'
import { Tag, TagLabel, TagProps } from '@chakra-ui/react'

import TopicIcon from '@/components/Icons/TopicIcon.tsx'
import { formatTopicString } from '@/components/MQTT/topic-utils.ts'

// TODO[NVL] Not sure adding ReactNode as possible children is a good move.
interface TopicProps extends TagProps {
topic: ReactNode
}

const Topic: FC<TopicProps> = ({ topic, ...rest }) => {
const expandedTopic = typeof topic === 'string' ? topic.split('/').join(' / ') : topic
const expandedTopic = typeof topic === 'string' ? formatTopicString(topic) : topic
return (
<Tag data-testid={'topic-wrapper'} {...rest} letterSpacing={'-0.05rem'}>
<TopicIcon boxSize="12px" mr={2} />
Expand Down
19 changes: 19 additions & 0 deletions hivemq-edge/src/frontend/src/components/MQTT/topic-utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { expect } from 'vitest'
import { formatTopicString } from '@/components/MQTT/topic-utils.ts'

interface TestEachSuite {
topic: string
expected: string
}

describe('formatTopicString', () => {
it.each<TestEachSuite>([
{ topic: '', expected: '' },
{ topic: '12345', expected: '12345' },
{ topic: '123/45', expected: '123 / 45' },
{ topic: '12/3/45', expected: '12 / 3 / 45' },
{ topic: '12/3/45/', expected: '12 / 3 / 45 / ' },
])('should returns $expected with $topic', ({ topic, expected }) => {
expect(formatTopicString(topic)).toStrictEqual(expected)
})
})
3 changes: 3 additions & 0 deletions hivemq-edge/src/frontend/src/components/MQTT/topic-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const TOPIC_SEPARATOR = ' / '

export const formatTopicString = (topic: string) => topic.split('/').join(TOPIC_SEPARATOR)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import { mockReactFlow } from '@/__test-utils__/react-flow/providers.tsx'
import { MOCK_NODE_ADAPTER } from '@/__test-utils__/react-flow/nodes.ts'
import { MOCK_TOPIC_REF1, MOCK_TOPIC_REF2 } from '@/__test-utils__/react-flow/topics.ts'
import { MOCK_ADAPTER_ID } from '@/__test-utils__/mocks.ts'

import { mockProtocolAdapter } from '@/api/hooks/useProtocolAdapters/__handlers__'
import { formatTopicString } from '@/components/MQTT/topic-utils.ts'

import NodeAdapter from './NodeAdapter.tsx'

Expand All @@ -21,8 +23,8 @@ describe('NodeAdapter', () => {
cy.getByTestId('connection-status').should('contain.text', 'Connected')
cy.getByTestId('topics-container')
.should('be.visible')
.should('contain.text', MOCK_TOPIC_REF1)
.should('contain.text', MOCK_TOPIC_REF2)
.should('contain.text', formatTopicString(MOCK_TOPIC_REF1))
.should('contain.text', formatTopicString(MOCK_TOPIC_REF2))
})

it('should be accessible', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import { MOCK_NODE_BRIDGE } from '@/__test-utils__/react-flow/nodes.ts'
import { mockReactFlow } from '@/__test-utils__/react-flow/providers.tsx'
import { MOCK_TOPIC_ACT1, MOCK_TOPIC_ALL } from '@/__test-utils__/react-flow/topics.ts'

import { mockProtocolAdapter } from '@/api/hooks/useProtocolAdapters/__handlers__'
import { formatTopicString } from '@/components/MQTT/topic-utils.ts'

import NodeBridge from './NodeBridge.tsx'
import { MOCK_TOPIC_ACT1, MOCK_TOPIC_ALL } from '@/__test-utils__/react-flow/topics.ts'

describe('NodeBridge', () => {
beforeEach(() => {
Expand All @@ -24,8 +26,14 @@ describe('NodeBridge', () => {
// .should('contain.text', MOCK_TOPIC_REF2)

cy.getByTestId('topics-container').should('have.length', 2)
cy.getByTestId('topics-container').eq(0).should('be.visible').should('contain.text', MOCK_TOPIC_ACT1)
cy.getByTestId('topics-container').eq(1).should('be.visible').should('contain.text', MOCK_TOPIC_ALL)
cy.getByTestId('topics-container')
.eq(0)
.should('be.visible')
.should('contain.text', formatTopicString(MOCK_TOPIC_ACT1))
cy.getByTestId('topics-container')
.eq(1)
.should('be.visible')
.should('contain.text', formatTopicString(MOCK_TOPIC_ALL))
})

it('should be accessible', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/// <reference types="cypress" />

import { formatTopicString } from '@/components/MQTT/topic-utils.ts'

import TopicsContainer from './TopicsContainer.tsx'

describe('NodeListener', () => {
describe('TopicsContainer', () => {
beforeEach(() => {
cy.viewport(400, 400)
})
Expand All @@ -22,8 +24,8 @@ describe('NodeListener', () => {
)

cy.getByTestId('topic-wrapper').should('have.length', 2)
cy.getByTestId('topic-wrapper').eq(0).should('contain.text', 'my/first/topic')
cy.getByTestId('topic-wrapper').eq(1).should('contain.text', 'my/second/topic')
cy.getByTestId('topic-wrapper').eq(0).should('contain.text', formatTopicString('my/first/topic'))
cy.getByTestId('topic-wrapper').eq(1).should('contain.text', formatTopicString('my/second/topic'))

cy.getByTestId('topics-show-more').should('not.exist')
})
Expand All @@ -46,8 +48,8 @@ describe('NodeListener', () => {
)

cy.getByTestId('topic-wrapper').should('have.length', 2)
cy.getByTestId('topic-wrapper').eq(0).should('contain.text', 'my/first/topic')
cy.getByTestId('topic-wrapper').eq(1).should('contain.text', 'my/second/topic')
cy.getByTestId('topic-wrapper').eq(0).should('contain.text', formatTopicString('my/first/topic'))
cy.getByTestId('topic-wrapper').eq(1).should('contain.text', formatTopicString('my/second/topic'))

cy.getByTestId('topics-show-more').should('be.visible').should('contain.text', '+1')
})
Expand Down

0 comments on commit 5e43b79

Please sign in to comment.