From 734bd82d8e201152c60d9ab89be563dbb3693387 Mon Sep 17 00:00:00 2001 From: Daniel Lo Nigro Date: Tue, 13 Aug 2019 16:44:55 -0700 Subject: [PATCH] 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 --- src/NonASCIIStringSnapshotSerializer.js | 51 +++++++++++++++++++ ...onvertFromHTMLToContentBlocks-test.js.snap | 4 +- .../convertFromHTMLToContentBlocks-test.js | 1 + .../__tests__/DraftRemovableWord-test.js | 1 + .../DraftRemovableWord-test.js.snap | 46 ++++++++--------- 5 files changed, 78 insertions(+), 25 deletions(-) create mode 100644 src/NonASCIIStringSnapshotSerializer.js diff --git a/src/NonASCIIStringSnapshotSerializer.js b/src/NonASCIIStringSnapshotSerializer.js new file mode 100644 index 0000000000..c88f7712d6 --- /dev/null +++ b/src/NonASCIIStringSnapshotSerializer.js @@ -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; diff --git a/src/model/encoding/__tests__/__snapshots__/convertFromHTMLToContentBlocks-test.js.snap b/src/model/encoding/__tests__/__snapshots__/convertFromHTMLToContentBlocks-test.js.snap index 7cf77dc415..cf1b0cd7e4 100644 --- a/src/model/encoding/__tests__/__snapshots__/convertFromHTMLToContentBlocks-test.js.snap +++ b/src/model/encoding/__tests__/__snapshots__/convertFromHTMLToContentBlocks-test.js.snap @@ -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 []`; diff --git a/src/model/encoding/__tests__/convertFromHTMLToContentBlocks-test.js b/src/model/encoding/__tests__/convertFromHTMLToContentBlocks-test.js index a18cd932e9..b1cad8ff4d 100644 --- a/src/model/encoding/__tests__/convertFromHTMLToContentBlocks-test.js +++ b/src/model/encoding/__tests__/convertFromHTMLToContentBlocks-test.js @@ -11,6 +11,7 @@ 'use strict'; jest.disableAutomock(); +expect.addSnapshotSerializer(require('NonASCIIStringSnapshotSerializer')); jest.mock('generateRandomKey'); diff --git a/src/model/modifier/__tests__/DraftRemovableWord-test.js b/src/model/modifier/__tests__/DraftRemovableWord-test.js index 239c49a389..22a5631641 100644 --- a/src/model/modifier/__tests__/DraftRemovableWord-test.js +++ b/src/model/modifier/__tests__/DraftRemovableWord-test.js @@ -12,6 +12,7 @@ 'use strict'; jest.disableAutomock(); +expect.addSnapshotSerializer(require('NonASCIIStringSnapshotSerializer')); const DraftRemovableWord = require('DraftRemovableWord'); diff --git a/src/model/modifier/__tests__/__snapshots__/DraftRemovableWord-test.js.snap b/src/model/modifier/__tests__/__snapshots__/DraftRemovableWord-test.js.snap index 5d1054fa5c..34a5a26d37 100644 --- a/src/model/modifier/__tests__/__snapshots__/DraftRemovableWord-test.js.snap +++ b/src/model/modifier/__tests__/__snapshots__/DraftRemovableWord-test.js.snap @@ -6,7 +6,7 @@ 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`] = `". . . . ."`; @@ -14,7 +14,7 @@ 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`] = `". . . . ."`; @@ -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"`; @@ -42,15 +42,15 @@ 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"`; @@ -58,45 +58,45 @@ 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"`; @@ -104,7 +104,7 @@ 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"`;