Skip to content

Commit 93dbb0a

Browse files
Merge pull request #24 from mrjones2014/feature/browser-utils
add BrowserUtils.isIE() method
2 parents 6a8da0a + 0f8640c commit 93dbb0a

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { BrowserUtils } from "./browser-utils";
2+
3+
describe("BrowserUtils", () => {
4+
describe("isIE", () => {
5+
it("when wondow.document.documentMode is undefined, then returns false", () => {
6+
// Arrange
7+
window.document.documentMode = undefined;
8+
9+
// Act
10+
const result = BrowserUtils.isIE();
11+
12+
// Assert
13+
expect(result).toBeFalse();
14+
});
15+
16+
it("when window.document.documentMode is truthy, then returns true", () => {
17+
// Arrange
18+
window.document.documentMode = true;
19+
20+
// Act
21+
const result = BrowserUtils.isIE();
22+
23+
// Assert
24+
expect(result).toBeTrue();
25+
});
26+
});
27+
28+
describe("isNotIE", () => {
29+
it("when wondow.document.documentMode is undefined, then returns true", () => {
30+
// Arrange
31+
window.document.documentMode = undefined;
32+
33+
// Act
34+
const result = BrowserUtils.isNotIE();
35+
36+
// Assert
37+
expect(result).toBeTrue();
38+
});
39+
40+
it("when window.document.documentMode is truthy, then returns false", () => {
41+
// Arrange
42+
window.document.documentMode = true;
43+
44+
// Act
45+
const result = BrowserUtils.isNotIE();
46+
47+
// Assert
48+
expect(result).toBeFalse();
49+
});
50+
});
51+
52+
// Clean up
53+
window.document.documentMode = undefined;
54+
});

src/utilities/browser-utils.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Allows us to use the documentMode for feature detection on globalThis.Document
3+
*/
4+
declare global {
5+
interface Document {
6+
documentMode?: any;
7+
}
8+
}
9+
10+
/**
11+
* Returns true if the detected browser is Internet Explorer.
12+
* @returns boolean
13+
*/
14+
const _isIE = (): boolean => {
15+
return /*@cc_on!@*/ false || !!document.documentMode;
16+
};
17+
18+
/**
19+
* Returns true if the brower is NOT Internet Explorer.
20+
* @returns boolean
21+
*/
22+
const _isNotIE = (): boolean => !_isIE();
23+
24+
export const BrowserUtils = {
25+
isIE: _isIE,
26+
isNotIE: _isNotIE,
27+
};

0 commit comments

Comments
 (0)