-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
return one object only from renderTo functions, allow assertions on `…
…takeRender`/`takeSnapshot`
- Loading branch information
Showing
7 changed files
with
131 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { RenderStream } from "./profile/profile.js"; | ||
|
||
export const assertableSymbol = Symbol.for( | ||
"@testing-library/react-render-stream:assertable" | ||
); | ||
|
||
/** | ||
* A function or object that can be used in assertions, like e.g. | ||
```ts | ||
expect(assertable).toRerender() | ||
expect(assertable).not.toRerender() | ||
expect(assertable).toRenderExactlyTimes(3) | ||
``` | ||
*/ | ||
export type Assertable = { | ||
[assertableSymbol]: RenderStream<any>; | ||
}; | ||
|
||
export function markAssertable<T extends {}>( | ||
assertable: T, | ||
stream: RenderStream<any> | ||
): T & Assertable { | ||
return Object.assign(assertable, { | ||
[assertableSymbol]: stream, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,31 @@ | ||
import { expect } from "@jest/globals"; | ||
import { toRerender, toRenderExactlyTimes } from "./ProfiledComponent.js"; | ||
import type { NextRenderOptions, RenderStream } from "../index.js"; | ||
import type { | ||
NextRenderOptions, | ||
RenderStream, | ||
Assertable, | ||
} from "@testing-library/react-render-stream"; | ||
|
||
expect.extend({ | ||
toRerender, | ||
toRenderExactlyTimes, | ||
}); | ||
interface ApolloCustomMatchers<R = void, T = {}> { | ||
toRerender: T extends RenderStream<any> | unknown // TODO | ||
interface CustomMatchers<R = void, T = {}> { | ||
toRerender: T extends RenderStream<any> | Assertable | ||
? (options?: NextRenderOptions) => Promise<R> | ||
: { error: "matcher needs to be called on a ProfiledComponent instance" }; | ||
: { | ||
error: "matcher needs to be called on a `takeRender` function, `takeSnapshot` function or `RenderStream` instance"; | ||
}; | ||
|
||
toRenderExactlyTimes: T extends RenderStream<any> | unknown // TODO | ||
toRenderExactlyTimes: T extends RenderStream<any> | Assertable | ||
? (count: number, options?: NextRenderOptions) => Promise<R> | ||
: { error: "matcher needs to be called on a ProfiledComponent instance" }; | ||
: { | ||
error: "matcher needs to be called on a `takeRender` function, `takeSnapshot` function or `RenderStream` instance"; | ||
}; | ||
} | ||
|
||
declare global { | ||
namespace jest { | ||
interface Matchers<R = void, T = {}> extends ApolloCustomMatchers<R, T> {} | ||
interface Matchers<R = void, T = {}> extends CustomMatchers<R, T> {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters