Skip to content

Commit

Permalink
feat: implement toBeEmptyElement
Browse files Browse the repository at this point in the history
  • Loading branch information
kyawthura-gg authored and mdjastrzebski committed Aug 19, 2023
1 parent 4a8072d commit 4c63680
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/matchers/__tests__/to-be-empty-element.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import { View } from 'react-native';
import { render, screen } from '../..';
import '../extend-expect';

test('.toBeEmptyElement', () => {
render(
<View testID="not-empty">
<View testID="empty" />
</View>
);

const empty = screen.getByTestId('empty');
const notEmpty = screen.queryByTestId('not-empty');
const nonExistentElement = screen.queryByTestId('not-exists');
const fakeElement = { thisIsNot: 'an html element' };

expect(empty).toBeEmptyElement();

Check failure on line 18 in src/matchers/__tests__/to-be-empty-element.test.tsx

View workflow job for this annotation

GitHub Actions / Typecheck

Property 'toBeEmptyElement' does not exist on type 'JestMatchers<ReactTestInstance>'.
expect(notEmpty).not.toBeEmptyElement();

Check failure on line 19 in src/matchers/__tests__/to-be-empty-element.test.tsx

View workflow job for this annotation

GitHub Actions / Typecheck

Property 'toBeEmptyElement' does not exist on type 'Matchers<void, ReactTestInstance | null>'.

expect(() => expect(empty).not.toBeEmptyElement()).toThrow();

Check failure on line 21 in src/matchers/__tests__/to-be-empty-element.test.tsx

View workflow job for this annotation

GitHub Actions / Typecheck

Property 'toBeEmptyElement' does not exist on type 'Matchers<void, ReactTestInstance>'.

expect(() => expect(notEmpty).toBeEmptyElement()).toThrow();

Check failure on line 23 in src/matchers/__tests__/to-be-empty-element.test.tsx

View workflow job for this annotation

GitHub Actions / Typecheck

Property 'toBeEmptyElement' does not exist on type 'JestMatchers<ReactTestInstance | null>'.

expect(() => expect(fakeElement).toBeEmptyElement()).toThrow();

Check failure on line 25 in src/matchers/__tests__/to-be-empty-element.test.tsx

View workflow job for this annotation

GitHub Actions / Typecheck

Property 'toBeEmptyElement' does not exist on type 'JestMatchers<{ thisIsNot: string; }>'.

expect(() => {
expect(nonExistentElement).toBeEmptyElement();

Check failure on line 28 in src/matchers/__tests__/to-be-empty-element.test.tsx

View workflow job for this annotation

GitHub Actions / Typecheck

Property 'toBeEmptyElement' does not exist on type 'JestMatchers<ReactTestInstance | null>'.
}).toThrow();
});
2 changes: 2 additions & 0 deletions src/matchers/extend-expect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/// <reference path="./extend-expect.d.ts" />

import { toBeOnTheScreen } from './to-be-on-the-screen';
import { toBeEmptyElement } from './to-be-empty-element';

expect.extend({
toBeOnTheScreen,
toBeEmptyElement,
});
1 change: 1 addition & 0 deletions src/matchers/index.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { toBeOnTheScreen } from './to-be-on-the-screen';
export { toBeEmptyElement } from './to-be-empty-element';
29 changes: 29 additions & 0 deletions src/matchers/to-be-empty-element.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ReactTestInstance } from 'react-test-renderer';
import { matcherHint, printReceived } from 'jest-matcher-utils';
import { getHostChildren } from '../helpers/component-tree';
import { checkHostElement } from './utils';

export function toBeEmptyElement(
this: jest.MatcherContext,
element: ReactTestInstance
) {
checkHostElement(element, toBeEmptyElement, this);

const pass = getHostChildren(element).length === 0;

return {
pass,
message: () => {
return [
matcherHint(
`${this.isNot ? '.not' : ''}.toBeEmptyElement`,
'element',
''
),
'',
'Received:',
` ${printReceived(element.children)}`,
].join('\n');
},
};
}

0 comments on commit 4c63680

Please sign in to comment.