1+ 'use strict' ;
2+
3+ var diff = require ( 'jest-diff' ) ;
4+ var prettyFormat = require ( 'pretty-format' ) ;
5+ var ReactElement = prettyFormat . plugins . ReactElement ;
6+
7+ var reactElement = Symbol . for ( 'react.element' ) ;
8+
9+ var defaultOptions = {
10+ expand : false ,
11+ colors : false
12+ } ;
13+
14+ var snapshotDiff = function snapshotDiff ( valueA , valueB ) {
15+ var options = arguments . length > 2 && arguments [ 2 ] !== undefined ? arguments [ 2 ] : defaultOptions ;
16+
17+ var difference = void 0 ;
18+
19+ if ( isReactComponent ( valueA ) && isReactComponent ( valueB ) ) {
20+ difference = diffReactComponents ( valueA , valueB , options ) ;
21+ } else {
22+ difference = diffStrings ( valueA , valueB , options ) ;
23+ }
24+
25+ if ( ! options . colors ) {
26+ var stripAnsi = require ( 'strip-ansi' ) ;
27+ return stripAnsi ( difference ) ;
28+ }
29+
30+ return difference ;
31+ } ;
32+
33+ var isReactComponent = function isReactComponent ( valueA ) {
34+ return valueA && valueA . $$typeof === reactElement ;
35+ } ;
36+
37+ function diffStrings ( valueA , valueB , options ) {
38+ return diff ( valueA , valueB , {
39+ expand : options . expand ,
40+ aAnnotation : 'First value' ,
41+ aAnnotation : 'Second value'
42+ } ) ;
43+ }
44+
45+ function diffReactComponents ( valueA , valueB , options ) {
46+ var renderer = require ( 'react-test-renderer' ) ;
47+ var reactValueA = renderer . create ( valueA ) . toJSON ( ) ;
48+ var reactValueB = renderer . create ( valueB ) . toJSON ( ) ;
49+ var aAnnotation = valueA . type . displayName || valueA . type . name || 'Unknown' ;
50+ var bAnnotation = valueB . type . displayName || valueB . type . name || 'Unknown' ;
51+ var prettyFormatOptions = { plugins : [ ReactElement ] , min : true } ;
52+
53+ return diff ( reactValueA , reactValueB , {
54+ expand : options . expand ,
55+ aAnnotation : prettyFormat ( valueA , prettyFormatOptions ) ,
56+ bAnnotation : prettyFormat ( valueB , prettyFormatOptions )
57+ } ) ;
58+ }
59+
60+ module . exports = snapshotDiff ;
0 commit comments