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

chore(tests): add unit tests for js/modules #180

Open
wants to merge 24 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fdc3b58
chore(tests): initial commit for module tests
mellodev Nov 30, 2024
c5f60be
chore(tests): initial commit for module tests
mellodev Nov 30, 2024
5757f40
chore(tests): work on analog.test.js
mellodev Nov 30, 2024
d21ff7b
Suggested changes for tests to analog.js (#1)
salbahra Dec 2, 2024
11d7f41
chore(merge): fix merge conflicts
mellodev Dec 2, 2024
f72e7c3
chore(tests): add tests for modules - card-list, cards, dates
mellodev Dec 4, 2024
7fbdde4
chore(tests): update tests for dates
mellodev Dec 4, 2024
78a1bfd
chore(tests): update tests for module: errors
mellodev Dec 4, 2024
5a28677
chore(tests): add tests for module: firmware
mellodev Dec 4, 2024
9dea1a5
chore(tests): add tests for module: language
mellodev Dec 5, 2024
65c531a
chore(tests): add tests for module: logs
mellodev Dec 5, 2024
25ca0a8
chore(tests): add tests for module: notifications
mellodev Dec 5, 2024
646d9f2
chore(tests): add tests for module: options
mellodev Dec 5, 2024
da0bb05
chore(tests): add tests for module: programs
mellodev Dec 5, 2024
c5fe22b
chore(tests): add tests for module: sites
mellodev Dec 5, 2024
521f9a6
chore(tests): add tests for module: station-attributes, station-queue
mellodev Dec 5, 2024
a0bd79a
chore(tests): add tests for module: stations
mellodev Dec 5, 2024
dbf6a3a
chore(tests): add tests for module: status
mellodev Dec 5, 2024
761efc2
chore(tests): add tests for module: storage
mellodev Dec 5, 2024
6ba6adc
chore(tests): add tests for module: supported
mellodev Dec 5, 2024
496ecac
chore(tests): add tests for module: status, utils
mellodev Dec 5, 2024
b1e0a5c
chore(tests): add tests for module: weather
mellodev Dec 5, 2024
988b73b
chore(tests): clean up comments
mellodev Dec 5, 2024
2909857
chore(tests): fix date related tests
mellodev Dec 5, 2024
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
19 changes: 19 additions & 0 deletions test/prepare_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,25 @@ before(function() {
});
});

before(function () {
OSApp.currentSession.ip = "demo.opensprinkler.com";
OSApp.currentSession.pass = "opendoor";
OSApp.currentSession.prefix = "https://";
OSApp.currentSession.fw183 = false;

OSApp.Sites.updateSiteList(["Test"], "Test");
});

describe("Page Initialization Checks", function () {
it("Start jQuery Mobile Page Initialization", function (done) {
$.mobile.document.one("pageshow", "#sprinklers", function () {
done();
});

OSApp.Sites.newLoad();
});
});

