From c50e64f8eecd780246e3ac47c2a54164cfc28b8f Mon Sep 17 00:00:00 2001 From: shpuld Date: Tue, 26 Mar 2019 22:11:45 +0200 Subject: Add tests for gesture service, fix bug with perpendicular directions --- .../gesture_service/gesture_service.spec.js | 120 +++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 test/unit/specs/services/gesture_service/gesture_service.spec.js (limited to 'test/unit/specs/services/gesture_service/gesture_service.spec.js') diff --git a/test/unit/specs/services/gesture_service/gesture_service.spec.js b/test/unit/specs/services/gesture_service/gesture_service.spec.js new file mode 100644 index 00000000..4a1b009a --- /dev/null +++ b/test/unit/specs/services/gesture_service/gesture_service.spec.js @@ -0,0 +1,120 @@ +import GestureService from 'src/services/gesture_service/gesture_service.js' + +const mockTouchEvent = (x, y) => ({ + touches: [ + { + screenX: x, + screenY: y + } + ] +}) + +describe.only('GestureService', () => { + describe('swipeGesture', () => { + it('calls the callback on a successful swipe', () => { + let swiped = false + const callback = () => { swiped = true } + const gesture = GestureService.swipeGesture( + GestureService.DIRECTION_RIGHT, + callback + ) + + GestureService.beginSwipe(mockTouchEvent(100, 100), gesture) + GestureService.updateSwipe(mockTouchEvent(200, 100), gesture) + + expect(swiped).to.eql(true) + }) + + it('calls the callback only once per begin', () => { + let hits = 0 + const callback = () => { hits += 1 } + const gesture = GestureService.swipeGesture( + GestureService.DIRECTION_RIGHT, + callback + ) + + GestureService.beginSwipe(mockTouchEvent(100, 100), gesture) + GestureService.updateSwipe(mockTouchEvent(150, 100), gesture) + GestureService.updateSwipe(mockTouchEvent(200, 100), gesture) + + expect(hits).to.eql(1) + }) + + it('doesn\'t call the callback on an opposite swipe', () => { + let swiped = false + const callback = () => { swiped = true } + const gesture = GestureService.swipeGesture( + GestureService.DIRECTION_RIGHT, + callback + ) + + GestureService.beginSwipe(mockTouchEvent(100, 100), gesture) + GestureService.updateSwipe(mockTouchEvent(0, 100), gesture) + + expect(swiped).to.eql(false) + }) + + it('doesn\'t call the callback on a swipe below threshold', () => { + let swiped = false + const callback = () => { swiped = true } + const gesture = GestureService.swipeGesture( + GestureService.DIRECTION_RIGHT, + callback, + 100 + ) + + GestureService.beginSwipe(mockTouchEvent(100, 100), gesture) + GestureService.updateSwipe(mockTouchEvent(150, 100), gesture) + + expect(swiped).to.eql(false) + }) + + it('doesn\'t call the callback on a perpendicular swipe', () => { + let swiped = false + const callback = () => { swiped = true } + const gesture = GestureService.swipeGesture( + GestureService.DIRECTION_RIGHT, + callback, + 30, + 0.5 + ) + + GestureService.beginSwipe(mockTouchEvent(100, 100), gesture) + GestureService.updateSwipe(mockTouchEvent(150, 200), gesture) + + expect(swiped).to.eql(false) + }) + + it('calls the callback on perpendicular swipe if within tolerance', () => { + let swiped = false + const callback = () => { swiped = true } + const gesture = GestureService.swipeGesture( + GestureService.DIRECTION_RIGHT, + callback, + 30, + 2.0 + ) + + GestureService.beginSwipe(mockTouchEvent(100, 100), gesture) + GestureService.updateSwipe(mockTouchEvent(150, 150), gesture) + + expect(swiped).to.eql(true) + }) + + it('works with any arbitrary 2d directions', () => { + let swiped = false + const callback = () => { swiped = true } + const gesture = GestureService.swipeGesture( + [-1, -1], + callback, + 30, + 0.1 + ) + + GestureService.beginSwipe(mockTouchEvent(100, 100), gesture) + GestureService.updateSwipe(mockTouchEvent(60, 60), gesture) + + expect(swiped).to.eql(true) + }) + }) +}) -- cgit v1.2.3-70-g09d2 From fd171da0dc6ee8404278ba4083a28cc01d724040 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Sat, 13 Apr 2019 18:28:50 +0300 Subject: attempt at making it work --- src/i18n/compare | 49 ++++++++++++++++++++++ src/i18n/compare.js | 49 ---------------------- test/unit/index.js | 4 +- .../gesture_service/gesture_service.spec.js | 2 +- 4 files changed, 52 insertions(+), 52 deletions(-) create mode 100755 src/i18n/compare delete mode 100755 src/i18n/compare.js (limited to 'test/unit/specs/services/gesture_service/gesture_service.spec.js') diff --git a/src/i18n/compare b/src/i18n/compare new file mode 100755 index 00000000..69cbe112 --- /dev/null +++ b/src/i18n/compare @@ -0,0 +1,49 @@ +//#!/usr/bin/env node +const arg = process.argv[2] + +if (typeof arg === 'undefined') { + console.log('This is a very simple and tiny tool that checks en.json with any other language and') + console.log('outputs all the things present in english but missing in foreign language.') + console.log('') + console.log('Usage: ./compare.js ') + console.log(' or') + console.log(' node ./compare.js ') + console.log('') + console.log('Where is name of .json file containing language. For ./fi.json it should be:') + console.log(' ./compare.js fi ') + console.log('') + console.log('Limitations: ') + console.log('* This program does not work with languages left over in messages.js') + console.log('* This program does not check for extra strings present in foreign language but missing') + console.log(' in english.js (for now)') + console.log('') + console.log('There are no other arguments or options. Make an issue if you encounter a bug or want') + console.log('some feature to be implemented. Merge requests are welcome as well.') + process.exit() +} + +const english = require('./en.json') +const foreign = require(`./${arg}.json`) + +function walker (a, b, path = []) { + Object.keys(a).forEach(k => { + const aVal = a[k] + const bVal = b[k] + const aType = typeof aVal + const bType = typeof bVal + const currentPath = [...path, k] + const article = aType[0] === 'o' ? 'an' : 'a' + + if (bType === 'undefined') { + console.log(`Foreign language is missing ${article} ${aType} at path ${currentPath.join('.')}`) + } else if (aType === 'object') { + if (bType !== 'object') { + console.log(`Type mismatch! English has ${aType} while foreign has ${bType} at path ${currentPath.join['.']}`) + } else { + walker(aVal, bVal, currentPath) + } + } + }) +} + +walker(english, foreign) diff --git a/src/i18n/compare.js b/src/i18n/compare.js deleted file mode 100755 index e9314376..00000000 --- a/src/i18n/compare.js +++ /dev/null @@ -1,49 +0,0 @@ -#!/usr/bin/env node -const arg = process.argv[2] - -if (typeof arg === 'undefined') { - console.log('This is a very simple and tiny tool that checks en.json with any other language and') - console.log('outputs all the things present in english but missing in foreign language.') - console.log('') - console.log('Usage: ./compare.js ') - console.log(' or') - console.log(' node ./compare.js ') - console.log('') - console.log('Where is name of .json file containing language. For ./fi.json it should be:') - console.log(' ./compare.js fi ') - console.log('') - console.log('Limitations: ') - console.log('* This program does not work with languages left over in messages.js') - console.log('* This program does not check for extra strings present in foreign language but missing') - console.log(' in english.js (for now)') - console.log('') - console.log('There are no other arguments or options. Make an issue if you encounter a bug or want') - console.log('some feature to be implemented. Merge requests are welcome as well.') - return -} - -const english = require('./en.json') -const foreign = require(`./${arg}.json`) - -function walker (a, b, path = []) { - Object.keys(a).forEach(k => { - const aVal = a[k] - const bVal = b[k] - const aType = typeof aVal - const bType = typeof bVal - const currentPath = [...path, k] - const article = aType[0] === 'o' ? 'an' : 'a' - - if (bType === 'undefined') { - console.log(`Foreign language is missing ${article} ${aType} at path ${currentPath.join('.')}`) - } else if (aType === 'object') { - if (bType !== 'object') { - console.log(`Type mismatch! English has ${aType} while foreign has ${bType} at path ${currentPath.join['.']}`) - } else { - walker(aVal, bVal, currentPath) - } - } - }) -} - -walker(english, foreign) diff --git a/test/unit/index.js b/test/unit/index.js index 03b19e32..642099db 100644 --- a/test/unit/index.js +++ b/test/unit/index.js @@ -9,5 +9,5 @@ testsContext.keys().forEach(testsContext) // require all src files except main.js for coverage. // you can also change this to match only the subset of files that // you want coverage for. -const srcContext = require.context('../../src', true, /^\.\/(?!main(\.js)?$)/) -srcContext.keys().forEach(srcContext) +// const srcContext = require.context('../../src', true, /^\.\/(?!main(\.js)?$)/) +// srcContext.keys().forEach(srcContext) diff --git a/test/unit/specs/services/gesture_service/gesture_service.spec.js b/test/unit/specs/services/gesture_service/gesture_service.spec.js index 4a1b009a..a91f95db 100644 --- a/test/unit/specs/services/gesture_service/gesture_service.spec.js +++ b/test/unit/specs/services/gesture_service/gesture_service.spec.js @@ -9,7 +9,7 @@ const mockTouchEvent = (x, y) => ({ ] }) -describe.only('GestureService', () => { +describe('GestureService', () => { describe('swipeGesture', () => { it('calls the callback on a successful swipe', () => { let swiped = false -- cgit v1.2.3-70-g09d2