|
1 | | -import { Component, DebugElement, Renderer2, Type } from '@angular/core'; |
| 1 | +import { Component, DebugElement, ElementRef, Renderer2 } from '@angular/core'; |
2 | 2 | import { ComponentFixture, TestBed } from '@angular/core/testing'; |
3 | 3 | import { By } from '@angular/platform-browser'; |
4 | 4 |
|
5 | 5 | import { HtmlAttributesDirective } from './html-attr.directive'; |
6 | 6 |
|
7 | 7 | @Component({ |
8 | | - template: `<div [cHtmlAttr]="{class: 'test', style: {backgroundColor: 'red'}, id: 'id-1'}"></div>` |
| 8 | + template: `<div [cHtmlAttr]="{ class: 'test', style: { backgroundColor: 'red' }, id: 'id-1' }"></div>` |
9 | 9 | }) |
10 | 10 | class TestComponent {} |
11 | 11 |
|
12 | | -describe('HtmlAttributesDirective', () => { |
| 12 | +class MockElementRef extends ElementRef {} |
13 | 13 |
|
| 14 | +describe('HtmlAttributesDirective', () => { |
14 | 15 | let fixture: ComponentFixture<TestComponent>; |
15 | | - let inputEl: DebugElement; |
16 | | - let renderer: Renderer2; |
| 16 | + let debugElement: DebugElement; |
17 | 17 |
|
18 | 18 | beforeEach(() => { |
19 | 19 | TestBed.configureTestingModule({ |
20 | 20 | declarations: [TestComponent], |
21 | | - imports: [HtmlAttributesDirective] |
| 21 | + imports: [HtmlAttributesDirective], |
| 22 | + providers: [Renderer2, { provide: ElementRef, useClass: MockElementRef }] |
22 | 23 | }); |
23 | 24 | fixture = TestBed.createComponent(TestComponent); |
24 | | - inputEl = fixture.debugElement.query(By.css('div')); |
25 | | - renderer = fixture.componentRef.injector.get(Renderer2 as Type<Renderer2>); |
| 25 | + debugElement = fixture.debugElement.query(By.css('div')); |
26 | 26 | }); |
27 | 27 |
|
28 | 28 | it('should create an instance', () => { |
29 | | - const directive = new HtmlAttributesDirective(renderer, inputEl); |
30 | | - expect(directive).toBeTruthy(); |
| 29 | + TestBed.runInInjectionContext(() => { |
| 30 | + const directive = new HtmlAttributesDirective(); |
| 31 | + expect(directive).toBeTruthy(); |
| 32 | + }); |
31 | 33 | }); |
32 | 34 |
|
33 | 35 | it('should render a class attr', () => { |
34 | 36 | fixture.detectChanges(); |
35 | | - expect(inputEl.nativeElement).toHaveClass('test'); |
| 37 | + expect(debugElement.nativeElement).toHaveClass('test'); |
36 | 38 | }); |
37 | 39 |
|
38 | 40 | it('should render a style attr', () => { |
39 | 41 | fixture.detectChanges(); |
40 | | - expect(inputEl.nativeElement.style.backgroundColor).toBe('red'); |
| 42 | + // console.log(inputEl.nativeElement.style.backgroundColor); |
| 43 | + expect(debugElement.nativeElement.style.backgroundColor).toBe('red'); |
41 | 44 | }); |
42 | 45 |
|
43 | 46 | it('should render an id attr', () => { |
44 | 47 | fixture.detectChanges(); |
45 | | - expect(inputEl.nativeElement.getAttribute('id')).toBe('id-1'); |
| 48 | + expect(debugElement.nativeElement.getAttribute('id')).toBe('id-1'); |
46 | 49 | }); |
47 | 50 | }); |
0 commit comments