1
1
import {
2
2
Component ,
3
3
DebugElement ,
4
- Renderer2 ,
5
4
} from '@angular/core' ;
6
5
import {
7
6
ComponentFixture ,
@@ -19,103 +18,50 @@ import {
19
18
20
19
@Component ( {
21
20
imports : [ ScalableDirective ] ,
22
- standalone : true ,
23
- template : `<div [connect]="connect" [scalable]="scalableValue"></div>`
21
+ template : `<div scalable></div>`
24
22
} )
25
- class TestComponent {
26
- public connect = 'testConnectId' ;
27
- public scalableValue : string | undefined = 'testScalableId' ;
28
- }
23
+ class TestComponent { }
29
24
30
25
describe ( 'ScalableDirective' , ( ) => {
31
26
let parentComponentFixture : ComponentFixture < TestComponent > ;
32
27
let directiveEl : DebugElement ;
33
28
let directiveInstance : ScalableDirective ;
34
29
let resizeHandlerService : ResizeConsumerService ;
35
- let renderer : Renderer2 ;
36
30
37
31
beforeEach ( ( ) => {
38
- const resizeHandlerServiceMock = {
39
- start : jest . fn ( ) ,
40
- newHeightFromChannel : jest . fn ( ) . mockReturnValue ( { channelId : 'testScalableId' , height : 200 } )
32
+ const resizeConsumerMock = {
33
+ heightPx : jest . fn ( ) . mockReturnValue ( 200 )
41
34
} ;
42
35
43
36
parentComponentFixture = TestBed . configureTestingModule ( {
44
- imports : [ TestComponent , ScalableDirective ] ,
45
- providers : [
46
- Renderer2 ,
47
- { provide : ResizeConsumerService , useValue : resizeHandlerServiceMock }
48
-
49
- ]
50
- } ) . createComponent ( TestComponent ) ;
37
+ imports : [ TestComponent , ScalableDirective ]
38
+ } )
39
+ . overrideDirective ( ScalableDirective , {
40
+ set : {
41
+ providers : [ { provide : ResizeConsumerService , useValue : resizeConsumerMock } ]
42
+ }
43
+ } )
44
+ . createComponent ( TestComponent ) ;
51
45
52
46
parentComponentFixture . detectChanges ( ) ;
53
47
directiveEl = parentComponentFixture . debugElement . query ( By . directive ( ScalableDirective ) ) ;
54
48
directiveInstance = directiveEl . injector . get ( ScalableDirective ) ;
55
49
resizeHandlerService = directiveEl . injector . get ( ResizeConsumerService ) ;
56
- renderer = directiveEl . injector . get ( Renderer2 ) ;
57
50
} ) ;
58
51
59
52
it ( 'should create an instance' , ( ) => {
60
53
expect ( directiveInstance ) . toBeTruthy ( ) ;
61
54
} ) ;
62
55
63
- it ( 'should start the resize handler service on initialization' , ( ) => {
64
- expect ( resizeHandlerService . start ) . toHaveBeenCalled ( ) ;
65
- } ) ;
66
-
67
- it ( 'should set the height style on the element with the channelId from scalable input' , ( ) => {
68
- const channelId = 'scalable-channel-id' ;
69
- jest . spyOn ( resizeHandlerService , 'newHeightFromChannel' ) . mockReturnValue ( { height : 300 , channelId } ) ;
70
- const rendererSpy = jest . spyOn ( renderer , 'setStyle' ) ;
71
- parentComponentFixture . componentInstance . scalableValue = channelId ;
72
- parentComponentFixture . detectChanges ( ) ;
73
- expect ( rendererSpy ) . toHaveBeenCalledWith ( directiveEl . nativeElement , 'height' , '300px' ) ;
74
- rendererSpy . mockClear ( ) ;
75
- } ) ;
76
-
77
- it ( 'should set the height style on the element with the channelId from connect input' , ( ) => {
78
- const channelId = 'connect-channel-id' ;
79
- jest . spyOn ( resizeHandlerService , 'newHeightFromChannel' ) . mockReturnValue ( { height : 400 , channelId } ) ;
80
- const rendererSpy = jest . spyOn ( renderer , 'setStyle' ) ;
81
- parentComponentFixture . componentInstance . scalableValue = undefined ;
82
- parentComponentFixture . componentInstance . connect = channelId ;
83
- parentComponentFixture . detectChanges ( ) ;
84
- expect ( rendererSpy ) . toHaveBeenCalledWith ( directiveEl . nativeElement , 'height' , '400px' ) ;
85
- rendererSpy . mockClear ( ) ;
86
- } ) ;
87
-
88
- it ( 'scalable input should take precedence over connect input' , ( ) => {
89
- const connectChannelId = 'connect-channel-id' ;
90
- const scalableChannelId = 'scalable-channel-id' ;
91
- jest . spyOn ( resizeHandlerService , 'newHeightFromChannel' ) . mockReturnValue ( { height : 400 , channelId : scalableChannelId } ) ;
92
- const rendererSpy = jest . spyOn ( renderer , 'setStyle' ) ;
93
- parentComponentFixture . componentInstance . scalableValue = scalableChannelId ;
94
- parentComponentFixture . componentInstance . connect = connectChannelId ;
95
- parentComponentFixture . detectChanges ( ) ;
96
- expect ( rendererSpy ) . toHaveBeenCalledWith ( directiveEl . nativeElement , 'height' , '400px' ) ;
97
- rendererSpy . mockClear ( ) ;
98
- } ) ;
99
-
100
- it ( 'should not update the style if channelId does not match' , ( ) => {
101
- const connectChannelId = 'connect-channel-id' ;
102
- const scalableChannelId = 'scalable-channel-id' ;
103
- jest . spyOn ( resizeHandlerService , 'newHeightFromChannel' ) . mockReturnValue ( { height : 400 , channelId : scalableChannelId } ) ;
104
- const rendererSpy = jest . spyOn ( renderer , 'setStyle' ) ;
105
- parentComponentFixture . componentInstance . scalableValue = 'not-matching-channel-id' ;
106
- parentComponentFixture . componentInstance . connect = connectChannelId ;
56
+ it ( 'should set the height style on the element' , ( ) => {
57
+ jest . spyOn ( resizeHandlerService , 'heightPx' ) . mockReturnValue ( 300 ) ;
107
58
parentComponentFixture . detectChanges ( ) ;
108
- expect ( rendererSpy ) . not . toHaveBeenCalled ( ) ;
109
- rendererSpy . mockClear ( ) ;
59
+ expect ( directiveEl . nativeElement . getAttribute ( 'style' ) ) . toBe ( 'height: 300px;' ) ;
110
60
} ) ;
111
61
112
- it ( 'should not set the height style on the element when newHeightFromChannel is not available' , ( ) => {
113
- const channelId = 'scalable-channel-id' ;
114
- jest . spyOn ( resizeHandlerService , 'newHeightFromChannel' ) . mockReturnValue ( undefined ) ;
115
- const rendererSpy = jest . spyOn ( renderer , 'setStyle' ) ;
116
- parentComponentFixture . componentInstance . scalableValue = channelId ;
62
+ it ( 'should not set the height style on the element when heightPx is not available' , ( ) => {
63
+ jest . spyOn ( resizeHandlerService , 'heightPx' ) . mockReturnValue ( undefined ) ;
117
64
parentComponentFixture . detectChanges ( ) ;
118
- expect ( rendererSpy ) . not . toHaveBeenCalled ( ) ;
119
- rendererSpy . mockClear ( ) ;
65
+ expect ( directiveEl . nativeElement . getAttribute ( 'style' ) ) . toBe ( '' ) ;
120
66
} ) ;
121
67
} ) ;
0 commit comments