2024-12-13 00:27:30 -08:00
|
|
|
import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'
|
2023-05-23 15:02:54 -07:00
|
|
|
import {
|
|
|
|
|
HttpTestingController,
|
2024-06-26 20:57:39 -07:00
|
|
|
provideHttpClientTesting,
|
2023-05-23 15:02:54 -07:00
|
|
|
} from '@angular/common/http/testing'
|
|
|
|
|
import { TestBed } from '@angular/core/testing'
|
2024-12-13 00:27:30 -08:00
|
|
|
import { Subscription } from 'rxjs'
|
|
|
|
|
import { SETTINGS_KEYS } from 'src/app/data/ui-settings'
|
2023-05-23 15:02:54 -07:00
|
|
|
import { environment } from 'src/environments/environment'
|
2024-05-02 09:15:56 -07:00
|
|
|
import { SettingsService } from '../settings.service'
|
2024-12-13 00:27:30 -08:00
|
|
|
import { SearchService } from './search.service'
|
2023-05-23 15:02:54 -07:00
|
|
|
|
|
|
|
|
let httpTestingController: HttpTestingController
|
|
|
|
|
let service: SearchService
|
|
|
|
|
let subscription: Subscription
|
2024-05-02 09:15:56 -07:00
|
|
|
let settingsService: SettingsService
|
2023-05-23 15:02:54 -07:00
|
|
|
const endpoint = 'search/autocomplete'
|
|
|
|
|
|
|
|
|
|
describe('SearchService', () => {
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
TestBed.configureTestingModule({
|
2024-06-26 20:57:39 -07:00
|
|
|
imports: [],
|
|
|
|
|
providers: [
|
|
|
|
|
SearchService,
|
|
|
|
|
provideHttpClient(withInterceptorsFromDi()),
|
|
|
|
|
provideHttpClientTesting(),
|
|
|
|
|
],
|
2023-05-23 15:02:54 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
httpTestingController = TestBed.inject(HttpTestingController)
|
2024-05-02 09:15:56 -07:00
|
|
|
settingsService = TestBed.inject(SettingsService)
|
2023-05-23 15:02:54 -07:00
|
|
|
service = TestBed.inject(SearchService)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
subscription?.unsubscribe()
|
|
|
|
|
httpTestingController.verify()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('should call correct api endpoint on autocomplete', () => {
|
|
|
|
|
const term = 'apple'
|
|
|
|
|
subscription = service.autocomplete(term).subscribe()
|
|
|
|
|
const req = httpTestingController.expectOne(
|
|
|
|
|
`${environment.apiBaseUrl}${endpoint}/?term=${term}`
|
|
|
|
|
)
|
|
|
|
|
expect(req.request.method).toEqual('GET')
|
|
|
|
|
})
|
2024-05-02 09:15:56 -07:00
|
|
|
|
|
|
|
|
it('should call correct api endpoint on globalSearch', () => {
|
|
|
|
|
const query = 'apple'
|
|
|
|
|
service.globalSearch(query).subscribe()
|
|
|
|
|
httpTestingController.expectOne(
|
|
|
|
|
`${environment.apiBaseUrl}search/?query=${query}`
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
settingsService.set(SETTINGS_KEYS.SEARCH_DB_ONLY, true)
|
|
|
|
|
subscription = service.globalSearch(query).subscribe()
|
|
|
|
|
httpTestingController.expectOne(
|
|
|
|
|
`${environment.apiBaseUrl}search/?query=${query}&db_only=true`
|
|
|
|
|
)
|
|
|
|
|
})
|
2023-05-23 15:02:54 -07:00
|
|
|
})
|