Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Commit 734bd82

Browse files
Daniel15facebook-github-bot
authored andcommitted
Encode non-ASCII characters in all snapshot tests
Summary: We're not supposed to commit non-ASCII text to www for some reason. However, some snapshots contained fancy characters. This diff updates Jest to use the `NonASCIIStringSnapshotSerializer` I added in D16570959 to be used for **all** snapshot tests. Reviewed By: inlineblock Differential Revision: D16633746 fbshipit-source-id: f3055ec873571f5bbe32e8c7d958f663bf7439c9
1 parent 1ff8c8c commit 734bd82

File tree

5 files changed

+78
-25
lines changed

5 files changed

+78
-25
lines changed
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Copyright 2004-present Facebook. All Rights Reserved.
3+
*
4+
* @emails oncall+ads_integration_management
5+
* @flow strict-local
6+
* @format
7+
*/
8+
9+
'use strict';
10+
11+
const MAX_ASCII_CHARACTER = 127;
12+
13+
/**
14+
* Serializes strings with non-ASCII characters to their Unicode escape
15+
* sequences (eg. \u2022), to avoid hitting this lint rule:
16+
* "Source code should only include printable US-ASCII bytes"
17+
*/
18+
const NonASCIIStringSnapshotSerializer = {
19+
test(val: mixed): boolean {
20+
if (typeof val !== 'string') {
21+
return false;
22+
}
23+
for (let i = 0; i < val.length; i++) {
24+
if (val.charCodeAt(i) > MAX_ASCII_CHARACTER) {
25+
return true;
26+
}
27+
}
28+
return false;
29+
},
30+
31+
print: (val: string): string => {
32+
return (
33+
'"' +
34+
val
35+
.split('')
36+
.map(char => {
37+
const code = char.charCodeAt(0);
38+
return code > MAX_ASCII_CHARACTER
39+
? '\\u' + code.toString(16).padStart(4, '0')
40+
: char;
41+
})
42+
.join('')
43+
// Keep the same behaviour as Jest's regular string snapshot
44+
// serialization, which escapes double quotes.
45+
.replace(/"/g, '\\"') +
46+
'"'
47+
);
48+
},
49+
};
50+
51+
module.exports = NonASCIIStringSnapshotSerializer;

src/model/encoding/__tests__/__snapshots__/convertFromHTMLToContentBlocks-test.js.snap

+2-2
Original file line numberDiff line numberDiff line change
@@ -3070,9 +3070,9 @@ Array [
30703070
]
30713071
`;
30723072

3073-
exports[`img with data protocol should be correctly parsed 1`] = `"📷"`;
3073+
exports[`img with data protocol should be correctly parsed 1`] = `"\\ud83d\\udcf7"`;
30743074

3075-
exports[`img with http protocol should have camera emoji content 1`] = `"📷"`;
3075+
exports[`img with http protocol should have camera emoji content 1`] = `"\\ud83d\\udcf7"`;
30763076

30773077
exports[`img with role presentation should not be rendered 1`] = `Array []`;
30783078

src/model/encoding/__tests__/convertFromHTMLToContentBlocks-test.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
'use strict';
1212

1313
jest.disableAutomock();
14+
expect.addSnapshotSerializer(require('NonASCIIStringSnapshotSerializer'));
1415

1516
jest.mock('generateRandomKey');
1617

src/model/modifier/__tests__/DraftRemovableWord-test.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
'use strict';
1313

1414
jest.disableAutomock();
15+
expect.addSnapshotSerializer(require('NonASCIIStringSnapshotSerializer'));
1516

1617
const DraftRemovableWord = require('DraftRemovableWord');
1718

src/model/modifier/__tests__/__snapshots__/DraftRemovableWord-test.js.snap

+23-23
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ exports[`must identify nothing in an empty string 2`] = `""`;
66

77
exports[`must identify punct/whitespace strings looking backward 1`] = `" "`;
88

9-
exports[`must identify punct/whitespace strings looking backward 2`] = `"؍﴾﴿"`;
9+
exports[`must identify punct/whitespace strings looking backward 2`] = `"\\u060d\\ufd3e\\ufd3f"`;
1010

1111
exports[`must identify punct/whitespace strings looking backward 3`] = `". . . . ."`;
1212

1313
exports[`must identify punct/whitespace strings looking backward 4`] = `" !!!??? "`;
1414

1515
exports[`must identify punct/whitespace strings looking forward 1`] = `" "`;
1616

17-
exports[`must identify punct/whitespace strings looking forward 2`] = `"؍﴾﴿"`;
17+
exports[`must identify punct/whitespace strings looking forward 2`] = `"\\u060d\\ufd3e\\ufd3f"`;
1818

1919
exports[`must identify punct/whitespace strings looking forward 3`] = `". . . . ."`;
2020

@@ -24,9 +24,9 @@ exports[`must identify punctuation with apostrophes 1`] = `"'hello"`;
2424

2525
exports[`must identify punctuation with apostrophes 2`] = `"hello'"`;
2626

27-
exports[`must identify punctuation with apostrophes 3`] = `"‘hello"`;
27+
exports[`must identify punctuation with apostrophes 3`] = `"\\u2018hello"`;
2828

29-
exports[`must identify punctuation with apostrophes 4`] = `"hello"`;
29+
exports[`must identify punctuation with apostrophes 4`] = `"hello\\u2019"`;
3030

3131
exports[`must identify punctuation with apostrophes 5`] = `"('hello"`;
3232

@@ -42,69 +42,69 @@ exports[`must identify words ended by punctuation looking backward 2`] = `"anima
4242

4343
exports[`must identify words ended by punctuation looking backward 3`] = `"animals^"`;
4444

45-
exports[`must identify words ended by punctuation looking backward 4`] = `"animals؍﴾﴿"`;
45+
exports[`must identify words ended by punctuation looking backward 4`] = `"animals\\u060d\\ufd3e\\ufd3f"`;
4646

4747
exports[`must identify words ended by punctuation looking backward 5`] = `"animals.. .. .."`;
4848

4949
exports[`must identify words ended by spaces looking backward 1`] = `"animals "`;
5050

51-
exports[`must identify words ended by spaces looking backward 2`] = `"fàbregas "`;
51+
exports[`must identify words ended by spaces looking backward 2`] = `"f\\u00e0bregas "`;
5252

53-
exports[`must identify words ended by spaces looking backward 3`] = `"غػؼ "`;
53+
exports[`must identify words ended by spaces looking backward 3`] = `"\\u063a\\u063b\\u063c "`;
5454

5555
exports[`must identify words led by punctuation looking forward 1`] = `".the"`;
5656

5757
exports[`must identify words led by punctuation looking forward 2`] = `"|the"`;
5858

5959
exports[`must identify words led by punctuation looking forward 3`] = `"^the"`;
6060

61-
exports[`must identify words led by punctuation looking forward 4`] = `"؍﴾﴿the"`;
61+
exports[`must identify words led by punctuation looking forward 4`] = `"\\u060d\\ufd3e\\ufd3fthe"`;
6262

6363
exports[`must identify words led by punctuation looking forward 5`] = `".. .. ..the"`;
6464

6565
exports[`must identify words led by spaces looking forward 1`] = `" the"`;
6666

67-
exports[`must identify words led by spaces looking forward 2`] = `" thé"`;
67+
exports[`must identify words led by spaces looking forward 2`] = `" th\\u00e9"`;
6868

69-
exports[`must identify words led by spaces looking forward 3`] = `" طظع"`;
69+
exports[`must identify words led by spaces looking forward 3`] = `" \\u0637\\u0638\\u0639"`;
7070

7171
exports[`must identify words looking backward 1`] = `"animals"`;
7272

73-
exports[`must identify words looking backward 2`] = `"fàbregas"`;
73+
exports[`must identify words looking backward 2`] = `"f\\u00e0bregas"`;
7474

75-
exports[`must identify words looking backward 3`] = `"غػؼ"`;
75+
exports[`must identify words looking backward 3`] = `"\\u063a\\u063b\\u063c"`;
7676

77-
exports[`must identify words looking backward 4`] = `"ハッシュ"`;
77+
exports[`must identify words looking backward 4`] = `"\\u30cf\\u30c3\\u30b7\\u30e5"`;
7878

79-
exports[`must identify words looking backward 5`] = `"트위터"`;
79+
exports[`must identify words looking backward 5`] = `"\\ud2b8\\uc704\\ud130"`;
8080

81-
exports[`must identify words looking backward 6`] = `"ᄀᄁᄇ"`;
81+
exports[`must identify words looking backward 6`] = `"\\uffa1\\uffa2\\uffb2"`;
8282

83-
exports[`must identify words looking backward 7`] = `"бДЖЦ"`;
83+
exports[`must identify words looking backward 7`] = `"\\u0431\\u0414\\u0416\\u0426"`;
8484

8585
exports[`must identify words looking backward 8`] = `"tomcat"`;
8686

8787
exports[`must identify words looking forward 1`] = `"the"`;
8888

89-
exports[`must identify words looking forward 2`] = `"thé"`;
89+
exports[`must identify words looking forward 2`] = `"th\\u00e9"`;
9090

91-
exports[`must identify words looking forward 3`] = `"طظع"`;
91+
exports[`must identify words looking forward 3`] = `"\\u0637\\u0638\\u0639"`;
9292

93-
exports[`must identify words looking forward 4`] = `"会議中"`;
93+
exports[`must identify words looking forward 4`] = `"\\u4f1a\\u8b70\\u4e2d"`;
9494

95-
exports[`must identify words looking forward 5`] = `"트위터"`;
95+
exports[`must identify words looking forward 5`] = `"\\ud2b8\\uc704\\ud130"`;
9696

97-
exports[`must identify words looking forward 6`] = `"ᆪᆭᄚ"`;
97+
exports[`must identify words looking forward 6`] = `"\\uffa3\\uffa6\\uffb0"`;
9898

99-
exports[`must identify words looking forward 7`] = `"ашок"`;
99+
exports[`must identify words looking forward 7`] = `"\\u0430\\u0448\\u043e\\u043a"`;
100100

101101
exports[`must identify words looking forward 8`] = `"f14"`;
102102

103103
exports[`must identify words with apostrophes looking backward 1`] = `"don't"`;
104104

105105
exports[`must identify words with apostrophes looking forward 1`] = `"you're"`;
106106

107-
exports[`must identify words with curly quotes looking forward 1`] = `"you’re"`;
107+
exports[`must identify words with curly quotes looking forward 1`] = `"you\\u2019re"`;
108108

109109
exports[`must identify words with underscores 1`] = `"under_score"`;
110110

0 commit comments

Comments
 (0)