Skip to content

Commit

Permalink
Add unit tests for topic naming functions, switch to lower-case
Browse files Browse the repository at this point in the history
  • Loading branch information
Jalle19 committed Oct 20, 2023
1 parent f90196b commit c2602ef
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/publisher/mqtt/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@ import { TOPIC_PREFIX } from '../mqtt'
import slugify from 'slugify'

const slugifyName = (name: string): string => {
// We can't have "+" in MQTT topic names
return slugify(name, { remove: /[+]/ })
return slugify(name, {
// We can't have "+" in MQTT topic names
remove: /[+]/,
// Since the rest of the topic name is lower-case, just lower-case everything
lower: true,
})
}

export const createCharacteristicsSensorTopicName = (characteristics: Characteristics, value: string): string => {
Expand Down
19 changes: 19 additions & 0 deletions tests/publisher/mqtt/util.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Characteristics } from '../../../src/characteristics'
import { createCharacteristicsSensorTopicName, createPowerSensorTopicName } from '../../../src/publisher/mqtt/util'
import { Circuit } from '../../../src/circuit'

test('creates correct topic names', () => {
const characteristics = {
name: 'Some characteristic',
} as unknown as Characteristics

let topic = createCharacteristicsSensorTopicName(characteristics, 'voltage')
expect(topic).toEqual('eachwatt/characteristic/some-characteristic/voltage')

const circuit = {
name: 'Some circuit + with plus sign',
} as unknown as Circuit

topic = createPowerSensorTopicName(circuit, 'power')
expect(topic).toEqual('eachwatt/circuit/some-circuit-with-plus-sign/power')
})

0 comments on commit c2602ef

Please sign in to comment.