Skip to content

Commit

Permalink
helpers tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Granipouss committed Dec 24, 2016
1 parent 51aace4 commit 2275fd6
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 1 deletion.
88 changes: 88 additions & 0 deletions tests/unit/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/* global describe it expect */
const { isArray, isString, isObject, getKey, createRecord, indexForKey } = require('../../src/helpers')

function random (n) {
return 0 | (Math.random() * n) + 1
}

function makeSnap (key, val) {
return {
key () { return key },
val () { return val }
}
}

describe('Type helpers', () => {
it('isArray', () => {
const A = []
const B = [0, 1, 2]
const C = '0 1 2'
expect(isArray(A)).toBe(true)
expect(isArray(B)).toBe(true)
expect(isArray(C)).toBe(false)
})

it('isString', () => {
const A = ''
const B = '0 1 2'
const C = [0, 1, 2]
expect(isString(A)).toBe(true)
expect(isString(B)).toBe(true)
expect(isString(C)).toBe(false)
})

it('isObject', () => {
const A = {}
const B = { foo: 'bar' }
const C = 'foobar'
expect(isObject(A)).toBe(true)
expect(isObject(B)).toBe(true)
expect(isObject(C)).toBe(false)
})
})

describe('Helpers', () => {
it('getKey', () => {
const key = random(100)
const A = { key }
const B = { key () { return key } }
expect(getKey(A)).toBe(key)
expect(getKey(B)).toBe(key)
})

it('createRecord simple', () => {
const key = '-' + random(100)
const val = random(100)
const snapshot = makeSnap(key, val)
const record = createRecord(snapshot)
expect(record).toEqual({
'.key': key,
'.value': val
})
})

it('createRecord object', () => {
const key = '-' + random(100)
const val = random(100)
const snapshot = makeSnap(key, { val, foo: 'bar' })
const record = createRecord(snapshot)
expect(record).toEqual({
val,
foo: 'bar',
'.key': key
})
})

it('indexForKey', () => {
const list = []
const len = random(20)
for (let i = 0; i < len; i++) {
list.push(createRecord(makeSnap(random(100), random(10))))
}
const index = random(len) - 1
const record = list[index]
const key = record['.key']
expect(indexForKey(list, key)).toBe(index)
expect(indexForKey(list, 'toto')).toBe(-1)
})
})
2 changes: 1 addition & 1 deletion tests/unit/jasmine.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"spec_dir": "test/unit",
"spec_dir": "tests/unit",
"spec_files": [
"**/*.spec.js"
]
Expand Down

0 comments on commit 2275fd6

Please sign in to comment.