Skip to content

Commit

Permalink
unit tests on Delegator and observe
Browse files Browse the repository at this point in the history
  • Loading branch information
frankthelen committed Jan 6, 2018
1 parent adebc4a commit ca59678
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,11 @@ This is a small rule engine for Node.
[![License Status](http://img.shields.io/npm/l/rools.svg)]()

*Primary goal* was to provide a nice and state-of-the-art interface for modern JavaScript (ES6).
Facts are plain JavaScript or JSON objects or objects from ES6 classes with getters and setters.
Rules are specified in pure JavaScript rather than in a separate, special-purpose language like DSL.
*Facts* are plain JavaScript or JSON objects or objects from ES6 classes with getters and setters.
*Rules* are specified in pure JavaScript rather than in a separate, special-purpose language like DSL.

*Secondary goal* was to provide [RETE](https://en.wikipedia.org/wiki/Rete_algorithm)-like efficiency and optimization.

*1.0.0-beta* is feature-complete and stable.

## Install

```bash
Expand Down Expand Up @@ -93,7 +91,7 @@ Both are JavaScript functions, i.e., classic functions or ES6 arrow functions.
Actions can also be asynchronous.

Rules access the facts in both, premises (`when`) and actions (`then`).
The can access properties directly, e.g., `facts.user.salery`,
They can access properties directly, e.g., `facts.user.salery`,
or through getters and getters if applicable, e.g., `facts.user.getSalery()`.

### Conflict resolution
Expand Down Expand Up @@ -362,7 +360,9 @@ This can be configured like this.
const delegate = ({ message, rule, error }) => {
console.error(message, rule, error);
};
const rools = new Rools({ logging: { error: true, debug: false, delegate } });
const rools = new Rools({
logging: { error: true, debug: false, delegate },
});
...
```

Expand All @@ -373,4 +373,3 @@ Some of the features for future releases are:
* Activation groups
* Agenda groups
* Extend rules
* More unit tests
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"rule",
"engine",
"rools",
"rule engine",
"rules engine"
],
"scripts": {
Expand Down
21 changes: 21 additions & 0 deletions test/delegate.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const Delegator = require('../src/Delegator');
require('./setup');

describe('Delegator', () => {
it('should delegate call', () => {
const delegator = new Delegator();
const spy = sinon.spy();
delegator.set(spy);
delegator.delegate('bla');
expect(spy.calledWith('bla')).to.be.equal(true);
});

it('should not delegate call if unset', () => {
const delegator = new Delegator();
const spy = sinon.spy();
delegator.set(spy);
delegator.unset();
delegator.delegate('bla');
expect(spy.called).to.be.equal(false);
});
});
55 changes: 55 additions & 0 deletions test/observe.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const observe = require('../src/observe');
require('./setup');

const object = {
prop: true,
sub: {
subsub: {
bla: true,
},
},
};

describe('observe', () => {
it('should notify on reading property', () => {
const spy = sinon.spy();
const proxy = observe(object, spy);
const temp = proxy.prop; // eslint-disable-line no-unused-vars
expect(spy.calledWith('prop')).to.be.equal(true);
});

it('should notify on writing property', () => {
const spy = sinon.spy();
const proxy = observe(object, spy);
proxy.prop = false;
expect(spy.calledWith('prop')).to.be.equal(true);
});

it('should notify on deleting property', () => {
const spy = sinon.spy();
const proxy = observe(object, spy);
delete proxy.prop;
expect(spy.calledWith('prop')).to.be.equal(true);
});

it('should notify on reading sub-property', () => {
const spy = sinon.spy();
const proxy = observe(object, spy);
const temp = proxy.sub.subsub.bla; // eslint-disable-line no-unused-vars
expect(spy.calledWith('sub')).to.be.equal(true);
});

it('should notify on writing sub-property', () => {
const spy = sinon.spy();
const proxy = observe(object, spy);
proxy.sub.subsub.bla = false;
expect(spy.calledWith('sub')).to.be.equal(true);
});

it('should notify on deleting sub-property', () => {
const spy = sinon.spy();
const proxy = observe(object, spy);
delete proxy.sub.subsub.bla;
expect(spy.calledWith('sub')).to.be.equal(true);
});
});

0 comments on commit ca59678

Please sign in to comment.