Created
December 23, 2015 19:23
-
-
Save allenmichael/5a501b5547f019ddaefd to your computer and use it in GitHub Desktop.
An example of creating a unit test for an Arc.js action
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This is a scaffold for unit tests for the custom function for | |
* `embedded.commerce.carts.addItem.before`. | |
* Modify the test conditions below. You may: | |
* - add special assertions for code actions from Simulator.assert | |
* - create a mock context with Simulator.context() and modify it | |
* - use and modify mock Mozu business objects from Simulator.fixtures | |
*/ | |
'use strict'; | |
var Simulator = require('mozu-action-simulator'); | |
var assert = Simulator.assert; | |
var _ = require('underscore'); | |
var actionName = 'embedded.commerce.carts.addItem.before'; | |
describe('embedded.commerce.carts.addItem.before implementing embedded.commerce.carts.addItem.before', function () { | |
var action; | |
before(function () { | |
action = require('../src/domains/commerce.carts/embedded.commerce.carts.addItem.before'); | |
}); | |
it('runs successfully', function(done) { | |
var callback = function(err) { | |
assert.ok(!err, "Callback was called with an error: " + err); | |
// more assertions | |
done(); | |
}; | |
var context = Simulator.context(actionName, callback); | |
// modify context as necessary | |
Simulator.simulate(actionName, action, context, callback); | |
}); | |
it('checks incoming product for hazard indicator and then removes hazardous items from the cart if the incoming item is hazardous', function(done) { | |
var callback = function(err) { | |
assert.ok(!err, "Callback was called with an error: " + err); | |
// more assertions | |
done(); | |
}; | |
//Test setup | |
var context = Simulator.context(actionName, callback); | |
var cart = context.get.cart(); | |
var cartItem = context.get.cartItem(); | |
var cartItemsCount = cart.items.length; | |
cart.items[0].product.properties.push({ | |
"attributeFQN": "tenant~hazardous", | |
"name": "Hazardous", | |
"dataType": "Bool", | |
"isMultiValue": false, | |
"values": [ | |
{ | |
"value": true | |
} | |
] | |
}); | |
cartItem.product.properties.push({ | |
"attributeFQN": "tenant~hazardous", | |
"name": "Hazardous", | |
"dataType": "Bool", | |
"isMultiValue": false, | |
"values": [ | |
{ | |
"value": true | |
} | |
] | |
}); | |
if(cart.items.length > 1) { | |
var hazardousCart = false; | |
var hazardousIds = []; | |
_.each(cart.items, function(item) { | |
_.each(item.product.properties, function(property) { | |
if(_.contains(property, 'tenant~hazardous')) { | |
hazardousCart = true; | |
hazardousIds.push(item.id) | |
} | |
}); | |
}); | |
//Extra unit test code -- not needed for main Arc.js action | |
if(hazardousCart) { | |
var removedItems = 0; | |
var newCartItems = cart.items.filter(function(item) { | |
if(_.contains(hazardousIds, item.id)) { | |
removedItems++; | |
return false; | |
} else { | |
return true; | |
} | |
}); | |
} | |
context.exec.removeItem = function() { | |
cart.items = newCartItems; | |
assert.equal(cart.items.length, (cartItemsCount - removedItems)); | |
}; | |
} | |
Simulator.simulate(actionName, action, context, callback); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment