Skip to content

Commit 3915e08

Browse files
committed
refactor: improve test cases for utility functions
1 parent 28e79d2 commit 3915e08

14 files changed

+248
-94
lines changed

src/utils/calculateAverage.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ import { describe, expect, test } from 'vitest'
33
import { calculateAverage } from './calculateAverage'
44

55
describe('calculateAverage', () => {
6-
test('returns 0 for empty array', () => {
7-
expect(calculateAverage([])).toBe(0)
6+
test('calculates average for multiple values', () => {
7+
expect(calculateAverage([1, 2, 3, 4, 5])).toBe(3)
88
})
99

1010
test('calculates average for single value', () => {
1111
expect(calculateAverage([5])).toBe(5)
1212
})
1313

14-
test('calculates average for multiple values', () => {
15-
expect(calculateAverage([1, 2, 3, 4, 5])).toBe(3)
16-
})
17-
1814
test('handles decimal numbers', () => {
1915
expect(calculateAverage([1.5, 2.5, 3.5])).toBe(2.5)
2016
})
2117

2218
test('handles negative numbers', () => {
2319
expect(calculateAverage([-1, 0, 1])).toBe(0)
2420
})
21+
22+
test('returns 0 for empty array', () => {
23+
expect(calculateAverage([])).toBe(0)
24+
})
2525
})

src/utils/camelCaseToKebabCase.spec.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ import { describe, it, expect } from 'vitest'
33
import { camelCaseToKebabCase } from './camelCaseToKebabCase'
44

55
describe('camelCaseToKebabCase', () => {
6-
it('converts single camelCase word to kebab-case', () => {
7-
expect(camelCaseToKebabCase('camelCase')).toBe('camel-case')
8-
})
9-
106
it('converts multiple camelCase words to kebab-case', () => {
117
expect(camelCaseToKebabCase('myVariableName')).toBe('my-variable-name')
128
})
139

14-
it('handles strings with no uppercase letters', () => {
15-
expect(camelCaseToKebabCase('lowercase')).toBe('lowercase')
10+
it('converts single camelCase word to kebab-case', () => {
11+
expect(camelCaseToKebabCase('camelCase')).toBe('camel-case')
1612
})
1713

1814
it('handles empty strings', () => {
1915
expect(camelCaseToKebabCase('')).toBe('')
2016
})
2117

18+
it('handles strings with no uppercase letters', () => {
19+
expect(camelCaseToKebabCase('lowercase')).toBe('lowercase')
20+
})
21+
2222
it('handles strings with numbers', () => {
2323
expect(camelCaseToKebabCase('camelCase123')).toBe('camel-case123')
2424
})

src/utils/createCloneSlides.spec.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { h, ComponentInternalInstance } from 'vue'
3+
4+
import { createCloneSlides } from './createCloneSlides'
5+
6+
function createMockSlide(index: number): ComponentInternalInstance {
7+
return {
8+
vnode: h('div', { key: index }, `Slide ${index}`),
9+
// ...other properties...
10+
} as ComponentInternalInstance
11+
}
12+
13+
describe('createCloneSlides', () => {
14+
it('should clone slides correctly after', () => {
15+
const slides = [createMockSlide(1), createMockSlide(2), createMockSlide(3)]
16+
const clones = createCloneSlides({ slides, position: 'after', toShow: 1 })
17+
expect(clones[0].key).toBe('clone-after-0')
18+
})
19+
20+
it('should clone slides correctly before', () => {
21+
const slides = [createMockSlide(1), createMockSlide(2), createMockSlide(3)]
22+
const clones = createCloneSlides({ slides, position: 'before', toShow: 1 })
23+
expect(clones[0].key).toBe('clone-before--1')
24+
})
25+
26+
it('should create the correct number of clones after', () => {
27+
const slides = [createMockSlide(1), createMockSlide(2), createMockSlide(3)]
28+
const clones = createCloneSlides({ slides, position: 'after', toShow: 2 })
29+
expect(clones.length).toBe(2)
30+
})
31+
32+
it('should create the correct number of clones before', () => {
33+
const slides = [createMockSlide(1), createMockSlide(2), createMockSlide(3)]
34+
const clones = createCloneSlides({ slides, position: 'before', toShow: 2 })
35+
expect(clones.length).toBe(2)
36+
})
37+
38+
it('should handle empty slides array', () => {
39+
const slides: ComponentInternalInstance[] = []
40+
const clones = createCloneSlides({ slides, position: 'before', toShow: 2 })
41+
expect(clones.length).toBe(0)
42+
})
43+
44+
it('should handle zero clones', () => {
45+
const slides = [createMockSlide(1), createMockSlide(2), createMockSlide(3)]
46+
const clones = createCloneSlides({ slides, position: 'before', toShow: 0 })
47+
expect(clones.length).toBe(0)
48+
})
49+
})
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { afterEach, beforeEach, describe, expect, it } from 'vitest'
2+
import { VNode } from 'vue'
3+
4+
import { disableChildrenTabbing } from './disableChildrenTabbing'
5+
6+
describe('disableChildrenTabbing', () => {
7+
let container: HTMLElement
8+
9+
beforeEach(() => {
10+
container = document.createElement('div')
11+
document.body.appendChild(container)
12+
})
13+
14+
afterEach(() => {
15+
document.body.removeChild(container)
16+
})
17+
18+
it('should disable tabbing for all child elements', () => {
19+
const child1 = document.createElement('button')
20+
const child2 = document.createElement('input')
21+
container.appendChild(child1)
22+
container.appendChild(child2)
23+
24+
disableChildrenTabbing({ el: container } as unknown as VNode)
25+
26+
expect(child1.tabIndex).toBe(-1)
27+
expect(child2.tabIndex).toBe(-1)
28+
})
29+
30+
it('should not affect elements outside the container', () => {
31+
const outsideChild = document.createElement('button')
32+
document.body.appendChild(outsideChild)
33+
34+
disableChildrenTabbing({ el: container } as unknown as VNode)
35+
36+
expect(outsideChild.tabIndex).not.toBe(-1)
37+
38+
document.body.removeChild(outsideChild)
39+
})
40+
41+
it('should not change tabIndex for elements that already have tabIndex -1', () => {
42+
const child = document.createElement('button')
43+
child.tabIndex = -1
44+
container.appendChild(child)
45+
46+
disableChildrenTabbing({ el: container } as unknown as VNode)
47+
48+
expect(child.tabIndex).toBe(-1)
49+
})
50+
})

src/utils/getDraggedSlidesCount.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,6 @@ describe('getDraggedSlidesCount', () => {
1313
expect(getDraggedSlidesCount(params)).toBe(-2)
1414
})
1515

16-
it('should calculate the correct number of slides for vertical drag', () => {
17-
const params = {
18-
isVertical: true,
19-
isReversed: false,
20-
dragged: { x: 0, y: 150 },
21-
effectiveSlideSize: 100,
22-
}
23-
expect(getDraggedSlidesCount(params)).toBe(-2)
24-
})
25-
2616
it('should calculate the correct number of slides for reversed horizontal drag', () => {
2717
const params = {
2818
isVertical: false,
@@ -43,6 +33,16 @@ describe('getDraggedSlidesCount', () => {
4333
expect(getDraggedSlidesCount(params)).toBe(2)
4434
})
4535

36+
it('should calculate the correct number of slides for vertical drag', () => {
37+
const params = {
38+
isVertical: true,
39+
isReversed: false,
40+
dragged: { x: 0, y: 150 },
41+
effectiveSlideSize: 100,
42+
}
43+
expect(getDraggedSlidesCount(params)).toBe(-2)
44+
})
45+
4646
it('should handle zero drag', () => {
4747
const params = {
4848
isVertical: false,

src/utils/getDraggedSlidesCount.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
interface DragParams {
1+
type DragParams = {
22
isVertical: boolean
33
isReversed: boolean
44
dragged: { x: number; y: number }

src/utils/getNumberInRange.spec.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ import { expect, it, describe } from 'vitest'
33
import { getNumberInRange } from '@/utils'
44

55
describe('getCurrentSlideIndex', () => {
6+
it('When min is larger than max should return val', () => {
7+
const val = 2
8+
const min = 10
9+
const max = 5
10+
const results = getNumberInRange({ val, min, max })
11+
12+
expect(results).toBe(val)
13+
})
14+
615
it('When the number inside the range should return the same value', () => {
716
const val = 5
817
const min = 0
@@ -30,16 +39,6 @@ describe('getCurrentSlideIndex', () => {
3039
expect(results).toBe(min)
3140
})
3241

33-
it('When the min is larger than max should return val', () => {
34-
const val = 2
35-
const min = 10
36-
const max = 5
37-
const results = getNumberInRange({ val, min, max })
38-
39-
expect(results).toBe(val)
40-
})
41-
42-
4342
it('doesn`t bound a NaN min or max', () => {
4443
const val = 20
4544
expect(getNumberInRange({ val, min: 0, max: NaN })).toBe(val)

src/utils/getSnapAlignOffset.spec.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,33 @@ import { describe, expect, test } from 'vitest'
33
import { getSnapAlignOffset } from './getSnapAlignOffset'
44

55
describe('getSnapAlignOffset', () => {
6-
describe('with itemsToShow parameter', () => {
7-
test('should return correct offset for start alignment', () => {
8-
expect(getSnapAlignOffset({ align: 'start', itemsToShow: 3 })).toBe(0)
6+
describe('edge cases', () => {
7+
test('should handle equal viewport and slide sizes', () => {
8+
expect(
9+
getSnapAlignOffset({
10+
align: 'center',
11+
slideSize: 800,
12+
viewportSize: 800,
13+
})
14+
).toBe(0)
15+
})
16+
17+
test('should return 0 for invalid alignment', () => {
18+
expect(
19+
getSnapAlignOffset({
20+
align: 'invalid' as any,
21+
slideSize: 200,
22+
viewportSize: 800,
23+
})
24+
).toBe(0)
925
})
1026

27+
test('should return 0 when no parameters provided', () => {
28+
expect(getSnapAlignOffset({ align: 'start' })).toBe(0)
29+
})
30+
})
31+
32+
describe('with itemsToShow parameter', () => {
1133
test('should return correct offset for center/center-odd alignment', () => {
1234
expect(getSnapAlignOffset({ align: 'center', itemsToShow: 3 })).toBe(1)
1335
expect(getSnapAlignOffset({ align: 'center-odd', itemsToShow: 5 })).toBe(2)
@@ -20,19 +42,13 @@ describe('getSnapAlignOffset', () => {
2042
test('should return correct offset for end alignment', () => {
2143
expect(getSnapAlignOffset({ align: 'center-even', itemsToShow: 4 })).toBe(1)
2244
})
23-
})
2445

25-
describe('with slideSize and viewportSize parameters', () => {
2646
test('should return correct offset for start alignment', () => {
27-
expect(
28-
getSnapAlignOffset({
29-
align: 'start',
30-
slideSize: 200,
31-
viewportSize: 800,
32-
})
33-
).toBe(0)
47+
expect(getSnapAlignOffset({ align: 'start', itemsToShow: 3 })).toBe(0)
3448
})
49+
})
3550

51+
describe('with slideSize and viewportSize parameters', () => {
3652
test('should return correct offset for center/center-odd alignment', () => {
3753
expect(
3854
getSnapAlignOffset({
@@ -70,31 +86,15 @@ describe('getSnapAlignOffset', () => {
7086
})
7187
).toBe(600)
7288
})
73-
})
74-
75-
describe('edge cases', () => {
76-
test('should return 0 when no parameters provided', () => {
77-
expect(getSnapAlignOffset({ align: 'start' })).toBe(0)
78-
})
7989

80-
test('should return 0 for invalid alignment', () => {
90+
test('should return correct offset for start alignment', () => {
8191
expect(
8292
getSnapAlignOffset({
83-
align: 'invalid' as any,
93+
align: 'start',
8494
slideSize: 200,
8595
viewportSize: 800,
8696
})
8797
).toBe(0)
8898
})
89-
90-
test('should handle equal viewport and slide sizes', () => {
91-
expect(
92-
getSnapAlignOffset({
93-
align: 'center',
94-
slideSize: 800,
95-
viewportSize: 800,
96-
})
97-
).toBe(0)
98-
})
9999
})
100100
})

src/utils/getSnapAlignOffset.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SnapAlign } from '@/shared'
22

3-
interface SnapAlignOffsetParams {
3+
type SnapAlignOffsetParams = {
44
align: SnapAlign
55
slideSize?: number
66
viewportSize?: number

src/utils/i18nFormatter.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { describe, expect, it } from 'vitest'
2+
3+
import { i18nFormatter } from './i18nFormatter'
4+
5+
describe('i18nFormatter', () => {
6+
it('handles empty string input', () => {
7+
const result = i18nFormatter('', { name: 'World' })
8+
expect(result).toBe('')
9+
})
10+
11+
it('handles no values input', () => {
12+
const result = i18nFormatter('Hello, World!')
13+
expect(result).toBe('Hello, World!')
14+
})
15+
16+
it('leaves placeholders without corresponding values unchanged', () => {
17+
const result = i18nFormatter('Hello, {name}!', {})
18+
expect(result).toBe('Hello, {name}!')
19+
})
20+
21+
it('replaces multiple placeholders', () => {
22+
const result = i18nFormatter('Hello, {name}! You have {count} messages.', {
23+
name: 'Alice',
24+
count: 5,
25+
})
26+
expect(result).toBe('Hello, Alice! You have 5 messages.')
27+
})
28+
29+
it('replaces placeholders with corresponding values', () => {
30+
const result = i18nFormatter('Hello, {name}!', { name: 'World' })
31+
expect(result).toBe('Hello, World!')
32+
})
33+
})

0 commit comments

Comments
 (0)