1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-19 01:33:33 +00:00
Files
browser/libs/tools/send/send-ui/src/services/send-items.service.spec.ts
Jordan Aasen af14c3fe6d [PM-9854] - Send Search Component (#10278)
* send list items container

* update send list items container

* finalize send list container

* remove unecessary file

* undo change to config

* prefer use of takeUntilDestroyed

* add send items service

* and send list filters and service

* undo changes to jest config

* add specs for send list filters

* Revert "Merge branch 'PM-9853' into PM-9852"

This reverts commit 9f65ded13f, reversing
changes made to 63f95600e8.

* add send items service

* Revert "Revert "Merge branch 'PM-9853' into PM-9852""

This reverts commit 81e9860c25.

* finish send search

* fix formControlName

* add specs

* finalize send search

* layout and copy fixes

* cleanup

* Remove unneeded empty file

* Remove the erroneous addition of send-list-filters to vault-export tsconfig

* update tests

* hide send list filters for non-premium users

* fix and add specss

* Fix small typo

* Re-add missing tests

* Remove unused NgZone

* Rename selector for send-search

---------

Co-authored-by: Daniel James Smith <djsmith85@users.noreply.github.com>
Co-authored-by: Daniel James Smith <2670567+djsmith85@users.noreply.github.com>
2024-08-07 14:34:03 +02:00

115 lines
3.7 KiB
TypeScript

import { TestBed } from "@angular/core/testing";
import { mock } from "jest-mock-extended";
import { BehaviorSubject, first, Subject } from "rxjs";
import { SearchService } from "@bitwarden/common/abstractions/search.service";
import { SendView } from "@bitwarden/common/tools/send/models/view/send.view";
import { SendService } from "@bitwarden/common/tools/send/services/send.service.abstraction";
import { SendItemsService } from "./send-items.service";
import { SendListFiltersService } from "./send-list-filters.service";
describe("SendItemsService", () => {
let testBed: TestBed;
let service: SendItemsService;
const sendServiceMock = mock<SendService>();
const sendListFiltersServiceMock = mock<SendListFiltersService>();
const searchServiceMock = mock<SearchService>();
beforeEach(() => {
sendServiceMock.sendViews$ = new BehaviorSubject<SendView[]>([]);
sendListFiltersServiceMock.filters$ = new BehaviorSubject({
sendType: null,
});
sendListFiltersServiceMock.filterFunction$ = new BehaviorSubject((sends: SendView[]) => sends);
searchServiceMock.searchSends.mockImplementation((sends) => sends);
testBed = TestBed.configureTestingModule({
providers: [
{ provide: SendService, useValue: sendServiceMock },
{ provide: SendListFiltersService, useValue: sendListFiltersServiceMock },
{ provide: SearchService, useValue: searchServiceMock },
SendItemsService,
],
});
service = testBed.inject(SendItemsService);
});
afterEach(() => {
jest.clearAllMocks();
});
it("should be created", () => {
expect(service).toBeTruthy();
});
it("should update and sort filteredAndSortedSends$ when filterFunction$ changes", (done) => {
const unsortedSends = [
{ id: "2", name: "Send B", type: 2, disabled: false },
{ id: "1", name: "Send A", type: 1, disabled: false },
] as SendView[];
(sendServiceMock.sendViews$ as BehaviorSubject<SendView[]>).next([...unsortedSends]);
service.filteredAndSortedSends$.subscribe((filteredAndSortedSends) => {
expect(filteredAndSortedSends).toEqual([unsortedSends[1], unsortedSends[0]]);
done();
});
});
it("should update loading$ when sends are loading", (done) => {
const sendsLoading$ = new Subject<void>();
(service as any)._sendsLoading$ = sendsLoading$;
service.loading$.subscribe((loading) => {
expect(loading).toBe(true);
done();
});
sendsLoading$.next();
});
it("should update hasFilterApplied$ when a filter is applied", (done) => {
searchServiceMock.isSearchable.mockImplementation(async () => true);
service.hasFilterApplied$.subscribe((canSearch) => {
expect(canSearch).toBe(true);
done();
});
service.applyFilter("test");
});
it("should return true for emptyList$ when there are no sends", (done) => {
(sendServiceMock.sendViews$ as BehaviorSubject<SendView[]>).next([]);
service.emptyList$.subscribe((empty) => {
expect(empty).toBe(true);
done();
});
});
it("should return true for noFilteredResults$ when there are no filtered sends", (done) => {
searchServiceMock.searchSends.mockImplementation(() => []);
service.noFilteredResults$.pipe(first()).subscribe((noResults) => {
expect(noResults).toBe(true);
done();
});
(sendServiceMock.sendViews$ as BehaviorSubject<SendView[]>).next([]);
});
it("should call searchService.searchSends when applyFilter is called", (done) => {
const searchText = "Hello";
service.applyFilter(searchText);
const searchServiceSpy = jest.spyOn(searchServiceMock, "searchSends");
service.filteredAndSortedSends$.subscribe(() => {
expect(searchServiceSpy).toHaveBeenCalledWith([], searchText);
done();
});
});
});