Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lopenchi committed Nov 14, 2024
1 parent 3f36d19 commit 7e87056
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 301 deletions.
1 change: 0 additions & 1 deletion packages/native/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
export * from "./toBeDisabled";
102 changes: 52 additions & 50 deletions packages/native/src/lib/ElementAssertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,65 @@ import { ReactTestInstance } from "react-test-renderer";

// Elements that support 'disabled'
const DISABLE_TYPES = [
"Button",
"Slider",
"Switch",
"Text",
"TouchableHighlight",
"TouchableOpacity",
"TouchableWithoutFeedback",
"TouchableNativeFeedback",
"View",
"TextInput",
"Pressable",
];
"Button",
"Slider",
"Switch",
"Text",
"TouchableHighlight",
"TouchableOpacity",
"TouchableWithoutFeedback",
"TouchableNativeFeedback",
"View",
"TextInput",
"Pressable",
];

export class ElementAssertion extends Assertion<ReactTestInstance> {
public constructor(actual: ReactTestInstance) {
super(actual);
}
public constructor(actual: ReactTestInstance) {
super(actual);
}

public toBeDisabled(): this {
const error = new AssertionError({
actual: this.actual,
message: `Expected <${this.actual}> to be disabled`,
});
const invertedError = new AssertionError({
actual: this.actual,
message: "Expected the value NOT to be empty",
});
public toBeDisabled(): this {
const error = new AssertionError({
actual: this.actual,
message: `Expected <${this.actual}> to be disabled`,
});
const invertedError = new AssertionError({
actual: this.actual,
message: "Expected the value NOT to be disabled",
});

return this.execute({
assertWhen: this.isElementDisabled(this.actual) || this.isAncestorDisabled(this.actual),
error,
invertedError,
});
return this.execute({
assertWhen: this.isElementDisabled(this.actual) || this.isAncestorDisabled(this.actual),
error,
invertedError,
});
}

private isElementDisabled(element: ReactTestInstance): boolean {
if (this.getType(element) === "TextInput" && element?.props?.editable === false) {
return true;
}

private isElementDisabled(element: ReactTestInstance): boolean {
if (this.getType(element) === "TextInput" && element?.props?.editable === false) {
return true;
}
if (!DISABLE_TYPES.includes(this.getType(element))) {
return false;
}

if (!DISABLE_TYPES.includes(this.getType(element))) {
return false;
}
return (
!!element?.props?.disabled ||
!!element?.props?.accessibilityState?.disabled ||
!!element?.props?.accessibilityStates?.includes("disabled")
);
}

return (
!!element?.props?.disabled ||
!!element?.props?.accessibilityState?.disabled ||
!!element?.props?.accessibilityStates?.includes("disabled")
);
}
private isAncestorDisabled(element: ReactTestInstance): boolean {
const parent = element.parent;
return parent !== null && (this.isElementDisabled(element) || this.isAncestorDisabled(parent));
}

private isAncestorDisabled(element: ReactTestInstance): boolean {
const parent = element.parent;
return parent !== null && (this.isElementDisabled(element) || this.isAncestorDisabled(parent));
}
private getType({ type }: ReactTestInstance): string {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return type.displayName || type.name || type;
}
private getType({ type }: ReactTestInstance): string {
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
return type.displayName || type.name || type;
}

}
243 changes: 0 additions & 243 deletions packages/native/test/lib/ElementAssertion.test.ts

This file was deleted.

27 changes: 27 additions & 0 deletions packages/native/test/lib/ElementAssertion.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { render } from "@testing-library/react-native";
import { TextInput, View } from "react-native";

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

import assert, { AssertionError } from "assert";

describe("[Unit] ElementAssertion.test.ts", () => {
describe(".toBeDisabled", () => {
context("when the component is disabled", () => {
it("returns the assertion instance", () => {
const element = render(
<View disabled={true} testID="id">
<TextInput />
</View>,
);
const test = new ElementAssertion(element.getByTestId("id"));

assert.deepStrictEqual(test.toBeDisabled(), test);
assert.throws(() => test.not.toBeDisabled(), {
message: "Expected the value NOT to be disabled",
name: AssertionError.name,
});
});
});
});
});
Loading

0 comments on commit 7e87056

Please sign in to comment.