1
0
mirror of https://github.com/bitwarden/browser synced 2026-02-17 18:09:17 +00:00
Files
browser/libs/vault/src/components/assign-collections.component.spec.ts
Brandon Treston 27089fbb57 [PM-24227] Enable TS-strict for Collection Domain models (#15765)
* wip ts-strict

* wip ts-strict

* wip

* cleanup

* cleanup

* fix story

* fix story

* fix story

* wip

* clean up CollectionAdminView construction

* fix deprecated function call

* fix cli

* clean up

* fix story

* wip

* fix cli

* requested changes

* clean up, fixing minor bugs, more type saftey

* assign props in static ctor, clean up
2025-08-14 13:08:24 -04:00

120 lines
4.4 KiB
TypeScript

import { ComponentFixture, TestBed } from "@angular/core/testing";
import { By } from "@angular/platform-browser";
import { mock } from "jest-mock-extended";
import { of } from "rxjs";
// This import has been flagged as unallowed for this class. It may be involved in a circular dependency loop.
// eslint-disable-next-line no-restricted-imports
import { CollectionService, CollectionView } from "@bitwarden/admin-console/common";
import { OrganizationService } from "@bitwarden/common/admin-console/abstractions/organization/organization.service.abstraction";
import { Organization } from "@bitwarden/common/admin-console/models/domain/organization";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { ProductTierType } from "@bitwarden/common/billing/enums";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { FakeAccountService, mockAccountServiceWith } from "@bitwarden/common/spec";
import { CollectionId, OrganizationId, UserId } from "@bitwarden/common/types/guid";
import { CipherService } from "@bitwarden/common/vault/abstractions/cipher.service";
import { CipherView } from "@bitwarden/common/vault/models/view/cipher.view";
import { ToastService } from "@bitwarden/components";
import {
AssignCollectionsComponent,
CollectionAssignmentParams,
} from "./assign-collections.component";
describe("AssignCollectionsComponent", () => {
let component: AssignCollectionsComponent;
let fixture: ComponentFixture<AssignCollectionsComponent>;
const mockUserId = "mock-user-id" as UserId;
const accountService: FakeAccountService = mockAccountServiceWith(mockUserId);
const editCollection = new CollectionView({
id: "collection-id" as CollectionId,
organizationId: "org-id" as OrganizationId,
name: "Editable Collection",
});
editCollection.readOnly = false;
editCollection.manage = true;
const readOnlyCollection1 = new CollectionView({
id: "read-only-collection-id" as CollectionId,
organizationId: "org-id" as OrganizationId,
name: "Read Only Collection",
});
readOnlyCollection1.readOnly = true;
const readOnlyCollection2 = new CollectionView({
id: "read-only-collection-id-2" as CollectionId,
organizationId: "org-id" as OrganizationId,
name: "Read Only Collection 2",
});
readOnlyCollection2.readOnly = true;
const params = {
organizationId: "org-id" as OrganizationId,
ciphers: [
{
id: "cipher-id",
name: "Cipher Name",
collectionIds: [readOnlyCollection1.id],
edit: true,
} as unknown as CipherView,
],
availableCollections: [editCollection, readOnlyCollection1, readOnlyCollection2],
} as CollectionAssignmentParams;
const org = {
id: "org-id",
name: "Test Org",
productTierType: ProductTierType.Enterprise,
} as Organization;
const organizations$ = jest.fn().mockReturnValue(of([org]));
beforeEach(async () => {
await TestBed.configureTestingModule({
providers: [
{ provide: CipherService, useValue: mock<CipherService>() },
{ provide: OrganizationService, useValue: mock<OrganizationService>({ organizations$ }) },
{ provide: CollectionService, useValue: mock<CollectionService>() },
{ provide: ToastService, useValue: mock<ToastService>() },
{ provide: AccountService, useValue: accountService },
{ provide: I18nService, useValue: { t: (...keys: string[]) => keys.join(" ") } },
],
}).compileComponents();
fixture = TestBed.createComponent(AssignCollectionsComponent);
component = fixture.componentInstance;
component.params = params;
fixture.detectChanges();
});
describe("read only collections", () => {
beforeEach(async () => {
await component.ngOnInit();
fixture.detectChanges();
});
it("shows read-only hint for assigned collections", () => {
const hint = fixture.debugElement.query(By.css('[data-testid="view-only-hint"]'));
expect(hint.nativeElement.textContent.trim()).toBe(
"cannotRemoveViewOnlyCollections Read Only Collection",
);
});
it("does not show read only collections in the list", () => {
expect(component["availableCollections"]).toEqual([
{
icon: "bwi-collection-shared",
id: editCollection.id,
labelName: editCollection.name,
listName: editCollection.name,
},
]);
});
});
});