11import { ComponentFixture , fakeAsync , TestBed , tick } from '@angular/core/testing' ;
2- import { Component } from '@angular/core' ;
32
43import { changeBrowserInnerWidth , configureTestSuite } from '../../../util-test/util-expect.spec' ;
54
65import { PoPageContentComponent } from './po-page-content.component' ;
76
8- @Component ( {
9- selector : 'po-page-content-div' ,
10- template : `
11- <div class="po-toolbar"></div>
12- <div class="po-page-header"></div>
13- ` ,
14- styles : [
15- `
16- .po-page-header {
17- height: 100px;
18- width: 100%;
19- }
20- .po-toolbar {
21- height: 33px;
22- width: 100%;
23- }
24- `
25- ] ,
26- standalone : false
27- } )
28- class ContentDivComponent { }
29-
307describe ( 'PoPageContentComponent:' , ( ) => {
318 let component : PoPageContentComponent ;
329 let fixture : ComponentFixture < PoPageContentComponent > ;
3310
34- let fixtureDiv : ComponentFixture < ContentDivComponent > ;
35-
3611 const eventResize = document . createEvent ( 'Event' ) ;
3712 eventResize . initEvent ( 'resize' , false , true ) ;
3813
3914 configureTestSuite ( ( ) => {
4015 TestBed . configureTestingModule ( {
41- declarations : [ ContentDivComponent , PoPageContentComponent ]
16+ declarations : [ PoPageContentComponent ]
4217 } ) ;
4318 } ) ;
4419
4520 beforeEach ( ( ) => {
46- fixtureDiv = TestBed . createComponent ( ContentDivComponent ) ;
47- fixtureDiv . detectChanges ( ) ;
48-
4921 fixture = TestBed . createComponent ( PoPageContentComponent ) ;
5022 component = fixture . componentInstance ;
5123 fixture . detectChanges ( ) ;
@@ -55,7 +27,7 @@ describe('PoPageContentComponent:', () => {
5527 expect ( component instanceof PoPageContentComponent ) . toBeTruthy ( ) ;
5628 } ) ;
5729
58- it ( 'constructor: should call recalculateHeaderSize' , ( ) => {
30+ it ( 'constructor: should call recalculateHeaderSize on resize ' , ( ) => {
5931 component [ 'initializeListeners' ] ( ) ;
6032 spyOn ( component , 'recalculateHeaderSize' ) ;
6133
@@ -66,12 +38,12 @@ describe('PoPageContentComponent:', () => {
6638 } ) ;
6739
6840 describe ( 'Methods:' , ( ) => {
69- it ( 'recalculateHeaderSize: should call setHeightContent and set `contentOpacity` to equal 1.' , fakeAsync ( ( ) => {
70- spyOn ( component , 'setHeightContent' ) ;
41+ it ( 'recalculateHeaderSize: should set height and contentOpacity to 1' , fakeAsync ( ( ) => {
7142 component . recalculateHeaderSize ( ) ;
7243 tick ( 100 ) ;
73- expect ( component . setHeightContent ) . toHaveBeenCalled ( ) ;
44+
7445 expect ( component . contentOpacity ) . toBe ( 1 ) ;
46+ expect ( component . height ) . toBeTruthy ( ) ;
7547 } ) ) ;
7648
7749 it ( 'ngAfterViewInit: should call recalculateHeaderSize' , ( ) => {
@@ -82,24 +54,44 @@ describe('PoPageContentComponent:', () => {
8254 expect ( component . recalculateHeaderSize ) . toHaveBeenCalled ( ) ;
8355 } ) ;
8456
85- it ( 'setHeightContent: should calculate height without pageHeader' , ( ) => {
86- component . setHeightContent ( undefined ) ;
57+ it ( 'should calculate height based on viewport when no .po-page ancestor' , fakeAsync ( ( ) => {
58+ component . recalculateHeaderSize ( ) ;
59+ tick ( 100 ) ;
60+
61+ expect ( component . height ) . toMatch ( / ^ \d + p x $ | ^ a u t o $ / ) ;
62+ } ) ) ;
8763
88- const bodyHeight = document . body . clientHeight ;
89- const valueExpected = bodyHeight ;
64+ it ( 'should set height to auto when inside nested po-page-content' , fakeAsync ( ( ) => {
65+ const wrapper = document . createElement ( 'div' ) ;
66+ wrapper . classList . add ( 'po-page-content' ) ;
67+ const poPage = document . createElement ( 'div' ) ;
68+ poPage . classList . add ( 'po-page' ) ;
69+ wrapper . appendChild ( poPage ) ;
70+ poPage . appendChild ( fixture . nativeElement ) ;
71+ document . body . appendChild ( wrapper ) ;
9072
91- expect ( component . height ) . toBe ( ` ${ valueExpected } px` ) ;
92- } ) ;
73+ component . recalculateHeaderSize ( ) ;
74+ tick ( 100 ) ;
9375
94- it ( 'setHeightContent: should calculate height with bottom actions' , ( ) => {
95- const pageHeaderElement = fixtureDiv . debugElement . nativeElement . querySelector ( '.po-page-header' ) as HTMLElement ;
96- const pageHeaderHeight = pageHeaderElement . offsetTop + pageHeaderElement . offsetHeight ;
97- const bodyHeight = document . body . clientHeight ;
98- const valueExpected = bodyHeight - pageHeaderHeight ;
76+ expect ( component . height ) . toBe ( 'auto' ) ;
9977
100- component . setHeightContent ( pageHeaderElement ) ;
78+ document . body . removeChild ( wrapper ) ;
79+ } ) ) ;
10180
102- expect ( component . height ) . toBe ( `${ valueExpected } px` ) ;
103- } ) ;
81+ it ( 'should fallback to 90% when calculated height is zero or negative without .po-page' , fakeAsync ( ( ) => {
82+ spyOn ( fixture . nativeElement , 'getBoundingClientRect' ) . and . returnValue ( {
83+ top : window . innerHeight + 100 ,
84+ bottom : window . innerHeight + 200 ,
85+ left : 0 ,
86+ right : 0 ,
87+ width : 0 ,
88+ height : 0
89+ } ) ;
90+
91+ component . recalculateHeaderSize ( ) ;
92+ tick ( 100 ) ;
93+
94+ expect ( component . height ) . toBe ( '90%' ) ;
95+ } ) ) ;
10496 } ) ;
10597} ) ;
0 commit comments