/
utils.js
61 lines (50 loc) · 2 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// @flow
import { all as cssProps } from "known-css-properties";
import voidElements from "void-elements";
import { spy } from "sinon";
import { camelCase, intersection, difference } from "lodash";
import {
getElementProps,
getEventProps,
getGlobalProps
} from "react-known-props";
const reducer = (acc, key) => ({ ...acc, [key]: key });
export const findHTMLTags = (wrapper: Object): Object =>
wrapper.findWhere(el => typeof el.type() === "string");
export const findHTMLTag = (wrapper: Object): Object =>
findHTMLTags(wrapper).first();
export const getHTMLTag = (wrapper: Object): string | void => {
const tag = findHTMLTag(wrapper);
return tag.length ? tag.type() : undefined;
};
export const getStyleProps = (): Object =>
cssProps
.filter(prop => !/^-/.test(prop))
.map(camelCase)
.reduce(reducer, {});
export const getReactProps = (type?: string): Object => {
const excludeProps = ["style", "className", "dangerouslySetInnerHTML"];
const excludePropsRegex = new RegExp(`^${excludeProps.join("|")}$`);
const props = type ? getElementProps(type) : getGlobalProps();
return props
.filter(prop => !excludePropsRegex.test(prop))
.reduce(reducer, {});
};
export const getReactEventHandlers = (): Object =>
getEventProps().reduce((acc, key) => ({ ...acc, [key]: spy() }), {});
export const getEventName = (prop: string = ""): string => {
const eventName = prop.replace(/^on/, "");
return eventName.charAt(0).toLowerCase() + eventName.slice(1);
};
export const isVoidElement = (wrapper: Object): boolean =>
!!voidElements[getHTMLTag(wrapper)];
export const getMissingClassName = (
originalClassName: string = "",
renderedClassName: string = ""
): string => {
const originalArray = originalClassName.split(" ");
const renderedArray = renderedClassName.split(" ");
const intersectionArray = intersection(originalArray, renderedArray);
const differenceArray = difference(originalArray, intersectionArray);
return differenceArray.filter(c => c !== "undefined").join(" ");
};