diff --git a/src/model/encoding/DraftStringKey.js b/src/model/encoding/DraftStringKey.js index a96bafd792..de454275a3 100644 --- a/src/model/encoding/DraftStringKey.js +++ b/src/model/encoding/DraftStringKey.js @@ -12,7 +12,7 @@ 'use strict'; const DraftStringKey = { - stringify: function(key: mixed): string { + stringify: function(key: ?string): string { return '_' + String(key); }, diff --git a/src/model/encoding/__tests__/DraftStringKey-test.js b/src/model/encoding/__tests__/DraftStringKey-test.js new file mode 100644 index 0000000000..8e37d99efc --- /dev/null +++ b/src/model/encoding/__tests__/DraftStringKey-test.js @@ -0,0 +1,25 @@ +/** + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @emails oncall+draft_js + * @flow strict-local + * @format + */ + +'use strict'; + +const {stringify, unstringify} = require('DraftStringKey'); + +test('must convert maybe strings to a string key', () => { + expect(stringify('anything')).toEqual('_anything'); + expect(stringify(null)).toEqual('_null'); +}); + +test('must convert string keys back to a string', () => { + expect(unstringify('_anything')).toEqual('anything'); + // This is a lossy conversion + expect(unstringify('_null')).toEqual('null'); +});