Skip to content
This repository has been archived by the owner on May 24, 2021. It is now read-only.

Commit

Permalink
chore(tests): Increase test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-heimbuch committed Feb 16, 2018
1 parent f117dc6 commit e68e707
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 17 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
"lint": "npm run lint:standard",
"commit": "git-cz",
"deploy:gh-pages": "scripts/deploy-ghpages.sh dist",
"deploy:version": "scripts/deploy-version.sh",
"deploy:latest": "scripts/deploy-version.sh latest",
"deploy:cdn": "rsync -rvt --chmod=D2755,F644 dist/ podlove@rsync.keycdn.com:applications/web-player/",
"deploy:npm": "npm publish --access public",
"deploy:release": "scripts/deploy-release.sh",
Expand Down
3 changes: 0 additions & 3 deletions scripts/version.js

This file was deleted.

20 changes: 10 additions & 10 deletions src/utils/dom.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { curry, compose, uniq, concat, join, filter, head } from 'lodash/fp'
import { curry, compose, uniq, concat, join, filter, head, identity } from 'lodash/fp'

export const findNode = selector => typeof selector === 'string' ? head(document.querySelectorAll(selector)) : selector
export const createNode = tag => document.createElement(tag)
Expand All @@ -15,34 +15,34 @@ export const tag = curry((tag, value = '', attributes = {}) => {
return `<${tag}${attr}>${value}</${tag}>`
})

export const setStyles = (attrs = {}) => el => {
export const setStyles = curry((attrs = {}, el) => {
Object.keys(attrs).forEach(property => {
el.style[property] = attrs[property]
})

return el
}
})

export const getClasses = el => el.className.split(' ')
export const getClasses = compose(filter(identity), el => el.className.split(' '))

export const hasOverflow = el => el.scrollWidth > el.clientWidth

export const addClasses = (...classes) => el => {
export const addClasses = curry((classes, el) => {
el.className = compose(join(' '), uniq, concat(classes), getClasses)(el)

return el
}
})

export const removeClasses = (...classes) => el => {
export const removeClasses = curry((classes, el) => {
el.className = compose(join(' '), filter(className => !~classes.indexOf(className)), getClasses)(el)

return el
}
})

export const setAttributes = (attrs = {}) => el => {
export const setAttributes = curry((attrs = {}, el) => {
Object.keys(attrs).forEach(property => {
el.setAttribute(property, attrs[property])
})

return el
}
})
49 changes: 47 additions & 2 deletions src/utils/dom.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava'
import sinon from 'sinon'
import browserEnv from 'browser-env'
import { findNode, createNode, appendNode, tag } from './dom'
import { findNode, createNode, appendNode, tag, setStyles, addClasses, getClasses } from './dom'

browserEnv()

Expand All @@ -26,9 +26,30 @@ test('exports a method called tag', t => {
t.truthy(typeof tag === 'function')
})

test('exports a method called setStyles', t => {
t.truthy(typeof setStyles === 'function')
})

test('exports a method called getClasses', t => {
t.truthy(typeof getClasses === 'function')
})

test('exports a method called addClasses', t => {
t.truthy(typeof addClasses === 'function')
})

test('findNode should call the document api', t => {
findNode('body')
const result = findNode('body')

t.is(document.querySelectorAll.getCall(0).args[0], 'body')
t.deepEqual(result, document.body)
})

test(`findNode returns a given dom node`, t => {
const testNode = document.createElement('p')
const result = findNode(testNode)

t.deepEqual(testNode, result)
})

test('createNode should call the document api', t => {
Expand All @@ -48,3 +69,27 @@ test('tag should create a html tag', t => {
t.is(tag('p', 'foo'), '<p>foo</p>')
t.is(tag('div', 'foo', {bar: 'baz', bla: 'blub'}), '<div bar="baz" bla="blub">foo</div>')
})

test('setStyles adds styles to a dom node', t => {
const testNode = createNode('div')

setStyles({ color: 'red', width: '200px' }, testNode)

t.is(testNode.style.color, 'red')
t.is(testNode.style.width, '200px')
})

test(`getClasses should return the class names`, t => {
const testNode = createNode('div')

addClasses(['foo', 'bar'], testNode)

t.deepEqual(getClasses(testNode), ['foo', 'bar'])
})

test(`addClasses should add classes to dom elements`, t => {
const testNode = createNode('div')
addClasses(['foo', 'bar'], testNode)

t.is(testNode.className, 'foo bar')
})
39 changes: 39 additions & 0 deletions src/utils/request.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import test from 'ava'
import superagent from 'superagent'
import nocker from 'superagent-nock'

import request from './request'

let nock

test.beforeEach(t => {
nock = nocker(superagent)
})

test(`request: exports a function`, t => {
t.is(typeof request, 'function')
})

test.cb(`request: should resolve an url`, t => {
t.plan(1)

nock('http://localhost')
.get('/foo')
.reply(200, { foo: 'bar' })

request('http://localhost/foo')
.then(result => {
t.deepEqual(result, { foo: 'bar' })
t.end()
})
})

test.cb(`request: should return an object`, t => {
t.plan(1)

request({ foo: 'bar' })
.then(result => {
t.deepEqual(result, { foo: 'bar' })
t.end()
})
})

0 comments on commit e68e707

Please sign in to comment.