Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Render element tree in query error messages #1378

Merged
merged 24 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
55f8026
feat: Render virtual DOM in byText error message
Mar 31, 2023
1f64a25
Preserve props that hide, since relevant to failure
Mar 31, 2023
e960d91
Refactor to add props, move logic to makeQueries
Apr 4, 2023
fb9e891
Add missing tests
Apr 5, 2023
4f0389f
Optimize findBy, findAllBy to only render DOM on timeout
Apr 5, 2023
d7cf14b
refactor: make queries clean up
mdjastrzebski Apr 5, 2023
f45b242
refactor: remove color control codes from error
mdjastrzebski Apr 5, 2023
0a0bb33
refactor: disable format element coloring just for our tests
mdjastrzebski Apr 5, 2023
e5d6080
fix: host elements error message
mdjastrzebski Apr 5, 2023
1b95f4d
refactor: cleanup host component names
mdjastrzebski Apr 5, 2023
701c39e
chore: reverse unnecessary reorder of imports
mdjastrzebski Apr 5, 2023
6be5df7
refactor: tweaks
mdjastrzebski Apr 5, 2023
5c7bb55
refactor: tweaks
mdjastrzebski Apr 5, 2023
7904d3a
fix: fix issue where findBy* doesn't print tree
Apr 20, 2023
6d3d50c
refactor: update wording 'DOM' -> 'element tree'
Apr 20, 2023
8f42584
refactor: tweaks
mdjastrzebski Apr 26, 2023
4fbdda0
refactor: tweaks
mdjastrzebski Apr 26, 2023
35c3393
refactor: tweaks
mdjastrzebski Apr 26, 2023
e13c56f
refactor: restore stack change
mdjastrzebski Apr 26, 2023
eb10229
refactor: improve onTimeout error typing
mdjastrzebski Apr 26, 2023
9f81589
chore: fix lint
mdjastrzebski Apr 26, 2023
139df37
chore: increase code coverage
mdjastrzebski Apr 27, 2023
37e4aeb
chore: tweaks
mdjastrzebski Apr 27, 2023
f5f64d5
refactor: improve unit tests
mdjastrzebski Apr 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: tweaks
  • Loading branch information
mdjastrzebski committed Apr 27, 2023
commit 5c7bb5549e2974bbcc43ac6f2aaa6630cda9f40c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ReactTestRendererJSON } from 'react-test-renderer';
import { mapPropsForQueryError } from '../helpers/mapProps';
import { defaultMapProps } from '../format-default';

const node: ReactTestRendererJSON = {
type: 'View',
Expand All @@ -24,13 +24,13 @@ describe('mapPropsForQueryError', () => {
defaultValue: 'DEFAULT_VALUE',
};

const result = mapPropsForQueryError(props, node);
const result = defaultMapProps(props, node);

expect(result).toStrictEqual(props);
});

