Skip to content

Commit

Permalink
Fix React testing exports
Browse files Browse the repository at this point in the history
  • Loading branch information
lemonmade committed Nov 4, 2023
1 parent 605d2d3 commit 4df2d66
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 75 deletions.
7 changes: 7 additions & 0 deletions .changeset/rotten-keys-applaud.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@quilted/preact-testing': patch
'@quilted/quilt': patch
'@quilted/react-testing': patch
---

Fix React testing exports
42 changes: 21 additions & 21 deletions packages/preact-testing/source/matchers.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
import type {ComponentType, Context as ReactContext} from 'preact';
import type {ComponentType, Context} from 'preact';

import type {PropsFor} from './types.ts';

import {toContainReactHTML, toHaveReactDataProps} from './matchers/dom.ts';
import {toHaveReactProps} from './matchers/props.ts';
import {toContainPreactHTML, toHavePreactDataProps} from './matchers/dom.ts';
import {toHavePreactProps} from './matchers/props.ts';
import {
toContainReactComponent,
toContainReactComponentTimes,
toContainPreactComponent,
toContainPreactComponentTimes,
} from './matchers/components.ts';
import {toContainReactText} from './matchers/text.ts';
import {toProvideReactContext} from './matchers/context.ts';
import {toContainPreactText} from './matchers/text.ts';
import {toProvidePreactContext} from './matchers/context.ts';

// @see https://vitest.dev/guide/extending-matchers.html

