feat: implement rules engine and management panel for custom holiday optimization
This commit is contained in:
1 parent
c0549de4dd
commit
08ee84e5c9
20 files changed
+3951
-16847
No files matched your search
@@ -0,0 +1,103 @@
|
||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||
import { provideZonelessChangeDetection } from '@angular/core';
|
||||
import { RulesPanel } from './rules-panel';
|
||||
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
|
||||
import { RuleService, RuleType } from '../../services/rule-service';
|
||||
|
||||
describe('RulesPanel', () => {
|
||||
let component: RulesPanel;
|
||||
let fixture: ComponentFixture<RulesPanel>;
|
||||
|
||||
beforeEach(async () => {
|
||||
// Clear localStorage before testing
|
||||
localStorage.clear();
|
||||
|
||||
await TestBed.configureTestingModule({
|
||||
imports: [RulesPanel, BrowserAnimationsModule],
|
||||
providers: [provideZonelessChangeDetection(), RuleService],
|
||||
}).compileComponents();
|
||||
|
||||
fixture = TestBed.createComponent(RulesPanel);
|
||||
component = fixture.componentInstance;
|
||||
fixture.detectChanges();
|
||||
});
|
||||
|
||||
it('should create', () => {
|
||||
expect(component).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should display initial rules from service', () => {
|
||||
expect(component.rules.length).toBe(2);
|
||||
});
|
||||
|
||||
it('should start adding new If-Then rule', () => {
|
||||
component.startAddNew(RuleType.IF_THEN);
|
||||
|
||||
expect(component['isAddingNew']()).toBeTrue();
|
||||
expect(component['newRuleType']()).toBe(RuleType.IF_THEN);
|
||||
expect(component['editBuffer'].type).toBe(RuleType.IF_THEN);
|
||||
expect(component['editBuffer'].name).toBe('Nouvelle Règle');
|
||||
});
|
||||
|
||||
it('should start adding new Time Window rule', () => {
|
||||
component.startAddNew(RuleType.TIME_WINDOW);
|
||||
|
||||
expect(component['isAddingNew']()).toBeTrue();
|
||||
expect(component['newRuleType']()).toBe(RuleType.TIME_WINDOW);
|
||||
expect(component['editBuffer'].type).toBe(RuleType.TIME_WINDOW);
|
||||
expect(component['editBuffer'].name).toBe('Nouvelle Période');
|
||||
});
|
||||
|
||||
it('should cancel edit', () => {
|
||||
component.startAddNew(RuleType.IF_THEN);
|
||||
component.cancelEdit();
|
||||
|
||||
expect(component['isAddingNew']()).toBeFalse();
|
||||
expect(component['editingRuleId']()).toBeNull();
|
||||
expect(Object.keys(component['editBuffer']).length).toBe(0);
|
||||
});
|
||||
|
||||
it('should save a new rule', () => {
|
||||
const initialCount = component.rules.length;
|
||||
|
||||
component.startAddNew(RuleType.IF_THEN);
|
||||
component['editBuffer'].name = 'Custom Test Rule';
|
||||
component.saveEdit();
|
||||
|
||||
expect(component.rules.length).toBe(initialCount + 1);
|
||||
expect(component.rules.some((r) => r.name === 'Custom Test Rule')).toBeTrue();
|
||||
});
|
||||
|
||||
it('should update an existing rule', () => {
|
||||
const targetRule = component.rules[0];
|
||||
|
||||
// Create a mock event with stopPropagation
|
||||
const mockEvent = { stopPropagation: () => {} } as Event;
|
||||
|
||||
component.startEdit(targetRule, mockEvent);
|
||||
component['editBuffer'].name = 'Updated Name';
|
||||
component.saveEdit();
|
||||
|
||||
expect(component.rules.find((r) => r.id === targetRule.id)?.name).toBe('Updated Name');
|
||||
});
|
||||
|
||||
it('should delete a rule', () => {
|
||||
const targetId = component.rules[0].id;
|
||||
const initialCount = component.rules.length;
|
||||
|
||||
const mockEvent = { stopPropagation: () => {} } as Event;
|
||||
component.deleteRule(targetId, mockEvent);
|
||||
|
||||
expect(component.rules.length).toBe(initialCount - 1);
|
||||
expect(component.rules.some((r) => r.id === targetId)).toBeFalse();
|
||||
});
|
||||
|
||||
it('should toggle a rule', () => {
|
||||
const targetRule = component.rules[0];
|
||||
const initialState = targetRule.enabled;
|
||||
|
||||
component.toggleRule(targetRule.id);
|
||||
|
||||
expect(component.rules.find((r) => r.id === targetRule.id)?.enabled).toBe(!initialState);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user