Skip to content

Commit

Permalink
add methods
Browse files Browse the repository at this point in the history
  • Loading branch information
arielpchara committed Dec 19, 2019
1 parent 284f0b3 commit 5471309
Showing 1 changed file with 44 additions and 21 deletions.
65 changes: 44 additions & 21 deletions tdd/README.md
Expand Up @@ -12,7 +12,6 @@ Writing the code of how it will be called
Like this:
```js
// beautifulness.spec.js

const { createBeauty, theNameOf, withRealJewels, inCaseOfFireCall } = require('./beautifulness')

const helpMe = jest.fn()
Expand All @@ -21,7 +20,8 @@ const helpMe = jest.fn()
const {
sayMyName,
hasRealJewels,
setFire
setFire,
doSomething
} = createBeauty(
// pass parameters an easy way
theNameOf('Precious'),
Expand All @@ -40,51 +40,74 @@ it('Should call helpMe method when set fire', () => {
setFire('fire')
expect(helpMe).toBeCalledWith('fire')
})
it('Should call helpMe method when doSomething', () => {
doSomething()
expect(helpMe).toBeCalledWith('fire')
})
```

Probably all these tests have gone fail, of course, I don't write the module yet.

## Writing the module or my real code

```js
// beautifulness.js

function compose(...fns) {
return fns.reduce((composed, fn) => {
return fn(composed)
}, {})
}

function sayMyName(beauty) {
return () => `My Name is ${beauty.name}`
// INPUTS

// Add name key in config factory
function theNameOf(name) {
return (config) => ({...config, name })
}

// Add hasJewel key in config factory
const withRealJewels = (config) => ({...config, hasJewel: true})

// Add event errorHandler in config factory
function inCaseOfFireCall(errorHandler) {
return (config) => ({...config, errorHandler })
}

function hasRealJewels(beauty) {
return () => beauty.hasJewel
// OUTPUTS
// Get name property
function sayMyName(properties) {
return () => `My Name is ${properties.name}`
}
// Get hasJewel property
function hasRealJewels(properties) {
return () => properties.hasJewel
}
// Run errorHandler event
function setFire(properties) {
return (err) => properties.errorHandler(err)
}

function setFire(beauty) {
return (err) => beauty.errorHandler(err)
function doSomething(properties) {
return (...args) => {
try {
get(...args)
} catch (err) {
properties.errorHandler(err)
}
}
}


// FACTORY
function createBeauty(...configs) {
return {
sayMyName: compose(...configs, sayMyName),
hasRealJewels: compose(...configs, hasRealJewels),
setFire: compose(...configs, setFire)
setFire: compose(...configs, setFire),
doSomething: compose(...configs, doSomething)
}
}

function theNameOf(name) {
return (config) => ({...config, name })
}

const withRealJewels = (config) => ({...config, hasJewel: true})

function inCaseOfFireCall(errorHandler) {
return (config) => ({...config, errorHandler })
}

// EXPORT FUNCTION
module.exports = {
createBeauty,
theNameOf,
Expand Down

0 comments on commit 5471309

Please sign in to comment.