export interface CustomMatchers<R = unknown> {
toHaveReactProps(props: Record<string, unknown>): void;
toContainReactComponent<Type extends string | ComponentType<any>>(
toHavePreactProps(props: Record<string, unknown>): void;
toContainPreactComponent<Type extends string | ComponentType<any>>(
type: Type,
props?: Partial<PropsFor<Type>>,
): R;
toContainReactComponentTimes<Type extends string | ComponentType<any>>(
toContainPreactComponentTimes<Type extends string | ComponentType<any>>(
type: Type,
times: number,
props?: Partial<PropsFor<Type>>,
): R;
toProvideReactContext<Type>(context: ReactContext<Type>, value?: Type): R;
toContainReactText(text: string): R;
toProvidePreactContext<Type>(context: Context<Type>, value?: Type): R;
toContainPreactText(text: string): R;
}

export const matchers = {
toHaveReactProps,
toContainReactComponent,
toContainReactComponentTimes,
toProvideReactContext,
toContainReactText,
toHavePreactProps,
toContainPreactComponent,
toContainPreactComponentTimes,
toProvidePreactContext,
toContainPreactText,
};

export interface CustomDOMMatchers<R = unknown> {
toContainReactHTML(text: string): R;
toHaveReactDataProps(data: {[key: string]: string}): R;
toContainPreactHTML(text: string): R;
toHavePreactDataProps(data: {[key: string]: string}): R;
}

export const domMatchers = {
toContainReactHTML,
toHaveReactDataProps,
toContainPreactHTML,
toHavePreactDataProps,
};
30 changes: 16 additions & 14 deletions packages/preact-testing/source/matchers/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
printType,
} from './utilities.ts';

export function toContainReactComponent<
export function toContainPreactComponent<
Type extends string | ComponentType<any>,
>(
this: MatcherState,
Expand All @@ -26,12 +26,12 @@ export function toContainReactComponent<
props?: Partial<PropsFor<Type>>,
) {
assertIsNode(node, {
expectation: 'toContainReactComponent',
expectation: 'toContainPreactComponent',
isNot: this.isNot,
});

assertIsType(type, {
expectation: 'toContainReactComponent',
expectation: 'toContainPreactComponent',
isNot: this.isNot,
});

Expand All @@ -49,8 +49,8 @@ export function toContainReactComponent<

const message = pass
? () =>
`${matcherHint('.not.toContainReactComponent')}\n\n` +
`Expected the React element:\n ${receivedColor(node.toString())}\n` +
`${matcherHint('.not.toContainPreactComponent')}\n\n` +
`Expected the Preact element:\n ${receivedColor(node.toString())}\n` +
`Not to contain component:\n ${expectedColor(printType(type))}\n${
props ? `With props matching:\n ${printExpected(props)}\n` : ''
}` +
Expand All @@ -59,8 +59,10 @@ export function toContainReactComponent<
} found.\n`
: () =>
`${
`${matcherHint('.toContainReactComponent')}\n\n` +
`Expected the React element:\n ${receivedColor(node.toString())}\n` +
`${matcherHint('.toContainPreactComponent')}\n\n` +
`Expected the Preact element:\n ${receivedColor(
node.toString(),
)}\n` +
`To contain component:\n ${expectedColor(printType(type))}\n${
props ? `With props matching:\n ${printExpected(props)}\n` : ''
}`
Expand All @@ -81,7 +83,7 @@ export function toContainReactComponent<
return {pass, message};
}

export function toContainReactComponentTimes<
export function toContainPreactComponentTimes<
Type extends string | ComponentType<any>,
>(
this: MatcherState,
Expand All @@ -91,12 +93,12 @@ export function toContainReactComponentTimes<
props?: Partial<PropsFor<Type>>,
) {
assertIsNode(node, {
expectation: 'toContainReactComponentTimes',
expectation: 'toContainPreactComponentTimes',
isNot: this.isNot,
});

assertIsType(type, {
expectation: 'toContainReactComponent',
expectation: 'toContainPreactComponent',
isNot: this.isNot,
});

Expand All @@ -115,15 +117,15 @@ export function toContainReactComponentTimes<
const message = pass
? () =>
[
`${matcherHint('.not.toContainReactComponentTimes')}\n`,
`Expected the React element:\n ${receivedColor(node.toString())}`,
`${matcherHint('.not.toContainPreactComponentTimes')}\n`,
`Expected the Preact element:\n ${receivedColor(node.toString())}`,
`Not to contain component:\n ${expectedColor(printType(type))}`,
`${times} ${pluralize('time', times)}, but it did.`,
].join('\n')
: () =>
[
`${matcherHint('.toContainReactComponentTimes')}\n`,
`Expected the React element:\n ${receivedColor(node.toString())}`,
`${matcherHint('.toContainPreactComponentTimes')}\n`,
`Expected the Preact element:\n ${receivedColor(node.toString())}`,
`To contain component:\n ${expectedColor(printType(type))}`,
`${times} ${pluralize('time', times)}, but it was found ${
foundByProps.length
Expand Down
14 changes: 8 additions & 6 deletions packages/preact-testing/source/matchers/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import type {Node} from '../types.ts';

import {assertIsNode, diffs, printType} from './utilities.ts';

export function toProvideReactContext<Type>(
export function toProvidePreactContext<Type>(
this: MatcherState,
node: Node<any>,
Context: Context<Type>,
value?: Type,
) {
assertIsNode(node, {
expectation: 'toProvideReactContext',
expectation: 'toProvidePreactContext',
isNot: this.isNot,
});

Expand All @@ -34,8 +34,8 @@ export function toProvideReactContext<Type>(

const message = pass
? () =>
`${matcherHint('.not.toProvideReactContext')}\n\n` +
`Expected the React element:\n ${receivedColor(node.toString())}\n` +
`${matcherHint('.not.toProvidePreactContext')}\n\n` +
`Expected the Preact element:\n ${receivedColor(node.toString())}\n` +
`Not to contain context provider:\n ${expectedColor(
printType(Context.Provider),
)}\n${
Expand All @@ -46,8 +46,10 @@ export function toProvideReactContext<Type>(
} found.\n`
: () =>
`${
`${matcherHint('.toProvideReactContext')}\n\n` +
`Expected the React element:\n ${receivedColor(node.toString())}\n` +
`${matcherHint('.toProvidePreactContext')}\n\n` +
`Expected the Preact element:\n ${receivedColor(
node.toString(),
)}\n` +
`To contain context provider:\n ${expectedColor(
printType(Context.Provider),
)}\n${
Expand Down
18 changes: 9 additions & 9 deletions packages/preact-testing/source/matchers/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import {

import type {Node} from '../types.ts';

import {toHaveReactProps} from './props.ts';
import {toHavePreactProps} from './props.ts';
import {assertIsNode, printReceivedWithHighlight} from './utilities.ts';

export function toContainReactHTML<Props>(
export function toContainPreactHTML<Props>(
this: MatcherState,
node: Node<Props>,
text: string,
) {
assertIsNode(node, {
expectation: 'toContainReactHTML',
expectation: 'toContainPreactHTML',
isNot: this.isNot,
});

Expand All @@ -27,27 +27,27 @@ export function toContainReactHTML<Props>(

const message = pass
? () =>
`${matcherHint('.not.toContainReactHTML', node.toString())}\n\n` +
`Expected the React element:\n ${receivedColor(node.toString())}\n` +
`${matcherHint('.not.toContainPreactHTML', node.toString())}\n\n` +
`Expected the Preact element:\n ${receivedColor(node.toString())}\n` +
`Not to contain HTML:\n ${printExpected(text)}\n` +
`But it did:\n ${printReceivedWithHighlight(
nodeHTML,
matchIndex,
text.length,
)}\n`
: () =>
`${matcherHint('.not.toContainReactHTML', node.toString())}\n\n` +
`Expected the React element:\n ${receivedColor(node.toString())}\n` +
`${matcherHint('.not.toContainPreactHTML', node.toString())}\n\n` +
`Expected the Preact element:\n ${receivedColor(node.toString())}\n` +
`With HTML content:\n ${printReceived(nodeHTML)}\n` +
`To contain HTML:\n ${printExpected(text)}\n`;

return {pass, message};
}

export function toHaveReactDataProps(
export function toHavePreactDataProps(
this: MatcherState,
node: Node<unknown>,
data: {[key: string]: string},
) {
return toHaveReactProps.call(this, node, data);
return toHavePreactProps.call(this, node, data);
}
14 changes: 8 additions & 6 deletions packages/preact-testing/source/matchers/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import type {Node} from '../types.ts';

import {assertIsNode, diffPropsForNode} from './utilities.ts';

export function toHaveReactProps<Props>(
export function toHavePreactProps<Props>(
this: MatcherState,
node: Node<Props>,
props: Partial<Props>,
) {
assertIsNode(node, {
expectation: 'toHaveReactProps',
expectation: 'toHavePreactProps',
isNot: this.isNot,
});

Expand All @@ -36,8 +36,8 @@ export function toHaveReactProps<Props>(

const message = pass
? () =>
`${matcherHint('.not.toHaveReactProps', node.toString())}\n\n` +
`Expected the React element:\n ${receivedColor(node.toString())}\n` +
`${matcherHint('.not.toHavePreactProps', node.toString())}\n\n` +
`Expected the Preact element:\n ${receivedColor(node.toString())}\n` +
`Not to have props:\n ${printExpected(props)}\n` +
`Received:\n ${printReceived(node.props)}\n`
: () => {
Expand All @@ -46,8 +46,10 @@ export function toHaveReactProps<Props>(
});

return (
`${matcherHint('.toHaveReactProps', node.toString())}\n\n` +
`Expected the React element:\n ${receivedColor(node.toString())}\n` +
`${matcherHint('.toHavePreactProps', node.toString())}\n\n` +
`Expected the Preact element:\n ${receivedColor(
node.toString(),
)}\n` +
`To have props:\n ${printExpected(props)}\n` +
`Received:\n ${printReceived(node.props)}\n${
diffString ? `Difference:\n${diffString}\n` : ''
Expand Down
12 changes: 6 additions & 6 deletions packages/preact-testing/source/matchers/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import type {Node} from '../types.ts';

import {assertIsNode, printReceivedWithHighlight} from './utilities.ts';

export function toContainReactText<Props>(
export function toContainPreactText<Props>(
this: MatcherState,
node: Node<Props>,
text: string,
) {
assertIsNode(node, {
expectation: 'toContainReactText',
expectation: 'toContainPreactText',
isNot: this.isNot,
});

Expand All @@ -26,17 +26,17 @@ export function toContainReactText<Props>(

const message = pass
? () =>
`${matcherHint('.not.toContainReactText', node.toString())}\n\n` +
`Expected the React element:\n ${receivedColor(node.toString())}\n` +
`${matcherHint('.not.toContainPreactText', node.toString())}\n\n` +
`Expected the Preact element:\n ${receivedColor(node.toString())}\n` +
`Not to contain text:\n ${printExpected(text)}\n` +
`But it did:\n ${printReceivedWithHighlight(
nodeText,
matchIndex,
text.length,
)}\n`
: () =>
`${matcherHint('.not.toContainReactText', node.toString())}\n\n` +
`Expected the React element:\n ${receivedColor(node.toString())}\n` +
`${matcherHint('.not.toContainPreactText', node.toString())}\n\n` +
`Expected the Preact element:\n ${receivedColor(node.toString())}\n` +
`With text content:\n ${printReceived(nodeText)}\n` +
`To contain string:\n ${printExpected(text)}\n`;

Expand Down
6 changes: 3 additions & 3 deletions packages/preact-testing/source/matchers/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function assertIsNode(
matcherHint(`.${expectation}`, undefined, undefined, {isNot}),
`${receivedColor(
'received',
)} value must be an @shopify/react-testing Root or Element object`,
)} value must be an @quilted/preact-testing Root or Element object`,
`Received ${receivedColor(
'null',
)}.\nThis usually means that your \`.findX\` method failed to find any matching elements.`,
Expand All @@ -37,7 +37,7 @@ export function assertIsNode(
matcherHint(`.${expectation}`, undefined, undefined, {isNot}),
`${receivedColor(
'received',
)} value must be an @shopify/react-testing Root or Element object`,
)} value must be an @quilted/preact-testing Root or Element object`,
`Received an ${receivedColor(
'array of Root or Element objects',
)}.\nThis usually means that you passed in the result of \`.findAllX\`. Pass the result of \`.findX\` instead.`,
Expand All @@ -51,7 +51,7 @@ export function assertIsNode(
matcherHint(`.${expectation}`, undefined, undefined, {isNot}),
`${receivedColor(
'received',
)} value must be an @shopify/react-testing Root or Element object`,
)} value must be an @quilted/preact-testing Root or Element object`,
printWithType('Received', node, printReceived),
),
);
Expand Down
10 changes: 3 additions & 7 deletions packages/quilt/source/react/testing.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {expect} from 'vitest';
import {matchers, CustomMatchers} from '@quilted/react-testing/matchers';
import {matchers, type CustomMatchers} from '@quilted/react-testing/matchers';

export {
render,
createRender,
rendered,
destroyAll,
} from '@quilted/react-testing';
} from '@quilted/react-testing/preact';
export type {
CustomRender,
CustomRenderResult,
Expand All @@ -22,11 +22,7 @@ export type {
NodeApi,
Root,
RootApi,
DebugOptions,
EmptyObject,
PlainObject,
Predicate,
} from '@quilted/react-testing';
} from '@quilted/react-testing/preact';

declare module 'vitest' {
interface Assertion<T = any> extends CustomMatchers<T> {}
Expand Down
Loading

0 comments on commit 4df2d66

Please sign in to comment.