Skip to content

Commit 8fbcd4d

Browse files
author
Zaydek Michels-Gualtieri
committed
Added mockUserAgent module and test suite
1 parent 7b9bd68 commit 8fbcd4d

8 files changed

Lines changed: 120 additions & 11 deletions

File tree

src/RichTextEditor/utils/keyDownTypeFor/history.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import isCtrlOrMetaKey from "../isCtrlOrMetaKey"
2-
import keyCodeFor from "../keyCodeFor"
3-
import userAgent from "lib/userAgent"
1+
import isCtrlOrMetaKey from "lib/Client/isCtrlOrMetaKey"
2+
import keyCodeFor from "lib/Client/keyCodeFor"
3+
import userAgent from "lib/Client/userAgent"
44

55
const history = {
66
undo(e) {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import history from "./history"
2+
import keyCodeFor from "lib/Client/keyCodeFor"
3+
4+
// Mocks a non-macOS user agent.
5+
function mockNonMacOS() {
6+
Object.defineProperty(window.navigator, "userAgent", {
7+
value: "...",
8+
configurable: true,
9+
})
10+
}
11+
12+
// Mocks a macOS user agent.
13+
function mockMacOS() {
14+
Object.defineProperty(window.navigator, "userAgent", {
15+
value: "... Mac OS X ...",
16+
configurable: true,
17+
})
18+
}
19+
20+
test("undo(...); non-macOS", () => {
21+
mockNonMacOS()
22+
expect(history.undo({
23+
shiftKey: false,
24+
ctrlKey: false,
25+
metaKey: true,
26+
keyCode: keyCodeFor("Z"),
27+
})).not.toBeTruthy()
28+
expect(history.undo({
29+
shiftKey: true,
30+
ctrlKey: false,
31+
metaKey: true,
32+
keyCode: keyCodeFor("Z"),
33+
})).not.toBeTruthy()
34+
})
35+
36+
test("undo(...); macOS", () => {
37+
mockMacOS()
38+
expect(history.undo({
39+
shiftKey: false,
40+
ctrlKey: false,
41+
metaKey: true,
42+
keyCode: keyCodeFor("Z"),
43+
})).toBeTruthy()
44+
expect(history.undo({
45+
shiftKey: true,
46+
ctrlKey: false,
47+
metaKey: true,
48+
keyCode: keyCodeFor("Z"),
49+
})).not.toBeTruthy()
50+
})
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { default } from "./detectKeydownType"
1+
export { default } from "./keyDownTypeFor"

src/RichTextEditor/utils/keyDownTypeFor/keyCodeFor.js renamed to src/RichTextEditor/utils/keyDownTypeFor/keyDownTypeFor.js

File renamed without changes.

src/lib/Client/isCtrlOrMetaKey/index.test.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import isCtrlOrMetaKey from "./index"
22

3-
// https://github.com/facebook/jest/issues/717#issuecomment-369872760
4-
5-
test("non-macOS", () => {
3+
// Mocks a non-macOS user agent.
4+
function mockNonMacOS() {
65
Object.defineProperty(window.navigator, "userAgent", {
76
value: "...",
87
configurable: true,
98
})
9+
}
10+
11+
// Mocks a macOS user agent.
12+
function mockMacOS() {
13+
Object.defineProperty(window.navigator, "userAgent", {
14+
value: "... Mac OS X ...",
15+
configurable: true,
16+
})
17+
}
18+
19+
test("non-macOS", () => {
20+
mockNonMacOS()
1021
/* eslint-disable no-multi-spaces */
1122
expect(isCtrlOrMetaKey({ metaKey: false, ctrlKey: false })).not.toBeTruthy()
1223
expect(isCtrlOrMetaKey({ metaKey: false, ctrlKey: true })).toBeTruthy()
@@ -16,10 +27,7 @@ test("non-macOS", () => {
1627
})
1728

1829
test("macOS", () => {
19-
Object.defineProperty(window.navigator, "userAgent", {
20-
value: "... Mac OS X ...",
21-
configurable: true,
22-
})
30+
mockMacOS()
2331
/* eslint-disable no-multi-spaces */
2432
expect(isCtrlOrMetaKey({ metaKey: false, ctrlKey: false })).not.toBeTruthy()
2533
expect(isCtrlOrMetaKey({ metaKey: false, ctrlKey: true })).not.toBeTruthy()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Mocks a non-macOS user agent.
2+
export function mockNonMacOS() {
3+
Object.defineProperty(window.navigator, "userAgent", {
4+
value: "...",
5+
configurable: true,
6+
})
7+
}
8+
9+
// Mocks a macOS user agent.
10+
export function mockMacOS() {
11+
Object.defineProperty(window.navigator, "userAgent", {
12+
value: "... Mac OS X ...",
13+
configurable: true,
14+
})
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { // Unsorted
2+
mockNonMacOS,
3+
mockMacOS,
4+
} from "./index"
5+
6+
// Tests the user agent for a substring.
7+
//
8+
// https://css-tricks.com/snippets/javascript/test-mac-pc-javascript
9+
function testUserAgent(substr) {
10+
return navigator.userAgent.indexOf(substr) >= 0
11+
}
12+
13+
test("non-macOS", () => {
14+
mockNonMacOS()
15+
expect(testUserAgent("Mac OS X")).not.toBeTruthy()
16+
})
17+
18+
test("macOS", () => {
19+
mockMacOS()
20+
expect(testUserAgent("Mac OS X")).toBeTruthy()
21+
})

src/lib/Client/userAgent/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,18 @@ const userAgent = {
1313
}
1414

1515
export default userAgent
16+
17+
// const userAgent = {
18+
// // Tests the user agent for a substring.
19+
// //
20+
// // https://css-tricks.com/snippets/javascript/test-mac-pc-javascript
21+
// test(substr) {
22+
// return navigator.userAgent.indexOf(substr) >= 0
23+
// },
24+
// // https://apple.com
25+
// isAAPL: this.test("Mac OS X"),
26+
// // https://android.com
27+
// isGOOG: this.test("Android"),
28+
// }
29+
//
30+
// export default userAgent

0 commit comments

Comments
 (0)