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

fix: v0.12.x linear count by status bug #1179

Merged
merged 2 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
49 changes: 13 additions & 36 deletions src/app/components/count-tasks-by-status.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { TasksStatusesGroup } from '@app/dashboard/types';
import { TasksFiltersService } from '@app/tasks/services/tasks-filters.service';
import { TasksGrpcService } from '@app/tasks/services/tasks-grpc.service';
import { StatusCount, TaskSummaryFilters } from '@app/tasks/types';
import { CacheService } from '@services/cache.service';
import { CountTasksByStatusComponent } from './count-tasks-by-status.component';

describe('CountTasksByStatusComponent', () => {
Expand Down Expand Up @@ -44,42 +43,37 @@ describe('CountTasksByStatusComponent', () => {
const refresh$ = new Subject<void>();
const refreshSpy = jest.spyOn(refresh$, 'next');

const cachedData: StatusCount[] = [{ status: TaskStatus.TASK_STATUS_CREATING, count: 3 }];
const mockCacheService = {
getStatuses: jest.fn((): StatusCount[] | undefined => cachedData),
saveStatuses: jest.fn()
};

beforeEach(() => {
component = TestBed.configureTestingModule({
providers: [
CountTasksByStatusComponent,
{ provide: TasksGrpcService, useValue: mockTasksGrpcService },
TasksFiltersService,
{ provide: CacheService, useValue: mockCacheService }
TasksFiltersService
]
}).inject(CountTasksByStatusComponent);

component.refresh = refresh$;
component.filters = filters;
component.statusesGroups = statusesGroups;
component.ngOnInit();
});

it('Should run', () => {
expect(component).toBeTruthy();
});

describe('initCount', () => {
it('should set id', () => {
expect(component.id).toEqual(filters[0][0].value);
describe('initialisation', () => {
it('should subscribe to refresh', () => {
expect(refresh$.observed).toBeTruthy();
});

it('should load data', () => {
expect(mockCacheService.getStatuses).toHaveBeenCalled();
it('should set id', () => {
expect(component.id).toEqual(filters[0][0].value);
});

it('should not set id if there is no filter value', () => {
component.initCount([]);
component.filters = [];
component.initId();
expect(component.id).toEqual(undefined);
});
});
Expand All @@ -95,37 +89,20 @@ describe('CountTasksByStatusComponent', () => {
});

describe('setting filters', () => {
it('should subscribe to refresh', () => {
expect(refresh$.observed).toBeTruthy();
});

it('should refresh counts', () => {
expect(refreshSpy).toHaveBeenCalled();
it('should set filters', () => {
expect(component.filters).toEqual(filters);
});
});

describe('Refreshing', () => {
it('should update statusesCounts', () => {
component.refresh.next();
refresh$.next();
expect(component.statusesCount()).toEqual(finalStatusesCount);
});

it('should set null if there is no response status', () => {
mockTasksGrpcService.countByStatus$.mockReturnValue(of({ status: undefined }));
component.refresh.next();
expect(component.statusesCount()).toEqual([]);
});
});

describe('loadFromCache', () => {
it('should load cached data', () => {
component.loadFromCache();
expect(component.statusesCount()).toEqual(cachedData);
});

it('should load a null value if there is no cache', () => {
mockCacheService.getStatuses.mockReturnValueOnce(undefined);
component.loadFromCache();
refresh$.next();
expect(component.statusesCount()).toEqual([]);
});
});
Expand Down
73 changes: 34 additions & 39 deletions src/app/components/count-tasks-by-status.component.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Component, Input, WritableSignal, inject, signal } from '@angular/core';
import { Subject, Subscription, switchMap } from 'rxjs';
import { Component, Input, OnInit, WritableSignal, inject, signal } from '@angular/core';
import { Subject, switchMap } from 'rxjs';
import { TasksStatusesGroup } from '@app/dashboard/types';
import { TasksFiltersService } from '@app/tasks/services/tasks-filters.service';
import { TasksGrpcService } from '@app/tasks/services/tasks-grpc.service';
import { StatusCount, TaskSummaryFilters } from '@app/tasks/types';
import { ViewTasksByStatusComponent } from '@components/view-tasks-by-status.component';
import { CacheService } from '@services/cache.service';

@Component({
selector: 'app-count-tasks-by-status',
Expand All @@ -29,60 +28,56 @@ import { CacheService } from '@services/cache.service';
ViewTasksByStatusComponent,
]
})
export class CountTasksByStatusComponent {
export class CountTasksByStatusComponent implements OnInit {
private readonly tasksGrpcService = inject(TasksGrpcService);

id: string | undefined;
statusesCount: WritableSignal<StatusCount[]> = signal([]);
loading = true;

private _statusesGroups: TasksStatusesGroup[] = [];
private _filters: TaskSummaryFilters;
private _refresh$: Subject<void>;

@Input({ required: true }) queryParams: Record<string, string> = {};
@Input({ required: true }) refresh: Subject<void>;

@Input({ required: true }) set refresh(subject: Subject<void>) {
this._refresh$ = subject;
this.initRefresh();
}

@Input({ required: true }) set statusesGroups(entries: TasksStatusesGroup[]) {
this._statusesGroups = entries;
if (this.refresh) {
this.refresh.next();
}
this._refresh$.next();
}

id: string | undefined;
private _statusesGroups: TasksStatusesGroup[] = [];

get statusesGroups(): TasksStatusesGroup[] {
return this._statusesGroups;
}

statusesCount: WritableSignal<StatusCount[]> = signal([]);

loading = true;

#tasksGrpcService = inject(TasksGrpcService);
readonly cacheService = inject(CacheService);

subscription = new Subscription();
get filters(): TaskSummaryFilters {
return this._filters;
}

@Input({ required: true }) set filters(entries: TaskSummaryFilters) {
this.initCount(entries);
this.refresh.pipe(
switchMap(() => this.#tasksGrpcService.countByStatus$(entries)),
).subscribe(response => {
this.loading = false;
this.statusesCount.set(response.status ?? []);
this.saveData(response.status);
});
this.refresh.next();
this._filters = entries;
}

initCount(filters: TaskSummaryFilters) {
this.#setId(filters);
this.loadFromCache();
ngOnInit(): void {
this.initId();
}

loadFromCache() {
if (this.id) {
this.statusesCount.set(this.cacheService.getStatuses(this.id) ?? []);
}
initId() {
this.#setId(this.filters);
}

saveData(data: StatusCount[] | undefined) {
if (this.id && data) {
this.cacheService.saveStatuses(this.id, data);
}
initRefresh() {
this._refresh$.pipe(
switchMap(() => this.tasksGrpcService.countByStatus$(this.filters)),
).subscribe(response => {
this.loading = false;
this.statusesCount.set(response.status ?? []);
});
}

#setId(filter: TaskSummaryFilters) {
Expand Down
2 changes: 1 addition & 1 deletion src/app/components/table/table-cell.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
@case ('count') {
<app-count-tasks-by-status
[refresh]="refreshStatuses"
[filters]="countFilters"
[statusesGroups]="statusesGroups"
[queryParams]="queryTasksParams"
[filters]="countFilters"
/>
}
@case ('select') {
Expand Down