Skip to content

Commit

Permalink
fix: Issues In Unit Tests
Browse files Browse the repository at this point in the history
- fix type issues
- use `takeUntil` with destroy subject in `trending-searches.service.ts` as a more reliable cleanup method to properly handle cleanup
- remove redundant 'declatartion: true' from 'tsconfig.schematics.json' files,
  that caused issues in unit tests due to type errors in '@schematics/angular' library
  'Buffer is not a generic type'. The issue appeared because '*.d.ts' files are taken into consideration when running tests, maybe unnecessary
  for more, see: https://github.com/angular/angular-cli/blob/4db4dd4315fd8c31872bbf1e82e3414eea15ffef/goldens/public-api/angular_devkit/schematics/index.api.md\?plain\=1\#L498
  • Loading branch information
pawelfras committed Dec 13, 2024
1 parent 51ae9c4 commit 71152c8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,11 @@ class MockCurrentLocationService {
altitudeAccuracy: 0,
heading: 0,
speed: 0,
toJSON: () => {},
},
timestamp: 0,
});
toJSON: () => {},
} as GeolocationPosition);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ export const MockWindowRef = {
altitudeAccuracy: 0,
heading: 0,
speed: 0,
},
toJSON: () => {},
} as GeolocationCoordinates,
timestamp: 0,
}),
toJSON: () => {},
} as GeolocationPosition),
},
},
},
Expand Down
1 change: 0 additions & 1 deletion feature-libs/pickup-in-store/tsconfig.schematics.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"compilerOptions": {
"baseUrl": ".",
"lib": ["es2018", "dom"],
"declaration": true,
"module": "commonjs",
"moduleResolution": "node",
"noEmitOnError": true,
Expand Down
1 change: 0 additions & 1 deletion feature-libs/quote/tsconfig.schematics.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"compilerOptions": {
"baseUrl": ".",
"lib": ["es2018", "dom"],
"declaration": true,
"module": "commonjs",
"moduleResolution": "node",
"noEmitOnError": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@
*/

import {
fakeAsync,
TestBed,
tick,
discardPeriodicTasks,
} from '@angular/core/testing';
provideHttpClient,
withInterceptorsFromDi,
} from '@angular/common/http';
import {
HttpTestingController,
provideHttpClientTesting,
} from '@angular/common/http/testing';
import {
fakeAsync,
TestBed,
tick
} from '@angular/core/testing';
import { CdsConfig } from '@spartacus/cds';
import { BaseSiteService, WindowRef } from '@spartacus/core';
import { TrendingSearchesService } from './trending-searches.service';
import { Observable, of } from 'rxjs';
import { SearchPhrases } from './model';
import { CdsConfig } from '@spartacus/cds';
import {
provideHttpClient,
withInterceptorsFromDi,
} from '@angular/common/http';
import { TrendingSearchesService } from './trending-searches.service';

const mockCDSConfig: CdsConfig = {
cds: {
Expand Down Expand Up @@ -87,11 +86,11 @@ describe('TrendingSearchesService', () => {
{ searchPhrase: 'test2', count: 15 },
];

let result: SearchPhrases[] | undefined;
const subscription = service
.getTrendingSearches()
.subscribe((searchPhrases) => {
result = searchPhrases;
.subscribe((result) => {
// Verify the result
expect(result).toEqual(mockSearchPhrases);
});

// Fast-forward through the availability check
Expand All @@ -104,15 +103,9 @@ describe('TrendingSearchesService', () => {
expect(req.request.method).toBe('GET');
req.flush({ searchPhrases: mockSearchPhrases });

// Verify the result
expect(result).toEqual(mockSearchPhrases);

// Clean up
subscription.unsubscribe();
service.ngOnDestroy();

// Discard any remaining periodic timers
discardPeriodicTasks();
}));

it('should not emit when cdsSiteId is not available', fakeAsync(() => {
Expand All @@ -132,13 +125,10 @@ describe('TrendingSearchesService', () => {
tick(250);
}

expect(emitted).toBeFalse();
expect(emitted).toBeFalsy();

// Clean up
subscription.unsubscribe();
service.ngOnDestroy();

// Discard any remaining periodic timers
discardPeriodicTasks();
}));
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { inject, Injectable, OnDestroy } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { CdsEndpointsService } from '../services';
import { CdsConfig } from '../config';
import { inject, Injectable } from '@angular/core';
import { BaseSiteService, WindowRef } from '@spartacus/core';
import {
BehaviorSubject,
catchError,
EMPTY,
filter,
map,
Observable,
timer,
shareReplay,
Subject,
switchMap,
map,
takeWhile,
take,
shareReplay,
EMPTY,
catchError,
filter,
takeUntil,
takeWhile,
timer
} from 'rxjs';
import { CdsConfig } from '../config';
import { CdsEndpointsService } from '../services';
import { SearchPhrases } from './model';

const AVAILABILITY_CHECK_INTERVAL = 250;
Expand All @@ -32,14 +33,14 @@ const TRENDING_SEARCHES_ENDPOINT_KEY = 'searchIntelligence';
@Injectable({
providedIn: 'root',
})
export class TrendingSearchesService implements OnDestroy {
export class TrendingSearchesService {
protected baseSiteService = inject(BaseSiteService);
protected cdsConfig = inject(CdsConfig);
protected cdsEndpointsService = inject(CdsEndpointsService);
protected httpClient = inject(HttpClient);
protected winRef = inject(WindowRef);

private destroy$ = new BehaviorSubject<boolean>(false);
private destroy$ = new Subject<boolean>();
private trendingSearches$ = this.initTrendingSearches().pipe(shareReplay(1));

protected checkAvailability(): Observable<string> {
Expand Down Expand Up @@ -80,7 +81,7 @@ export class TrendingSearchesService implements OnDestroy {
const url = this.constructTrendingSearchUrl(cdsSiteId);
return timer(0, POLL_INTERVAL).pipe(
switchMap(() => this.fetchTrendingSearches(url)),
takeWhile(() => !this.destroy$.value)
takeUntil(this.destroy$)
);
})
);
Expand All @@ -89,7 +90,6 @@ export class TrendingSearchesService implements OnDestroy {
getTrendingSearches(): Observable<SearchPhrases[]> {
return this.trendingSearches$;
}

ngOnDestroy() {
this.destroy$.next(true);
this.destroy$.complete();
Expand Down

0 comments on commit 71152c8

Please sign in to comment.