Skip to content

Commit

Permalink
UI: Unit tests for autarchy, selfconsumption and consumption (#2241)
Browse files Browse the repository at this point in the history
- Refactor modals of autarchy, selfConsumption and consumption-modal in live
- add unittests
  • Loading branch information
lukasrgr authored Sep 24, 2023
1 parent 8397d6d commit a286584
Show file tree
Hide file tree
Showing 24 changed files with 710 additions and 246 deletions.
4 changes: 0 additions & 4 deletions ui/src/app/edge/live/common/autarchy/modal/modal.html

This file was deleted.

30 changes: 30 additions & 0 deletions ui/src/app/edge/live/common/autarchy/modal/modal.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { LINE_INFO } from "src/app/shared/edge/edgeconfig.spec";
import { OeFormlyViewTester } from "src/app/shared/genericComponents/shared/tester";
import { sharedSetup, TestContext } from "src/app/shared/test/utils.spec";

import { ModalComponent } from "./modal";

export const VIEW_CONTEXT: OeFormlyViewTester.Context = ({});

export function expectView(testContext: TestContext, viewContext: OeFormlyViewTester.Context, view: OeFormlyViewTester.View): void {

const generatedView = OeFormlyViewTester.apply(ModalComponent.generateView(testContext.translate), viewContext);

expect(generatedView).toEqual(view);
};

describe('Autarkie - Modal', () => {
let TEST_CONTEXT: TestContext;
beforeEach(() => TEST_CONTEXT = sharedSetup());

it('generateView()', () => {
{
expectView(TEST_CONTEXT, VIEW_CONTEXT, {
title: "Autarkie",
lines: [
LINE_INFO("Die Autarkie gibt an zu wie viel Prozent die aktuell genutzte Leistung durch Erzeugung und Speicherentladung gedeckt wird.")
]
});
}
});
});
23 changes: 20 additions & 3 deletions ui/src/app/edge/live/common/autarchy/modal/modal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import { Component } from '@angular/core';
import { AbstractModal } from 'src/app/shared/genericComponents/modal/abstractModal';
import { TranslateService } from '@ngx-translate/core';
import { AbstractFormlyComponent, OeFormlyView } from 'src/app/shared/genericComponents/shared/oe-formly-component';
import { EdgeConfig } from 'src/app/shared/shared';
import { Role } from 'src/app/shared/type/role';

@Component({
templateUrl: './modal.html'
templateUrl: '../../../../../shared/formly/formly-field-modal/template.html'
})
export class ModalComponent extends AbstractModal { }
export class ModalComponent extends AbstractFormlyComponent {
protected override generateView(config: EdgeConfig, role: Role): OeFormlyView {
return ModalComponent.generateView(this.translate);
}

public static generateView(translate: TranslateService): OeFormlyView {
return {
title: translate.instant('General.autarchy'),
lines: [{
type: 'info-line',
name: translate.instant("Edge.Index.Widgets.autarchyInfo")
}]
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { DummyConfig } from "src/app/shared/edge/edgeconfig.spec";
import { EdgeConfig } from "src/app/shared/shared";
import { TestContext } from "src/app/shared/test/utils.spec";
import { Role } from "src/app/shared/type/role";

import { OeFormlyViewTester } from "../../../../../shared/genericComponents/shared/tester";
import { ModalComponent } from "./modal";

export function expectView(config: EdgeConfig, role: Role, viewContext: OeFormlyViewTester.Context, testContext: TestContext, view: OeFormlyViewTester.View): void {

const generatedView = OeFormlyViewTester.apply(ModalComponent.generateView(DummyConfig.convertDummyEdgeConfigToRealEdgeConfig(config), role, testContext.translate), viewContext);

expect(generatedView).toEqual(view);
};
58 changes: 0 additions & 58 deletions ui/src/app/edge/live/common/consumption/modal/modal.html

This file was deleted.

160 changes: 160 additions & 0 deletions ui/src/app/edge/live/common/consumption/modal/modal.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import { CHANNEL_LINE, DummyConfig, EVCS_KEBA_KECONTACT, LINE_HORIZONTAL, LINE_INFO_PHASES_DE, SOCOMEC_CONSUMPTION_METER, VALUE_FROM_CHANNELS_LINE } from "src/app/shared/edge/edgeconfig.spec";
import { TextIndentation } from "src/app/shared/genericComponents/modal/modal-line/modal-line";
import { OeFormlyViewTester } from "src/app/shared/genericComponents/shared/tester";
import { sharedSetup } from "src/app/shared/test/utils.spec";
import { Role } from "src/app/shared/type/role";

import { expectView } from "./modal.constants.spec";

describe('Consumption - Modal', () => {
let TEST_CONTEXT;
beforeEach(() => TEST_CONTEXT = sharedSetup());

it('generateView()', () => {

// No evcs and consumptionMeters and negative ConsumptionActivePower
{
const VIEW_CONTEXT: OeFormlyViewTester.Context = {
'_sum/ConsumptionActivePower': -1000,
'_sum/ConsumptionActivePowerL1': -1000,
'_sum/ConsumptionActivePowerL2': 1000,
'_sum/ConsumptionActivePowerL3': -1000
};
const EMS = DummyConfig.from();

expectView(EMS, Role.ADMIN, VIEW_CONTEXT, TEST_CONTEXT, {
title: "Verbrauch",
lines: [
CHANNEL_LINE("Gesamt", "0 W"),
CHANNEL_LINE("Phase L1", "0 W", TextIndentation.SINGLE),
CHANNEL_LINE("Phase L2", "1.000 W", TextIndentation.SINGLE),
CHANNEL_LINE("Phase L3", "0 W", TextIndentation.SINGLE),
LINE_HORIZONTAL,
VALUE_FROM_CHANNELS_LINE("Sonstiger", "0 W"),
LINE_INFO_PHASES_DE
]
});
}

// two evcs and two consumptionMeter, negative consumptionMeter phase
{
const EMS = DummyConfig.from(
SOCOMEC_CONSUMPTION_METER("meter0", "Waermepumpe"),
SOCOMEC_CONSUMPTION_METER("meter1", "Heizung"),
EVCS_KEBA_KECONTACT("evcs0", "Evcs"),
EVCS_KEBA_KECONTACT("evcs1", "Evcs 2"),
EVCS_KEBA_KECONTACT("evcs2", "Evcs 3")
);
const VIEW_CONTEXT: OeFormlyViewTester.Context = {
'_sum/ConsumptionActivePower': 1000,
'_sum/ConsumptionActivePowerL1': 300,
'_sum/ConsumptionActivePowerL2': 350,
'_sum/ConsumptionActivePowerL3': 350,
'meter0/ActivePower': 1000,
'meter0/ActivePowerL1': 1000,
'meter0/ActivePowerL2': -1000,
'meter0/ActivePowerL3': 1000,
'meter1/ActivePower': null,
'meter1/ActivePowerL1': null,
'meter1/ActivePowerL2': null,
'meter1/ActivePowerL3': null,
'evcs0/ChargePower': 1000,
'evcs1/ChargePower': -1000,
'evcs2/ChargePower': null
};

expectView(EMS, Role.ADMIN, VIEW_CONTEXT, TEST_CONTEXT, {
title: "Verbrauch",
lines: [
CHANNEL_LINE("Gesamt", "1.000 W"),
CHANNEL_LINE("Phase L1", "300 W", TextIndentation.SINGLE),
CHANNEL_LINE("Phase L2", "350 W", TextIndentation.SINGLE),
CHANNEL_LINE("Phase L3", "350 W", TextIndentation.SINGLE),
LINE_HORIZONTAL,
CHANNEL_LINE("Evcs", "1.000 W"),
LINE_HORIZONTAL,
CHANNEL_LINE("Evcs 2", "0 W"),
LINE_HORIZONTAL,
CHANNEL_LINE("Evcs 3", "-"),
LINE_HORIZONTAL,
CHANNEL_LINE("Waermepumpe", "1.000 W"),
CHANNEL_LINE("Phase L1", "1.000 W", TextIndentation.SINGLE),
CHANNEL_LINE("Phase L2", "0 W", TextIndentation.SINGLE),
CHANNEL_LINE("Phase L3", "1.000 W", TextIndentation.SINGLE),
LINE_HORIZONTAL,
CHANNEL_LINE("Heizung", "-"),
CHANNEL_LINE("Phase L1", "-", TextIndentation.SINGLE),
CHANNEL_LINE("Phase L2", "-", TextIndentation.SINGLE),
CHANNEL_LINE("Phase L3", "-", TextIndentation.SINGLE),
LINE_HORIZONTAL,
VALUE_FROM_CHANNELS_LINE("Sonstiger", "0 W"),
LINE_INFO_PHASES_DE
]
});
}

// No consumptionMeter, one evcs
{
const EMS = DummyConfig.from(
EVCS_KEBA_KECONTACT("evcs0", "Evcs")
);
const VIEW_CONTEXT: OeFormlyViewTester.Context = {
'_sum/ConsumptionActivePower': 1000,
'_sum/ConsumptionActivePowerL1': 300,
'_sum/ConsumptionActivePowerL2': 350,
'_sum/ConsumptionActivePowerL3': 350,
'evcs0/ChargePower': 1000
};

expectView(EMS, Role.ADMIN, VIEW_CONTEXT, TEST_CONTEXT, {
title: "Verbrauch",
lines: [
CHANNEL_LINE("Gesamt", "1.000 W"),
CHANNEL_LINE("Phase L1", "300 W", TextIndentation.SINGLE),
CHANNEL_LINE("Phase L2", "350 W", TextIndentation.SINGLE),
CHANNEL_LINE("Phase L3", "350 W", TextIndentation.SINGLE),
LINE_HORIZONTAL,
CHANNEL_LINE("Evcs", "1.000 W"),
LINE_HORIZONTAL,
VALUE_FROM_CHANNELS_LINE("Sonstiger", "0 W"),
LINE_INFO_PHASES_DE
]
});
}

// One consumptionMeter, no evcs
{
const EMS = DummyConfig.from(
SOCOMEC_CONSUMPTION_METER("meter0", "Waermepumpe")
);
const VIEW_CONTEXT: OeFormlyViewTester.Context = {
'_sum/ConsumptionActivePower': 1000,
'_sum/ConsumptionActivePowerL1': 300,
'_sum/ConsumptionActivePowerL2': 350,
'_sum/ConsumptionActivePowerL3': 350,
'meter0/ActivePower': 1000,
'meter0/ActivePowerL1': 1000,
'meter0/ActivePowerL2': -1000,
'meter0/ActivePowerL3': 1000
};

expectView(EMS, Role.ADMIN, VIEW_CONTEXT, TEST_CONTEXT, {
title: "Verbrauch",
lines: [
CHANNEL_LINE("Gesamt", "1.000 W"),
CHANNEL_LINE("Phase L1", "300 W", TextIndentation.SINGLE),
CHANNEL_LINE("Phase L2", "350 W", TextIndentation.SINGLE),
CHANNEL_LINE("Phase L3", "350 W", TextIndentation.SINGLE),
LINE_HORIZONTAL,
CHANNEL_LINE("Waermepumpe", "1.000 W"),
CHANNEL_LINE("Phase L1", "1.000 W", TextIndentation.SINGLE),
CHANNEL_LINE("Phase L2", "0 W", TextIndentation.SINGLE),
CHANNEL_LINE("Phase L3", "1.000 W", TextIndentation.SINGLE),
LINE_HORIZONTAL,
VALUE_FROM_CHANNELS_LINE("Sonstiger", "0 W"),
LINE_INFO_PHASES_DE
]
});
}
});
});
Loading

0 comments on commit a286584

Please sign in to comment.