Skip to content

Commit

Permalink
Add basic unit test infrastructure (bkkhack#33)
Browse files Browse the repository at this point in the history
* add basic unit test infrastructure
* add jest config and tests
  • Loading branch information
waf authored Oct 5, 2017
1 parent ff4cd8e commit f69f914
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist/
node_modules/
coverage/
*.log
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: node_js
node_js:
- "node"
cache:
directories:
- "node_modules"
script: npm run continuous-integration
16 changes: 13 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"description": "A tool to help collaborate at bkkhack",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "node build/dev-server.js",
"start": "node build/dev-server.js",
"build": "node build/build.js",
"deploy": "node build/build.js&&push-dir --dir=dist --branch=gh-pages --cleanup",
"deploy": "node build/build.js && push-dir --dir=dist --branch=gh-pages --cleanup",
"test": "jest --watch",
"continuous-integration": "CI=true jest",
"lint": "eslint --ext .js,.vue src"
},
"author": "",
Expand All @@ -21,6 +22,7 @@
"autoprefixer": "^7.1.2",
"babel-core": "^6.22.1",
"babel-eslint": "^7.1.1",
"babel-jest": "^21.2.0",
"babel-loader": "^7.1.1",
"babel-plugin-transform-runtime": "^6.22.0",
"babel-preset-env": "^1.3.2",
Expand All @@ -45,6 +47,8 @@
"friendly-errors-webpack-plugin": "^1.1.3",
"html-webpack-plugin": "^2.28.0",
"http-proxy-middleware": "^0.17.3",
"jest": "^21.2.1",
"jest-vue-preprocessor": "^1.2.0",
"opn": "^5.1.0",
"optimize-css-assets-webpack-plugin": "^2.0.0",
"ora": "^1.2.0",
Expand All @@ -70,5 +74,11 @@
"> 1%",
"last 2 versions",
"not ie <= 8"
]
],
"jest": {
"transform": {
"^.+\\.js$": "<rootDir>/node_modules/babel-jest",
".*\\.(vue)$": "<rootDir>/node_modules/jest-vue-preprocessor"
}
}
}
45 changes: 45 additions & 0 deletions test/components/right.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import Vue from 'vue'
import RightComponent from '../../src/components/Right.vue'

function mount(Component, props) {
const Constructor = Vue.extend(Component)
const component = new Constructor({ propsData: props }).$mount()
return component
}

describe('RightComponent', () => {
let props;

beforeEach(function() {
props = {
selectedProject: {
id: 1,
title: 'Doomsday Device',
description: 'Time to take over the world!',
username: 'Dr_Horrible'
},
username: 'Dr_Horrible'
}
})

it('shows the edit button when the selected project belongs to the logged-in user', () => {
props.selectedProject.username = "Dr_Horrible"
props.username = "Dr_Horrible"

const sidebar = mount(RightComponent, props)

const editButton = sidebar.$el.querySelector(".edit-button")
expect(editButton).not.toBeNull()
})

it('hides the edit button when the selected project does not belong to the logged-in user', () => {
props.selectedProject.username = "Dr_Horrible"
props.username = "Captain_Hammer"

const sidebar = mount(RightComponent, props)

const editButton = sidebar.$el.querySelector(".edit-button")
expect(editButton).toBeNull()
})
})

17 changes: 17 additions & 0 deletions test/github-serialization.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import serialization from '../src/github-serialization.js'

describe('serialization', () => {
it('should deserialize a github comment to a project object', () => {
let comment = {
id: 555,
body: 'doomsday device\r\ntime to take over the world!<!--0.8,0.2-->',
user: {
login: 'Bob',
avatar_url: 'http://www.example.com/bob.jpg'
}
}
let project = serialization.deserializeCommentToProject(comment)
expect(project.title).toEqual('doomsday device')
})
})

0 comments on commit f69f914

Please sign in to comment.