tests(web-app): serie unit tests

This commit is contained in:
fastium
2025-06-14 17:28:39 +02:00
parent 5e9f9d6ad2
commit cecd04dd20
2 changed files with 133 additions and 3 deletions

View File

@@ -0,0 +1,132 @@
/// <reference types="cypress" />
import { Serie } from "../../src/Measures/Serie";
import { TEMPERATURE, HUMIDITY } from "../../src/const";
import { Colors } from "../../src/Measures/Utils";
describe("Serie Component Tests", () => {
// Mock data for tests
const mockDataTemperature = [
{ time: 1625097600000, value: new Date(22.5) },
{ time: 1625184000000, value: new Date(23.8) },
];
const mockDataHumidity = [
{ time: 1625097600000, value: new Date(45) },
{ time: 1625184000000, value: new Date(48) },
];
it("should initialize with correct properties", () => {
// Create a new Serie instance
const serie = new Serie(
TEMPERATURE,
mockDataTemperature,
"user1",
"bedroom",
"sensor1"
);
// Verify that it has the correct type
expect(serie).to.be.instanceOf(Serie);
});
it("should return the correct label for temperature", () => {
const serie = new Serie(
TEMPERATURE,
mockDataTemperature,
"user1",
"bedroom",
"sensor1"
);
expect(serie.getLabel()).to.equal("Temperature [°C]");
});
it("should return the correct label for humidity", () => {
const serie = new Serie(
HUMIDITY,
mockDataHumidity,
"user1",
"bedroom",
"sensor1"
);
expect(serie.getLabel()).to.equal("Humidity [%]");
});
it("should return 'Unknown' label for unknown type", () => {
const serie = new Serie(
"UNKNOWN_TYPE",
mockDataTemperature,
"user1",
"bedroom",
"sensor1"
);
expect(serie.getLabel()).to.equal("Unknown");
});
it("should format temperature data correctly with getSerie()", () => {
const serie = new Serie(
TEMPERATURE,
mockDataTemperature.map((item) => ({
time: item.time,
value: new Date(item.value),
})),
"user1",
"bedroom",
"sensor1"
);
const result = serie.getSerie();
// Verify the properties of the formatted data
expect(result).to.have.property("label", "Temperature [°C]");
expect(result).to.have.property("borderColor", Colors.BLUE);
expect(result).to.have.property("backgroundColor", Colors.BLUE);
expect(result).to.have.property("borderWidth", 1);
expect(result).to.have.property("yAxisID", TEMPERATURE);
// Verify the data points
expect(result.data).to.have.length(mockDataTemperature.length);
expect(result.data[0]).to.have.property("x", mockDataTemperature[0].time);
expect(result.data[1]).to.have.property("x", mockDataTemperature[1].time);
});
it("should format humidity data correctly with getSerie()", () => {
const serie = new Serie(
HUMIDITY,
mockDataHumidity,
"user1",
"bedroom",
"sensor1"
);
const result = serie.getSerie();
// Verify the properties of the formatted data
expect(result).to.have.property("label", "Humidity [%]");
expect(result).to.have.property("borderColor", Colors.GREEN);
expect(result).to.have.property("backgroundColor", Colors.GREEN);
expect(result).to.have.property("borderWidth", 1);
expect(result).to.have.property("yAxisID", HUMIDITY);
});
it("should handle unknown types appropriately in getSerie()", () => {
const serie = new Serie(
"UNKNOWN_TYPE",
mockDataTemperature,
"user1",
"bedroom",
"sensor1"
);
const result = serie.getSerie();
// Verify the properties for unknown type
expect(result).to.have.property("label", "Unknown");
expect(result).to.have.property("borderColor", Colors.RED);
expect(result).to.have.property("backgroundColor", Colors.RED);
expect(result).to.have.property("yAxisID", TEMPERATURE); // Defaults to TEMPERATURE
});
});

View File

@@ -7,9 +7,7 @@
"build": "vue-cli-service build",
"cypress:open": "cypress open",
"cypress:run": "cypress run",
"test:unit": "cypress run --config-file cypress.unit.ts --spec 'cypress/unit/*.cy.ts'",
"test:e2e": "cypress run --spec 'cypress/e2e/*.cy.ts'",
"test": "npm run test:unit && npm run test:e2e"
"test": "cypress run --spec 'cypress/e2e/*.cy.ts'"
},
"dependencies": {
"axios": "^1.9.0",