Skip to content

Commit

Permalink
- reactioncommerce#536 fixed by putting cart sub inside Tracker.autorun;
Browse files Browse the repository at this point in the history
- preparation for `cart/mergeCart` tests;
- various fixes of app client tests;
- changed dep for collections tests;
- added few tests for `cart` publications;
- added file for client tests for subscriptions;
  • Loading branch information
newsiberian committed Dec 26, 2015
1 parent bfdc705 commit df25b98
Show file tree
Hide file tree
Showing 12 changed files with 336 additions and 246 deletions.
2 changes: 1 addition & 1 deletion packages/reaction-collections/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Package.onUse(function (api) {
Package.onTest(function (api) {
api.use("sanjo:[email protected]");
api.use("ecmascript");
api.use("jquery");
api.use("random");
api.use("underscore");
api.use("velocity:[email protected]");
api.use("velocity:[email protected]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ describe("Publication", function () {
let shop;
beforeEach(function () {
// reset
ReactionCore.Collections.Cart.remove({});
ReactionCore.Collections.Orders.remove({});
ReactionCore.Collections.Products.remove({});
ReactionCore.Collections.Shops.remove({});
Expand Down Expand Up @@ -34,9 +35,9 @@ describe("Publication", function () {
shop);
spyOn(Roles, "userIsInRole").and.returnValue(true);
// execute
cursor = Meteor.server.publish_handlers.Products();
const cursor = Meteor.server.publish_handlers.Products();
// verify
data = cursor.fetch()[0];
const data = cursor.fetch()[0];
expect(data.title).toEqual("My Little Pony");
});
it("should return only visible products to visitors",
Expand All @@ -46,9 +47,9 @@ describe("Publication", function () {
shop);
spyOn(Roles, "userIsInRole").and.returnValue(false);
// execute
cursor = Meteor.server.publish_handlers.Products();
const cursor = Meteor.server.publish_handlers.Products();
// verify
data = cursor.fetch()[0];
const data = cursor.fetch()[0];
expect(data.title).toEqual("Shopkins - Peachy");
});
it(
Expand All @@ -62,27 +63,27 @@ describe("Publication", function () {
});
spyOn(Roles, "userIsInRole").and.returnValue(true);
// execute
cursor = Meteor.server.publish_handlers.Products(
const cursor = Meteor.server.publish_handlers.Products(
productScrollLimit, shopIds);
// verify
data = cursor.fetch()[0];
const data = cursor.fetch()[0];
expect(data.title).toEqual("My Little Pony");
});
});

describe("Product", function () {
it("should return a product based on an id", function () {
// setup
product = ReactionCore.Collections.Products.findOne({
const product = ReactionCore.Collections.Products.findOne({
isVisible: true
});
spyOn(ReactionCore, "getCurrentShop").and.returnValue(
shop);
// execute
cursor = Meteor.server.publish_handlers.Product(
const cursor = Meteor.server.publish_handlers.Product(
product._id);
// verify
data = cursor.fetch()[0];
const data = cursor.fetch()[0];
expect(data.title).toEqual("Shopkins - Peachy");
});

Expand All @@ -91,10 +92,10 @@ describe("Publication", function () {
spyOn(ReactionCore, "getCurrentShop").and.returnValue(
shop);
// execute
cursor = Meteor.server.publish_handlers.Product(
const cursor = Meteor.server.publish_handlers.Product(
"shopkins");
// verify
data = cursor.fetch()[0];
const data = cursor.fetch()[0];
expect(data.title).toEqual("Shopkins - Peachy");
});

Expand All @@ -106,9 +107,9 @@ describe("Publication", function () {
shop);
spyOn(Roles, "userIsInRole").and.returnValue(false);
// execute
cursor = Meteor.server.publish_handlers.Product("my");
const cursor = Meteor.server.publish_handlers.Product("my");
// verify
data = cursor.fetch()[0];
const data = cursor.fetch()[0];
expect(data).toBeUndefined();
});

Expand All @@ -120,9 +121,9 @@ describe("Publication", function () {
shop);
spyOn(Roles, "userIsInRole").and.returnValue(true);
// execute
cursor = Meteor.server.publish_handlers.Product("my");
const cursor = Meteor.server.publish_handlers.Product("my");
// verify
data = cursor.fetch()[0];
const data = cursor.fetch()[0];
expect(data.title).toEqual("My Little Pony");
});
});
Expand All @@ -146,18 +147,80 @@ describe("Publication", function () {
spyOn(ReactionCore, "getCurrentShop").and.returnValue(shop);
spyOn(Roles, "userIsInRole").and.returnValue(true);
// execute
cursor = Meteor.server.publish_handlers.Orders();
const cursor = Meteor.server.publish_handlers.Orders();
// verify
data = cursor.fetch()[0];
const data = cursor.fetch()[0];
expect(data.shopId).toBe(order.shopId);
});

it("should not return shop orders for non admin", function () {
// setup
spyOn(ReactionCore, "getCurrentShop").and.returnValue(shop);
spyOn(Roles, "userIsInRole").and.returnValue(false);
cursor = Meteor.server.publish_handlers.Orders();
const cursor = Meteor.server.publish_handlers.Orders();
expect(cursor).toEqual([]);
});
});

describe("Cart", () => {
const user = Factory.create("user");
const userId = user._id;
const sessionId = ReactionCore.sessionId = Random.id();

beforeEach(() => {
Meteor.call("cart/createCart", userId, shop._id);
});

it(
"should return a cart cursor",
done => {
const originalCart = Meteor.server.publish_handlers["Cart"];
spyOn(ReactionCore, "shopIdAutoValue").and.returnValue(shop._id);
spyOn(ReactionCore, "getShopId").and.returnValue(shop._id);
spyOn(Meteor.server.publish_handlers, "Cart").and.callFake(
function () {
this.userId = userId;
return originalCart.apply(this, arguments);
});
const cursor = Meteor.server.publish_handlers.Cart(sessionId);
const data = cursor.fetch()[0];

expect(data.userId).toEqual(userId);

return done();
}
);

it(
"should return only one cart in cursor",
done => {
const user2 = Factory.create("user");
// mock the `this.userId` call
const originalCart = Meteor.server.publish_handlers["Cart"];
spyOn(ReactionCore, "shopIdAutoValue").and.returnValue(shop._id);
spyOn(ReactionCore, "getShopId").and.returnValue(shop._id);
spyOn(Meteor.server.publish_handlers, "Cart").and.callFake(
function () {
this.userId = userId;
return originalCart.apply(this, arguments);
});

Meteor.call("cart/createCart", user2._id, shop._id);

const cursor = Meteor.server.publish_handlers.Cart(sessionId);
const data = cursor.fetch();

expect(data.length).toEqual(1);

return done();
}
);

//it(
// "should",
// done => {
// return done();
// }
//);
});
});
28 changes: 14 additions & 14 deletions packages/reaction-core/client/subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,22 @@ Tracker.autorun(function () {
Session.get("sessionId"));
});

// todo maybe we need to move cart subscription inside above autorun?
// maybe this is the reason
// why https://github.com/reactioncommerce/reaction/issues/536 happens?
// Load order is important here, sessions come before cart.
ReactionCore.Subscriptions.Cart = Meteor.subscribe("Cart",
Session.get("sessionId"),
Meteor.userId()
);
// @see http://guide.meteor.com/data-loading.html#changing-arguments
Tracker.autorun(() => {
ReactionCore.Subscriptions.Cart = Meteor.subscribe("Cart",
Session.get("sessionId"),
Meteor.userId()
);
});

// detect when a cart has been deleted
// resubscribe will force cart to be rebuilt
let cart = ReactionCore.Collections.Cart.find();
cart.observeChanges({
removed: function () {
Meteor.subscribe("Cart", Session.get("sessionId"), Meteor.userId());
}
});
//let cart = ReactionCore.Collections.Cart.find();
//cart.observeChanges({
// removed: function () {
// Meteor.subscribe("Cart", Session.get("sessionId")/*, Meteor.userId()*/);
// }
//});

/**
* General Subscriptions
Expand Down
43 changes: 43 additions & 0 deletions packages/reaction-core/tests/jasmine/server/integration/cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,49 @@ describe("cart methods", function () {
let userId = user._id;
ReactionCore.sessionId = Random.id(); // Required for creating a cart

describe("cart/mergeCart", () => {
it(
"should merge all `anonymous` carts into `normal` user cart per session",
done => {
let account = Factory.create("account");
return done();
}
);


it(
"should ignore `normal` user carts from been merged",
done => {
let account = Factory.create("account");
return done();
}
);

it(
"should merge only into registered user cart",
done => {
let account = Factory.create("account");
return done();
}
);

it(
"should",
done => {
let account = Factory.create("account");
return done();
}
);

it(
"should",
done => {
let account = Factory.create("account");
return done();
}
);
});

describe("cart/createCart", function () {
it("should create a test cart", function (done) {
spyOn(ReactionCore, "shopIdAutoValue").and.returnValue(shop._id);
Expand Down
Loading

0 comments on commit df25b98

Please sign in to comment.