Skip to content

Commit 31b30c5

Browse files
committed
done some tests
1 parent fa8acdb commit 31b30c5

File tree

3 files changed

+63
-18
lines changed

3 files changed

+63
-18
lines changed

cypress/support/component.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ export function getInt(max: number) {
3939
return Math.floor(Math.random() * max);
4040
}
4141

42+
export function getIntRange(min: number, max: number) {
43+
min = Math.ceil(min);
44+
max = Math.floor(max);
45+
return Math.floor(Math.random() * (max - min + 1) + min);
46+
}
47+
4248
export function getRandomSequence(maxLength: number) {
4349
const sequence: number[] = [];
4450

tests/App.cy.ts

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,45 @@
11
// @ts-ignore
22
import App from './App.vue';
3-
import { getInt, getRandomSequence } from '../cypress/support/component';
3+
import { getIntRange, getRandomSequence } from '../cypress/support/component';
44

5-
it('Should give priority to URL hash on mount', () => {
6-
const targetsLength = 30;
5+
it('Should jump to last target', () => {
6+
const targetsLength = 20;
77

88
cy.mount(App, {
99
props: {
10+
jumpToLast: true,
1011
targetsLength,
1112
},
1213
});
1314

14-
const randomIndex = getInt(targetsLength);
15+
cy.scrollTo('bottom');
16+
cy.wait(2000);
1517

1618
cy.get('a')
17-
.eq(randomIndex)
18-
.click()
19-
.should('have.class', 'active')
20-
.invoke('attr', 'href')
21-
.then((href) => {
22-
cy.hash().should('eq', href);
23-
});
19+
.eq(targetsLength - 1)
20+
.should('have.class', 'active');
2421
});
2522

26-
it.only('Should set active clicked links without scroll interferences', () => {
27-
const targetsLength = 30;
23+
it('Should not jump to last target', () => {
24+
const targetsLength = 20;
25+
26+
cy.mount(App, {
27+
props: {
28+
jumpToLast: false,
29+
targetsLength,
30+
},
31+
});
32+
33+
cy.scrollTo('bottom');
34+
cy.wait(2000);
35+
36+
cy.get('a')
37+
.eq(targetsLength - 1)
38+
.should('not.have.class', 'active');
39+
});
40+
41+
it('Should set active clicked links without scroll interferences', () => {
42+
const targetsLength = 20;
2843

2944
cy.mount(App, {
3045
props: {
@@ -35,15 +50,36 @@ it.only('Should set active clicked links without scroll interferences', () => {
3550
const randomIndices = getRandomSequence(targetsLength);
3651

3752
randomIndices.forEach((index) => {
38-
cy.wait(100); // Trigger smoothscroll
3953
cy.get('a').eq(index).click().should('have.class', 'active');
40-
cy.wait(100);
54+
cy.wait(200); // Wait for some smoothscrolling
55+
cy.get('a').eq(index).should('have.class', 'active');
4156
});
4257
});
4358

44-
/* it('Should update targets on cancel while scrolling from click', () => {});
59+
it('Should update targets on cancel while scrolling from click', () => {
60+
const targetsLength = 20;
61+
62+
cy.mount(App, {
63+
props: {
64+
jumpToLast: true,
65+
targetsLength,
66+
},
67+
});
68+
69+
for (let i = 0; i < 10; i++) {
70+
const randomIndex = getIntRange(10, 19);
71+
cy.get('a').eq(randomIndex).click().should('have.class', 'active');
72+
73+
cy.get('.Content').trigger('pointerdown', { force: true });
74+
cy.get('a').eq(randomIndex).should('not.have.class', 'active');
75+
76+
// Back to top
77+
cy.get('a').eq(0).click();
78+
cy.wait(1000);
79+
}
80+
});
4581

46-
it('Should jump to first target', () => {});
82+
/* it('Should jump to first target', () => {});
4783
4884
it('Should jump to last target', () => {});
4985

tests/App.vue

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ import { useActive } from '../src/useActive';
55
66
const props = defineProps<{
77
targetsLength: number;
8+
jumpToLast: boolean;
89
}>();
910
1011
const { sections, menuItems } = useFakeData(props.targetsLength);
1112
1213
const targets = computed(() => sections.map(({ id }) => id));
13-
const { setActive, isActive } = useActive(targets);
14+
const { setActive, isActive } = useActive(targets, {
15+
jumpToLast: props.jumpToLast,
16+
});
1417
</script>
1518

1619
<template>

0 commit comments

Comments
 (0)