2018-10-06 23:42:52 +02:00
|
|
|
// @flow
|
2018-10-16 21:45:56 +02:00
|
|
|
/* eslint-disable no-console */
|
2018-10-06 23:42:52 +02:00
|
|
|
import * as React from 'react';
|
2018-11-17 19:13:44 +01:00
|
|
|
import prettyFormat, { plugins } from 'pretty-format';
|
2018-10-06 23:42:52 +02:00
|
|
|
import shallow from './shallow';
|
2018-10-16 21:45:56 +02:00
|
|
|
import render from './render';
|
2018-10-06 23:42:52 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Log pretty-printed shallow test component instance
|
|
|
|
|
*/
|
2018-10-16 21:45:56 +02:00
|
|
|
function debugShallow(
|
2018-10-06 23:42:52 +02:00
|
|
|
instance: ReactTestInstance | React.Element<*>,
|
|
|
|
|
message?: any
|
|
|
|
|
) {
|
|
|
|
|
const { output } = shallow(instance);
|
2018-10-16 21:45:56 +02:00
|
|
|
|
2018-10-06 23:42:52 +02:00
|
|
|
console.log(format(output), message || '');
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-16 21:45:56 +02:00
|
|
|
/**
|
|
|
|
|
* Log pretty-printed deep test component instance
|
|
|
|
|
*/
|
2018-10-18 09:13:47 +02:00
|
|
|
function debugDeep(
|
|
|
|
|
instance: React.Element<*> | ?ReactTestRendererJSON,
|
|
|
|
|
message?: any = ''
|
|
|
|
|
) {
|
|
|
|
|
try {
|
|
|
|
|
// We're assuming React.Element<*> here and fallback to
|
|
|
|
|
// rendering ?ReactTestRendererJSON
|
|
|
|
|
// $FlowFixMe
|
|
|
|
|
const { toJSON } = render(instance);
|
|
|
|
|
console.log(format(toJSON()), message);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(format(instance), message);
|
|
|
|
|
}
|
2018-10-16 21:45:56 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-06 23:42:52 +02:00
|
|
|
const format = input =>
|
|
|
|
|
prettyFormat(input, {
|
|
|
|
|
plugins: [plugins.ReactTestComponent, plugins.ReactElement],
|
2018-10-16 21:45:56 +02:00
|
|
|
highlight: true,
|
2018-10-06 23:42:52 +02:00
|
|
|
});
|
2018-10-16 21:45:56 +02:00
|
|
|
|
|
|
|
|
const debug = debugShallow;
|
|
|
|
|
|
|
|
|
|
debug.shallow = debugShallow;
|
|
|
|
|
debug.deep = debugDeep;
|
|
|
|
|
|
|
|
|
|
export default debug;
|