Skip to content
Snippets Groups Projects
Commit e453fe2c authored by Paul Kaplan's avatar Paul Kaplan
Browse files

Fix problem with updating cloud vars to zero.

Added a unit test and covered the other case (not used) where you might be trying to update the index zero of a list
parent fc1e1cce
No related branches found
No related tags found
No related merge requests found
......@@ -79,11 +79,14 @@ class CloudProvider {
msg.user = this.username;
msg.project_id = this.projectId;
// Optional string params can use simple falsey undefined check
if (dataName) msg.name = dataName;
if (dataValue) msg.value = dataValue;
if (dataIndex) msg.index = dataIndex;
if (dataNewName) msg.new_name = dataNewName;
// Optional number params need different undefined check
if (typeof dataValue !== 'undefined') msg.value = dataValue;
if (typeof dataValue !== 'undefined') msg.index = dataIndex;
const dataToWrite = JSON.stringify(msg);
this.sendCloudData(dataToWrite);
}
......
import CloudProvider from '../../../src/lib/cloud-provider';
// Disable window.WebSocket
global.WebSocket = null;
describe('CloudProvider', () => {
let cloudProvider = null;
let sentMessage = null;
beforeEach(() => {
cloudProvider = new CloudProvider();
// Stub connection
cloudProvider.connection = {
send: msg => {
sentMessage = msg;
}
};
});
test('updateVariable', () => {
cloudProvider.updateVariable('hello', 1);
const obj = JSON.parse(sentMessage);
expect(obj.method).toEqual('set');
expect(obj.name).toEqual('hello');
expect(obj.value).toEqual(1);
});
test('updateVariable with falsey value', () => {
cloudProvider.updateVariable('hello', 0);
const obj = JSON.parse(sentMessage);
expect(obj.method).toEqual('set');
expect(obj.name).toEqual('hello');
expect(obj.value).toEqual(0);
});
test('writeToServer with falsey index value', () => {
cloudProvider.writeToServer('method', 'name', 5, 0);
const obj = JSON.parse(sentMessage);
expect(obj.method).toEqual('method');
expect(obj.name).toEqual('name');
expect(obj.value).toEqual(5);
expect(obj.index).toEqual(0);
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment