This repository has been archived by the owner on Oct 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add custom mount * Delete api.ts * Fix tests * Really fix tests * Really really fix tests
- Loading branch information
Showing
11 changed files
with
366 additions
and
44 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
This file was deleted.
Oops, something went wrong.
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,7 @@ | ||
import {connected} from './root'; | ||
|
||
export function destroyAll() { | ||
for (const wrapper of [...connected]) { | ||
wrapper.destroy(); | ||
} | ||
} |
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 +1,4 @@ | ||
export * from './api'; | ||
export {Root} from './root'; | ||
export {Element} from './element'; | ||
export * from './mount'; | ||
export * from './destroy'; |
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,129 @@ | ||
import * as React from 'react'; | ||
import {IfAllOptionalKeys} from '@shopify/useful-types'; | ||
|
||
import {Root} from './root'; | ||
import {Element} from './element'; | ||
|
||
export {Root, Element}; | ||
|
||
export function mount<Props>(element: React.ReactElement<Props>) { | ||
return new Root<Props>(element); | ||
} | ||
|
||
type AfterMountOption< | ||
MountOptions extends object, | ||
Context extends object, | ||
Async extends boolean | ||
> = Async extends true | ||
? { | ||
afterMount( | ||
wrapper: CustomRoot<unknown, Context>, | ||
options: MountOptions, | ||
): PromiseLike<void>; | ||
} | ||
: { | ||
afterMount?( | ||
wrapper: CustomRoot<unknown, Context>, | ||
options: MountOptions, | ||
): void; | ||
}; | ||
|
||
type ContextOption< | ||
MountOptions extends object, | ||
Context extends object | ||
> = IfAllOptionalKeys< | ||
Context, | ||
{ | ||
context?(options: MountOptions): Context; | ||
}, | ||
{ | ||
context(options: MountOptions): Context; | ||
} | ||
>; | ||
|
||
export type CustomMountOptions< | ||
MountOptions extends object = {}, | ||
Context extends object = {}, | ||
Async extends boolean = false | ||
> = { | ||
render( | ||
element: React.ReactElement<any>, | ||
context: Context, | ||
options: MountOptions, | ||
): React.ReactElement<any>; | ||
} & ContextOption<MountOptions, Context> & | ||
AfterMountOption<MountOptions, Context, Async>; | ||
|
||
type CustomMount< | ||
MountOptions extends object, | ||
Context extends object, | ||
Async extends boolean | ||
> = IfAllOptionalKeys< | ||
MountOptions, | ||
<Props>( | ||
element: React.ReactElement<any>, | ||
options?: MountOptions, | ||
) => CustomMountResult<Props, Context, Async>, | ||
<Props>( | ||
element: React.ReactElement<any>, | ||
options: MountOptions, | ||
) => CustomMountResult<Props, Context, Async> | ||
>; | ||
|
||
type CustomMountResult< | ||
Props, | ||
Context extends object, | ||
Async extends boolean | ||
> = Async extends true | ||
? Promise<CustomRoot<Props, Context>> | ||
: CustomRoot<Props, Context>; | ||
|
||
class CustomRoot<Props, Context extends object> extends Root<Props> { | ||
constructor( | ||
tree: React.ReactElement<Props>, | ||
public readonly context: Context, | ||
resolve: (element: Element<unknown>) => Element<unknown> | null, | ||
) { | ||
super(tree, resolve); | ||
} | ||
} | ||
|
||
export function createMount< | ||
MountOptions extends object = {}, | ||
Context extends object = {}, | ||
Async extends boolean = false | ||
>({ | ||
render, | ||
context: createContext = defaultContext, | ||
afterMount = defaultAfterMount, | ||
}: CustomMountOptions<MountOptions, Context, Async>): CustomMount< | ||
MountOptions, | ||
Context, | ||
Async | ||
> { | ||
function mount<Props>( | ||
element: React.ReactElement<Props>, | ||
options: MountOptions = {} as any, | ||
) { | ||
const context = createContext(options); | ||
const rendered = render(element, context, options); | ||
|
||
const wrapper = new CustomRoot(rendered, context, root => | ||
root.find(element.type), | ||
); | ||
|
||
const afterMountResult = afterMount(wrapper, options); | ||
|
||
return afterMountResult != null && 'then' in afterMountResult | ||
? afterMountResult.then(() => wrapper) | ||
: wrapper; | ||
} | ||
|
||
return mount as any; | ||
} | ||
|
||
function defaultContext() { | ||
return {} as any; | ||
} | ||
|
||
function defaultAfterMount() {} |
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
16 changes: 2 additions & 14 deletions
16
...ages/react-testing/src/tests/api.test.tsx → .../react-testing/src/tests/destroy.test.tsx
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
Oops, something went wrong.