Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: utils test #88

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 31 additions & 37 deletions src/pahoMqttPlugin/utils/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { getMqttOptions, setMqttOptions } from '~/config/options';
import { MqttMode } from '~/types/types';
import * as UTILS from '~/utils';

/* subscribe */
let unhandledTopicsList: string[] = [];
describe.runIf(process.env.NODE_ENV === 'broker')('utils', () => {
test('if status is set right before connection', () => {
expect(UTILS.status()).toBe('disconnected');
Expand Down Expand Up @@ -33,50 +35,42 @@ describe.runIf(process.env.NODE_ENV === 'broker')('utils', () => {
);

const mqttModes = Object.keys(MQTT_STATE) as MqttMode[];
/* subscribe */
let unhandledTopicsList: string[] = [];
describe.concurrent('multiple subscribers', () => {
test.each(mqttModes)(
'should subscribe to "%s"',
(mode: MqttMode) =>
new Promise((done: DoneCallback) => {
setTimeout(() => {
const topic = `test${mode}`;
const payload = `test${mode}`;
unhandledTopicsList.push(topic);
UTILS.subscribe(topic, (data: string) => {
expect(data).toBe(payload);
unhandledTopicsList = unhandledTopicsList.filter(
(_top: string) => _top !== topic,
);
});
done();
}, 0);
}),
);

describe('multiple subscribers', () => {
test.each(mqttModes)('should subscribe to "%s"', (mode: MqttMode) => {
const topic = `test${mode}`;
const payload = `test${mode}`;
unhandledTopicsList.push(topic);
UTILS.subscribe(topic, (data: string) => {
expect(data).toBe(payload);
unhandledTopicsList = unhandledTopicsList.filter(
(_top: string) => _top !== topic,
);
});
// Simulate immediate payload handling
UTILS.publish(topic, payload, mode);
});
});

describe.concurrent('multiple publish with different modes', () => {
describe('multiple publish with different modes', () => {
/* publish for each */
it.concurrent.each(mqttModes)(
`should publish publish to ${getMqttOptions().mainTopic}/test%s'`,
(mode: MqttMode) =>
new Promise((done: DoneCallback) => {
const topic = `test${mode}`;
const payload = `test${mode}`;
setTimeout(() => {
UTILS.publish(topic, payload, mode);
done();
}, 200);
}),
test.each(mqttModes)(
`should publish to ${getMqttOptions().mainTopic}/test%s'`,
(mode: MqttMode) => {
const topic = `test${mode}`;
const payload = `test${mode}`;
// Subscribe and immediately verify publication
UTILS.subscribe(topic, (data: string) => {
expect(data).toBe(payload);
});
UTILS.publish(topic, payload, mode);
},
);
test.concurrent('publish test message with Fnr mode', () => {
UTILS.publish('testFnr', 'testFnr', 'Fnr');
});
});
/* msgHandlers */
describe('msgHandlers', () => {
beforeEach(() => {
unhandledTopicsList = [];
UTILS.clearMsgHandlers();
});
test('if msgHandlers gets cleared', () => {
Expand Down Expand Up @@ -109,10 +103,10 @@ describe.runIf(process.env.NODE_ENV === 'broker')('utils', () => {

test('if all subscribed topics recieved the payload', () => {
if (unhandledTopicsList[0] !== 'testFnr') {
console.log(unhandledTopicsList);
expect(unhandledTopicsList).toHaveLength(0);
} else expect(unhandledTopicsList).toHaveLength(1);
});

test('if disconnects from the broker and sets correct status', () => {
expect(UTILS.disconnectClient()).resolves.toBe(true);
});
Expand Down
Loading