|
| 1 | +import { Component } from '@angular/core'; |
| 2 | +import { |
| 3 | + ComponentFixture, |
| 4 | + TestBed, |
| 5 | +} from '@angular/core/testing'; |
| 6 | +import { By } from '@angular/platform-browser'; |
| 7 | + |
| 8 | +import { EntityIconDirective } from './entity-icon.directive'; |
| 9 | + |
| 10 | +describe('EntityIconDirective', () => { |
| 11 | + let component: TestComponent; |
| 12 | + let fixture: ComponentFixture<TestComponent>; |
| 13 | + |
| 14 | + beforeEach(async () => { |
| 15 | + await TestBed.configureTestingModule({ |
| 16 | + imports: [EntityIconDirective, |
| 17 | + TestComponent], |
| 18 | + }) |
| 19 | + .compileComponents(); |
| 20 | + }); |
| 21 | + |
| 22 | + describe('with default value provided', () => { |
| 23 | + beforeEach(() => { |
| 24 | + fixture = TestBed.createComponent(TestComponent); |
| 25 | + component = fixture.componentInstance; |
| 26 | + fixture.detectChanges(); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should create', () => { |
| 30 | + expect(component).toBeTruthy(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should display a text-success icon', () => { |
| 34 | + const successIcon = fixture.debugElement.query(By.css('[data-test="entityTestComponent"]')).query(By.css('i.text-success')); |
| 35 | + expect(successIcon).toBeTruthy(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should display a text-success icon after span', () => { |
| 39 | + const successIcon = fixture.debugElement.query(By.css('[data-test="entityTestComponent"]')).query(By.css('i.text-success')); |
| 40 | + const entityElement = fixture.debugElement.query(By.css('[data-test="entityTestComponent"]')); |
| 41 | + // position 1 because the icon is after the span |
| 42 | + expect(entityElement.nativeElement.children[1]).toBe(successIcon.nativeNode); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('with primary value provided', () => { |
| 47 | + beforeEach(() => { |
| 48 | + fixture = TestBed.createComponent(TestComponent); |
| 49 | + component = fixture.componentInstance; |
| 50 | + component.metadata.entityType = 'person'; |
| 51 | + component.metadata.entityStyle = 'personStaff'; |
| 52 | + component.iconPosition = 'before'; |
| 53 | + fixture.detectChanges(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should display a text-primary icon', () => { |
| 57 | + const primaryIcon = fixture.debugElement.query(By.css('[data-test="entityTestComponent"]')).query(By.css('i.text-primary')); |
| 58 | + expect(primaryIcon).toBeTruthy(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should display a text-primary icon before span', () => { |
| 62 | + const primaryIcon = fixture.debugElement.query(By.css('[data-test="entityTestComponent"]')).query(By.css('i.text-primary')); |
| 63 | + const entityElement = fixture.debugElement.query(By.css('[data-test="entityTestComponent"]')); |
| 64 | + // position 0 because the icon is before the span |
| 65 | + expect(entityElement.nativeElement.children[0]).toBe(primaryIcon.nativeNode); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('when given type doesn\'t exist and fallback on default disabled', () => { |
| 70 | + beforeEach(() => { |
| 71 | + fixture = TestBed.createComponent(TestComponent); |
| 72 | + component = fixture.componentInstance; |
| 73 | + component.fallbackOnDefault = false; |
| 74 | + component.metadata.entityType = 'TESTFAKE'; |
| 75 | + component.metadata.entityStyle = 'personFallback'; |
| 76 | + component.iconPosition = 'before'; |
| 77 | + fixture.detectChanges(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should not display a text-primary icon', () => { |
| 81 | + const primaryIcon = fixture.debugElement.query(By.css('[data-test="entityTestComponent"]')).query(By.css('i')); |
| 82 | + expect(primaryIcon).toBeFalsy(); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('when given style doesn\'t exist and fallback on default disabled', () => { |
| 87 | + beforeEach(() => { |
| 88 | + fixture = TestBed.createComponent(TestComponent); |
| 89 | + component = fixture.componentInstance; |
| 90 | + component.fallbackOnDefault = false; |
| 91 | + component.metadata.entityType = 'person'; |
| 92 | + component.metadata.entityStyle = 'personFallback'; |
| 93 | + component.iconPosition = 'before'; |
| 94 | + fixture.detectChanges(); |
| 95 | + }); |
| 96 | + |
| 97 | + it('should not display a text-primary icon', () => { |
| 98 | + const primaryIcon = fixture.debugElement.query(By.css('[data-test="entityTestComponent"]')).query(By.css('i')); |
| 99 | + expect(primaryIcon).toBeFalsy(); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | +}); |
| 104 | + |
| 105 | +// declare a test component |
| 106 | +@Component({ |
| 107 | + selector: 'ds-test-cmp', |
| 108 | + template: ` |
| 109 | + <div [attr.data-test]="'entityTestComponent'"> |
| 110 | + <span dsEntityIcon |
| 111 | + [iconPosition]="iconPosition" |
| 112 | + [entityType]="metadata.entityType" |
| 113 | + [entityStyle]="metadata.entityStyle" |
| 114 | + [fallbackOnDefault]="fallbackOnDefault">{{ metadata.value }}</span></div>`, |
| 115 | + standalone: true, |
| 116 | + imports: [ |
| 117 | + EntityIconDirective, |
| 118 | + ], |
| 119 | +}) |
| 120 | +class TestComponent { |
| 121 | + |
| 122 | + metadata = { |
| 123 | + authority: null, |
| 124 | + value: 'Test', |
| 125 | + orcidAuthenticated: null, |
| 126 | + entityType: 'default', |
| 127 | + entityStyle: 'default', |
| 128 | + }; |
| 129 | + iconPosition = 'after'; |
| 130 | + fallbackOnDefault = true; |
| 131 | + |
| 132 | +} |
0 commit comments