Skip to content

Commit 30740cd

Browse files
committed
Test - Edit commands to match new API
1 parent 2aa2c1a commit 30740cd

File tree

3 files changed

+28
-64
lines changed

3 files changed

+28
-64
lines changed

cypress/support/commands.ts

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
import { DEFAULT_LEAVE_DELTA, DEFAULT_ENTER_DELTA } from '../../src/constants'
21
import { isCustomContainer } from './constants'
32

43
import type { CSSProperties } from 'vue'
54

6-
type scrollRootWithDeltaOptions = {
7-
delta: number
8-
scrollDown?: boolean
9-
minDuration?: number
10-
}
5+
import 'cypress-wait-frames'
116

127
declare global {
138
namespace Cypress {
149
interface Chainable {
1510
getScrollSubject: () => Cypress.Chainable
16-
scrollRootWithDelta: (options: scrollRootWithDeltaOptions) => Cypress.Chainable
17-
scrollToHide: () => Cypress.Chainable
18-
scrollToShow: () => Cypress.Chainable
11+
scrollUp: () => Cypress.Chainable
12+
scrollDown: () => Cypress.Chainable
1913
checkStyles: (styles: CSSProperties) => Cypress.Chainable
2014
resizeRoot: (newWidth: number) => Cypress.Chainable
2115
}
@@ -29,41 +23,29 @@ Cypress.Commands.add('getScrollSubject', () => {
2923

3024
Cypress.Commands.add('resizeRoot', (newWidth: number) => {
3125
if (isCustomContainer) {
32-
return cy.get('.Scroller').then(($el) => {
33-
$el.css({ width: `${newWidth}px` })
34-
})
26+
return cy
27+
.get('.Scroller')
28+
.then(($el) => {
29+
$el.css({ width: `${newWidth}px` })
30+
})
31+
.waitFrames({
32+
subject: () => cy.get('.Scroller'),
33+
property: 'clientWidth',
34+
frames: 10,
35+
})
3536
}
3637

37-
return cy.viewport(newWidth, newWidth)
38+
return cy.viewport(newWidth, newWidth).waitFrames({
39+
subject: cy.window,
40+
property: 'outerWidth',
41+
frames: 10,
42+
})
3843
})
3944

40-
Cypress.Commands.add(
41-
'scrollRootWithDelta',
42-
({ delta, scrollDown = true, minDuration = 1000 }: scrollRootWithDeltaOptions) => {
43-
let distance = delta * minDuration
44-
let duration = minDuration
45-
46-
// If obtained distance is below header height, throw an error
47-
cy.get('header').then(($header) => {
48-
const headerHeight = $header.height()
49-
50-
if (headerHeight && distance < headerHeight) {
51-
throw new Error(`Scrolling distance is less than ${headerHeight}px. Increase duration.`)
52-
}
53-
})
54-
55-
const scrollDistance = scrollDown ? distance : -1 * distance
56-
57-
cy.log(`Scrolling ${scrollDistance}px with a delta of ${delta} in ${duration}ms`)
58-
59-
return cy.getScrollSubject().scrollTo(0, scrollDistance, { duration })
60-
}
61-
)
62-
63-
Cypress.Commands.add('scrollToHide', () => cy.scrollRootWithDelta({ delta: DEFAULT_LEAVE_DELTA }))
45+
Cypress.Commands.add('scrollUp', () => cy.getScrollSubject().scrollTo('top', { duration: 300 }))
6446

65-
Cypress.Commands.add('scrollToShow', () =>
66-
cy.scrollRootWithDelta({ delta: DEFAULT_ENTER_DELTA, scrollDown: false })
47+
Cypress.Commands.add('scrollDown', () =>
48+
cy.getScrollSubject().scrollTo('bottom', { duration: 300 }),
6749
)
6850

6951
Cypress.Commands.add('checkStyles', { prevSubject: 'element' }, (subject, styles) => {

tests/components/ContainerScroll.vue

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
<script setup lang="ts">
2-
import { onBeforeMount, ref, type CSSProperties } from 'vue'
2+
import { onBeforeMount, ref } from 'vue'
33
import { useFixedHeader } from '../../src'
44
55
const props = defineProps<{
6-
enterDelta?: number
7-
leaveDelta?: number
8-
enterStyles?: CSSProperties
9-
leaveStyles?: CSSProperties
10-
simulateInstantRestoration?: boolean
6+
instantScrollRestoration?: boolean
117
}>()
128
139
const containerRef = ref<HTMLElement | null>(null)
1410
const headerRef = ref<HTMLElement | null>(null)
1511
1612
onBeforeMount(() => {
17-
if (!props.simulateInstantRestoration) return
13+
if (!props.instantScrollRestoration) return
1814
1915
window.requestAnimationFrame(() => {
2016
if (!containerRef.value) throw new Error('containerRef is null')
@@ -25,10 +21,6 @@ onBeforeMount(() => {
2521
2622
const { styles } = useFixedHeader(headerRef, {
2723
root: containerRef,
28-
enterDelta: props.enterDelta,
29-
leaveDelta: props.leaveDelta,
30-
enterStyles: props.enterStyles,
31-
leaveStyles: props.leaveStyles,
3224
})
3325
</script>
3426

tests/components/WindowScroll.vue

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,22 @@
11
<script setup lang="ts">
2-
import { onBeforeMount, ref, type CSSProperties } from 'vue'
2+
import { onBeforeMount, ref } from 'vue'
33
import { useFixedHeader } from '../../src'
44
55
const props = defineProps<{
6-
enterDelta?: number
7-
leaveDelta?: number
8-
enterStyles?: CSSProperties
9-
leaveStyles?: CSSProperties
10-
simulateInstantRestoration?: boolean
6+
instantScrollRestoration?: boolean
117
}>()
128
139
const headerRef = ref<HTMLElement | null>(null)
1410
1511
onBeforeMount(() => {
16-
if (!props.simulateInstantRestoration) return
12+
if (!props.instantScrollRestoration) return
1713
1814
window.requestAnimationFrame(() => {
1915
window.scroll(0, window.innerHeight / 3)
2016
})
2117
})
2218
23-
const { styles } = useFixedHeader(headerRef, {
24-
enterDelta: props.enterDelta,
25-
leaveDelta: props.leaveDelta,
26-
enterStyles: props.enterStyles,
27-
leaveStyles: props.leaveStyles,
28-
})
19+
const { styles } = useFixedHeader(headerRef)
2920
</script>
3021

3122
<template>
@@ -61,4 +52,3 @@ const { styles } = useFixedHeader(headerRef, {
6152
}
6253
}
6354
</style>
64-
../../src/useFixedHeader

0 commit comments

Comments
 (0)