Mocking Data Provider
Hi. How can i mock data provider to return lists that i wanted?
I am testing with jest and its been 3 days i haven't solved this problem.
37 Replies
Hi, I am refine support bot :wave:. I am here to help you with your question. I searched for the answer to your question in the refine documentation and found an answer! 🏆 If this is the answer you need, then we are very lucky. If not, please wait for someone from the refine core team or the community to try to help you.
Answer: You can use the swizzle to create a data provider and modify it according to your API. You can also use the mockUsers array to create a login function in the authProvider. Additionally, you can use the Nestjsx Crud Data Provider to connect to your Nestjsx API. SOURCES: ./docs/tutorial/2-understanding-dataprovider/1-swizzle.md, ./docs/tutorial/5-understanding-authprovider/1-create-authprovider.md, ./docs/advanced-tutorials/data-provider/strapi-v4.md, ./docs/examples/data-provider/nestjsxCrud.md
Answer: You can use the swizzle to create a data provider and modify it according to your API. You can also use the mockUsers array to create a login function in the authProvider. Additionally, you can use the Nestjsx Crud Data Provider to connect to your Nestjsx API. SOURCES: ./docs/tutorial/2-understanding-dataprovider/1-swizzle.md, ./docs/tutorial/5-understanding-authprovider/1-create-authprovider.md, ./docs/advanced-tutorials/data-provider/strapi-v4.md, ./docs/examples/data-provider/nestjsxCrud.md
rising-crimson•2y ago
Hey @thedevkim,
You need to create a wrapper that contains
<Refine/>
. After you need to pass mock data provider to <Refine/>
.
You can find our mock data provider in here: https://github.com/refinedev/refine/blob/next/packages/core/test/dataMocks.ts#L41
You can find usage of TestWrapper: https://github.com/refinedev/refine/blob/next/packages/core/src/hooks/useTable/index.spec.ts
Also, you can checkout this issue for more information: https://github.com/refinedev/refine/issues/3432harsh-harlequinOP•2y ago
@salihozdemir
Hi. I would like to confirm this
import { MockJSONServer, TestWrapper } from "@test";Can you give me the link to this module/package ?
rising-crimson•2y ago
You can find our
<TestWrapper>
in here:
https://github.com/refinedev/refine/blob/next/packages/core/test/index.tsxharsh-harlequinOP•2y ago
Just by putting
dataProvider={MockDataProvider}
i can already mock my lists output right?
@salihozdemir
I will check it in few mins. Because as i have experienced. i tried mocking the dataProvider and it says an error, that
"needs to provider a dataProviderName" something like that, ill let you know
Hey @thedevkim ,
Could you check this issue? https://github.com/refinedev/refine/issues/3432
GitHub
[BUG] Can't test a component that uses useCustomMutation · Issue #3...
Describe the bug Components that are using refine hooks are impossible to test. One of the examples is a component making a custom request to server. Here is my component code. import { FileUpload,...
harsh-harlequinOP•2y ago
Hi. What version does this have?
import { MockJSONServer, TestWrapper } from "@test";I get error when importing "@test" Hi. Do you know where he gets the RefineTestWrapper ?
harsh-harlequinOP•2y ago
Hi, im sorry, im a bit confused.
Is it?
rising-crimson•2y ago
No, you need to create your own
<TestWrapper>
. When you render your hook in test environment, the hook needs a wrapper which contains refine providers.
You can copy our TestWrapper component here: https://github.com/refinedev/refine/blob/next/packages/core/test/index.tsx#L65-L171
Then, you can use it like here: https://github.com/refinedev/refine/blob/next/packages/core/src/hooks/useTable/index.spec.ts#L23-L28harsh-harlequinOP•2y ago
Is it the same with this RefineTestWrapper in this section ?
https://github.com/refinedev/refine/issues/3432#issuecomment-1398627534
GitHub
[BUG] Can't test a component that uses useCustomMutation · Issue #3...
Describe the bug Components that are using refine hooks are impossible to test. One of the examples is a component making a custom request to server. Here is my component code. import { FileUpload,...
harsh-harlequinOP•2y ago
Where do i get these files ?
harsh-harlequinOP•2y ago
Do i need to copy all ?
rising-crimson•2y ago
Actually you're right, sorry. Can you create
<TestWrapper>
like here?
https://github.com/refinedev/refine/blob/next/packages/react-table/test/index.tsx#L13harsh-harlequinOP•2y ago
Just this one right? or the entire code?
rising-crimson•2y ago
Just component
harsh-harlequinOP•2y ago
How about the MockJSONServer
from ./dataMock
rising-crimson•2y ago
Off course, you can use
MockJSONServer
.
Also, MockRouterProvider
harsh-harlequinOP•2y ago
Okay so i copied the code of testwrapper and have this on my test.
harsh-harlequinOP•2y ago
When i test it, i get infinite test error.
rising-crimson•2y ago
Can you share your TestWrapper and error messages?
harsh-harlequinOP•2y ago
This is my TestWrapper.
harsh-harlequinOP•2y ago
rising-crimson•2y ago
You don't need line 40
harsh-harlequinOP•2y ago
i manage to take a screnshot while in infinite error
sec
harsh-harlequinOP•2y ago
rising-crimson•2y ago
MockDataProvider is a function. If you invoke it, it probably works.
harsh-harlequinOP•2y ago
This is my test
harsh-harlequinOP•2y ago
can you tell me the problem on this one ?
kinda confused sorry
rising-crimson•2y ago
Yes, you need to use MockDataProvider like this:
harsh-harlequinOP•2y ago
oh
The error is gone now, I will in the component if it really rendered the lists.
Thanks
Hi. I already have a data, but in the table the list is not showing and there is no pagination. I am using jest testing
When i console log it on the render Table.Column, there is a value
It seems like no row has been created
harsh-harlequinOP•2y ago
harsh-harlequinOP•2y ago
I console log the tableProps and the data is actually there, but its not rendering in the row
rising-crimson•2y ago
I don't know the parts with Antd Table. If you are writing unit tests, you should test small functions, if you want to test useTable and Antd.Table together, writing e2e test seems like a better solution.
You can see our unit tests about useTable in here, may it can inspire you.
https://github.com/refinedev/refine/blob/next/packages/antd/src/hooks/table/useTable/useTable.spec.ts
harsh-harlequinOP•2y ago
How do i test if its an edit page ?
harsh-harlequinOP•2y ago
This one seems to be working only for getList
harsh-harlequinOP•2y ago
How do i test a page for edit ?
It should use the getOne right ?