Skip to content

Commit

Permalink
Change VectorBounds test and fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
David Cetinkaya committed Mar 29, 2019
1 parent 2f88b03 commit 1aaea9c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 21 deletions.
41 changes: 27 additions & 14 deletions src/__tests__/vectorBounds.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Animation } from '../components/animation'
import { VectorBounds } from '../components/vectorBounds'
import { Vector1D } from '../components/vector1d'
import { Limit } from '../components/limit'
Expand All @@ -6,8 +7,8 @@ import { Mover } from '../components/mover'
let vectorBounds: VectorBounds
let location: Vector1D
let mover: Mover
const animation = Animation(() => {})
const limit = Limit({ low: 0, high: 10 })
const offset = 5

beforeEach(() => {
location = Vector1D(0)
Expand All @@ -17,31 +18,43 @@ beforeEach(() => {
maxForce: 100,
speed: 10,
})
vectorBounds = VectorBounds({ limit, location, mover, offset })
vectorBounds = VectorBounds({ limit, location, mover, animation })
})

describe('VectorBounds', () => {
test('Constrains given Vector to Bound low when location is less than low', () => {
test('Constrains given Vector to Bound low when location is less than low', done => {
const vector = Vector1D(-20)
const locationPastLowLimitPlusOffset = limit.low - offset - 1
location.setNumber(locationPastLowLimitPlusOffset)
const locationPastLowLimit = limit.low - 1
location.setNumber(locationPastLowLimit)
vectorBounds.constrain(vector)
expect(vector.get()).toBe(0)

setTimeout(() => {
expect(vector.get()).toBe(0)
done()
}, 50)
})

test('Constrains given Vector to Bound high when location is greater than high', () => {
test('Constrains given Vector to Bound high when location is greater than high', done => {
const vector = Vector1D(20)
const locationPastHighLimitPlusOffset = limit.high + offset + 1
location.setNumber(locationPastHighLimitPlusOffset)
const locationPastHighLimit = limit.high + 1
location.setNumber(locationPastHighLimit)
vectorBounds.constrain(vector)
expect(vector.get()).toBe(10)

setTimeout(() => {
expect(vector.get()).toBe(10)
done()
}, 50)
})

test('Does not change given Vector when location is within Bounds', () => {
test('Does not change given Vector when location is within Bounds', done => {
const vector = Vector1D(20)
const locationWithinHighLimitPlusOffset = limit.high + offset
location.setNumber(locationWithinHighLimitPlusOffset)
const locationWithinHighLimit = limit.high
location.setNumber(locationWithinHighLimit)
vectorBounds.constrain(vector)
expect(vector.get()).toBe(20)

setTimeout(() => {
expect(vector.get()).toBe(20)
done()
}, 50)
})
})
4 changes: 2 additions & 2 deletions src/components/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,20 @@ export function Engine(
location,
loop: options.loop,
mover,
onSelect,
pointer: Pointer(chunkSize),
target,
travel,
onSelect,
})

// Slider
const slider = {
animation,
bounds: VectorBounds({
animation,
limit,
location,
mover,
animation,
}),
index,
infinite: VectorLooper({
Expand Down
10 changes: 5 additions & 5 deletions src/components/vectorBounds.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Animation } from './animation'
import { Limit } from './limit'
import { Mover } from './mover'
import { Vector1D } from './vector1d'
import { Animation } from './animation'

interface Params {
limit: Limit
Expand All @@ -21,11 +21,11 @@ export function VectorBounds(params: Params): VectorBounds {

function shouldConstrain(v: Vector1D): boolean {
const l = location.get()
const isNotLowLimit = v.get() !== limit.low
const isNotHighLimit = v.get() !== limit.high
const isLowLimit = v.get() === limit.low
const isHighLimit = v.get() === limit.high
return (
(limit.reached.low(l) && isNotLowLimit) ||
(limit.reached.high(l) && isNotHighLimit)
(limit.reached.low(l) && !isLowLimit) ||
(limit.reached.high(l) && !isHighLimit)
)
}

Expand Down

0 comments on commit 1aaea9c

Please sign in to comment.