Giter VIP home page Giter VIP logo

Comments (15)

erheron avatar erheron commented on June 13, 2024 1

It would be nice if the jest-native matchers contained more inline documentation 🙏🏻

This is what it looks like in my node_modules rn -- only toBeOnTheScreen contains the explanation 😒

export interface JestNativeMatchers<R> {
    /**
     * Assert whether an element is present in the element tree or not.
     */
    toBeOnTheScreen(): R;
    toBeChecked(): R;
    toBeCollapsed(): R;
    toBeDisabled(): R;
    toBeBusy(): R;
    toBeEmptyElement(): R;
    toBeEnabled(): R;
    toBeExpanded(): R;
    toBePartiallyChecked(): R;
    toBeSelected(): R;
    toBeVisible(): R;
    toContainElement(element: ReactTestInstance | null): R;
    toHaveAccessibilityValue(expectedValue: AccessibilityValueMatcher): R;
    toHaveAccessibleName(expectedName?: TextMatch, options?: TextMatchOptions): R;
    toHaveDisplayValue(expectedValue: TextMatch, options?: TextMatchOptions): R;
    toHaveProp(name: string, expectedValue?: unknown): R;
    toHaveStyle(style: StyleProp<Style>): R;
    toHaveTextContent(expectedText: TextMatch, options?: TextMatchOptions): R;
}

from react-native-testing-library.

mdjastrzebski avatar mdjastrzebski commented on June 13, 2024

@erheron Could you post a minimal repro for your issue?

from react-native-testing-library.

mdjastrzebski avatar mdjastrzebski commented on June 13, 2024

You should not inspect children for such purpose, as they hold some internal types. Try using toBeEmptyElement() matcher.

from react-native-testing-library.

erheron avatar erheron commented on June 13, 2024

Yeah, toBeEmptyElement seems to be working 🤔

from react-native-testing-library.

erheron avatar erheron commented on June 13, 2024

For the record, this is what I had in mind (omitting the whole renderWithProviders topic):

interface Props {
	username: string;
}

function Component(props: Props) {
	if (props.username === '') {
		return null;
	}

	return (
		<View testID="main_component_view">
			<Text> {props.username} </Text>
		</View>
	);
}

describe('Test Component', () => {
	test('Renders null if username is an empty string', () => {
		render(<Component username="" />);

		// WHAT TO EXPECT HERE?
	});
});

from react-native-testing-library.

erheron avatar erheron commented on June 13, 2024

Let's close this -- toBeEmptyElement does what I need. Sorry for hustle 🙇🏻

from react-native-testing-library.

mdjastrzebski avatar mdjastrzebski commented on June 13, 2024

Would screen.toBeNull() work?

describe('Test Component', () => {
	test('Renders null if username is an empty string', () => {
		render(<Component username="" />);
                 screen.root.toBeNull();
	});
});

from react-native-testing-library.

erheron avatar erheron commented on June 13, 2024

Yeah I think it would.

I actually ended up implementing a custom util very similar to what you wrote:

/// file TestingUtils.tsx
export function expectScreenToBeEmpty(){
   expect(screen.getByTestId('artificial_root')).toBeEmptyElement();
}


/// file renderWithProviders.tsx

function renderWithProviders(ui, options){

  const Wrapper = <View testID="artificial_root>; // and more providers

  return render(ui, { wrapper: Wrapper, options});

}

/// file test.tsx


test('Test empty render', () => {
  TestUtils.expectScreenToBeEmpty;
}

from react-native-testing-library.

mdjastrzebski avatar mdjastrzebski commented on June 13, 2024

@erheron whatever floats your boat ⛵️

I think that screen.root.toBeNull() is the most idiomatic approach (assuming it works 🤣). It says that the component you rendered resulted in null (non-existent) root host component.

re missing TSDocs: yeah, I need to add them 👍🏻

from react-native-testing-library.

guvenkaranfil avatar guvenkaranfil commented on June 13, 2024

But instead of checking the nullability of the component why not check that the string you would look for is not there by queryByText. I just wondered about your case to broad my perspective @erheron

from react-native-testing-library.

mdjastrzebski avatar mdjastrzebski commented on June 13, 2024

@guvenkaranfil There are some cases when checking for empty render or empty children might be better, e.g., when testing a component displaying a notification dot in the UI. It does not have a text representation, and you want to check whether the element is rendered depending on the notification state. As there are many ways to test things, you could also check for tesiID in such cases.

from react-native-testing-library.

guvenkaranfil avatar guvenkaranfil commented on June 13, 2024

Hmm makes sense thanks for the reply @mdjastrzebski

from react-native-testing-library.

erheron avatar erheron commented on June 13, 2024

But instead of checking the nullability of the component why not check that the string you would look for is not there by queryByText. I just wondered about your case to broad my perspective @erheron

@guvenkaranfil imagine a component, originally looking like this:

function Component(props){
   if(!validate(props)){ return null; }
   
   return (<View testID="component_root"> .... </View>);
}

Now, writing a test which checks that e.g. screen.getByTestID("component_root") is not on the screen, will not get us far -- how do I really know what is on a screen? I have to be sure that it's null.

Moreover, a component may change in the future to something like this:

function Component(newProps){
   if(!anotherValidate(newProps)){ return <PlaceholderUI />; }

   return (<View testID="component_root"> .... </View>);
}

Now, a test which checks for null'ish render would "catch" this change and that test would probably need to be updated.
On the contrary, a test which checks that something is not on the screen, wouldn't reflect what this component is doing anymore

from react-native-testing-library.

guvenkaranfil avatar guvenkaranfil commented on June 13, 2024

Thanks for detailed explanation. Yeap it much more make sense that we might want to check nullability

from react-native-testing-library.

erheron avatar erheron commented on June 13, 2024

@erheron whatever floats your boat ⛵️

I think that screen.root.toBeNull() is the most idiomatic approach (assuming it works 🤣). It says that the component you rendered resulted in null (non-existent) root host component.

re missing TSDocs: yeah, I need to add them 👍🏻

@mdjastrzebski oh right, screen.root.toBeNull of course won't work, since we render some providers in our renderWithProviders function.

from react-native-testing-library.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.