after(function() {
if (server) {
server.restore();
Expand Down
124 changes: 124 additions & 0 deletions test/tests/modules/analog.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/* eslint-disable */

/* OpenSprinkler App
* Copyright (C) 2015 - present, Samer Albahra. All rights reserved.
*
* This file is part of the OpenSprinkler project <http://opensprinkler.com>.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License version 3 as
* published by the Free Software Foundation.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/* global describe, beforeEach, afterEach, OSApp, assert */

// Having test timeout issue, skipping until resolved
describe.skip("OSApp.Analog", function () {
describe("checkAnalogSensorAvail", function () {
it("should return true if currentSession.controller.options.feature is 'ASB'", function () {
assert.isTrue(OSApp.Analog.checkAnalogSensorAvail());
});
});

describe("updateProgramAdjustments", function () {
it("should update OSApp.Analog.progAdjusts with data from /se?pw=", function (done) {
OSApp.Analog.updateProgramAdjustments(function () {
assert.deepEqual(OSApp.Analog.progAdjusts, [{ nr: 1, type: 1, sensor: 1, prog: 1, current: 0.8, factor1: 1.2, factor2: 0.8, min: 10, max: 40 },
{ nr: 2, type: 2, sensor: 2, prog: 2, current: 0.5, factor1: 1.5, factor2: 0.5, min: 20, max: 80 }]);
done();
});
});
});

describe("updateAnalogSensor", function () {
it("should update OSApp.Analog.analogSensors with data from /sl?pw=", function (done) {
OSApp.Analog.updateAnalogSensor(function () {
assert.deepEqual(OSApp.Analog.analogSensors, [{ nr: 1, name: "Sensor 1", data: 25.5, unit: "°C", show: true, last: 1678886400, data_ok: true },
{ nr: 2, name: "Sensor 2", data: 60.3, unit: "%", show: false, last: 1678886400, data_ok: false }]);
done();
});
});
});

describe("updateSensorShowArea", function () {
it("should update the #os-sensor-show area with sensor and program adjustment data", function () {
var page = $("<div><div id='os-sensor-show'></div></div>");
OSApp.Analog.updateSensorShowArea(page);
// Assertions would go here, but the current mocking makes it difficult to assert the exact HTML structure
});
});

describe("toByteArray", function () {
it("should convert an integer to a byte array", function () {
assert.deepEqual(OSApp.Analog.toByteArray(16777216), Uint8Array.from([0, 0, 0, 1]));
});
});

describe("intFromBytes", function () {
it("should convert a byte array to an integer", function () {
assert.equal(OSApp.Analog.intFromBytes([0, 0, 0, 1]), 16777216);
});
});

describe("showAdjustmentsEditor", function () {
it("should open a popup with program adjustment editor", function (done) {
var progAdjust = { nr: 1, type: 1, sensor: 1, prog: 1, factor1: 1.2, factor2: 0.8, min: 10, max: 40 };
OSApp.Analog.showAdjustmentsEditor(progAdjust, function (progAdjustOut) {
assert.deepEqual(progAdjustOut, { nr: 1, type: 1, sensor: 1, prog: 1, factor1: 1.2, factor2: 0.8, min: 10, max: 40 });
done();
}).then(function () {
console.log("clicking submit");
document.querySelector(("#progAdjustEditor .submit")).click();
});
});
});

describe("isSmt100", function () {
it("should return true if sensorType is 1 or 2", function () {
assert.isTrue(OSApp.Analog.isSmt100(1));
});
});

describe("showSensorEditor", function () {
it("should open a popup with sensor editor", function (done) {
var sensor = { nr: 1, name: "Sensor 1", type: 1, ri: 600, enable: 1, log: 1 };
OSApp.Analog.showSensorEditor(sensor, function (sensorOut) {
assert.deepEqual(sensorOut, { nr: 1, type: 1, group: 1, name: "test", ip: 16777216, port: 1, id: 1, ri: 1, fac: 1, div: 1, unit: "test", enable: 1, log: 1, show: 1 });
done();
});
});
});

describe("showAnalogSensorConfig", function () {
it("should change header, update content and append page", function () {
OSApp.Analog.showAnalogSensorConfig();
// Assertions would go here, but the current mocking makes it difficult to assert the exact HTML structure
});
});

describe("buildSensorConfig", function () {
it("should build the analog sensor configuration HTML", function () {
var result = OSApp.Analog.buildSensorConfig();
// Assertions would go here, but the current mocking makes it difficult to assert the exact HTML structure
});
});

describe("showAnalogSensorCharts", function () {
it("should show the analog sensor charts", function () {
OSApp.Analog.showAnalogSensorCharts();
// Assertions would go here, but the current mocking makes it difficult to assert the exact behavior
});
});

describe("buildGraph", function () {
it("should build the graph with the given data", function () {
var chart = [];
var csv = "1;1678872000;25.5\n2;1678872000;60.3";
OSApp.Analog.buildGraph("#myChart", chart, csv, "last 24h", "HH:mm");
// Assertions would go here, but the current mocking makes it difficult to assert the exact behavior
});
});
});
57 changes: 57 additions & 0 deletions test/tests/modules/card-list.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* OpenSprinkler App
* Copyright (C) 2015 - present, Samer Albahra. All rights reserved.
*
* This file is part of the OpenSprinkler project <http://opensprinkler.com>.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License version 3 as
* published by the Free Software Foundation.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/* global describe, beforeEach, OSApp, assert */

describe('OSApp.CardList', function() {
var cardList;

beforeEach(function() {
// Mock the card list using jQuery to create a set of dummy cards
cardList = $("<div class='card' data-station='A'></div>" +
"<div class='card' data-station='B'></div>" +
"<div class='card' data-station='C'></div>");
});

describe('getAllCards', function() {
it('should return all elements with the class "card"', function() {
var cards = OSApp.CardList.getAllCards(cardList);
assert.equal(cards.length, 3);
});

it('should return an empty jQuery object if no cards are found', function() {
cardList = $("<div></div>"); // No .card elements
var cards = OSApp.CardList.getAllCards(cardList);
assert.equal(cards.length, 0);
});
});

describe('getCardBySID', function() {
it('should return the card with the specified SID', function() {
var card = OSApp.CardList.getCardBySID(cardList, 'B');
assert.equal(card.length, 1);
assert.equal(card.data('station'), 'B');
});

it('should return an empty jQuery object if no card with the SID is found', function() {
var card = OSApp.CardList.getCardBySID(cardList, 'Z');
assert.equal(card.length, 0);
});
});

describe('getCardByIndex', function() {
it('should return the card at the specified index', function() {
// Skipping this test for now since it uses jQuery internally
});
});
});
128 changes: 128 additions & 0 deletions test/tests/modules/cards.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@


/* OpenSprinkler App
* Copyright (C) 2015 - present, Samer Albahra. All rights reserved.
*
* This file is part of the OpenSprinkler project <http://opensprinkler.com>.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License version 3 as
* published by the Free Software Foundation.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

describe('OSApp.Cards', function() {
var cardObj;

beforeEach(function() {
cardObj = $("<div class='card' data-station='A'>" +
"<div><span></span><span data-gid='1'></span></div>" +
"<div class='content-divider'></div>" +
"<div class='station-gid'></div>" +
"</div>");

// Store original values
originalSupported = OSApp.Supported;
originalStations = OSApp.Stations;

// Mock dependencies
OSApp.Supported = { groups: function() { return true; } };
OSApp.Groups = { mapGIDValueToName: function(gid) {
return 'Group ' + gid;
}
};
OSApp.Stations = {
getGIDValue: function(sid) {
return 1;
},
isMaster: function(sid) {
return sid === 'A';
}
};
});

afterEach(function() {
// Restore original values
OSApp.Supported = originalSupported;
OSApp.Stations = originalStations;
});

describe('getSID', function() {
it('should return the station ID', function() {
var sid = OSApp.Cards.getSID(cardObj);
assert.equal(sid, 'A');
});
});

describe('getDivider', function() {
it('should return the divider element', function() {
var divider = OSApp.Cards.getDivider(cardObj);
assert.equal(divider.hasClass('content-divider'), true);
});
});

describe('getGroupLabel', function() {
it('should return the group label (groups supported)', function() {
var groupLabel = OSApp.Cards.getGroupLabel(cardObj);
assert.equal(groupLabel.hasClass('station-gid'), true);
});

it('should return undefined (groups not supported)', function() {
OSApp.Supported.groups = function() { return false; };
var groupLabel = OSApp.Cards.getGroupLabel(cardObj);
assert.equal(groupLabel, undefined);
});
});

describe('setGroupLabel', function() {
it('should set group label text (groups supported)', function() {
OSApp.Cards.setGroupLabel(cardObj, 'Group 1');
var groupLabel = OSApp.Cards.getGroupLabel(cardObj);
assert.equal(groupLabel.text(), 'Group 1');
assert.equal(groupLabel.hasClass('hidden'), false);
});

it('should do nothing (groups not supported)', function() {
OSApp.Supported.groups = function() { return false; };
var groupLabelBefore = OSApp.Cards.getGroupLabel(cardObj);
OSApp.Cards.setGroupLabel(cardObj, 'Group 1');
var groupLabelAfter = OSApp.Cards.getGroupLabel(cardObj);
assert.equal(groupLabelBefore, groupLabelAfter);
});
});

describe('getGIDValue', function() {
it('should return the GID value', function() {
var gid = OSApp.Cards.getGIDValue(cardObj);
assert.equal(gid, 1);
});

it('should return 0 (groups not supported)', function() {
OSApp.Supported.groups = function() { return false; };
var gid = OSApp.Cards.getGIDValue(cardObj);
assert.equal(gid, 0);
});
});

describe('getGIDName', function() {
it('should return the GID name', function() {
var gidName = OSApp.Cards.getGIDName(cardObj);
assert.equal(gidName, 'Group 1');
});
});

describe('isMasterStation', function() {
it('should return true for master station', function() {
var isMaster = OSApp.Cards.isMasterStation(cardObj);
assert.equal(isMaster, true);
});

it('should return false for non-master station', function() {
cardObj.data('station', 'B');
var isMaster = OSApp.Cards.isMasterStation(cardObj);
assert.equal(isMaster, false);
});
});
});
Loading
Loading