Skip to content

Commit

Permalink
Added tests to reproduce #261
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed May 26, 2016
1 parent c2eb387 commit a573027
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 57 deletions.
85 changes: 56 additions & 29 deletions test/babel/babel-tests.js
Expand Up @@ -142,24 +142,40 @@ test('issue 191 - shared initializers (babel)', function(t) {
t.end();
})

function normalizeSpyEvents(events) {
events.forEach(ev => {
delete ev.fn;
delete ev.time;
});
return events;
}

test("action decorator (babel)", function(t) {
class Store {
constructor(multiplier) {
this.multiplier = multiplier;
}

@action
add(a, b) {
return a + b;
return (a + b) * this.multiplier;
}
}

const store = new Store();
const store1 = new Store(2);
const store2 = new Store(3);
const events: any[] = [];
const d = spy(events.push.bind(events));
t.equal(store.add(3, 4), 7);

delete events[0].fn;
delete events[1].time;
t.equal(store1.add(3, 4), 14);
t.equal(store2.add(3, 4), 21);
t.equal(store1.add(1, 1), 4);

t.deepEqual(events, [
{ arguments: [ 3, 4 ], name: "add", spyReportStart: true, target: store, type: "action" },
t.deepEqual(normalizeSpyEvents(events), [
{ arguments: [ 3, 4 ], name: "add", spyReportStart: true, target: store1, type: "action" },
{ spyReportEnd: true },
{ arguments: [ 3, 4 ], name: "add", spyReportStart: true, target: store2, type: "action" },
{ spyReportEnd: true },
{ arguments: [ 1, 1 ], name: "add", spyReportStart: true, target: store1, type: "action" },
{ spyReportEnd: true }
]);

Expand All @@ -169,23 +185,31 @@ test("action decorator (babel)", function(t) {

test("custom action decorator (babel)", function(t) {
class Store {
constructor(multiplier) {
this.multiplier = multiplier;
}

@action("zoem zoem")
add(a, b) {
return a + b;
return (a + b) * this.multiplier;
}
}

const store = new Store();
const store1 = new Store(2);
const store2 = new Store(3);
const events: any[] = [];
const d = spy(events.push.bind(events));
t.equal(store.add(3, 4), 7);
t.equal(store1.add(3, 4), 14);
t.equal(store2.add(3, 4), 21);
t.equal(store1.add(1, 1), 4);

delete events[0].fn;
delete events[1].time;

t.deepEqual(events, [
{ arguments: [ 3, 4 ], name: "zoem zoem", spyReportStart: true, target: store, type: "action" },
{ spyReportEnd: true }
t.deepEqual(normalizeSpyEvents(events), [
{ arguments: [ 3, 4 ], name: "zoem zoem", spyReportStart: true, target: store1, type: "action" },
{ spyReportEnd: true },
{ arguments: [ 3, 4 ], name: "zoem zoem", spyReportStart: true, target: store2, type: "action" },
{ spyReportEnd: true },
{ arguments: [ 1, 1 ], name: "zoem zoem", spyReportStart: true, target: store1, type: "action" },
{ spyReportEnd: true },
]);

d();
Expand All @@ -194,29 +218,32 @@ test("custom action decorator (babel)", function(t) {

test("custom action decorator on field (babel)", function(t) {
class Store {
multiplier = 2;
constructor(multiplier) {
this.multiplier = multiplier;
}


@action("zoem zoem")
add = (a, b) => {
return (a + b) * this.multiplier;
};
}

const store = new Store();
const store1 = new Store(2);
const store2 = new Store(7);

const events: any[] = [];
const d = spy(events.push.bind(events));
t.equal(store.add(3, 4), 14);
t.equal(store.add(2, 2), 8);
t.equal(store1.add(3, 4), 14);
t.equal(store2.add(5, 4), 18);
t.equal(store1.add(2, 2), 8);

delete events[0].fn;
delete events[1].time;
delete events[2].fn;
delete events[3].time;

t.deepEqual(events, [
{ arguments: [ 3, 4 ], name: "zoem zoem", spyReportStart: true, target: store, type: "action" },
t.deepEqual(normalizeSpyEvents(events), [
{ arguments: [ 3, 4 ], name: "zoem zoem", spyReportStart: true, target: store1, type: "action" },
{ spyReportEnd: true },
{ arguments: [ 5, 4 ], name: "zoem zoem", spyReportStart: true, target: store2, type: "action" },
{ spyReportEnd: true },
{ arguments: [ 2, 2 ], name: "zoem zoem", spyReportStart: true, target: store, type: "action" },
{ arguments: [ 2, 2 ], name: "zoem zoem", spyReportStart: true, target: store1, type: "action" },
{ spyReportEnd: true }
]);

Expand Down
76 changes: 48 additions & 28 deletions test/typescript-tests.ts
Expand Up @@ -437,24 +437,38 @@ test('issue 191 - shared initializers (ts)', function(t) {
t.end();
});

function normalizeSpyEvents(events: any[]) {
events.forEach(ev => {
delete ev.fn;
delete ev.time;
});
return events;
}

test("action decorator (typescript)", function(t) {
class Store {
constructor(private multiplier: number) {}

@action
add(a: number, b: number): number {
return a + b;
return (a + b) * this.multiplier;
}
}

const store = new Store();
const store1 = new Store(2);
const store2 = new Store(3);
const events: any[] = [];
const d = spy(events.push.bind(events));
t.equal(store.add(3, 4), 7);
t.equal(store1.add(3, 4), 14);
t.equal(store2.add(2, 2), 12);
t.equal(store1.add(1, 1), 4);

delete events[0].fn;
delete events[1].time;

t.deepEqual(events, [
{ arguments: [ 3, 4 ], name: "add", spyReportStart: true, target: store, type: "action" },
t.deepEqual(normalizeSpyEvents(events), [
{ arguments: [ 3, 4 ], name: "add", spyReportStart: true, target: store1, type: "action" },
{ spyReportEnd: true },
{ arguments: [ 2, 2 ], name: "add", spyReportStart: true, target: store2, type: "action" },
{ spyReportEnd: true },
{ arguments: [ 1, 1 ], name: "add", spyReportStart: true, target: store1, type: "action" },
{ spyReportEnd: true }
]);

Expand All @@ -464,22 +478,28 @@ test("action decorator (typescript)", function(t) {

test("custom action decorator (typescript)", function(t) {
class Store {
constructor(private multiplier: number) {}

@action("zoem zoem")
add(a: number, b: number): number {
return a + b;
return (a + b) * this.multiplier;
}
}

const store = new Store();
const store1 = new Store(2);
const store2 = new Store(3);
const events: any[] = [];
const d = spy(events.push.bind(events));
t.equal(store.add(3, 4), 7);

delete events[0].fn;
delete events[1].time;
t.equal(store1.add(3, 4), 14);
t.equal(store2.add(2, 2), 12);
t.equal(store1.add(1, 1), 4);

t.deepEqual(events, [
{ arguments: [ 3, 4 ], name: "zoem zoem", spyReportStart: true, target: store, type: "action" },
t.deepEqual(normalizeSpyEvents(events), [
{ arguments: [ 3, 4 ], name: "zoem zoem", spyReportStart: true, target: store2, type: "action" },
{ spyReportEnd: true },
{ arguments: [ 2, 2 ], name: "zoem zoem", spyReportStart: true, target: store2, type: "action" },
{ spyReportEnd: true },
{ arguments: [ 1, 1 ], name: "zoem zoem", spyReportStart: true, target: store2, type: "action" },
{ spyReportEnd: true }
]);

Expand All @@ -490,29 +510,29 @@ test("custom action decorator (typescript)", function(t) {

test("custom action decorator on field (typescript)", function(t) {
class Store {
multiplier = 2;
constructor(private multiplier: number) {}

@action("zoem zoem")
add = (a: number, b: number) => {
return (a + b) * this.multiplier;
};
}

const store = new Store();
const store1 = new Store(2);
const store2 = new Store(7);

const events: any[] = [];
const d = spy(events.push.bind(events));
t.equal(store.add(3, 4), 14);
t.equal(store.add(2, 2), 8);

delete events[0].fn;
delete events[1].time;
delete events[2].fn;
delete events[3].time;
t.equal(store1.add(3, 4), 14);
t.equal(store2.add(4, 5), 140);
t.equal(store1.add(2, 2), 8);

t.deepEqual(events, [
{ arguments: [ 3, 4 ], name: "zoem zoem", spyReportStart: true, target: store, type: "action" },
t.deepEqual(normalizeSpyEvents(events), [
{ arguments: [ 3, 4 ], name: "zoem zoem", spyReportStart: true, target: store1, type: "action" },
{ spyReportEnd: true },
{ arguments: [ 4, 5 ], name: "zoem zoem", spyReportStart: true, target: store2, type: "action" },
{ spyReportEnd: true },
{ arguments: [ 2, 2 ], name: "zoem zoem", spyReportStart: true, target: store, type: "action" },
{ arguments: [ 2, 2 ], name: "zoem zoem", spyReportStart: true, target: store1, type: "action" },
{ spyReportEnd: true }
]);

Expand Down

0 comments on commit a573027

Please sign in to comment.