Skip to content

Commit

Permalink
fix intialization and destroy behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
BeritJanssen committed Dec 12, 2024
1 parent b410da3 commit cfaa837
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
28 changes: 17 additions & 11 deletions frontend/src/app/visualization/ngram/ngram.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ describe('NgramComponent', () => {
let apiService: ApiServiceMock;
let visualizationService: VisualizationService;
let cacheKey = 's:2,p:any,c:false,a:none,m:50,n:10,f:date';
let defaultSettings = {
size: 2,
positions: 'any',
freqCompensation: false,
analysis: 'none',
maxDocuments: 50,
numberOfNgrams: 10,
dateField: 'date'
} as NgramSettings;

beforeEach(waitForAsync(() => {
commonTestBed().testingModule.compileComponents();
Expand Down Expand Up @@ -55,16 +64,13 @@ describe('NgramComponent', () => {
});

it('should initialize ngramParameters with default values', () => {
const defaultSettings = {
size: 2,
positions: 'any',
freqCompensation: false,
analysis: 'none',
maxDocuments: 50,
numberOfNgrams: 10,
dateField: 'date'
} as NgramSettings;
expect(component.ngramParameters.state$.value).toEqual(defaultSettings);
});

it('should not abort tasks when `onParameterChange` is triggered during initialization', () => {
spyOn(component.stopPolling$, 'next');
component.onParameterChange('size', 2);
expect(component.stopPolling$.next).not.toHaveBeenCalled();
})

it('should stop polling and abort running tasks when changing settings', () => {
Expand All @@ -73,9 +79,9 @@ describe('NgramComponent', () => {
const eventObj = { parameter: 'size', value };
dropdown.triggerEventHandler('onChange', eventObj);
};
spyOn(fixture.componentInstance.stopPolling$, 'next');
spyOn(component.stopPolling$, 'next');
changeSizeDropdown(10);
expect(fixture.componentInstance.stopPolling$.next).toHaveBeenCalled();
expect(component.stopPolling$.next).toHaveBeenCalled();
component.dataHasLoaded = false; // fake working response
expect(component.tasksToCancel).toBeUndefined();
});
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/app/visualization/ngram/ngram.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export class NgramComponent implements OnChanges {

ngOnDestroy(): void {
this.stopPolling$.next();
this.ngramParameters.complete();
}

ngOnChanges(changes: SimpleChanges): void {
Expand Down Expand Up @@ -262,6 +263,9 @@ export class NgramComponent implements OnChanges {
}

onParameterChange(parameter: string, value: any) {
if (_.get(this.currentSettings, parameter) === value) {
return;
}
_.assign(this.currentSettings, {[parameter]: value});

if (parameter === 'size' && value) {
Expand Down

0 comments on commit cfaa837

Please sign in to comment.