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,51 @@ import {
19
18
20
19
@Component ( {
21
20
imports : [ ScalableDirective ] ,
22
- standalone : true ,
23
- template : ` <div [connect]="connect" [ scalable]="scalableValue" ></div>`
21
+ template : `
22
+ <div [scalable]></div>`
24
23
} )
25
- class TestComponent {
26
- public connect = 'testConnectId' ;
27
- public scalableValue : string | undefined = 'testScalableId' ;
28
- }
24
+ class TestComponent { }
29
25
30
26
describe ( 'ScalableDirective' , ( ) => {
31
27
let parentComponentFixture : ComponentFixture < TestComponent > ;
32
28
let directiveEl : DebugElement ;
33
29
let directiveInstance : ScalableDirective ;
34
30
let resizeHandlerService : ResizeConsumerService ;
35
- let renderer : Renderer2 ;
36
31
37
32
beforeEach ( ( ) => {
38
- const resizeHandlerServiceMock = {
39
- start : jest . fn ( ) ,
40
- newHeightFromChannel : jest . fn ( ) . mockReturnValue ( { channelId : 'testScalableId' , height : 200 } )
33
+ const resizeConsumerMock = {
34
+ heightPx : jest . fn ( ) . mockReturnValue ( 200 )
41
35
} ;
42
36
43
37
parentComponentFixture = TestBed . configureTestingModule ( {
44
- imports : [ TestComponent , ScalableDirective ] ,
45
- providers : [
46
- Renderer2 ,
47
- { provide : ResizeConsumerService , useValue : resizeHandlerServiceMock }
48
-
49
- ]
50
- } ) . createComponent ( TestComponent ) ;
38
+ imports : [ TestComponent , ScalableDirective ]
39
+ } )
40
+ . overrideDirective ( ScalableDirective , {
41
+ set : {
42
+ providers : [ { provide : ResizeConsumerService , useValue : resizeConsumerMock } ]
43
+ }
44
+ } )
45
+ . createComponent ( TestComponent ) ;
51
46
52
47
parentComponentFixture . detectChanges ( ) ;
53
48
directiveEl = parentComponentFixture . debugElement . query ( By . directive ( ScalableDirective ) ) ;
54
49
directiveInstance = directiveEl . injector . get ( ScalableDirective ) ;
55
50
resizeHandlerService = directiveEl . injector . get ( ResizeConsumerService ) ;
56
- renderer = directiveEl . injector . get ( Renderer2 ) ;
57
51
} ) ;
58
52
59
53
it ( 'should create an instance' , ( ) => {
60
54
expect ( directiveInstance ) . toBeTruthy ( ) ;
61
55
} ) ;
62
56
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 ;
57
+ it ( 'should set the height style on the element' , ( ) => {
58
+ jest . spyOn ( resizeHandlerService , 'heightPx' ) . mockReturnValue ( 300 ) ;
107
59
parentComponentFixture . detectChanges ( ) ;
108
- expect ( rendererSpy ) . not . toHaveBeenCalled ( ) ;
109
- rendererSpy . mockClear ( ) ;
60
+ expect ( directiveEl . nativeElement . getAttribute ( 'style' ) ) . toBe ( 'height: 300px;' ) ;
110
61
} ) ;
111
62
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 ;
63
+ it ( 'should not set the height style on the element when heightPx is not available' , ( ) => {
64
+ jest . spyOn ( resizeHandlerService , 'heightPx' ) . mockReturnValue ( undefined ) ;
117
65
parentComponentFixture . detectChanges ( ) ;
118
- expect ( rendererSpy ) . not . toHaveBeenCalled ( ) ;
119
- rendererSpy . mockClear ( ) ;
66
+ expect ( directiveEl . nativeElement . getAttribute ( 'style' ) ) . toBe ( '' ) ;
120
67
} ) ;
121
68
} ) ;
0 commit comments