Skip to content

Commit

Permalink
converted inferno-test-utils tests to tsx github#1632
Browse files Browse the repository at this point in the history
  • Loading branch information
Havunen committed Mar 16, 2024
1 parent c4930a7 commit 2fd100e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 42 deletions.
Expand Up @@ -151,7 +151,7 @@ exports[`Snapshots JSX Should render fragment root 1`] = `

exports[`Snapshots JSX Should render html element 1`] = `
<a
aria-colspan="3"
aria-colspan={3}
className="foo"
onClick={[Function]}
>
Expand Down
Expand Up @@ -2,7 +2,7 @@ import { Component, createFragment, Fragment } from 'inferno';
import { renderToSnapshot } from 'inferno-test-utils';
import { ChildFlags } from 'inferno-vnode-flags';

if (window.usingJest) {
if ((window as any).usingJest) {
describe('Snapshots', () => {
class Foobar extends Component {
render({ children }) {
Expand Down Expand Up @@ -37,7 +37,7 @@ if (window.usingJest) {
it('Should render html element', () => {
expect(
renderToSnapshot(
<a onClick={() => {}} aria-colspan="3" className="foo">
<a onClick={() => {}} aria-colspan={3} className="foo">
Bar
</a>,
),
Expand Down Expand Up @@ -142,13 +142,19 @@ if (window.usingJest) {
});

it('Should not fail when returning children array from component root, which contains text node. Github #1404', () => {
interface LabelProps {
label?: string;
htmlFor?: string;
optional?: boolean;
children: any;
}
const Label = ({
label,
htmlFor,
children,
optional = false,
...props
}) => {
}: LabelProps) => {
if (optional && !label) {
return children;
}
Expand Down Expand Up @@ -180,13 +186,19 @@ if (window.usingJest) {
});

it('Should not fail when returning children array from component root, Github #1404', () => {
interface LabelProps {
label?: string;
htmlFor?: string;
optional?: boolean;
children: any;
}
const Label = ({
label,
htmlFor,
children,
optional = false,
...props
}) => {
label,
htmlFor,
children,
optional = false,
...props
}: LabelProps) => {
if (optional && !label) {
return children;
}
Expand All @@ -208,12 +220,8 @@ if (window.usingJest) {

it('Should flush setStates before building snapshot', () => {
class App extends Component {
constructor(props) {
super(props);

this.state = {
foo: '',
};
state = {
foo: ''
}

componentDidMount() {
Expand Down
Expand Up @@ -5,7 +5,7 @@ const FunctionalComponent = function (props) {
return createElement('div', props);
};

const usingJest = window.usingJest;
const usingJest = (window as any).usingJest as boolean;

describe('renderToSnapshot', () => {
it('should return a snapshot from a valid vNode', () => {
Expand Down
Expand Up @@ -180,11 +180,11 @@ describe('Test Utils', () => {

it('should return false for VNodes of incorrect type', () => {
expect(isDOMVNodeOfType(createElement('div'), 'foo')).toBe(false);
expect(isDOMVNodeOfType(createElement('div'), {})).toBe(false);
expect(isDOMVNodeOfType(createElement('div'), [])).toBe(false);
expect(isDOMVNodeOfType(createElement('div'), 10)).toBe(false);
expect(isDOMVNodeOfType(createElement('div'), undefined)).toBe(false);
expect(isDOMVNodeOfType(createElement('div'), null)).toBe(false);
expect(isDOMVNodeOfType(createElement('div'), {} as never)).toBe(false);
expect(isDOMVNodeOfType(createElement('div'), [] as never)).toBe(false);
expect(isDOMVNodeOfType(createElement('div'), 10 as never)).toBe(false);
expect(isDOMVNodeOfType(createElement('div'), undefined as never)).toBe(false);
expect(isDOMVNodeOfType(createElement('div'), null as never)).toBe(false);
});
});

Expand Down Expand Up @@ -333,13 +333,13 @@ describe('Test Utils', () => {

it('should return false for DOMElements of incorrect type', () => {
expect(isDOMElementOfType(createDOMElement('div'), 'foo')).toBe(false);
expect(isDOMElementOfType(createDOMElement('div'), {})).toBe(false);
expect(isDOMElementOfType(createDOMElement('div'), [])).toBe(false);
expect(isDOMElementOfType(createDOMElement('div'), 10)).toBe(false);
expect(isDOMElementOfType(createDOMElement('div'), undefined)).toBe(
expect(isDOMElementOfType(createDOMElement('div'), {} as never)).toBe(false);
expect(isDOMElementOfType(createDOMElement('div'), [] as never)).toBe(false);
expect(isDOMElementOfType(createDOMElement('div'), 10 as never)).toBe(false);
expect(isDOMElementOfType(createDOMElement('div'), undefined as never)).toBe(
false,
);
expect(isDOMElementOfType(createDOMElement('div'), null)).toBe(false);
expect(isDOMElementOfType(createDOMElement('div'), null as never)).toBe(false);
});
});

Expand Down Expand Up @@ -411,7 +411,7 @@ describe('Test Utils', () => {
expect(
isRenderedClassComponentOfType(
renderIntoContainer(createClassVNode),
'div',
'div' as never,
),
).toBe(false);

Expand All @@ -436,7 +436,7 @@ describe('Test Utils', () => {
expect(
isRenderedClassComponentOfType(
renderIntoContainer(extendClassVNode),
'div',
'div' as never,
),
).toBe(false);
});
Expand All @@ -459,6 +459,8 @@ describe('Test Utils', () => {
expect(spy).not.toHaveBeenCalled();
findAllInRenderedTree(tree1, (args) => {
spy(args.type);

return true
});
// 0: section
// 1: FunctionalComponent
Expand All @@ -470,8 +472,11 @@ describe('Test Utils', () => {
});

it('should call predicate in the correct order', () => {
const types = [];
findAllInRenderedTree(tree1, ({ type }) => types.push(type));
const types: unknown[] = [];
findAllInRenderedTree(tree1, ({ type }) => {
types.push(type)
return true
});
expect(types).toEqual(['section', FunctionalComponent, 'div']);
});

Expand Down Expand Up @@ -507,8 +512,7 @@ describe('Test Utils', () => {
);

it('should throw an error when not passed a VNode', () => {
const errorRegex = /findAllInVNodeTree/;
const predicate = (vNode) => {
const predicate = () => {
return true;
};
const testValue = (value) => {
Expand All @@ -534,6 +538,7 @@ describe('Test Utils', () => {

findAllInVNodeTree(tree2, (args) => {
predicate(args.type);
return true;
});
// 0: section
// 1: FunctionalComponent
Expand All @@ -543,8 +548,11 @@ describe('Test Utils', () => {
});

it('should call predicate in the correct order', () => {
const types = [];
findAllInVNodeTree(tree2, ({ type }) => types.push(type));
const types: unknown[] = [];
findAllInVNodeTree(tree2, ({ type }) => {
types.push(type);
return true;
});
expect(types).toEqual(['section', FunctionalComponent]);
});
});
Expand Down Expand Up @@ -616,7 +624,7 @@ describe('Test Utils', () => {
);

it('should return an array of matched DOM elements', () => {
const testValue = (tagName, length, instance) => {
const testValue = (tagName, length) => {
const result = scryRenderedDOMElementsWithTag(tree4, tagName);
expect(result instanceof Array).toBeTruthy();
expect(result.length).toBe(length);
Expand Down Expand Up @@ -708,7 +716,6 @@ describe('Test Utils', () => {
);

it('should throw an error when more than one result is found #1', () => {
const errorRegex = /Did not find exactly one match/;
const testValue = (classNames) => {
expect(() => {
findRenderedDOMElementWithClass(tree7, classNames);
Expand All @@ -719,7 +726,7 @@ describe('Test Utils', () => {
});

it('should return a matched DOM element', () => {
const testValue = (classNames, instance) => {
const testValue = (classNames) => {
const result = findRenderedDOMElementWithClass(tree7, classNames);
const arrOfClassName = classNames.split(' ');
for (let i = 0; i < arrOfClassName.length; i++) {
Expand Down Expand Up @@ -762,7 +769,6 @@ describe('Test Utils', () => {
);

it('should throw an error when more than one result is found #2', () => {
const errorRegex = /Did not find exactly one match/;
const testValue = (tagName) => {
expect(() => {
findRenderedDOMElementWithTag(tree8, tagName);
Expand All @@ -773,7 +779,7 @@ describe('Test Utils', () => {
});

it('should return a matched DOM element', () => {
const testValue = (tagName, instance) => {
const testValue = (tagName) => {
const result = findRenderedDOMElementWithTag(tree8, tagName);

expect(result.tagName).toBe(tagName.toUpperCase());
Expand All @@ -796,7 +802,6 @@ describe('Test Utils', () => {
);

it('should throw an error when more than one result is found #3', () => {
const errorRegex = /Did not find exactly one match/;
const testValue = (type) => {
expect(() => {
findRenderedVNodeWithType(tree9, type);
Expand Down Expand Up @@ -834,7 +839,6 @@ describe('Test Utils', () => {
);

it('should throw an error when more than one result is found #4', () => {
const errorRegex = /Did not find exactly one match/;
const testValue = (type) => {
expect(() => {
findVNodeWithType(tree10, type);
Expand Down

0 comments on commit 2fd100e

Please sign in to comment.