From 38b1444d621fa9b3a8fba5e6164b22adf82b21d0 Mon Sep 17 00:00:00 2001 From: olusegun odunukan Date: Mon, 27 Nov 2023 17:47:15 +0000 Subject: [PATCH] Update case-file-view-folder-selector.component.spec.ts --- ...ile-view-folder-selector.component.spec.ts | 59 ++++++++++ ...ase-file-view-folder-selector.component.ts | 108 +++++++++--------- 2 files changed, 113 insertions(+), 54 deletions(-) diff --git a/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/case-file-view/components/case-file-view-folder-selector/case-file-view-folder-selector.component.spec.ts b/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/case-file-view/components/case-file-view-folder-selector/case-file-view-folder-selector.component.spec.ts index e69de29bb2..983e1350a3 100644 --- a/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/case-file-view/components/case-file-view-folder-selector/case-file-view-folder-selector.component.spec.ts +++ b/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/case-file-view/components/case-file-view-folder-selector/case-file-view-folder-selector.component.spec.ts @@ -0,0 +1,59 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { MatLegacyDialogRef as MatDialogRef, MAT_LEGACY_DIALOG_DATA } from '@angular/material/legacy-dialog'; +import { CaseFileViewFolderSelectorComponent } from './case-file-view-folder-selector.component'; +import { categoriesAndDocumentsTestData } from '../../test-data/categories-and-documents-test-data'; + +describe('CaseFileViewFolderSelectorComponent', () => { + let component: CaseFileViewFolderSelectorComponent; + let fixture: ComponentFixture; + let matDialogRef: jasmine.SpyObj>; + + beforeEach(() => { + matDialogRef = jasmine.createSpyObj>('matDialogRef', ['close']); + + TestBed.configureTestingModule({ + declarations: [CaseFileViewFolderSelectorComponent], + providers: [ + { + provide: MatDialogRef, + useValue: matDialogRef + }, + { + provide: MAT_LEGACY_DIALOG_DATA, + useValue: { categories: categoriesAndDocumentsTestData.categories, document: categoriesAndDocumentsTestData.uncategorised_documents } + } + ] + }); + fixture = TestBed.createComponent(CaseFileViewFolderSelectorComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create the component', () => { + expect(component).toBeTruthy(); + }); + it('should close the dialog on cancel', () => { + component.cancel(); + expect(component.dialogRef.close).toHaveBeenCalled(); + }); + + it('should close the dialog with null on save if selected is empty', () => { + component.selected = ''; + component.save(); + expect(component.dialogRef.close).toHaveBeenCalledWith(null); + }); + + it('should select the checkbox and clear lower levels when radio is checked', () => { + const event = { + target: { + checked: true, + id: 'TestId', + name: 'level-2' + } + }; + + component.handleChange(event); + + expect(component.selected).toBe('TestId'); + }); +}); diff --git a/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/case-file-view/components/case-file-view-folder-selector/case-file-view-folder-selector.component.ts b/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/case-file-view/components/case-file-view-folder-selector/case-file-view-folder-selector.component.ts index ff735a8a3f..10c8d10891 100644 --- a/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/case-file-view/components/case-file-view-folder-selector/case-file-view-folder-selector.component.ts +++ b/projects/ccd-case-ui-toolkit/src/lib/shared/components/palette/case-file-view/components/case-file-view-folder-selector/case-file-view-folder-selector.component.ts @@ -4,73 +4,73 @@ import { DocumentTreeNode } from '../../../../../domain/case-file-view'; import { CaseFileViewCategory } from '../../../../../domain/case-file-view/case-file-view-category.model'; @Component({ - selector: 'xui-case-file-view-folder-selector', - templateUrl: './case-file-view-folder-selector.component.html', - styleUrls: ['./case-file-view-folder-selector.component.scss'], - encapsulation: ViewEncapsulation.None + selector: 'xui-case-file-view-folder-selector', + templateUrl: './case-file-view-folder-selector.component.html', + styleUrls: ['./case-file-view-folder-selector.component.scss'], + encapsulation: ViewEncapsulation.None }) export class CaseFileViewFolderSelectorComponent implements AfterViewInit { + public currentCategories: CaseFileViewCategory[] = []; + public selected: string = ''; - public currentCategories: CaseFileViewCategory[] = []; - public selected: string = ''; - - constructor( + constructor( public dialogRef: MatDialogRef, @Inject(MAT_LEGACY_DIALOG_DATA) public data: { categories: CaseFileViewCategory[], document: DocumentTreeNode } - ) { - this.currentCategories = [...this.data.categories]; - } + ) { + this.currentCategories = [...this.data.categories]; + } - public ngAfterViewInit(): void { - const path = this.findPath(); - path.forEach(p => (document.getElementById(p) as HTMLInputElement).checked = true); - } + public ngAfterViewInit(): void { + const path = this.findPath(); + path.forEach((p) => (document.getElementById(p) as HTMLInputElement).checked = true); + } - public handleChange(evt) { - if (evt.target.checked) { - this.select(evt.target.id); - // get level of this checkbox so we can clear all lower levels - let level = parseInt(evt.target.name.split('-')[1], 10) + 1; - let nodes = document.getElementsByName(`level-${level}`); - while (nodes.length > 0) { - nodes.forEach((node: HTMLInputElement) => node.checked = false); - level += 1; - nodes = document.getElementsByName(`level-${level}`); - } - } + public handleChange(evt) { + if (evt.target.checked) { + this.select(evt.target.id); + // get level of this checkbox so we can clear all lower levels + let level = parseInt(evt.target.name.split('-')[1], 10) + 1; + let nodes = document.getElementsByName(`level-${level}`); + while (nodes.length > 0) { + nodes.forEach((node: HTMLInputElement) => node.checked = false); + level += 1; + nodes = document.getElementsByName(`level-${level}`); + } } + } - public select(categoryId: string) { - this.selected = categoryId; - } + public select(categoryId: string) { + this.selected = categoryId; + } - public cancel() { - this.dialogRef.close(); - } + public cancel() { + this.dialogRef.close(); + } - public save() { - this.dialogRef.close(this.selected.length > 0 ? this.selected : null); - } + public save() { + this.dialogRef.close(this.selected.length > 0 ? this.selected : null); + } - private findPath(): string[] { - for (const c of this.data.categories) { - const r = this.containsDocument(c, this.data.document); - if (r) { - return r; - } - } + private findPath(): string[] { + for (const c of this.data.categories) { + const r = this.containsDocument(c, this.data.document); + if (r) { + return r; + } } + return []; + } - private containsDocument(cat: CaseFileViewCategory, document: DocumentTreeNode): string[] | null { - if (cat.documents.findIndex(doc => doc.document_binary_url === document.document_binary_url) > -1) { - return [cat.category_id]; - } - for (const c of cat.sub_categories) { - const r = this.containsDocument(c, document); - if (r) { - return [cat.category_id, ...r]; - } - } - return null; + private containsDocument(cat: CaseFileViewCategory, document: DocumentTreeNode): string[] | null { + if (cat.documents.findIndex((doc) => doc.document_binary_url === document.document_binary_url) > -1) { + return [cat.category_id]; + } + for (const c of cat.sub_categories) { + const r = this.containsDocument(c, document); + if (r) { + return [cat.category_id, ...r]; + } } + return null; + } }