test('does not preserve less helpful props', () => {
const result = mapPropsForQueryError(
const result = defaultMapProps(
{
style: [{ flex: 1 }, { display: 'flex' }],
onPress: () => null,
Expand All @@ -43,7 +43,7 @@ describe('mapPropsForQueryError', () => {
});

test('preserves "display: none" style but no other style', () => {
const result = mapPropsForQueryError(
const result = defaultMapProps(
{ style: [{ flex: 1 }, { display: 'none', flex: 2 }] },
node
);
Expand All @@ -54,7 +54,7 @@ describe('mapPropsForQueryError', () => {
});

test('removes undefined keys from accessibilityState', () => {
const result = mapPropsForQueryError(
const result = defaultMapProps(
{ accessibilityState: { checked: undefined, selected: false } },
node
);
Expand All @@ -65,7 +65,7 @@ describe('mapPropsForQueryError', () => {
});

test('removes accessibilityState if all keys are undefined', () => {
const result = mapPropsForQueryError(
const result = defaultMapProps(
{ accessibilityState: { checked: undefined, selected: undefined } },
node
);
Expand All @@ -74,28 +74,28 @@ describe('mapPropsForQueryError', () => {
});

test('does not fail if accessibilityState is a string, passes through', () => {
const result = mapPropsForQueryError({ accessibilityState: 'foo' }, node);
const result = defaultMapProps({ accessibilityState: 'foo' }, node);
expect(result).toStrictEqual({ accessibilityState: 'foo' });
});

test('does not fail if accessibilityState is an array, passes through', () => {
const result = mapPropsForQueryError({ accessibilityState: [1] }, node);
const result = defaultMapProps({ accessibilityState: [1] }, node);
expect(result).toStrictEqual({ accessibilityState: [1] });
});

test('does not fail if accessibilityState is null, passes through', () => {
const result = mapPropsForQueryError({ accessibilityState: null }, node);
const result = defaultMapProps({ accessibilityState: null }, node);
expect(result).toStrictEqual({ accessibilityState: null });
});

test('does not fail if accessibilityState is nested object, passes through', () => {
const accessibilityState = { 1: { 2: 3 }, 2: undefined };
const result = mapPropsForQueryError({ accessibilityState }, node);
const result = defaultMapProps({ accessibilityState }, node);
expect(result).toStrictEqual({ accessibilityState: { 1: { 2: 3 } } });
});

test('removes undefined keys from accessibilityValue', () => {
const result = mapPropsForQueryError(
const result = defaultMapProps(
{ accessibilityValue: { min: 1, max: undefined } },
node
);
Expand All @@ -104,7 +104,7 @@ describe('mapPropsForQueryError', () => {
});

test('removes accessibilityValue if all keys are undefined', () => {
const result = mapPropsForQueryError(
const result = defaultMapProps(
{ accessibilityValue: { min: undefined } },
node
);
Expand Down
50 changes: 25 additions & 25 deletions src/helpers/mapProps.ts → src/helpers/format-default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,10 @@ const propsToDisplay = [
'title',
];

function isObject(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}

function removeUndefinedKeys(prop: unknown) {
if (!isObject(prop)) {
return prop;
}

const result: Record<string, unknown> = {};
Object.keys(prop).forEach((propName) => {
if (prop[propName] !== undefined) {
result[propName] = prop[propName];
}
});

// If object does not have any props we will ignore it.
if (Object.keys(result).length === 0) {
return undefined;
}

return result;
}

/**
* Preserve props that are helpful in diagnosing test failures, while stripping rest
*/
export const mapPropsForQueryError: MapPropsFunction = (props) => {
export const defaultMapProps: MapPropsFunction = (props) => {
const accessibilityState = removeUndefinedKeys(props.accessibilityState);
const accessibilityValue = removeUndefinedKeys(props.accessibilityValue);

Expand All @@ -65,3 +41,27 @@ export const mapPropsForQueryError: MapPropsFunction = (props) => {

return result;
};

function isObject(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}

function removeUndefinedKeys(prop: unknown) {
if (!isObject(prop)) {
return prop;
}

const result: Record<string, unknown> = {};
Object.keys(prop).forEach((propName) => {
if (prop[propName] !== undefined) {
result[propName] = prop[propName];
}
});

// If object does not have any props we will ignore it.
if (Object.keys(result).length === 0) {
return undefined;
}

return result;
}
4 changes: 2 additions & 2 deletions src/queries/makeQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import waitFor from '../waitFor';
import type { WaitForOptions } from '../waitFor';
import format from '../helpers/format';
import { screen } from '../screen';
import { mapPropsForQueryError } from '../helpers/mapProps';
import { defaultMapProps } from '../helpers/format-default';

export type GetByQuery<Predicate, Options = void> = (
predicate: Predicate,
Expand Down Expand Up @@ -97,7 +97,7 @@ function formatErrorMessage(message: string, printElementTree: boolean) {
}

return `${message}\n\n${format(json, {
mapProps: mapPropsForQueryError,
mapProps: defaultMapProps,
})}`;
}

Expand Down