Skip to content

Commit

Permalink
Add test for external aspect (#66)
Browse files Browse the repository at this point in the history
* add ts-node so you can easily debug in webstorm

* update test to include overridden method

* add failing test using imported external aspect

* add comment

* add external advice test
  • Loading branch information
matthewadams authored and mgechev committed Oct 10, 2018
1 parent 97bf62a commit 79c21d1
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 10 deletions.
66 changes: 65 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -41,6 +41,7 @@
"prettier": "^1.7.4",
"rxjs": "^5.2.0",
"sinon": "^4.4.2",
"ts-node": "^7.0.1",
"tsviz": "^1.0.11",
"typescript": "^2.0.2",
"zone.js": "^0.7.7"
Expand Down
28 changes: 28 additions & 0 deletions test/advices/external_advice.spec.ts
@@ -0,0 +1,28 @@
import {
Wove,
resetRegistry
} from '../../lib/src/core';

import { expect } from 'chai';

import './external_aspect'

describe('sync advices', () => {
afterEach(() => {
resetRegistry();
});

describe('External AroundAdvice', () => {
it('should invoke the external advice with the appropriate metadata', () => {
let demo: any;

@Wove()
class Demo {
get(foo: any, bar: any): string { return 'Demo' }
}

demo = new Demo();
expect(demo.get(42, 1.618)).to.equal('ExternalAspect');
});
});
});
16 changes: 16 additions & 0 deletions test/advices/external_aspect.ts
@@ -0,0 +1,16 @@
import { aroundMethod, Metadata } from "../../lib";
import { expect } from "chai";

export default class ExternalAspect {
@aroundMethod({ classNamePattern: /.*/, methodNamePattern: /.*/ })
around(metadata: Metadata) {
expect(this).to.deep.equal(ExternalAspect.prototype);
expect(metadata.className).to.equal('Demo');
expect(metadata.method.name).to.equal('get');
expect(metadata.method.args).to.deep.equal([42, 1.618]);
expect(metadata.method.invoke).to.be.a('function');

metadata.method.proceed = false
metadata.method.result = 'ExternalAspect'
}
}
49 changes: 47 additions & 2 deletions test/advices/sync_advices.spec.ts
Expand Up @@ -12,15 +12,20 @@ import {
beforeStaticMethod,
beforeGetter,
beforeSetter,
afterMethod
} from '../../lib/index';
afterMethod, aroundMethod
} from "../../lib/index";

import { expect } from 'chai';

import './external_aspect'

describe('sync advices', () => {
beforeEach(() => {
resetRegistry();
});
afterEach(() => {
resetRegistry();
});
describe('BeforeAdvice', () => {
// beforeEach(() => {
// resetRegistry();
Expand Down Expand Up @@ -273,6 +278,46 @@ describe('sync advices', () => {
});
});

describe('AroundAdvice', () => {
it('should invoke the advice with the appropriate metadata', () => {
let demo: any;

class Aspect {
@aroundMethod({ classNamePattern: /.*/, methodNamePattern: /.*/ })
around(metadata: Metadata) {
expect(this).to.deep.equal(Aspect.prototype);
expect(metadata.method.context).to.eq(demo);
expect(metadata.className).to.equal('Demo');
expect(metadata.method.name).to.equal('get');
expect(metadata.method.args).to.deep.equal([42, 1.618]);
expect(metadata.method.invoke).to.be.a('function');

metadata.method.proceed = false;
metadata.method.result = 'Aspect';
}
}
@Wove()
class Demo {
get(foo: any, bar: any): string { return 'Demo'; }
}

demo = new Demo();
expect(demo.get(42, 1.618)).to.equal('Aspect');
});

it('should invoke the external advice with the appropriate metadata', () => {
let demo: any;

@Wove()
class Demo {
get(foo: any, bar: any): string { return 'Demo' }
}

demo = new Demo();
expect(demo.get(42, 1.618)).to.equal('ExternalAspect');
});
});

describe('Wove', () => {
it('should pass the Wove config as `woveMetadata`', () => {
let adviceCalls = 0;
Expand Down
25 changes: 18 additions & 7 deletions test/core/wove.spec.ts
Expand Up @@ -5,10 +5,13 @@ import { Wove } from './../../lib/src/core';

import { spy } from 'sinon';

@Wove()
const o = 42;

@Wove()
class ClassA {
foo() {}

overridden() { return o; }
}

@Wove()
Expand All @@ -23,6 +26,8 @@ class ClassB extends ClassA {
bar() {}

qux() {}

overridden() { return super.overridden() + 1; }
}

const methods: string[] = [];
Expand All @@ -40,16 +45,22 @@ class LoggerAspect {
describe('@Wove', () => {
beforeEach(() => (methods.length = 0));

it('should work with extended classes', () => {
it('should work with subclasses', () => {
const fooSpy = spy(ClassA.prototype, 'foo');
const barSpy = spy(ClassB.prototype, 'bar');
const quxSpy = spy(ClassB.prototype, 'qux');
const overriddenSpyA = spy(ClassA.prototype, 'overridden')
const overriddenSpyB = spy(ClassB.prototype, 'overridden')

const b = new ClassB();

new ClassB();
expect(fooSpy.called).to.be.true;
expect(barSpy.called).to.be.true;
expect(quxSpy.called).to.be.true;
expect(methods).to.deep.equal(['foo', 'bar', 'qux']);

expect(fooSpy.called).equal(true);
expect(barSpy.called).equal(true);
expect(quxSpy.called).equal(true);
expect(methods).deep.eq(['foo', 'bar', 'qux']);
expect(b.overridden()).to.equal(o + 1);
expect(overriddenSpyA.called).to.be.true;
expect(overriddenSpyB.called).to.be.true;
});
});

0 comments on commit 79c21d1

Please sign in to comment.