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

Commit

Permalink
Encode non-ASCII characters in all snapshot tests
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Daniel15 authored and facebook-github-bot committed Aug 13, 2019
1 parent 1ff8c8c commit 734bd82
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 25 deletions.
51 changes: 51 additions & 0 deletions src/NonASCIIStringSnapshotSerializer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* Copyright 2004-present Facebook. All Rights Reserved.
*
* @emails oncall+ads_integration_management
* @flow strict-local
* @format
*/

'use strict';

const MAX_ASCII_CHARACTER = 127;

/**
* Serializes strings with non-ASCII characters to their Unicode escape
* sequences (eg. \u2022), to avoid hitting this lint rule:
* "Source code should only include printable US-ASCII bytes"
*/
const NonASCIIStringSnapshotSerializer = {
test(val: mixed): boolean {
if (typeof val !== 'string') {
return false;
}
for (let i = 0; i < val.length; i++) {
if (val.charCodeAt(i) > MAX_ASCII_CHARACTER) {
return true;
}
}
return false;
},

print: (val: string): string => {
return (
'"' +
val
.split('')
.map(char => {
const code = char.charCodeAt(0);
return code > MAX_ASCII_CHARACTER
? '\\u' + code.toString(16).padStart(4, '0')
: char;
})
.join('')
// Keep the same behaviour as Jest's regular string snapshot
// serialization, which escapes double quotes.
.replace(/"/g, '\\"') +
'"'
);
},
};

module.exports = NonASCIIStringSnapshotSerializer;
Original file line number Diff line number Diff line change
Expand Up @@ -3070,9 +3070,9 @@ Array [
]
`;

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

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
'use strict';

jest.disableAutomock();
expect.addSnapshotSerializer(require('NonASCIIStringSnapshotSerializer'));

jest.mock('generateRandomKey');

Expand Down
1 change: 1 addition & 0 deletions src/model/modifier/__tests__/DraftRemovableWord-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
'use strict';

jest.disableAutomock();
expect.addSnapshotSerializer(require('NonASCIIStringSnapshotSerializer'));

const DraftRemovableWord = require('DraftRemovableWord');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ exports[`must identify nothing in an empty string 2`] = `""`;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Expand Down

0 comments on commit 734bd82

Please sign in to comment.