optimistic-gold
optimistic-gold16mo ago

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
refine-support-bot
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
noble-gold
noble-gold16mo 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/3432
optimistic-gold
optimistic-gold16mo ago
@salihozdemir Hi. I would like to confirm this
import { MockJSONServer, TestWrapper } from "@test";
Can you give me the link to this module/package ?
noble-gold
noble-gold16mo ago
optimistic-gold
optimistic-gold16mo ago
const MockDataProvider = () => {
return {
default: {
create: () => Promise.resolve({ data: posts[0] }),
createMany: () => Promise.resolve({ data: posts }),
deleteOne: () => Promise.resolve({ data: posts[0] }),
deleteMany: () => Promise.resolve({ data: [] }),
getList: () => Promise.resolve({ data: posts, total: 2 }),
getMany: () => Promise.resolve({ data: [...posts] }),
getOne: () => Promise.resolve({ data: posts[0] }),
update: () => Promise.resolve({ data: posts[0] }),
updateMany: () => Promise.resolve({ data: [] }),
getApiUrl: () => "https://api.fake-rest.refine.dev",
custom: () => Promise.resolve({ data: [...posts] }),
},
};
};
const MockDataProvider = () => {
return {
default: {
create: () => Promise.resolve({ data: posts[0] }),
createMany: () => Promise.resolve({ data: posts }),
deleteOne: () => Promise.resolve({ data: posts[0] }),
deleteMany: () => Promise.resolve({ data: [] }),
getList: () => Promise.resolve({ data: posts, total: 2 }),
getMany: () => Promise.resolve({ data: [...posts] }),
getOne: () => Promise.resolve({ data: posts[0] }),
update: () => Promise.resolve({ data: posts[0] }),
updateMany: () => Promise.resolve({ data: [] }),
getApiUrl: () => "https://api.fake-rest.refine.dev",
custom: () => Promise.resolve({ data: [...posts] }),
},
};
};
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
Omer
Omer16mo ago
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,...
optimistic-gold
optimistic-gold16mo 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 ?
optimistic-gold
optimistic-gold16mo ago
Hi, im sorry, im a bit confused. Is it?
import { TestWrapper } from '@testing-library/react';
import { TestWrapper } from '@testing-library/react';
noble-gold
noble-gold16mo 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-L28
optimistic-gold
optimistic-gold16mo 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,...
optimistic-gold
optimistic-gold16mo ago
Where do i get these files ?
No description
optimistic-gold
optimistic-gold16mo ago
Do i need to copy all ?
noble-gold
noble-gold16mo 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#L13
optimistic-gold
optimistic-gold16mo ago
Just this one right? or the entire code?
No description
noble-gold
noble-gold16mo ago
Just component
optimistic-gold
optimistic-gold16mo ago
How about the MockJSONServer from ./dataMock
noble-gold
noble-gold16mo ago
Off course, you can use MockJSONServer . Also, MockRouterProvider
optimistic-gold
optimistic-gold16mo ago
Okay so i copied the code of testwrapper and have this on my test.
No description
optimistic-gold
optimistic-gold16mo ago
When i test it, i get infinite test error.
noble-gold
noble-gold16mo ago
Can you share your TestWrapper and error messages?
optimistic-gold
optimistic-gold16mo ago
This is my TestWrapper.
optimistic-gold
optimistic-gold16mo ago
No description
noble-gold
noble-gold16mo ago
You don't need line 40
optimistic-gold
optimistic-gold16mo ago
i manage to take a screnshot while in infinite error sec
optimistic-gold
optimistic-gold16mo ago
No description
noble-gold
noble-gold16mo ago
MockDataProvider is a function. If you invoke it, it probably works.
optimistic-gold
optimistic-gold16mo ago
This is my test
No description
optimistic-gold
optimistic-gold16mo ago
can you tell me the problem on this one ? kinda confused sorry
noble-gold
noble-gold16mo ago
Yes, you need to use MockDataProvider like this:
let d = render(
<CampaingList/>,
wrapper: TestWrapper({
dataProvider: MockDataProvider(),
})
)
let d = render(
<CampaingList/>,
wrapper: TestWrapper({
dataProvider: MockDataProvider(),
})
)
optimistic-gold
optimistic-gold16mo 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
optimistic-gold
optimistic-gold16mo ago
No description
optimistic-gold
optimistic-gold16mo ago
I console log the tableProps and the data is actually there, but its not rendering in the row
noble-gold
noble-gold16mo 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
optimistic-gold
optimistic-gold16mo ago
How do i test if its an edit page ?
optimistic-gold
optimistic-gold16mo ago
This one seems to be working only for getList
No description
optimistic-gold
optimistic-gold16mo ago
How do i test a page for edit ? It should use the getOne right ?