Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
F
frontend
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
frontend
Commits
958a8131
Commit
958a8131
authored
Jun 12, 2023
by
tom
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
better router mock
parent
f33a23c7
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
8 deletions
+72
-8
next-router.ts
jest/mocks/next-router.ts
+10
-4
useQueryWithPages.test.tsx
ui/shared/pagination/useQueryWithPages.test.tsx
+62
-4
No files found.
jest/mocks/next-router.ts
View file @
958a8131
import
type
{
NextRouter
}
from
'
next/router
'
;
export
function
mockRouter
(
params
?:
Partial
<
NextRouter
>
)
{
return
{
useRouter
:
jest
.
fn
(()
=>
({
export
const
router
=
{
query
:
{},
push
:
jest
.
fn
(),
};
export
const
useRouter
=
jest
.
fn
<
unknown
,
Array
<
Partial
<
NextRouter
>>>
(()
=>
(
router
));
export
const
mockUseRouter
=
(
params
?:
Partial
<
NextRouter
>
)
=>
{
return
{
useRouter
:
jest
.
fn
(()
=>
({
...
router
,
...
params
,
})),
};
}
}
;
ui/shared/pagination/useQueryWithPages.test.tsx
View file @
958a8131
import
fetch
from
'
jest-fetch-mock
'
;
import
{
renderHook
,
wrapper
,
act
}
from
'
jest/lib
'
;
import
{
mockR
outer
}
from
'
jest/mocks/next-router
'
;
import
{
useRouter
,
r
outer
}
from
'
jest/mocks/next-router
'
;
import
flushPromises
from
'
jest/utils/flushPromises
'
;
import
*
as
addressMock
from
'
mocks/address/address
'
;
jest
.
mock
(
'
next/router
'
,
()
=>
({
useRouter
}));
import
type
{
Params
}
from
'
./useQueryWithPages
'
;
import
useQueryWithPages
from
'
./useQueryWithPages
'
;
jest
.
mock
(
'
next/router
'
,
()
=>
mockRouter
());
it
(
'
returns correct data if there is only one page
'
,
async
()
=>
{
const
params
:
Params
<
'
address_txs
'
>
=
{
resourceName
:
'
address_txs
'
,
...
...
@@ -29,8 +29,66 @@ it('returns correct data if there is only one page', async() => {
canGoBackwards
:
true
,
hasNextPage
:
false
,
isLoading
:
false
,
isVisible
:
false
,
});
});
describe
(
'
if there are multiple pages
'
,
()
=>
{
it
(
'
return correct data for the first page
'
,
async
()
=>
{
const
params
:
Params
<
'
address_txs
'
>
=
{
resourceName
:
'
address_txs
'
,
pathParams
:
{
hash
:
addressMock
.
hash
},
};
const
response
=
{
items
:
[
{
hash
:
'
11
'
},
{
hash
:
'
12
'
}
],
next_page_params
:
{
block_number
:
11
,
index
:
12
,
items_count
:
13
,
},
};
fetch
.
mockResponse
(
JSON
.
stringify
(
response
));
const
{
result
}
=
renderHook
(()
=>
useQueryWithPages
(
params
),
{
wrapper
});
await
waitForApiResponse
();
expect
(
result
.
current
.
data
).
toEqual
(
response
);
expect
(
result
.
current
.
pagination
).
toMatchObject
({
page
:
1
,
canGoBackwards
:
true
,
hasNextPage
:
true
,
isLoading
:
false
,
isVisible
:
true
,
});
});
});
describe
(
'
if there is page query param in URL
'
,
()
=>
{
it
(
'
sets this param as the page number
'
,
async
()
=>
{
useRouter
.
mockReturnValueOnce
({
...
router
,
query
:
{
page
:
'
3
'
}
});
const
params
:
Params
<
'
address_txs
'
>
=
{
resourceName
:
'
address_txs
'
,
pathParams
:
{
hash
:
addressMock
.
hash
},
};
const
response
=
{
items
:
[],
next_page_params
:
null
,
};
fetch
.
mockResponse
(
JSON
.
stringify
(
response
));
const
{
result
}
=
renderHook
(()
=>
useQueryWithPages
(
params
),
{
wrapper
});
await
waitForApiResponse
();
expect
(
result
.
current
.
data
).
toEqual
(
response
);
expect
(
result
.
current
.
pagination
).
toMatchObject
({
page
:
3
,
canGoBackwards
:
false
,
hasNextPage
:
false
,
isLoading
:
false
,
isVisible
:
true
,
});
});
});
async
function
waitForApiResponse
()
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment