Skip to content

Commit 38aec14

Browse files
zouxuozcpojer
authored andcommitted
Pretty format for DOMStringMap and NamedNodeMap (#5246)
1 parent b86d932 commit 38aec14

7 files changed

Lines changed: 112 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
([#5230](https://github.com/facebook/jest/pull/5230))
1313
* `[expect]` Do not override `Error` stack (with `Error.captureStackTrace`) for
1414
custom matchers. ([#5162](https://github.com/facebook/jest/pull/5162))
15+
* `[pretty-format]` Pretty format for DOMStringMap and NamedNodeMap
16+
([#5233](https://github.com/facebook/jest/pull/5233))
1517

1618
### Features
1719

packages/jest-diff/src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {NO_DIFF_MESSAGE, SIMILAR_MESSAGE} from './constants';
1717

1818
const {
1919
AsymmetricMatcher,
20+
DOMCollection,
2021
DOMElement,
2122
Immutable,
2223
ReactElement,
@@ -27,6 +28,7 @@ const PLUGINS = [
2728
ReactTestComponent,
2829
ReactElement,
2930
DOMElement,
31+
DOMCollection,
3032
Immutable,
3133
AsymmetricMatcher,
3234
];

packages/jest-matcher-utils/src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import getType from 'jest-get-type';
1212
import prettyFormat from 'pretty-format';
1313
const {
1414
AsymmetricMatcher,
15+
DOMCollection,
1516
DOMElement,
1617
Immutable,
1718
ReactElement,
@@ -22,6 +23,7 @@ const PLUGINS = [
2223
ReactTestComponent,
2324
ReactElement,
2425
DOMElement,
26+
DOMCollection,
2527
Immutable,
2628
AsymmetricMatcher,
2729
];

packages/jest-snapshot/src/plugins.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import prettyFormat from 'pretty-format';
1313
import jestMockSerializer from './mock_serializer';
1414

1515
const {
16+
DOMCollection,
1617
DOMElement,
1718
Immutable,
1819
ReactElement,
@@ -23,6 +24,7 @@ let PLUGINS = [
2324
ReactTestComponent,
2425
ReactElement,
2526
DOMElement,
27+
DOMCollection,
2628
Immutable,
2729
jestMockSerializer,
2830
];
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @jest-environment jsdom
8+
* @flow
9+
*/
10+
/* eslint-env browser*/
11+
12+
'use strict';
13+
14+
const prettyFormat = require('../');
15+
const {DOMCollection} = prettyFormat.plugins;
16+
const toPrettyPrintTo = require('./expect_util').getPrettyPrint([
17+
DOMCollection,
18+
]);
19+
20+
const expect: any = global.expect;
21+
expect.extend({toPrettyPrintTo});
22+
23+
describe('DOMCollection Plugin', () => {
24+
it('supports a DOMStringMap', () => {
25+
const el = document.createElement('div');
26+
el.dataset.foo = 'bar';
27+
28+
expect(el.dataset).toPrettyPrintTo('DOMStringMap {\n "foo": "bar",\n}');
29+
});
30+
31+
it('supports a NamedNodeMap', () => {
32+
const el = document.createElement('div');
33+
el.setAttribute('foo', 'bar');
34+
35+
expect(el.attributes).toPrettyPrintTo('NamedNodeMap {\n "foo": "bar",\n}');
36+
});
37+
});

packages/pretty-format/src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030

3131
import AsymmetricMatcher from './plugins/asymmetric_matcher';
3232
import ConvertAnsi from './plugins/convert_ansi';
33+
import DOMCollection from './plugins/dom_collection';
3334
import DOMElement from './plugins/dom_element';
3435
import Immutable from './plugins/immutable';
3536
import ReactElement from './plugins/react_element';
@@ -489,6 +490,7 @@ function prettyFormat(val: any, options?: OptionsReceived): string {
489490
prettyFormat.plugins = {
490491
AsymmetricMatcher,
491492
ConvertAnsi,
493+
DOMCollection,
492494
DOMElement,
493495
Immutable,
494496
ReactElement,
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow
8+
*/
9+
10+
import type {Config, NewPlugin, Printer, Refs} from 'types/PrettyFormat';
11+
12+
import {printObjectProperties} from '../collections';
13+
14+
const SPACE = ' ';
15+
16+
const COLLECTION_NAMES = ['DOMStringMap', 'NamedNodeMap'];
17+
18+
export const test = (val: any) =>
19+
val &&
20+
val.constructor &&
21+
COLLECTION_NAMES.indexOf(val.constructor.name) !== -1;
22+
23+
const convertCollectionToObject = (collection: any) => {
24+
let result = {};
25+
26+
if (collection.constructor.name === 'NamedNodeMap') {
27+
for (let i = 0; i < collection.length; i++) {
28+
result[collection[i].name] = collection[i].value;
29+
}
30+
} else {
31+
result = Object.assign({}, collection);
32+
}
33+
34+
return result;
35+
};
36+
37+
export const serialize = (
38+
collection: any,
39+
config: Config,
40+
indentation: string,
41+
depth: number,
42+
refs: Refs,
43+
printer: Printer,
44+
): string => {
45+
if (++depth > config.maxDepth) {
46+
return '[' + collection.constructor.name + ']';
47+
}
48+
49+
return (
50+
collection.constructor.name +
51+
SPACE +
52+
'{' +
53+
printObjectProperties(
54+
convertCollectionToObject(collection),
55+
config,
56+
indentation,
57+
depth,
58+
refs,
59+
printer,
60+
) +
61+
'}'
62+
);
63+
};
64+
65+
export default ({serialize, test}: NewPlugin);

0 commit comments

Comments
 (0)