Skip to content

Commit 4e2feee

Browse files
committed
Add a window check, and test for it
1 parent 7c51aff commit 4e2feee

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

angular/projects/spark-angular/src/lib/utilities/isElementVisible/isElementVisible.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Test1Component {
2323
describe('isElementVisible', () => {
2424
let component: Test1Component;
2525
let componentFixture: ComponentFixture<Test1Component>;
26+
let windowSpy;
2627

2728
beforeEach(async(() => {
2829
TestBed.configureTestingModule({
@@ -31,11 +32,16 @@ describe('isElementVisible', () => {
3132
}));
3233

3334
beforeEach(() => {
35+
windowSpy = jest.spyOn(window, 'window', 'get');
3436
componentFixture = TestBed.createComponent(Test1Component);
3537
component = componentFixture.componentInstance;
3638
componentFixture.detectChanges();
3739
});
3840

41+
afterEach(() => {
42+
windowSpy.mockRestore();
43+
});
44+
3945
it('should return false for element with display: none', () => {
4046
expect(isElementVisible(component.elDisplayNone)).toEqual(false);
4147
});
@@ -51,4 +57,10 @@ describe('isElementVisible', () => {
5157
it('should return true for element with visibility: visible', () => {
5258
expect(isElementVisible(component.elVisibilityVisible)).toEqual(true);
5359
});
60+
61+
it('should not use window scope if window is undefined', () => {
62+
windowSpy.mockImplementation(() => undefined);
63+
expect(window).toBeUndefined();
64+
expect(isElementVisible(component.elDisplayNone)).toEqual(undefined);
65+
});
5466
});

angular/projects/spark-angular/src/lib/utilities/isElementVisible/isElementVisible.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ export const isElementVisible = (elRef: ElementRef): boolean => {
1010
if (!elRef) {
1111
return;
1212
}
13-
const elementDisplayValue = window.getComputedStyle(elRef.nativeElement)
14-
.display;
15-
const elementVisibilityValue = window.getComputedStyle(elRef.nativeElement)
16-
.visibility;
17-
const elementIsVisible =
18-
elementDisplayValue === 'none' || elementVisibilityValue === 'hidden'
19-
? false
20-
: true;
21-
return elementIsVisible;
13+
if (typeof window !== 'undefined') {
14+
const elementDisplayValue = window.getComputedStyle(elRef.nativeElement)
15+
.display;
16+
const elementVisibilityValue = window.getComputedStyle(elRef.nativeElement)
17+
.visibility;
18+
const elementIsVisible =
19+
elementDisplayValue === 'none' || elementVisibilityValue === 'hidden'
20+
? false
21+
: true;
22+
return elementIsVisible;
23+
}
2224
};

0 commit comments

Comments
 (0)