-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelpers.ts
More file actions
82 lines (70 loc) · 2.26 KB
/
helpers.ts
File metadata and controls
82 lines (70 loc) · 2.26 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import { StyleSheet } from "react-native";
import { ReactTestInstance } from "react-test-renderer";
import { AssertiveStyle, StyleObject, TestableTextMatcher } from "./types";
/**
* Checks if a value is empty.
*
* @param value - The value to check.
* @returns `true` if the value is empty, `false` otherwise.
*/
export function isEmpty(value: unknown): boolean {
if (!value) {
return true;
}
if (Array.isArray(value)) {
return value.length === 0;
}
return false;
}
/**
* Converts a ReactTestInstance to a string representation.
*
* @param instance - The ReactTestInstance to convert.
* @returns A string representation of the instance.
*/
export function instanceToString(instance: ReactTestInstance | null): string {
if (instance === null) {
return "null";
}
return `<${instance.type.toString()} ... />`;
}
/**
* Checks if a text matches a given matcher.
*
* @param text - The text to check.
* @param matcher - The matcher to use for comparison.
* @returns `true` if the text matches the matcher, `false` otherwise.
* @throws Error if the matcher is not a string, RegExp, or function.
* @example
* ```ts
* textMatches("Hello World", "Hello World"); // true
* textMatches("Hello World", /Hello/); // true
* textMatches("Hello World", (text) => text.startsWith("Hello")); // true
* textMatches("Hello World", "Goodbye"); // false
* textMatches("Hello World", /Goodbye/); // false
* textMatches("Hello World", (text) => text.startsWith("Goodbye")); // false
* ```
*/
export function textMatches(
text: string,
matcher: TestableTextMatcher,
): boolean {
if (typeof matcher === "string") {
return text.includes(matcher);
}
if (matcher instanceof RegExp) {
return matcher.test(text);
}
if (typeof matcher === "function") {
return matcher(text);
}
throw new Error("Matcher must be a string, RegExp, or function.");
}
export function getFlattenedStyle(style: AssertiveStyle): StyleObject {
const flattenedStyle = StyleSheet.flatten(style);
return flattenedStyle ? (flattenedStyle as StyleObject) : {};
}
export function styleToString(flattenedStyle: StyleObject): string {
const styleEntries = Object.entries(flattenedStyle);
return styleEntries.map(([key, value]) => `\t- ${key}: ${String(value)};`).join("\n");
}