1
- import { waitFor , screen } from "@testing-library/react" ;
2
- import userEvent from "@testing-library/user-event" ;
3
- import { createMemoryRouter } from "react-router-dom" ;
4
- import { renderWithRouter } from "testHelpers/renderHelpers" ;
5
-
1
+ import { waitFor } from "@testing-library/react" ;
6
2
import * as M from "../../testHelpers/entities" ;
7
3
import { type Workspace } from "api/typesGenerated" ;
8
4
import { useWorkspaceDuplication } from "./useWorkspaceDuplication" ;
9
5
import { MockWorkspace } from "testHelpers/entities" ;
10
6
import CreateWorkspacePage from "./CreateWorkspacePage" ;
7
+ import { renderHookWithAuth } from "testHelpers/renderHelpers" ;
11
8
12
- // Tried really hard to get these tests working with RTL's renderHook, but I
13
- // kept running into weird function mismatches, mostly stemming from the fact
14
- // that React Router's RouteProvider does not accept children, meaning that you
15
- // can't inject values into it with renderHook's wrapper
16
- function WorkspaceMock ( { workspace } : { workspace ?: Workspace } ) {
17
- const { duplicateWorkspace, isDuplicationReady } =
18
- useWorkspaceDuplication ( workspace ) ;
19
-
20
- return (
21
- < button onClick = { duplicateWorkspace } disabled = { ! isDuplicationReady } >
22
- Click me!
23
- </ button >
24
- ) ;
25
- }
26
-
27
- function renderInMemory ( workspace ?: Workspace ) {
28
- const router = createMemoryRouter ( [
29
- { path : "/" , element : < WorkspaceMock workspace = { workspace } /> } ,
9
+ function render ( workspace ?: Workspace ) {
10
+ return renderHookWithAuth (
11
+ ( { workspace } : { workspace ?: Workspace } ) => {
12
+ return useWorkspaceDuplication ( workspace ) ;
13
+ } ,
30
14
{
31
- path : "/templates/:template/workspace" ,
32
- element : < CreateWorkspacePage /> ,
15
+ initialProps : { workspace } ,
16
+ extraRoutes : [
17
+ {
18
+ path : "/templates/:template/workspace" ,
19
+ element : < CreateWorkspacePage /> ,
20
+ } ,
21
+ ] ,
33
22
} ,
34
- ] ) ;
35
-
36
- return renderWithRouter ( router ) ;
23
+ ) ;
37
24
}
38
25
26
+ type RenderResult = Awaited < ReturnType < typeof render > > ;
27
+
39
28
async function performNavigation (
40
- button : HTMLElement ,
41
- router : ReturnType < typeof createMemoryRouter > ,
29
+ result : RenderResult [ "result" ] ,
30
+ router : RenderResult [ "router" ] ,
42
31
) {
43
- await waitFor ( ( ) => expect ( button ) . not . toBeDisabled ( ) ) ;
44
- await userEvent . click ( button ) ;
32
+ await waitFor ( ( ) => expect ( result . current . isDuplicationReady ) . toBe ( true ) ) ;
33
+ result . current . duplicateWorkspace ( ) ;
45
34
46
35
return waitFor ( ( ) => {
47
36
expect ( router . state . location . pathname ) . toEqual (
@@ -52,34 +41,30 @@ async function performNavigation(
52
41
53
42
describe ( `${ useWorkspaceDuplication . name } ` , ( ) => {
54
43
it ( "Will never be ready when there is no workspace passed in" , async ( ) => {
55
- const { rootComponent, rerender } = renderInMemory ( undefined ) ;
56
- const button = await screen . findByRole ( "button" ) ;
57
- expect ( button ) . toBeDisabled ( ) ;
44
+ const { result, rerender } = await render ( undefined ) ;
45
+ expect ( result . current . isDuplicationReady ) . toBe ( false ) ;
58
46
59
47
for ( let i = 0 ; i < 10 ; i ++ ) {
60
- rerender ( rootComponent ) ;
61
- expect ( button ) . toBeDisabled ( ) ;
48
+ rerender ( { workspace : undefined } ) ;
49
+ expect ( result . current . isDuplicationReady ) . toBe ( false ) ;
62
50
}
63
51
} ) ;
64
52
65
53
it ( "Will become ready when workspace is provided and build params are successfully fetched" , async ( ) => {
66
- renderInMemory ( MockWorkspace ) ;
67
- const button = await screen . findByRole ( "button" ) ;
54
+ const { result } = await render ( MockWorkspace ) ;
68
55
69
- expect ( button ) . toBeDisabled ( ) ;
70
- await waitFor ( ( ) => expect ( button ) . not . toBeDisabled ( ) ) ;
56
+ expect ( result . current . isDuplicationReady ) . toBe ( false ) ;
57
+ await waitFor ( ( ) => expect ( result . current . isDuplicationReady ) . toBe ( true ) ) ;
71
58
} ) ;
72
59
73
- it ( "duplicateWorkspace navigates the user to the workspace creation page" , async ( ) => {
74
- const { router } = renderInMemory ( MockWorkspace ) ;
75
- const button = await screen . findByRole ( "button" ) ;
76
- await performNavigation ( button , router ) ;
60
+ it ( "Is able to navigate the user to the workspace creation page" , async ( ) => {
61
+ const { result, router } = await render ( MockWorkspace ) ;
62
+ await performNavigation ( result , router ) ;
77
63
} ) ;
78
64
79
65
test ( "Navigating populates the URL search params with the workspace's build params" , async ( ) => {
80
- const { router } = renderInMemory ( MockWorkspace ) ;
81
- const button = await screen . findByRole ( "button" ) ;
82
- await performNavigation ( button , router ) ;
66
+ const { result, router } = await render ( MockWorkspace ) ;
67
+ await performNavigation ( result , router ) ;
83
68
84
69
const parsedParams = new URLSearchParams ( router . state . location . search ) ;
85
70
const mockBuildParams = [
@@ -97,9 +82,8 @@ describe(`${useWorkspaceDuplication.name}`, () => {
97
82
} ) ;
98
83
99
84
test ( "Navigating appends other necessary metadata to the search params" , async ( ) => {
100
- const { router } = renderInMemory ( MockWorkspace ) ;
101
- const button = await screen . findByRole ( "button" ) ;
102
- await performNavigation ( button , router ) ;
85
+ const { result, router } = await render ( MockWorkspace ) ;
86
+ await performNavigation ( result , router ) ;
103
87
104
88
const parsedParams = new URLSearchParams ( router . state . location . search ) ;
105
89
const extraMetadataEntries = [
0 commit comments