/** * 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. * * @format */ import {render, fireEvent} from '@testing-library/react'; import React from 'react'; import {act} from 'react-dom/test-utils'; import {DataFormatter, TruncateHelper} from '../DataFormatter'; test('default formatter', () => { expect(DataFormatter.format(true)).toMatchInlineSnapshot(`"true"`); expect(DataFormatter.format(false)).toMatchInlineSnapshot(`"false"`); expect(DataFormatter.format(3)).toMatchInlineSnapshot(`"3"`); expect(DataFormatter.format(null)).toMatchInlineSnapshot(`""`); expect(DataFormatter.format(undefined)).toMatchInlineSnapshot(`""`); expect( DataFormatter.format(new Date(2020, 2, 3, 5, 8, 4, 244654)), ).toMatchInlineSnapshot(`"05:12:08.654"`); expect(DataFormatter.format('test')).toMatchInlineSnapshot(`"test"`); expect(DataFormatter.format({hello: 'world'})).toMatchInlineSnapshot(` "{ \\"hello\\": \\"world\\" }" `); expect(DataFormatter.format({hello: ['world']})).toMatchInlineSnapshot(` "{ \\"hello\\": [ \\"world\\" ] }" `); expect(DataFormatter.format(new Map([['hello', 'world']]))) .toMatchInlineSnapshot(` "[ [ \\"hello\\", \\"world\\" ] ]" `); expect(DataFormatter.format(new Set([['hello', 'world']]))) .toMatchInlineSnapshot(` "[ [ \\"hello\\", \\"world\\" ] ]" `); const unserializable: any = {}; unserializable.x = unserializable; expect(DataFormatter.format(unserializable)).toMatchInlineSnapshot(` " starting at object with constructor 'Object' --- property 'x' closes the circle>" `); // make sure we preserve newlines expect(DataFormatter.format('Test 123\n\t\t345\n\t\t67')) .toMatchInlineSnapshot(` "Test 123 345 67" `); }); test('linkify formatter', () => { const linkify = (value: any) => DataFormatter.format(value, DataFormatter.linkify); // verify fallback expect(linkify({hello: 'world'})).toMatchInlineSnapshot(` "{ \\"hello\\": \\"world\\" }" `); expect(linkify('hi there!')).toMatchInlineSnapshot(`"hi there!"`); expect(linkify('https://www.google.com')).toMatchInlineSnapshot(` https://www.google.com `); expect(linkify('www.google.com')).toMatchInlineSnapshot(`"www.google.com"`); expect(linkify('stuff.google.com')).toMatchInlineSnapshot( `"stuff.google.com"`, ); expect(linkify('test https://www.google.com test')).toMatchInlineSnapshot(` test https://www.google.com test `); expect(linkify('https://www.google.com test http://fb.com')) .toMatchInlineSnapshot(` https://www.google.com test http://fb.com `); expect(linkify('fb.com')).toMatchInlineSnapshot(`"fb.com"`); }); test('jsonify formatter', () => { const jsonify = (value: any) => DataFormatter.format(value, DataFormatter.prettyPrintJson); expect(jsonify({hello: 'world'})).toMatchInlineSnapshot(` "{ \\"hello\\": \\"world\\" }" `); expect(jsonify([{hello: 'world'}])).toMatchInlineSnapshot(` "[ { \\"hello\\": \\"world\\" } ]" `); // linkify json! expect( DataFormatter.format({hello: 'http://facebook.com'}, [ DataFormatter.prettyPrintJson, DataFormatter.linkify, ]), ).toMatchInlineSnapshot(` { "hello": " http://facebook.com " } `); }); test("jsonify doesn't process react elements", () => { const jsonify = (value: any) => DataFormatter.format(value, DataFormatter.prettyPrintJson); expect(jsonify('abcde')).toEqual('abcde'); expect(jsonify('{ a: 1 }')).toMatchInlineSnapshot(`"{ a: 1 }"`); expect(jsonify({a: 1})).toMatchInlineSnapshot(` "{ \\"a\\": 1 }" `); expect(jsonify(hi)).toMatchInlineSnapshot(` hi `); }); test('truncate formatter', () => { const truncate = (value: any) => DataFormatter.format(value, DataFormatter.truncate(5)); expect(truncate({test: true})).toMatchInlineSnapshot(` "{ \\"test\\": true }" `); expect(truncate('abcde')).toEqual('abcde'); expect(truncate('abcdefghi')).toMatchInlineSnapshot(` `); }); test('render truncate helper', () => { const res = render( , ); expect(res.baseElement).toMatchInlineSnapshot(`
!! C
`); act(() => { fireEvent.click(res.getAllByText(/and \d+ more/)[0]); }); expect(res.baseElement).toMatchInlineSnapshot(`
!! COOL CONTENT !!
`); });