Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: TypeError: Cannot read properties of undefined (reading 'map') in AutoHeight #1009

Closed
4 of 15 tasks
suraj776 opened this issue Sep 20, 2024 · 9 comments · Fixed by #1012
Closed
4 of 15 tasks
Assignees
Labels
bug Something isn't working plugin Issue is related to plugins resolved This issue is resolved

Comments

@suraj776
Copy link

suraj776 commented Sep 20, 2024

Which variants of Embla Carousel are you using?

  • embla-carousel (Core)
  • embla-carousel-react
  • embla-carousel-vue
  • embla-carousel-svelte
  • embla-carousel-autoplay
  • embla-carousel-auto-scroll
  • embla-carousel-solid
  • embla-carousel-auto-height
  • embla-carousel-class-names
  • embla-carousel-fade
  • embla-carousel-docs (Documentation)
  • embla-carousel-docs (Generator)

Steps to reproduce

I'm trying to create a integration test using React Testing library.

Error:- I'm getting.

detail: TypeError: Cannot read properties of undefined (reading 'map')
at map (/node_modules/embla-carousel-auto-height/src/components/AutoHeight.ts:51:8)
at highestInView (/node_modules/embla-carousel-auto-height/src/components/AutoHeight.ts:56:48)

I tried all these mocks in my file. it didn't work. Also tried mocking the autoheight, then emblaCrousel started giving error.

https://github.com/davidjerleke/embla-carousel/tree/master/packages/embla-carousel/src/__tests__/mocks

Expected Behavior

It should be mockable when applying mocks.

Additional Context

I have also tried...

What browsers are you seeing the problem on?

All browsers

Version

^8.2.0

CodeSandbox

No response

Before submitting

  • I've made research efforts and searched the documentation
  • I've searched for existing issues
  • I agree to follow this project's Contributing Guidelines for bug reports
@suraj776 suraj776 added the bug Something isn't working label Sep 20, 2024
@davidjerleke
Copy link
Owner

@suraj776
Copy link
Author

suraj776 commented Sep 20, 2024

@davidjerleke yes, i do have all the mocks. Still getting error and it is from AutoHeight plugin.If i remove plugin, test cases do work.

@davidjerleke
Copy link
Owner

davidjerleke commented Sep 20, 2024

Thanks for clarifying @suraj776. I guess we need a null check here in the code.

@suraj776
Copy link
Author

suraj776 commented Sep 20, 2024

@davidjerleke actually here, here you need to add null check before map.

@davidjerleke davidjerleke changed the title [Bug]: TypeError: Cannot read properties of undefined (reading 'map') in AutoHeightts [Bug]: TypeError: Cannot read properties of undefined (reading 'map') in AutoHeight Sep 20, 2024
@davidjerleke
Copy link
Owner

davidjerleke commented Sep 20, 2024

@suraj776 no here, because we're mapping the selectedIndexes variable which is undefined. We could do:

- const selectedIndexes = slideRegistry[emblaApi.selectedScrollSnap()]
+ const selectedIndexes = slideRegistry[emblaApi.selectedScrollSnap()] || []

But that might lead to errors in other places. Like the function that depends on the highestInView() function:

function setContainerHeight(): void {
  emblaApi.containerNode().style.height = `${highestInView()}px`
}

Can you create a CodeSandbox that reproduces the problem? If you do I can solve it faster.

@davidjerleke davidjerleke added the plugin Issue is related to plugins label Sep 20, 2024
@suraj776
Copy link
Author

@davidjerleke

Please check this sandbox code.
https://codesandbox.io/p/devbox/elegant-moser-forked-psvcn3

run command

npm run test

@davidjerleke
Copy link
Owner

davidjerleke commented Sep 23, 2024

@suraj776 thanks. That helps. So this solves it:

function highestInView(): number | null {
  const { slideRegistry } = emblaApi.internalEngine()
  const selectedIndexes = slideRegistry[emblaApi.selectedScrollSnap()]

  if (!selectedIndexes) return null // Bail here if no selectedIndexes are found

  return selectedIndexes
    .map((index) => slideHeights[index])
    .reduce((a, b) => Math.max(a, b), 0)
}

function setContainerHeight(): void {
  const height = highestInView()
  if (height === null) return // Bail here if we don't have any height to apply

  emblaApi.containerNode().style.height = `${highestInView()}px`
}

Feel free to create a PR with the solution above.

Best,
David

@suraj776
Copy link
Author

@davidjerleke, Thanks for quick support.

@suraj776 suraj776 reopened this Sep 23, 2024
suraj776 pushed a commit to suraj776/embla-carousel that referenced this issue Sep 23, 2024
davidjerleke added a commit that referenced this issue Sep 24, 2024
[Bug]: TypeError: Cannot read properties of undefined (reading 'map') in AutoHeight
@davidjerleke davidjerleke added the resolved This issue is resolved label Sep 24, 2024
@davidjerleke
Copy link
Owner

@suraj776 I just released a bug fix for this in v8.3.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working plugin Issue is related to plugins resolved This issue is resolved
Projects
None yet
2 participants