|
| 1 | +import { DsoEditMetadataValueComponent } from './dso-edit-metadata-value.component'; |
| 2 | +import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; |
| 3 | +import { VarDirective } from '../../../shared/utils/var.directive'; |
| 4 | +import { TranslateModule } from '@ngx-translate/core'; |
| 5 | +import { RouterTestingModule } from '@angular/router/testing'; |
| 6 | +import { DebugElement, NO_ERRORS_SCHEMA } from '@angular/core'; |
| 7 | +import { RelationshipService } from '../../../core/data/relationship.service'; |
| 8 | +import { DSONameService } from '../../../core/breadcrumbs/dso-name.service'; |
| 9 | +import { of } from 'rxjs/internal/observable/of'; |
| 10 | +import { ItemMetadataRepresentation } from '../../../core/shared/metadata-representation/item/item-metadata-representation.model'; |
| 11 | +import { MetadataValue, VIRTUAL_METADATA_PREFIX } from '../../../core/shared/metadata.models'; |
| 12 | +import { DsoEditMetadataChangeType, DsoEditMetadataValue } from '../dso-edit-metadata-form'; |
| 13 | +import { By } from '@angular/platform-browser'; |
| 14 | + |
| 15 | +const EDIT_BTN = 'edit'; |
| 16 | +const CONFIRM_BTN = 'confirm'; |
| 17 | +const REMOVE_BTN = 'remove'; |
| 18 | +const UNDO_BTN = 'undo'; |
| 19 | +const DRAG_BTN = 'drag'; |
| 20 | + |
| 21 | +describe('DsoEditMetadataValueComponent', () => { |
| 22 | + let component: DsoEditMetadataValueComponent; |
| 23 | + let fixture: ComponentFixture<DsoEditMetadataValueComponent>; |
| 24 | + |
| 25 | + let relationshipService: RelationshipService; |
| 26 | + let dsoNameService: DSONameService; |
| 27 | + |
| 28 | + let editMetadataValue: DsoEditMetadataValue; |
| 29 | + let metadataValue: MetadataValue; |
| 30 | + |
| 31 | + function initServices(): void { |
| 32 | + relationshipService = jasmine.createSpyObj('relationshipService', { |
| 33 | + resolveMetadataRepresentation: of(new ItemMetadataRepresentation(metadataValue)), |
| 34 | + }); |
| 35 | + dsoNameService = jasmine.createSpyObj('dsoNameService', { |
| 36 | + getName: 'Related Name', |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + beforeEach(waitForAsync(() => { |
| 41 | + metadataValue = Object.assign(new MetadataValue(), { |
| 42 | + value: 'Regular Name', |
| 43 | + language: 'en', |
| 44 | + place: 0, |
| 45 | + authority: undefined, |
| 46 | + }); |
| 47 | + editMetadataValue = new DsoEditMetadataValue(metadataValue); |
| 48 | + |
| 49 | + initServices(); |
| 50 | + |
| 51 | + TestBed.configureTestingModule({ |
| 52 | + declarations: [DsoEditMetadataValueComponent, VarDirective], |
| 53 | + imports: [TranslateModule.forRoot(), RouterTestingModule.withRoutes([])], |
| 54 | + providers: [ |
| 55 | + { provide: RelationshipService, useValue: relationshipService }, |
| 56 | + { provide: DSONameService, useValue: dsoNameService }, |
| 57 | + ], |
| 58 | + schemas: [NO_ERRORS_SCHEMA] |
| 59 | + }).compileComponents(); |
| 60 | + })); |
| 61 | + |
| 62 | + beforeEach(() => { |
| 63 | + fixture = TestBed.createComponent(DsoEditMetadataValueComponent); |
| 64 | + component = fixture.componentInstance; |
| 65 | + component.mdValue = editMetadataValue; |
| 66 | + component.saving$ = of(false); |
| 67 | + fixture.detectChanges(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should not show a badge', () => { |
| 71 | + expect(fixture.debugElement.query(By.css('ds-type-badge'))).toBeNull(); |
| 72 | + }); |
| 73 | + |
| 74 | + describe('when no changes have been made', () => { |
| 75 | + assertButton(EDIT_BTN, true, false); |
| 76 | + assertButton(CONFIRM_BTN, false); |
| 77 | + assertButton(REMOVE_BTN, true, false); |
| 78 | + assertButton(UNDO_BTN, true, true); |
| 79 | + assertButton(DRAG_BTN, true, false); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('when this is the only metadata value within its field', () => { |
| 83 | + beforeEach(() => { |
| 84 | + component.isOnlyValue = true; |
| 85 | + fixture.detectChanges(); |
| 86 | + }); |
| 87 | + |
| 88 | + assertButton(DRAG_BTN, true, true); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('when the value is marked for removal', () => { |
| 92 | + beforeEach(() => { |
| 93 | + editMetadataValue.change = DsoEditMetadataChangeType.REMOVE; |
| 94 | + fixture.detectChanges(); |
| 95 | + }); |
| 96 | + |
| 97 | + assertButton(REMOVE_BTN, true, true); |
| 98 | + assertButton(UNDO_BTN, true, false); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('when the value is being edited', () => { |
| 102 | + beforeEach(() => { |
| 103 | + editMetadataValue.editing = true; |
| 104 | + fixture.detectChanges(); |
| 105 | + }); |
| 106 | + |
| 107 | + assertButton(EDIT_BTN, false); |
| 108 | + assertButton(CONFIRM_BTN, true, false); |
| 109 | + assertButton(UNDO_BTN, true, false); |
| 110 | + }); |
| 111 | + |
| 112 | + describe('when the value is new', () => { |
| 113 | + beforeEach(() => { |
| 114 | + editMetadataValue.change = DsoEditMetadataChangeType.ADD; |
| 115 | + fixture.detectChanges(); |
| 116 | + }); |
| 117 | + |
| 118 | + assertButton(REMOVE_BTN, true, false); |
| 119 | + assertButton(UNDO_BTN, true, false); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('when the metadata value is virtual', () => { |
| 123 | + beforeEach(() => { |
| 124 | + metadataValue = Object.assign(new MetadataValue(), { |
| 125 | + value: 'Virtual Name', |
| 126 | + language: 'en', |
| 127 | + place: 0, |
| 128 | + authority: `${VIRTUAL_METADATA_PREFIX}authority-key`, |
| 129 | + }); |
| 130 | + editMetadataValue = new DsoEditMetadataValue(metadataValue); |
| 131 | + component.mdValue = editMetadataValue; |
| 132 | + component.ngOnInit(); |
| 133 | + fixture.detectChanges(); |
| 134 | + }); |
| 135 | + |
| 136 | + it('should show a badge', () => { |
| 137 | + expect(fixture.debugElement.query(By.css('ds-type-badge'))).toBeTruthy(); |
| 138 | + }); |
| 139 | + |
| 140 | + assertButton(EDIT_BTN, true, true); |
| 141 | + assertButton(CONFIRM_BTN, false); |
| 142 | + assertButton(REMOVE_BTN, true, true); |
| 143 | + assertButton(UNDO_BTN, true, true); |
| 144 | + assertButton(DRAG_BTN, true, false); |
| 145 | + }); |
| 146 | + |
| 147 | + function assertButton(name: string, exists: boolean, disabled: boolean = false): void { |
| 148 | + describe(`${name} button`, () => { |
| 149 | + let btn: DebugElement; |
| 150 | + |
| 151 | + beforeEach(() => { |
| 152 | + btn = fixture.debugElement.query(By.css(`#metadata-${name}-btn`)); |
| 153 | + }); |
| 154 | + |
| 155 | + if (exists) { |
| 156 | + it('should exist', () => { |
| 157 | + expect(btn).toBeTruthy(); |
| 158 | + }); |
| 159 | + |
| 160 | + it(`should${disabled ? ' ' : ' not '}be disabled`, () => { |
| 161 | + expect(btn.nativeElement.disabled).toBe(disabled); |
| 162 | + }); |
| 163 | + } else { |
| 164 | + it('should not exist', () => { |
| 165 | + expect(btn).toBeNull(); |
| 166 | + }); |
| 167 | + } |
| 168 | + }); |
| 169 | + } |
| 170 | +}); |
0 commit comments