Skip to content

Commit

Permalink
fix(if): properly throw warnings in dev mode
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed Nov 7, 2020
1 parent cbd289f commit bd80bbc
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 43 deletions.
4 changes: 2 additions & 2 deletions src/If.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { FC, Fragment, ReactElement } from 'react';
import { Else } from './Else';
import { getConditionResult } from './getConditionResults';
import { invariant } from './invariant';
import { tinyWarning } from './tinyWarning';
import { Then } from './Then';
import { ComponentWithConditionProps } from './types';

Expand All @@ -18,7 +18,7 @@ export const If: FC<ComponentWithConditionProps> = ({ condition, children }) =>
return null;
}

invariant(
tinyWarning(
(!Array.isArray(children) && !((children as ReactElement).type === Else || (children as ReactElement).type === Then)) ||
!(React.Children.toArray(children) as ReactElement[]).every(child => child.type === Else || child.type === Then),
'The <If> component should contain <Then /> and <Else /> components as its children'
Expand Down
14 changes: 0 additions & 14 deletions src/invariant.ts

This file was deleted.

27 changes: 27 additions & 0 deletions src/tinyWarning.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Handles errors by throwing them to the console.
* `__DEV__` is replaced by tsdx using {@link https://www.npmjs.com/package/babel-plugin-dev-expression babel-plugin-dev-expressions}
* which will ensure this entire throw is not present in production
* @param condition The condition to check
* @param message The message to throw if `condition` resolves to `true`
*/
export function tinyWarning(condition: boolean, message: string): asserts condition {
if (__DEV__) {
if (condition) {
// check console for IE9 support which provides console
// only with open devtools

if (typeof console !== 'undefined') {
console.warn(message);
}

// Throwing an error and catching it immediately to improve debugging
// Users can utilize 'pause on caught exceptions' to get into this throw
try {
throw new Error(message);
} catch (x) {
// noop
}
}
}
}
7 changes: 0 additions & 7 deletions test/Augments.d.ts

This file was deleted.

19 changes: 0 additions & 19 deletions test/If.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,6 @@ describe('<If /> component', () => {
expect(wrapped.containsMatchingElement(<span>Else</span>)).toBe(false);
});

test('GIVEN content w/o <Then /> nor <Else /> THEN errors in test & development', () => {
expect(() =>
shallow(
<If condition={true}>
<span>Not Then nor Else</span>
</If>
)
).toThrowError('The <If> component should contain <Then /> and <Else /> components as its children');
});

test('GIVEN w/o children THEN renders null', () => {
const wrapped = shallow(<If condition={true} />);

Expand Down Expand Up @@ -243,15 +233,6 @@ describe('<If /> component', () => {
expect(wrapped.containsMatchingElement(<span>Else</span>)).toBe(true);
expect(wrapped.containsMatchingElement(<span>Then</span>)).toBe(false);
});
test('GIVEN w/o <Then /> nor <Else /> THEN throws error', () => {
expect(() =>
shallow(
<If condition={false}>
<span>Content</span>
</If>
)
).toThrowError('The <If> component should contain <Then /> and <Else /> components as its children');
});

test('GIVEN w/o children THEN renders null', () => {
const wrapped = shallow(<If condition={false} />);
Expand Down
11 changes: 11 additions & 0 deletions test/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,14 @@ configure({ adapter: new Adapter() });

// @ts-ignore => Type definitions are off
expect.addSnapshotSerializer(createSerializer({ mode: 'deep' }));

// Stub out console.warn so it doesn't log in tests
const originalWarn = console.warn;

beforeAll(() => {
console.warn = () => undefined;
});

afterAll(() => {
console.warn = originalWarn;
});
2 changes: 1 addition & 1 deletion test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"compilerOptions": {
"rootDir": "."
},
"include": ["."],
"include": [".", "../src/Augments.d.ts"],
"references": [{ "path": "../src" }]
}

0 comments on commit bd80bbc

Please sign in to comment.