Skip to content

Commit 93f527e

Browse files
committed
added tests
- setup jest - implemented general tests - tested non-direction specific cases
1 parent 7c005a4 commit 93f527e

File tree

9 files changed

+3556
-75
lines changed

9 files changed

+3556
-75
lines changed

.npmignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ src
55
rollup.config.js
66

77
# tests
8-
test
9-
coverage
8+
tests
9+
coverage
10+
jest.config.js

jest.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
"roots": [
3+
"<rootDir>/tests"
4+
]
5+
}

lib/vue-animate-onscroll.cjs.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/vue-animate-onscroll.es.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"scripts": {
99
"clean": "rimraf lib",
1010
"build": "npm run clean && rollup --config rollup.config.js",
11-
"deploy": "npm build && npm version patch && npm publish"
11+
"test": "npm run build && jest",
12+
"deploy": "npm run test && npm version patch && npm publish"
1213
},
1314
"author": "yev",
1415
"repository": {
@@ -24,6 +25,7 @@
2425
"license": "MIT",
2526
"dependencies": {},
2627
"devDependencies": {
28+
"jest": "^23.6.0",
2729
"rimraf": "^2.6.2",
2830
"rollup": "^0.66.4",
2931
"rollup-plugin-terser": "^3.0.0"

src/scroll-animate.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ export default () => {
1212
const applyAnimationClass = (el, current, newClass) => el.className = `${current} ${(newClass || '')}`
1313

1414
return {
15+
isInView: isInScrollView,
1516
run(el, {value: params, modifiers}, {isUpwards, currentClassName}) {
1617

17-
if(!isInScrollView(el.getBoundingClientRect())) {
18+
if(!this.isInView(el.getBoundingClientRect())) {
1819
if (modifiers.repeat && isDirectionAgnostic(params)) {
1920
return applyAnimationClass(el, currentClassName)
2021
}

tests/scroll-animate.test.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const { ScrollAnimate } = require('../lib/vue-animate-onscroll.cjs')
2+
const createFixture = require('./utils')
3+
4+
describe('ScrollAnimate', function() {
5+
let scrollAnimate = null
6+
7+
beforeEach(() => scrollAnimate = ScrollAnimate() )
8+
9+
describe('General tests', () => {
10+
test('should not remove existing classes', () => {
11+
const currentClassName = 'default'
12+
const animationClass = 'flip'
13+
const fixture = createFixture(scrollAnimate, {
14+
currentClassName,
15+
animationClass,
16+
isUpwards: true
17+
})
18+
fixture.run(true)
19+
expect(fixture.getClassName()).not.toBeNull()
20+
expect(fixture.getClassNames()).toContain(currentClassName)
21+
})
22+
})
23+
24+
describe('Non-direction specific', () => {
25+
26+
const animationClass = 'flip'
27+
28+
test('should add animation classes', () => {
29+
const fixture = createFixture(scrollAnimate, {
30+
animationClass,
31+
isUpwards: false
32+
})
33+
fixture.run(true)
34+
expect(fixture.getClassName()).toEqual(animationClass)
35+
})
36+
37+
test('should toggle animation when repeat is on', () => {
38+
const fixture = createFixture(scrollAnimate, {
39+
animationClass,
40+
isUpwards: false,
41+
repeat: true
42+
})
43+
fixture.run(true)
44+
expect(fixture.getClassName()).toEqual(animationClass)
45+
fixture.run(false)
46+
expect(fixture.getClassName()).toBeFalsy()
47+
fixture.run(true)
48+
expect(fixture.getClassName()).toEqual(animationClass)
49+
})
50+
51+
test('should not toggle animation when repeat is off', () => {
52+
const fixture = createFixture(scrollAnimate, {
53+
animationClass,
54+
isUpwards: false,
55+
repeat: false
56+
})
57+
fixture.run(true)
58+
expect(fixture.getClassName()).toEqual(animationClass)
59+
fixture.run(false)
60+
expect(fixture.getClassName()).toEqual(animationClass)
61+
fixture.run(true)
62+
expect(fixture.getClassName()).toEqual(animationClass)
63+
fixture.run(false)
64+
fixture.run(true)
65+
fixture.run(false)
66+
expect(fixture.getClassName()).toEqual(animationClass)
67+
})
68+
69+
})
70+
71+
})

tests/utils.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const createFixture = (instance, { currentClassName = '',
2+
animationClass,
3+
isUpwards,
4+
repeat = false }) => {
5+
return {
6+
el: {
7+
className: currentClassName,
8+
getBoundingClientRect(){}
9+
},
10+
binding: {
11+
value: animationClass,
12+
modifiers: {repeat}
13+
},
14+
state: { isUpwards, currentClassName },
15+
getClassName: function() { return this.el.className.trim() },
16+
getClassNames: function() { return this.getClassName().split(' ') },
17+
run: function(isInView) {
18+
instance.isInView = () => isInView
19+
instance.run(this.el, this.binding, this.state)
20+
}
21+
}
22+
}
23+
24+
module.exports = createFixture

0 commit comments

Comments
 (0)