Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/rollup'
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed May 27, 2017
2 parents 0feefe9 + fefaeca commit 5a0e1e4
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 169 deletions.
61 changes: 2 additions & 59 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,60 +1,3 @@
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

node_modules/
.idea/

## File-based project format:
*.iws
*.iml

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

### Node template
# Logs
logs
*.log
npm-debug.log*

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules
jspm_packages

# Optional npm cache directory
.npm

# Optional REPL history
.node_repl_history

dist/
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ before_install:
before_script:
- npm prune
script:
- istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- -R spec
- node build && istanbul cover ./node_modules/.bin/_mocha --report lcovonly -- -R spec
- codecov
after_success:
- 'curl -Lo travis_after_all.py https://git.io/travis_after_all'
Expand Down
39 changes: 39 additions & 0 deletions build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const rollup = require('rollup')
const commonjs = require('rollup-plugin-commonjs')
const babel = require('rollup-plugin-babel')

rollup.rollup({
entry: 'src/index.js',
external: [
'sinon',
'fluent-arguments',
'ramda/src/compose',
'ramda/src/curry',
'ramda/src/filter',
'ramda/src/forEach'
],
plugins: [
commonjs(),
babel({
presets: [
['env', {
modules: false,
targets: {node: 4}
}]
],
plugins: [
'ramda',
'external-helpers'
]
})
]
}).then(bundle => {
bundle.write({
dest: 'dist/index.js',
format: 'cjs'
})
bundle.write({
dest: 'dist/index.mjs',
format: 'es'
})
})
24 changes: 18 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
"description": "Create easily configurable sinon stubs that mimic constructors and keep track of their instances",
"author": "Lukas Taegert <[email protected]>",
"license": "MIT",
"main": "src/index.js",
"main": "dist/index.js",
"module": "dist/index.mjs",
"jsnext:main": "dist/index.mjs",
"files": [
"dist"
],
"config": {
"validate-commit-msg": {
"types": [
Expand Down Expand Up @@ -36,25 +41,32 @@
"sinon": ">=1.15.0"
},
"dependencies": {
"fluent-arguments": ">= 1.0.6",
"ramda": ">= 0.23.0"
"fluent-arguments": "^1.0.7",
"ramda": "^0.23.0"
},
"devDependencies": {
"babel-plugin-external-helpers": "^6.22.0",
"babel-plugin-ramda": "^1.2.0",
"babel-preset-env": "^1.5.1",
"chai": "^4.0.0",
"codecov": "^2.2.0",
"husky": "^0.13.3",
"istanbul": "^0.4.5",
"lint-staged": "^3.5.0",
"mocha": "^3.4.2",
"rollup": "^0.41.6",
"rollup-plugin-babel": "^2.7.1",
"rollup-plugin-commonjs": "^8.0.2",
"rollup-plugin-node-resolve": "^3.0.0",
"semantic-release": "^6.3.6",
"standard": "^10.0.2",
"sinon": "^2.3.2",
"standard": "^10.0.2",
"validate-commit-msg": "^2.12.1"
},
"scripts": {
"test": "mocha",
"test": "node build && mocha",
"lint": "standard",
"semantic-release": "semantic-release pre && npm publish && semantic-release post",
"semantic-release": "npm install && node build && semantic-release pre && npm publish && semantic-release post",
"precommit": "lint-staged",
"commitmsg": "validate-commit-msg",
"prepush": "npm test",
Expand Down
108 changes: 46 additions & 62 deletions src/constructors.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
var R = require('ramda')
var fa = require('fluent-arguments')
import fa from 'fluent-arguments'
import R from 'ramda'

function getArrayFromArrayLikeObject (args) {
return Array.prototype.slice.call(args)
}
const getArrayFromArrayLikeObject = args => Array.prototype.slice.call(args)

var isMethod = R.curry(function (object, propName) {
return !Object.getOwnPropertyDescriptor(object, propName).get &&
typeof object[propName] === 'function' && !(propName === 'constructor')
})
const isMethod = R.curry((object, propName) =>
!Object.getOwnPropertyDescriptor(object, propName).get &&
typeof object[propName] === 'function' &&
!(propName === 'constructor'))

var applyToEachFunctionKeyInObject = function (appliedFunction, object) {
R.compose(
R.forEach(appliedFunction),
R.filter(isMethod(object))
)(Object.getOwnPropertyNames(object))
}
const applyToEachFunctionKeyInObject = (appliedFunction, object) => R.compose(
R.forEach(appliedFunction),
R.filter(isMethod(object))
)(Object.getOwnPropertyNames(object))

var applyToEachFunctionKeyInPrototypeChain = function (appliedFunction, object) {
const applyToEachFunctionKeyInPrototypeChain = (appliedFunction, object) => {
if (object) {
applyToEachFunctionKeyInObject(appliedFunction, object)
applyToEachFunctionKeyInPrototypeChain(appliedFunction, Object.getPrototypeOf(object))
}
}

var getInstanceIndexWithValidation = function (index, numInstances) {
var instanceIndex = index || 0
const getInstanceIndexWithValidation = (index, numInstances) => {
const instanceIndex = index || 0

if (typeof index === 'undefined') {
if (numInstances > 1) {
Expand All @@ -40,56 +36,44 @@ var getInstanceIndexWithValidation = function (index, numInstances) {
return instanceIndex
}

module.exports = function getStubOrSpyConstructor (getConstructorProperties) {
return function (Target) {
var constructorProps = getConstructorProperties(Target)
var instances = []
var instanceArgs = []
var methodParams = []
var afterCreation

function StubOrSpyConstructor () {
constructorProps.SourceConstructor.apply(this, arguments)
instanceArgs.push(getArrayFromArrayLikeObject(arguments))
instances.push(this)

Target && applyToEachFunctionKeyInPrototypeChain(
constructorProps.processMethodOfInstance(this), constructorProps.getInstanceMethodNameSource(this))
methodParams.forEach(constructorProps.configureMethodOfInstance(this))
afterCreation && afterCreation(this)
}

StubOrSpyConstructor.prototype = constructorProps.SourceConstructor.prototype
export default getConstructorProperties => Target => {
const constructorProps = getConstructorProperties(Target)
const instances = []
const instanceArgs = []
let methodParams = []
let afterCreation

function StubOrSpyConstructor () {
constructorProps.SourceConstructor.apply(this, arguments)
instanceArgs.push(getArrayFromArrayLikeObject(arguments))
instances.push(this)

Target && applyToEachFunctionKeyInPrototypeChain(
constructorProps.processMethodOfInstance(this), constructorProps.getInstanceMethodNameSource(this))
methodParams.forEach(constructorProps.configureMethodOfInstance(this))
afterCreation && afterCreation(this)
}

function configureMethods (methods) {
methodParams = methods
return this
}
StubOrSpyConstructor.prototype = constructorProps.SourceConstructor.prototype

StubOrSpyConstructor[constructorProps.configureMethodsKey] = fa.createFunc(configureMethods)
StubOrSpyConstructor[constructorProps.configureMethodsKey] = fa.createFunc(function (methods) {
methodParams = methods
return this
})

StubOrSpyConstructor.afterCreation = function (onAfterCreation) {
afterCreation = onAfterCreation
return this
}
StubOrSpyConstructor.afterCreation = function (onAfterCreation) {
afterCreation = onAfterCreation
return this
}

StubOrSpyConstructor.getInstances = function () {
return instances
}
StubOrSpyConstructor.getInstances = () => instances

StubOrSpyConstructor.getInstance = function (index) {
return instances[getInstanceIndexWithValidation(index, instances.length)]
}
StubOrSpyConstructor.getInstance = index => instances[getInstanceIndexWithValidation(index, instances.length)]

StubOrSpyConstructor.getInstancesArgs = function () {
return instanceArgs
}
StubOrSpyConstructor.getInstancesArgs = () => instanceArgs

StubOrSpyConstructor.getInstanceArgs = function (index) {
return instanceArgs[getInstanceIndexWithValidation(index, instances.length)]
}
StubOrSpyConstructor.getInstanceArgs = index => instanceArgs[getInstanceIndexWithValidation(index, instances.length)]

Target && applyToEachFunctionKeyInObject(constructorProps.processMethodOfConstructor(StubOrSpyConstructor), Target)
return StubOrSpyConstructor
}
Target && applyToEachFunctionKeyInObject(constructorProps.processMethodOfConstructor(StubOrSpyConstructor), Target)
return StubOrSpyConstructor
}
Loading

0 comments on commit 5a0e1e4

Please sign in to comment.