Skip to content

Commit

Permalink
edit watch prop api, add watch test
Browse files Browse the repository at this point in the history
  • Loading branch information
smastrom committed Jul 19, 2023
1 parent b0844cc commit 546252f
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cypress/support/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ declare global {
getScrollSubject: () => Cypress.Chainable
scrollRootWithDelta: (options: scrollRootWithDeltaOptions) => Cypress.Chainable
scrollToHide: () => Cypress.Chainable
checkStyles: (styles: CSSProperties) => void
checkStyles: (styles: CSSProperties) => Cypress.Chainable
resizeRoot: (newWidth: number) => Cypress.Chainable
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Ref } from 'vue'
import type { UseFixedHeaderOptions } from './types'

export const CAPTURE_DELTA_FRAME_COUNT = 10
Expand All @@ -23,5 +24,5 @@ export const defaultOptions: UseFixedHeaderOptions = {
transition: `transform 0.5s ${easing} 0s`,
transform: 'translateY(-101%)',
},
watch: [],
watch: (() => null) as unknown as Ref<any>,
}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ export interface UseFixedHeaderOptions<T = any> {
root: MaybeTemplateRef
enterStyles: CSSProperties
leaveStyles: CSSProperties
watch: (Ref<T> | ComputedRef<T> | null)[]
watch: Ref<T> | ComputedRef<T>
}
2 changes: 1 addition & 1 deletion src/useFixedHeader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export function useFixedHeader(
}
})

watch(() => mergedOptions.watch, toggleFunctionalities, { flush: 'post' })
watch(mergedOptions.watch, toggleFunctionalities, { flush: 'post' })

return { isVisible }
}
42 changes: 42 additions & 0 deletions tests/components/Watch.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<script setup lang="ts">
import { ref, unref, computed, type Ref } from 'vue'
import { useFixedHeader } from '../../src/useFixedHeader'
const props = defineProps<{
watch: Ref<boolean>
}>()
const headerRef = ref<HTMLElement | null>(null)
useFixedHeader(headerRef, {
watch: props.watch,
})
const position = computed(() => (props.watch.value ? 'relative' : 'fixed'))
</script>

<template>
<div class="Wrapper">
<header
class="Header"
ref="headerRef"
:style="{
position,
}"
/>
</div>
</template>

<style scoped>
.Wrapper {
height: 2000px;
display: flex;
}
.Header {
width: 100%;
top: 0;
left: 0;
height: 80px;
background: red;
}
</style>
2 changes: 1 addition & 1 deletion tests/components/WindowScroll.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { onBeforeMount, ref, type CSSProperties } from 'vue'
import { onBeforeMount, ref, type Ref, type CSSProperties } from 'vue'
import { useFixedHeader } from '../../src/useFixedHeader'
const props = defineProps<{
Expand Down
33 changes: 33 additions & 0 deletions tests/watch.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import App from './components/Watch.vue'

import { ref } from 'vue'

import { DEFAULT_ENTER_DELTA, defaultOptions } from '../src/constants'

describe('Watch', () => {
if (!Cypress.env('CONTAINER')) {
it('Toggles functionalities when reactive source changes', () => {
const isRelative = ref(false)

cy.mount(App, { props: { watch: isRelative } })
.scrollToHide()
.get('header')
.checkStyles(defaultOptions.leaveStyles)

.then(() => (isRelative.value = true))
.get('header')
.should('have.attr', 'style')
.and('be.eq', 'position: relative;')

.then(() => (isRelative.value = false))
.scrollRootWithDelta({ delta: DEFAULT_ENTER_DELTA, scrollDown: false })
.get('header')
.checkStyles(defaultOptions.enterStyles)

.then(() => (isRelative.value = true))
.get('header')
.should('have.attr', 'style')
.and('be.eq', 'position: relative;')
})
}
})

0 comments on commit 546252f

Please sign in to comment.