-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pretty format for DOMStringMap and NamedNodeMap (#5246)
- Loading branch information
Showing
7 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
packages/pretty-format/src/__tests__/dom_collection.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @jest-environment jsdom | ||
* @flow | ||
*/ | ||
/* eslint-env browser*/ | ||
|
||
'use strict'; | ||
|
||
const prettyFormat = require('../'); | ||
const {DOMCollection} = prettyFormat.plugins; | ||
const toPrettyPrintTo = require('./expect_util').getPrettyPrint([ | ||
DOMCollection, | ||
]); | ||
|
||
const expect: any = global.expect; | ||
expect.extend({toPrettyPrintTo}); | ||
|
||
describe('DOMCollection Plugin', () => { | ||
it('supports a DOMStringMap', () => { | ||
const el = document.createElement('div'); | ||
el.dataset.foo = 'bar'; | ||
|
||
expect(el.dataset).toPrettyPrintTo('DOMStringMap {\n "foo": "bar",\n}'); | ||
}); | ||
|
||
it('supports a NamedNodeMap', () => { | ||
const el = document.createElement('div'); | ||
el.setAttribute('foo', 'bar'); | ||
|
||
expect(el.attributes).toPrettyPrintTo('NamedNodeMap {\n "foo": "bar",\n}'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat'; | ||
|
||
import {printObjectProperties} from '../collections'; | ||
|
||
const SPACE = ' '; | ||
|
||
const COLLECTION_NAMES = ['DOMStringMap', 'NamedNodeMap']; | ||
|
||
export const test = (val: any) => | ||
val && | ||
val.constructor && | ||
COLLECTION_NAMES.indexOf(val.constructor.name) !== -1; | ||
|
||
const convertCollectionToObject = (collection: any) => { | ||
let result = {}; | ||
|
||
if (collection.constructor.name === 'NamedNodeMap') { | ||
for (let i = 0; i < collection.length; i++) { | ||
result[collection[i].name] = collection[i].value; | ||
} | ||
} else { | ||
result = Object.assign({}, collection); | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
export const serialize = ( | ||
collection: any, | ||
config: Config, | ||
indentation: string, | ||
depth: number, | ||
refs: Refs, | ||
printer: Printer, | ||
): string => { | ||
if (++depth > config.maxDepth) { | ||
return '[' + collection.constructor.name + ']'; | ||
} | ||
|
||
return ( | ||
collection.constructor.name + | ||
SPACE + | ||
'{' + | ||
printObjectProperties( | ||
convertCollectionToObject(collection), | ||
config, | ||
indentation, | ||
depth, | ||
refs, | ||
printer, | ||
) + | ||
'}' | ||
); | ||
}; | ||
|
||
export default ({serialize, test}: NewPlugin); |