Skip to content

Commit 5817dc1

Browse files
committed
Refactor and add tests, modify isDisable function, add example annotation
1 parent e0cb309 commit 5817dc1

File tree

2 files changed

+67
-65
lines changed

2 files changed

+67
-65
lines changed

packages/native/src/lib/ElementAssertion.ts

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,6 @@ import { ReactTestInstance } from "react-test-renderer";
55

66
const { ReactTestComponent, ReactElement } = plugins;
77

8-
// Elements that support 'disabled'
9-
const DISABLE_TYPES = [
10-
"Button",
11-
"Slider",
12-
"Switch",
13-
"Text",
14-
"TouchableHighlight",
15-
"TouchableOpacity",
16-
"TouchableWithoutFeedback",
17-
"TouchableNativeFeedback",
18-
"View",
19-
"TextInput",
20-
"Pressable",
21-
];
22-
238
export class ElementAssertion extends Assertion<ReactTestInstance> {
249
public constructor(actual: ReactTestInstance) {
2510
super(actual);
@@ -47,6 +32,17 @@ export class ElementAssertion extends Assertion<ReactTestInstance> {
4732
);
4833
};
4934

35+
/**
36+
* Check if the component is diabled.
37+
*
38+
* @example
39+
* ```
40+
* expect(component).toBeDisabled();
41+
* ```
42+
*
43+
* @returns the assertion instance
44+
*/
45+
5046
public toBeDisabled(): this {
5147
const error = new AssertionError({
5248
actual: this.actual,
@@ -71,19 +67,16 @@ export class ElementAssertion extends Assertion<ReactTestInstance> {
7167
return true;
7268
}
7369

74-
if (!DISABLE_TYPES.includes(elementType)) {
75-
return false;
76-
}
77-
7870
return (
79-
!!element?.props?.disabled ||
80-
get<ReactTestInstance, boolean>(element, "props.accessibilityState.disabled", false) ||
71+
!! element.props?.["aria-disabled"] ||
72+
get(element, "props.disabled", false) ||
73+
get(element, "props.accessibilityState.disabled", false) ||
8174
get<ReactTestInstance, [string]>(element, "props.accessibilityStates", []).includes("disabled")
8275
);
8376
}
8477

8578
private isAncestorDisabled(element: ReactTestInstance): boolean {
86-
const parent = element.parent;
79+
const { parent } = element;
8780
return parent !== null && (this.isElementDisabled(element) || this.isAncestorDisabled(parent));
8881
}
8982
}
Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,67 @@
1-
import { render } from "@testing-library/react-native";
1+
import { AssertionError, expect } from "@assertive-ts/core";
2+
import { render, screen } from "@testing-library/react-native";
23
import {
3-
TouchableHighlight,
4-
TouchableWithoutFeedback,
5-
TouchableNativeFeedback,
64
View,
75
TextInput,
8-
Pressable,
96
} from "react-native";
107

118
import { ElementAssertion } from "../../src/lib/ElementAssertion";
129

13-
import assert, { AssertionError } from "assert";
14-
15-
const ALLOWED_COMPONENTS = {
16-
Pressable,
17-
TextInput,
18-
TouchableHighlight,
19-
TouchableNativeFeedback,
20-
TouchableWithoutFeedback,
21-
View,
22-
};
23-
2410
describe("[Unit] ElementAssertion.test.ts", () => {
2511
describe(".toBeDisabled", () => {
26-
context("when the component TextInput is disabled", () => {
27-
it("returns the assertion instance", () => {
28-
const element = render(
29-
<TextInput testID="id" editable={false} />,
30-
);
31-
const test = new ElementAssertion(element.getByTestId("id"));
32-
33-
assert.deepStrictEqual(test.toBeDisabled(), test);
34-
assert.throws(() => test.not.toBeDisabled(), {
35-
message: "Expected the value NOT to be disabled",
36-
name: AssertionError.name,
37-
});
38-
});
12+
it("returns the assertion instance when the component TextInput is not editable", () => {
13+
const element = render(
14+
<TextInput testID="id" editable={false} />,
15+
);
16+
const test = new ElementAssertion(element.getByTestId("id"));
17+
18+
expect(test.toBeDisabled()).toBe(test);
19+
expect(() => test.not.toBeDisabled())
20+
.toThrowError(AssertionError)
21+
.toHaveMessage("Expected the value NOT to be disabled");
22+
});
23+
24+
it("checks aria-disabled prop for parent and child element", () => {
25+
const element = render(
26+
<View aria-disabled={true} testID="parentId">
27+
<View testID="childId">
28+
<TextInput />
29+
</View>
30+
</View>,
31+
);
32+
33+
const parent = new ElementAssertion(element.getByTestId("parentId"));
34+
const child = new ElementAssertion(element.getByTestId("childId"));
35+
36+
expect(parent.toBeDisabled()).toBeTruthy();
37+
expect(child.toBeDisabled()).toBeTruthy();
38+
expect(() => parent.not.toBeDisabled())
39+
.toThrowError(AssertionError)
40+
.toHaveMessage("Expected the value NOT to be disabled");
41+
expect(() => child.not.toBeDisabled())
42+
.toThrowError(AssertionError)
43+
.toHaveMessage("Expected the value NOT to be disabled");
44+
3945
});
4046

41-
Object.entries(ALLOWED_COMPONENTS).forEach(([name, Component]) => {
42-
it(`handle disabled prop for element ${name}`, () => {
43-
const element = render(
44-
// @ts-expect-error JSX element 'Component'
45-
<Component disabled={true} testID={name}>
47+
it("checks aria-disabled prop for child element", () => {
48+
const element = render(
49+
<View testID="parentId">
50+
<View aria-disabled={true} testID="childId">
4651
<TextInput />
47-
</Component>,
48-
);
49-
const test = new ElementAssertion(element.getByTestId(name));
50-
assert.deepStrictEqual(test.toBeDisabled(), test);
51-
assert.throws(() => test.not.toBeDisabled(), {
52-
message: "Expected the value NOT to be disabled",
53-
name: AssertionError.name,
54-
});
55-
});
52+
</View>
53+
</View>,
54+
);
55+
56+
const parent = new ElementAssertion(element.getByTestId("parentId"));
57+
const child = new ElementAssertion(element.getByTestId("childId"));
58+
59+
expect(child.toBeDisabled()).toBeTruthy();
60+
expect(parent.not.toBeDisabled()).toBeTruthy();
61+
expect(() => child.not.toBeDisabled())
62+
.toThrowError(AssertionError)
63+
.toHaveMessage("Expected the value NOT to be disabled");
64+
5665
});
5766
});
5867
});

0 commit comments

Comments
 (0)