|
| 1 | +import React, { PropsWithChildren } from 'react'; |
| 2 | + |
| 3 | +import { renderHook, act, waitFor } from '@testing-library/react'; |
| 4 | + |
| 5 | +import { |
| 6 | + WorkflowManager, |
| 7 | + WorkflowManagerConfig, |
| 8 | + useMqtt, |
| 9 | + useSubscribe, |
| 10 | + useUnsubscribe, |
| 11 | +} from '../src'; |
| 12 | +import { store } from './store'; |
| 13 | +import { MqttClientExtended } from './types'; |
| 14 | + |
| 15 | +const props = { |
| 16 | + brokerUrl: 'ws://broker.mqttdashboard.com:8000/mqtt', |
| 17 | + options: { |
| 18 | + clientId: `clientId-${Math.random().toString(16).substring(2, 8)}`, |
| 19 | + reconnectPeriod: 1000, |
| 20 | + }, |
| 21 | +}; |
| 22 | + |
| 23 | +let wrapper: React.FC<PropsWithChildren>; |
| 24 | + |
| 25 | +describe('WorkflowManager component', () => { |
| 26 | + beforeEach(() => { |
| 27 | + WorkflowManagerConfig.setStore(store); |
| 28 | + |
| 29 | + wrapper = ({ children }) => ( |
| 30 | + <WorkflowManager {...props}>{children}</WorkflowManager> |
| 31 | + ); |
| 32 | + }); |
| 33 | + |
| 34 | + it('should connect with mqtt', async () => { |
| 35 | + const { result } = renderHook(() => useMqtt(), { wrapper }); |
| 36 | + |
| 37 | + await waitFor( |
| 38 | + () => { |
| 39 | + const status = result.current?.status; |
| 40 | + return expect(status === 'connected').toBe(true); |
| 41 | + }, |
| 42 | + { timeout: 2000 }, |
| 43 | + ); |
| 44 | + |
| 45 | + expect(result.current?.status).toBe('connected'); |
| 46 | + |
| 47 | + await act(async () => { |
| 48 | + result.current.client?.end(); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should not connect with mqtt: invalid broker url', async () => { |
| 53 | + const { result } = renderHook(() => useMqtt(), { |
| 54 | + wrapper: ({ children }) => ( |
| 55 | + <WorkflowManager |
| 56 | + brokerUrl="ws://broker.mqttdashboard@@.com:8000/mqtt" |
| 57 | + options={{ ...props.options, connectTimeout: 2000 }} |
| 58 | + > |
| 59 | + {children} |
| 60 | + </WorkflowManager> |
| 61 | + ), |
| 62 | + }); |
| 63 | + |
| 64 | + await waitFor( |
| 65 | + () => { |
| 66 | + const status = result.current?.status; |
| 67 | + |
| 68 | + return expect(status === 'reconnecting').toBe(true); |
| 69 | + }, |
| 70 | + { timeout: 2000 }, |
| 71 | + ); |
| 72 | + |
| 73 | + expect(result.current?.status).toBe('reconnecting'); |
| 74 | + |
| 75 | + await act(async () => { |
| 76 | + result.current.client?.end(); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should subscribe to topics', async () => { |
| 81 | + const { result: resultSub } = renderHook(() => useSubscribe(), { wrapper }); |
| 82 | + const { result: resultMqtt } = renderHook(() => useMqtt(), { wrapper }); |
| 83 | + |
| 84 | + const subscribe = resultSub.current; |
| 85 | + const client = resultMqtt.current.client as MqttClientExtended; |
| 86 | + |
| 87 | + await waitFor( |
| 88 | + () => { |
| 89 | + const status = resultMqtt.current?.status; |
| 90 | + return expect(status === 'connected').toBe(true); |
| 91 | + }, |
| 92 | + { timeout: 2000 }, |
| 93 | + ); |
| 94 | + |
| 95 | + expect(resultMqtt.current.status).toBe('connected'); |
| 96 | + |
| 97 | + const topic = 'workflowManager/test/1'; |
| 98 | + |
| 99 | + subscribe(topic); |
| 100 | + |
| 101 | + const lastMessageId = client?.getLastMessageId(); |
| 102 | + const subscribedTopics = client?.messageIdToTopic[lastMessageId]; |
| 103 | + |
| 104 | + expect(subscribedTopics).toContain(topic); |
| 105 | + |
| 106 | + await act(async () => { |
| 107 | + client?.end(); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + it('should unsubscribe from topics', async () => { |
| 112 | + const { result: resultSub } = renderHook(() => useSubscribe(), { wrapper }); |
| 113 | + const { result: resultUnsub } = renderHook(() => useUnsubscribe(), { |
| 114 | + wrapper, |
| 115 | + }); |
| 116 | + const { result: resultMqtt } = renderHook(() => useMqtt(), { wrapper }); |
| 117 | + |
| 118 | + const subscribe = resultSub.current; |
| 119 | + const unsubscribe = resultUnsub.current; |
| 120 | + const client = resultMqtt.current.client as MqttClientExtended; |
| 121 | + |
| 122 | + await waitFor( |
| 123 | + () => { |
| 124 | + const status = resultMqtt.current?.status; |
| 125 | + return expect(status === 'connected').toBe(true); |
| 126 | + }, |
| 127 | + { timeout: 2000 }, |
| 128 | + ); |
| 129 | + |
| 130 | + expect(resultMqtt.current.status).toBe('connected'); |
| 131 | + |
| 132 | + const topic = 'workflowManager/test/1'; |
| 133 | + |
| 134 | + subscribe(topic); |
| 135 | + |
| 136 | + const lastMessageId = client?.getLastMessageId(); |
| 137 | + const subscribedTopics = client?.messageIdToTopic[lastMessageId]; |
| 138 | + |
| 139 | + expect(subscribedTopics).toContain(topic); |
| 140 | + |
| 141 | + unsubscribe(topic); |
| 142 | + |
| 143 | + const newLastMessageId = client?.getLastMessageId(); |
| 144 | + const newSubscribedTopics = |
| 145 | + client?.messageIdToTopic[newLastMessageId] || []; |
| 146 | + |
| 147 | + expect(newSubscribedTopics).not.toContain(topic); |
| 148 | + |
| 149 | + await act(async () => { |
| 150 | + client?.end(); |
| 151 | + }); |
| 152 | + }); |
| 153 | +}); |
0 commit comments