-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(94): add status connection update routine for the workspace
- add tests
- Loading branch information
Showing
2 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
hivemq-edge/src/frontend/src/modules/EdgeVisualisation/utils/status-utils.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { expect } from 'vitest' | ||
import { Node, NodeProps } from 'reactflow' | ||
|
||
import { Adapter, ConnectionStatus } from '@/api/__generated__' | ||
|
||
import { MOCK_NODE_ADAPTER, MOCK_NODE_BRIDGE, MOCK_NODE_LISTENER } from '@/__test-utils__/react-flow/nodes.ts' | ||
import { updateNodeStatus } from '@/modules/EdgeVisualisation/utils/status-utils.ts' | ||
import { NodeTypes } from '@/modules/EdgeVisualisation/types.ts' | ||
import { mockBridgeId } from '@/api/hooks/useGetBridges/__handlers__' | ||
import { MOCK_ADAPTER_ID } from '@/__test-utils__/mocks.ts' | ||
|
||
const disconnectedBridge: NodeProps = { | ||
...MOCK_NODE_BRIDGE, | ||
data: { | ||
...MOCK_NODE_BRIDGE.data, | ||
bridgeRuntimeInformation: { | ||
connectionStatus: { | ||
status: ConnectionStatus.status.DISCONNECTED, | ||
}, | ||
}, | ||
}, | ||
} | ||
const disconnectedAdapter: NodeProps<Adapter> = { | ||
...MOCK_NODE_ADAPTER, | ||
data: { | ||
...MOCK_NODE_ADAPTER.data, | ||
adapterRuntimeInformation: { | ||
connectionStatus: { | ||
status: ConnectionStatus.status.DISCONNECTED, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
interface Suite { | ||
nodes: Node[] | ||
status: ConnectionStatus[] | ||
expected: Node[] | ||
} | ||
const validationSuite: Suite[] = [ | ||
{ | ||
nodes: [], | ||
status: [], | ||
expected: [], | ||
}, | ||
{ | ||
nodes: [], | ||
status: [ | ||
{ status: ConnectionStatus.status.DISCONNECTED, type: NodeTypes.BRIDGE_NODE, id: 'one' }, | ||
{ status: ConnectionStatus.status.DISCONNECTED, type: NodeTypes.ADAPTER_NODE, id: 'two' }, | ||
], | ||
expected: [], | ||
}, | ||
{ | ||
// @ts-ignore | ||
nodes: [disconnectedBridge, disconnectedAdapter], | ||
status: [{ status: ConnectionStatus.status.CONNECTED, type: NodeTypes.BRIDGE_NODE, id: 'non-existing-bridge' }], | ||
// @ts-ignore | ||
expected: [disconnectedBridge, disconnectedAdapter], | ||
}, | ||
{ | ||
// @ts-ignore | ||
nodes: [MOCK_NODE_LISTENER], | ||
status: [{ status: ConnectionStatus.status.CONNECTED, type: NodeTypes.BRIDGE_NODE, id: 'non-existing-bridge' }], | ||
// @ts-ignore | ||
expected: [MOCK_NODE_LISTENER], | ||
}, | ||
{ | ||
// @ts-ignore | ||
nodes: [disconnectedBridge, disconnectedAdapter], | ||
status: [ | ||
{ status: ConnectionStatus.status.DISCONNECTED, type: NodeTypes.BRIDGE_NODE, id: mockBridgeId }, | ||
{ status: ConnectionStatus.status.DISCONNECTED, type: NodeTypes.ADAPTER_NODE, id: MOCK_ADAPTER_ID }, | ||
], | ||
// @ts-ignore | ||
expected: [disconnectedBridge, disconnectedAdapter], | ||
}, | ||
{ | ||
// @ts-ignore | ||
nodes: [disconnectedBridge, disconnectedAdapter], | ||
status: [ | ||
{ status: ConnectionStatus.status.CONNECTING, type: NodeTypes.BRIDGE_NODE, id: mockBridgeId }, | ||
{ status: ConnectionStatus.status.CONNECTING, type: NodeTypes.ADAPTER_NODE, id: MOCK_ADAPTER_ID }, | ||
], | ||
expected: expect.arrayContaining([ | ||
expect.objectContaining({ | ||
data: expect.objectContaining({ | ||
bridgeRuntimeInformation: expect.objectContaining({ | ||
connectionStatus: expect.objectContaining({ status: ConnectionStatus.status.CONNECTING }), | ||
}), | ||
}), | ||
}), | ||
]), | ||
}, | ||
] | ||
|
||
describe('updateNodeStatus', () => { | ||
it.each<Suite>(validationSuite)('should work', ({ nodes, status, expected }) => { | ||
const updatedNodes = updateNodeStatus(nodes, status) | ||
expect(updatedNodes.length).toBe(nodes.length) | ||
expect(updatedNodes).toStrictEqual(expected) | ||
}) | ||
}) |
37 changes: 37 additions & 0 deletions
37
hivemq-edge/src/frontend/src/modules/EdgeVisualisation/utils/status-utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Node } from 'reactflow' | ||
import { Adapter, Bridge, ConnectionStatus } from '@/api/__generated__' | ||
import { NodeTypes } from '@/modules/EdgeVisualisation/types.ts' | ||
|
||
export const updateNodeStatus = (currentNodes: Node[], updates: ConnectionStatus[]) => { | ||
return currentNodes.map((n) => { | ||
if (n.type === NodeTypes.BRIDGE_NODE) { | ||
const newData = { ...n.data } as Bridge | ||
const newStatus = updates.find((s) => s.id === newData.id) | ||
if (!newStatus) return n | ||
if (newStatus.status === newData.bridgeRuntimeInformation?.connectionStatus?.status) return n | ||
|
||
n.data = { | ||
...newData, | ||
bridgeRuntimeInformation: { | ||
connectionStatus: newStatus, | ||
}, | ||
} | ||
return n | ||
} | ||
if (n.type === NodeTypes.ADAPTER_NODE) { | ||
const newData = { ...n.data } as Adapter | ||
const newStatus = updates.find((s) => s.id === newData.id) | ||
if (!newStatus) return n | ||
if (newStatus.status === newData.adapterRuntimeInformation?.connectionStatus?.status) return n | ||
|
||
n.data = { | ||
...newData, | ||
adapterRuntimeInformation: { | ||
connectionStatus: newStatus, | ||
}, | ||
} | ||
return n | ||
} | ||
return n | ||
}) | ||
} |