Skip to content

Commit

Permalink
feat(operators): add sumGreaterThan and functional test for ecommerce…
Browse files Browse the repository at this point in the history
… cases
  • Loading branch information
yuxblank committed Nov 1, 2020
1 parent e28cb7e commit c445df4
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 4 deletions.
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ These steps will guide you through contributing to this project:
- Fork the repo
- Clone it and install dependencies

git clone https://github.com/YOUR-USERNAME/typescript-library-starter
git clone https://github.com/YOUR-USERNAME/rulerx
npm install

Keep in mind that after running `npm install` the git repo is reset. So a good way to cope with this is to have a copy of the folder to push the changes, and the other to try them.

Make and commit your changes. Make sure the commands npm run build and npm run test:prod are working.

Finally send a [GitHub Pull Request](https://github.com/alexjoverm/typescript-library-starter/compare?expand=1) with a clear list of what you've done (read more [about pull requests](https://help.github.com/articles/about-pull-requests/)). Make sure all of your commits are atomic (one feature per commit).
Finally send a [GitHub Pull Request](https://github.com/yuxblank/rulerx/compare?expand=1) with a clear list of what you've done (read more [about pull requests](https://help.github.com/articles/about-pull-requests/)). Make sure all of your commits are atomic (one feature per commit).
2 changes: 1 addition & 1 deletion code-of-conduct.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ further defined and clarified by project maintainers.
## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported by contacting the project team at alexjovermorales@gmail.com. All
reported by contacting the project team at yuxblank@gmail.com. All
complaints will be reviewed and investigated and will result in a response that
is deemed necessary and appropriate to the circumstances. The project team is
obligated to maintain confidentiality with regard to the reporter of an incident.
Expand Down
4 changes: 4 additions & 0 deletions src/rule/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ export function noneOfList(object: any[], value: any[]): boolean {
).length === object.length
)
}

export function sumGreaterThan(object: any[], value: any[]): boolean{
return object.reduce((a, b) => a + b, 0) > value;
}
2 changes: 1 addition & 1 deletion src/rule/rule-rx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class RuleRx<T> implements RuleEvaluator<T> {
rules: evaluateRule
})
}
})
0})
}

if (rules.any) {
Expand Down
98 changes: 98 additions & 0 deletions test/rulerx.functional.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import {RuleRx} from '../src/rule/rule-rx'
import {sumGreaterThan} from '../src/rule/operators'
import {BehaviorSubject} from 'rxjs'
import {finalize, mergeAll, tap} from 'rxjs/operators'

interface Basket {
products: Product[]
customer: {name:string}
discount?: number;
}
interface Product {
name: string;
price: number;
}

describe('RuleRX functional uses cases', () => {

it('RuleRX can be used to trigger a discount whenever the user carts reach a certain amount', done => {

let customerCart1 = new BehaviorSubject<Basket>({
products: [{
name: "Art1",
price: 10
}],
customer: {name: "Jhon"}
});

let customerCart2 = new BehaviorSubject<Basket>( {
products: [{
name: "Art3",
price: 50
}],
customer: {name: "Ada"}
});

let carts= [customerCart1,customerCart2];

new RuleRx<Basket>()
.evaluate(
{
all: [
{
fact: 'user cart sum is beyond 100',
operator: sumGreaterThan,
path: '$.products..price',
value: 100
}
]
},
...carts
)
.pipe(
mergeAll(),
tap(p => p.element.discount = 10),
finalize(() => {
done();
})
)
.subscribe(next => {
expect(next.element.customer.name).toEqual("Jhon");
expect(next.element.discount).toEqual(10);
});

customerCart1.next(
{
products: [{
name: "Art1",
price: 10
},
{
name: "Art2",
price: 100
}],
customer: {name: "Jhon"}
}
);


customerCart2.next(
{
products: [{
name: "Art3",
price: 50
},
{
name: "Art4",
price: 50
}],
customer: {name: "Ada"}
}
);

customerCart1.complete();
customerCart2.complete();


})
})

0 comments on commit c445df4

Please sign in to comment.