Ajout des tests

This commit is contained in:
2026-05-23 20:18:18 +02:00
parent 5b5edbb3da
commit 80f8b56ec4
10 changed files with 1222 additions and 9 deletions
+172
View File
@@ -0,0 +1,172 @@
import { TestBed } from '@angular/core/testing';
import { IssueEntity, IssuesStore } from './issues.store';
const makeIssue = (overrides: Partial<IssueEntity> = {}): IssueEntity => ({
id: 99,
type: 'Story',
assignee: '',
epic: '',
name: 'Test Issue',
dueDate: '',
description: '',
estimatedTime: null,
dependsOnIds: [],
comments: [],
priority: 'Moyenne',
status: 'draft',
progress: 0,
...overrides,
});
describe('IssuesStore', () => {
let store: IssuesStore;
beforeEach(() => {
localStorage.clear();
TestBed.configureTestingModule({});
store = TestBed.inject(IssuesStore);
});
afterEach(() => {
localStorage.clear();
});
it('should be created', () => {
expect(store).toBeTruthy();
});
it('should load default issues when localStorage is empty', () => {
expect(store.issues().length).toBeGreaterThan(0);
});
describe('getById', () => {
it('returns the issue with the given id', () => {
const issue = store.getById(1);
expect(issue?.id).toBe(1);
});
it('returns undefined for an unknown id', () => {
expect(store.getById(9999)).toBeUndefined();
});
});
describe('getNextId', () => {
it('returns max id + 1', () => {
const ids = store.issues().map((i) => i.id);
const expectedNext = Math.max(...ids) + 1;
expect(store.getNextId()).toBe(expectedNext);
});
it('returns 1 when there are no issues', () => {
store.deleteById(1);
store.deleteById(2);
store.deleteById(3);
expect(store.getNextId()).toBe(1);
});
});
describe('upsert', () => {
it('adds a new issue when the id does not exist', () => {
const before = store.issues().length;
store.upsert(makeIssue({ id: 999 }));
expect(store.issues().length).toBe(before + 1);
expect(store.getById(999)?.name).toBe('Test Issue');
});
it('updates an existing issue', () => {
store.upsert(makeIssue({ id: 1, name: 'Updated Name' }));
expect(store.getById(1)?.name).toBe('Updated Name');
expect(store.issues().filter((i) => i.id === 1).length).toBe(1);
});
it('persists the issue list to localStorage', () => {
store.upsert(makeIssue({ id: 999 }));
const raw = localStorage.getItem('bonsai.issues');
expect(raw).not.toBeNull();
const parsed = JSON.parse(raw!);
expect(parsed.some((i: IssueEntity) => i.id === 999)).toBe(true);
});
it('normalizes legacy dependsOnId (single number) to dependsOnIds array when dependsOnIds is absent', () => {
// dependsOnIds must be omitted (not an array) for the legacy field to take effect
store.upsert({ id: 998, type: 'Story', name: 'Legacy', dependsOnId: 1 } as any);
expect(store.getById(998)?.dependsOnIds).toEqual([1]);
});
it('filters non-number values from dependsOnIds', () => {
store.upsert({ ...makeIssue({ id: 997 }), dependsOnIds: [1, 'two', null] } as any);
expect(store.getById(997)?.dependsOnIds).toEqual([1]);
});
it('ensures comments is always an array when missing', () => {
store.upsert({ ...makeIssue({ id: 996 }), comments: undefined } as any);
expect(store.getById(996)?.comments).toEqual([]);
});
it('sets default type to Story when type is missing', () => {
store.upsert({ ...makeIssue({ id: 995 }), type: undefined } as any);
expect(store.getById(995)?.type).toBe('Story');
});
it('sets estimatedTime to null when missing', () => {
store.upsert({ ...makeIssue({ id: 994 }), estimatedTime: undefined } as any);
expect(store.getById(994)?.estimatedTime).toBeNull();
});
});
describe('deleteById', () => {
it('removes the issue from the store', () => {
store.upsert(makeIssue({ id: 999 }));
store.deleteById(999);
expect(store.getById(999)).toBeUndefined();
});
it('removes the deleted id from dependsOnIds of other issues', () => {
store.upsert(makeIssue({ id: 100 }));
store.upsert(makeIssue({ id: 101, dependsOnIds: [100] }));
store.deleteById(100);
expect(store.getById(101)?.dependsOnIds).toEqual([]);
});
it('persists the updated list to localStorage', () => {
store.upsert(makeIssue({ id: 999 }));
store.deleteById(999);
const raw = localStorage.getItem('bonsai.issues');
const parsed = JSON.parse(raw!);
expect(parsed.some((i: IssueEntity) => i.id === 999)).toBe(false);
});
});
describe('localStorage persistence', () => {
it('loads issues from localStorage on construction', () => {
const saved: IssueEntity[] = [makeIssue({ id: 42, name: 'From storage' })];
localStorage.setItem('bonsai.issues', JSON.stringify(saved));
TestBed.resetTestingModule();
TestBed.configureTestingModule({});
const freshStore = TestBed.inject(IssuesStore);
expect(freshStore.getById(42)?.name).toBe('From storage');
});
it('falls back to defaults when localStorage contains invalid JSON', () => {
localStorage.setItem('bonsai.issues', 'not-valid-json');
TestBed.resetTestingModule();
TestBed.configureTestingModule({});
const freshStore = TestBed.inject(IssuesStore);
expect(freshStore.issues().length).toBeGreaterThan(0);
});
it('falls back to defaults when localStorage contains a non-array', () => {
localStorage.setItem('bonsai.issues', '{"key":"value"}');
TestBed.resetTestingModule();
TestBed.configureTestingModule({});
const freshStore = TestBed.inject(IssuesStore);
expect(freshStore.issues().length).toBeGreaterThan(0);
});
});
});