- Notifications
You must be signed in to change notification settings - Fork410
-
I am using mock-service-worker rest.get<DefaultRequestBody,PathParams,Buffer>('/uploads/*',(req,res,ctx)=>{constimageBuffer=readFileSync(resolve(__dirname,'../fixtures/image.jpg'));returnres(ctx.status(200),ctx.set('Content-Length',imageBuffer.byteLength.toString()),ctx.set('Content-Type','image/jpeg'),ctx.body(imageBuffer));}), to mock this function that downloads image exportconstgetImage=async(url:string):Promise<File>=>{constresponse=awaitaxiosInstance.get(url,{responseType:'blob'});constfile=newFile([response.data],'default-image');// here, this constructor just gives {} alwaysreturnfile;};
construnHeader=async(user:ClientUser)=>{constheaderUrl=getHeaderImagePath(user);constheader=awaitgetImage(headerUrl);// herereset({...getValues(),header,}asSettingsFormData);setIsHeaderLoading(false);}; then Dropzone displays that image using
then inside the test file I asert that img tag has // assert header image - asyncconstheaderImage=awaitscreen.findByRole('img',{name:/header\-image/i});expect(headerImage).toBeInTheDocument();expect(headerImage).toHaveAttribute('href',expect.stringMatching(/^blob:https?:\/\//i)); mock-service-worker mock works fine and returns binary data, main problem is in File constructor which in jsdom always returns just empty object I also plan to mock Object.defineProperty(window.URL,'createObjectURL',{value:jest.fn().mockImplementation((arg)=>arg),}); Jest environment is: testEnvironment:'jsdom', Do you know where I can find complete type safe example how to mock |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 1 comment
-
This looks like something that you should be asking about in jsdom. It's not related to jest-dom, is it? |
BetaWas this translation helpful?Give feedback.