Running tests with localStorage - gruntjs

i have followed this tutorial from Codelab and yeoman. When implemented right you are using local storage to store the TodoList. I have problems with setting up with my tests, to test if this works. This is what i've got so far:
'use strict';
describe('Controller: MainCtrl', function () {
// load the controller's module
beforeEach(module('yeoTodoApp'), module('LocalStorageModule'));
var MainCtrl,
scope;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope, $httpBackend) {
scope = $rootScope.$new();
MainCtrl = $controller('MainCtrl', {
$scope: scope
});
}));
it('should add items to the list', function () {
var beforeLength = scope.todos.length;
scope.todo = 'Test 1';
scope.addTodo();
var afterLength = scope.todos.length;
expect(afterLength-beforeLength).toBe(1);
});
it('should add items to the list then remove', function () {
var beforeLength = scope.todos.length;
scope.todo = 'Test 1';
scope.addTodo();
scope.removeTodo(0);
var afterLength = scope.todos.length;
expect(afterLength-beforeLength).toBe(0);
});
});
The error i get is
line 12 col 68 '$httpBackend' is defined but never used.
});
How would i write my unit tests to sit the local storage?

I think at the moment the idea is kind of mocking your local storage:
Write unit tests
For an extra challenge, revisit unit testing in Step 8 and consider
how you might update your tests now that the code is using local
storage.
Tip: It's not a straight forward answer and involves knowing about
mock services. Check out Unit Testing Best Practices in AngularJS,
specifically the Mocking Services and Modules in AngularJS section.
Things may have changed since this question was asked. Anyhow, here is my solution:
'use strict';
describe('Controller: MainCtrl', function () {
// load the controller's module
beforeEach(module('mytodoyoappApp'));
var MainCtrl,
scope,
localStorage, store;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
MainCtrl = $controller('MainCtrl', {
$scope:scope
// place here mocked dependencies
});
/*mock the localStorageService*/
store={};
localStorage = {
set: function(key, value) {
store[key] = value;
},
get: function(key) {
return store[key];
}
};
}));
it('should check the list length', function () {
expect(MainCtrl.todos.length).toBe(0);
});
it('should add items to the list', function () {
MainCtrl.todoadded = 'Test 1';
MainCtrl.addTodo();
expect(MainCtrl.todos.length).toBe(1);
});
it('should add then remove an item from the list', function () {
MainCtrl.todoadded = 'Test 2';
MainCtrl.addTodo();
MainCtrl.removeTodo(0);
expect(MainCtrl.todos.length).toBe(0);
});
it('should check that the localstorage is undefined before being set', function() {
var a=localStorage.get('todos');
expect(a).toBeUndefined();
});
it('should set and get the localstorage', function() {
localStorage.set('todos', ['Test 3']);
var a=localStorage.get('todos');
expect(a).toEqual(['Test 3']);
localStorage.set('todos', ['Test 4']);
var b=localStorage.get('todos');
expect(b).toEqual(['Test 4']);
});
});

your setup is correct now (after you removed $httpBackend from the arguments list)
Controller: MainCtrl should add items to the list then remove FAILED
this error is a simple test error, which means that your code somewhere doesnt work as expected (your second test fails)
i for myself would check todos length, and not the result of a mathematical operation.
i would write your tests the test like this:
it('should add items to the list then remove', function () {
scope.todo = 'Test 1';
expect(scope.todos.length).toBe(0);
scope.addTodo();
expect(scope.todos.length).toBe(1);
scope.removeTodo(0);
expect(scope.todos.length).toBe(0);
});
you use jasmine as a test-tool. jasmine logs on errors exactly which expectation fails, so you should get something like
expect '1' to be '0'
go from there!

Related

Cypress - Setting global variable from one test to use in other tests

I am trying to set a variable that will contain a part of a URL (a UUID) which I would then like to use in separate test suites. This snippet of the URL will be different every time so I cannot set it in the cypress.json within the "env" options. Code is as follows -
cy.location().then(fullUrl => {
let pathName = fullUrl.pathname
let arr = pathName.split('/');
const teamsTeamID = arr[4]
cy.log(teamsTeamID)
})
I would then like to use teamsTeamID in a separate teardown test to delete the team at the end of every test run but the team ID will be different every time I run the test - Is there a way to do this?
You can use fixtures and then use readFile and writeFile to achieve this.
First create a json inside your fixtures folder urldata.json
{
"uuid": "17289-YEHBE-893"
}
Then in your test you can write:
var teamsTeamID;
cy.location().then(fullUrl => {
let pathName = fullUrl.pathname
let arr = pathName.split('/');
teamsTeamID = arr[4]
cy.log(teamsTeamID)
})
cy.readFile("cypress/fixtures/urldata.json", (err, data) => {
if (err) {
return console.error(err);
};
}).then((data) => {
data.uuid = teamsTeamID
cy.writeFile("cypress/fixtures/urldata.json", JSON.stringify(data))
})
So now every time you run the test, you will have different values of UUID in your json file. Next you can use the value from fixtures directly into other tests:
describe('Some page', () => {
beforeEach(function () {
// "this" points at the test context object
cy.fixture('urldata.json').then((urldata) => {
// "this" is still the test context object
this.urldata = urldata
})
})
// the test callback is in "function () { ... }" form
it('check uuid', function () {
// this.urldata exists
expect(this.urldata.uuid).to.equal('some uuid')
})
})
Point to be noted:
If you store and access the fixture data using this test context
object, make sure to use function () { ... } callbacks. Otherwise the
test engine will NOT have this pointing at the test context.

How to unit test collection methods with velocity/jasmine

I'm completely new to javascript testing and I am trying to get a grasp on how to approach testing methods that touch the database
For example, I have this method that returns true if there are any documents in the db matching the query
Payments = new Mongo.Collection('payments');
_.extend(Payments, {
hasAnyPayments: function(userId) {
var payments = Payments.find({ userId: userId });
return payments.count() > 0;
}
});
So far I have only written the structure that I think is correct, but I am pretty lost
describe('Payments', function() {
describe('#hasAnyPayments', function() {
it('should return true when user has any payments', function() {
});
});
});
Are such tests even supposed to touch the database? Any advice is much appreciated
Unless you are manually inputting data into Mongo manually (or outside of Meteor) then you don't need to test the database.
What you should be testing, are the execution paths in your code.
So for the case above, hasAnyPayments is a unit that finds all user payments and returns true if there are more than 0. So your test would look something like this:
describe('Payments', function() {
describe('#hasAnyPayments', function() {
it('should return true when user has any payments', function() {
// SETUP
Payments.find = function() { return 1; } // stub to return a positive value
// EXECUTE
var actualValue = Payments.hasAnyPayments(); // you don't really care about the suer
// VERIFY
expect(actualValue).toBe(true);
});
});
});

Test asynchronous functionality in Jasmine 2.0.0 with done()

I am trying to implement a jasmine test on a simple promise implementation (asynchronous code) with the done() function and my test fails although the code being tested works perfectly fine.
Can anyone please help me to figure out what is missing in my test?
var Test = (function () {
function Test(fn) {
this.tool = null;
fn(this.resolve.bind(this));
}
Test.prototype.then = function (cb) {
this.callback = cb;
};
Test.prototype.resolve = function (value) {
var me = this;
setTimeout(function () {
me.callback(value);
}, 5000);
};
return Test;
})();
describe("setTimeout", function () {
var test, newValue = false,
originalTimeout;
beforeEach(function (done) {
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
test = new Test(function (cb) {
setTimeout(function () {
cb();
}, 5000);
});
test.then(function () {
newValue = true;
console.log(1, newValue);
done();
});
});
it("Should be true", function (done) {
expect(1).toBe(1);
expect(newValue).toBeTruthy();
});
afterEach(function () {
jasmine.DEFAULT_TIMEOUT_INTERVAL = originalTimeout;
});
});
the same test in jsfiddle: http://jsfiddle.net/ravitb/zsachqpg/
This code is testing a simple promise like object, so will call the Test object a promise for convenience.
There are two different async events after the promise creation:
1. The call to the .then() method
2. The resolving of the promise by calling the cb() function in the beforeEach() function.
In the real world these two can be called in any order and at any time.
For the test, the .then() call must be moved to the it() section's callback and all spec methods (e.g expect()) need to be called in it's callback or they'll run before it's resolved. The beforeEach() is part of the test setup while the it() function is the spec, the test itself.
The done() method needs to be called twice,
When the beforeEach() async action is finished (i.e after the cb() is called), that will start running the spec. So it should look something like this:
beforeEach(function (done) {
test = new Test(function (cb) {
setTimeout(function () {
console.log("in beforeEach() | setTimeout()");
cb(resolvedValue);
done()
}, 500);
});
});
When the spec's (it() section's) async action is finished inside the .then() method after all calls to jasmine test methods, this will tell Jasmine the spec finished running (and so the time-out won't be reached). So:
it("Should be " + resolvedValue, function (done) {
test.then(function (value) {
console.log("in then()");
expect(value).toBe(resolvedValue);
done();
});
});
Also, as you can see instead of testing that a variable's value has changed I'm testing that the value passed to the .then() method is the same as the one passed to the promise resolve cb() function as that is the right behaviour you are expecting.
Here's an updated version of your fiddle.
You can check in the browser's console to see that all callbacks are being called
Note: Changing Jasmine's DEFAULT_TIMEOUT_INTERVAL just makes it more convoluted for no reason, so I removed it and some extraneous code.

Implement added, changed and removed server side

Context : I am using a Collection Params to call method from the Server to a C app. The C app does its stuff and then calls the server by RPC to send me the results. With the result, I get the Params ID to delete the corresponding element.
With the deletion of the Element of Params, the C app gets a removed message. I want to prevent this behavior to avoid overloading the C app of messages.
I've thinked about implementing the removed event into the Publish method on the server to prevent the server from informing the C app. I just want the C app to be inform about added events.
On the Meteor Doc, there is an example of implementation of added and removed but I don't understand it. Can someone help me ?
I've tried this (don't work at all) :
Meteor.publish('expert_mode_parameters', function ()
{
var self = this;
var handle = Expert_Mode_Parameters.find().observeChanges({
added: function ()
{
return Expert_Mode_Parameters.find();
},
removed: function ()
{
return [];
}
});
self.ready();
self.onStop(function () {
handle.stop();
});
}
It looks like your goal is to subscribe to a data set but only receive added messages, not changed or removed.
The below code should do this:
Meteor.publish('expert_mode_parameters', function () {
var self = this;
var handle = Expert_Mode_Parameters.find().observe({
added: function (document) {
self.added("expert_mode_parameters", document._id, document);
}
});
self.ready();
self.onStop(function () {
handle.stop();
});
}
The concept is, you're watching the results of Expert_Mode_Parameters.find() and then calling self.added(document) when there is a new item. The same thing can easily be expanded to include changed.

Working with Meteor and Async balanced-payments functions

I'm using balanced-payments and their version 1.1 of balanced.js within Meteor.
I'm trying to create a new customer using
balanced.marketplace.customers.create(formData);
Here is my CheckFormSubmitEvents.js file
Template.CheckFormSubmit.events({
'submit form': function (e, tmpl) {
e.preventDefault();
var recurringStatus = $(e.target).find('[name=is_recurring]').is(':checked');
var checkForm = {
name: $(e.target).find('[name=name]').val(),
account_number: $(e.target).find('[name=account_number]').val(),
routing_number: $(e.target).find('[name=routing_number]').val(),
recurring: { is_recurring: recurringStatus },
created_at: new Date
}
checkForm._id = Donations.insert(checkForm);
Meteor.call("balancedCardCreate", checkForm, function(error, result) {
console.log(result);
// Successful tokenization
if(result.status_code === 201 && result.href) {
// Send to your backend
jQuery.post(responseTarget, {
uri: result.href
}, function(r) {
// Check your backend result
if(r.status === 201) {
// Your successful logic here from backend
} else {
// Your failure logic here from backend
}
});
} else {
// Failed to tokenize, your error logic here
}
// Debuging, just displays the tokenization result in a pretty div
$('#response .panel-body pre').html(JSON.stringify(result, false, 4));
$('#response').slideDown(300);
});
}
});
Here is my Methods.js file
var wrappedDelayedFunction = Async.wrap(balanced.marketplace.customers.create);
Meteor.methods({
balancedCardCreate: function (formData) {
console.log(formData);
var response = wrappedDelayedFunction(formData);
console.log(response);
return response;
}
});
I get nothing back when I submit the form, except that on the server console I do see the log of the form data.
I'm sure I'm not calling some of these async functions correctly. The hard part for me here is that the balanced function are async, but I don't know if they fit into the same mold as some of the examples I've seen.
I've tried to follow this example code.
http://meteorhacks.com/improved-async-utilities-in-meteor-npm.html
Is there a specific change that needs to be done in regard to working with balanced here? Does anyone have any tips for working with Async functions or see something specific about my code that I've done wrong?
Thanks
The NPM utilities Async.wrap does the same thing as the undocumented Meteor function Meteor._wrapAsync, in that it takes an asynchronous function with the last argument function(err, result) {} and turns it into a synchronous function which takes the same arguments, but either returns a result or throws an error instead of using the callback. The function yields in a Fiber until the asynchronous callback returns, so that other code in the event loop can run.
One pitfall with this is that you need to make sure that the function you wrap is called with the correct context. So if balanced.marketplace.customers.create is a prototype method that expects this to be set to something, it will not be set properly unless you bind it yourself, using function.bind or any of the other various library polyfills.
For more information, see https://stackoverflow.com/a/21542356/586086.
What I ended up doing was using a future. This works great, I just need to do better at catching errors. Which will be a question for a pro I think ; - )
Credit should go to user3374348 for answering another similar question of mine, which solved both of these.
https://stackoverflow.com/a/23777507/582309
var Future = Npm.require("fibers/future");
function extractFromPromise(promise) {
var fut = new Future();
promise.then(function (result) {
fut["return"](result);
}, function (error) {
fut["throw"](error);
});
return fut.wait();
}
Meteor.methods({
createCustomer: function (data) {
balanced.configure(Meteor.settings.balancedPaymentsAPI);
var customerData = extractFromPromise(balanced.marketplace.customers.create({
'name': data.fname + " " + data.lname,
"address": {
"city": data.city,
"state": data.region,
"line1": data.address_line1,
"line2": data.address_line2,
"postal_code": data.postal_code,
},
'email': data.email_address,
'phone': data.phone_number
}));
var card = extractFromPromise(balanced.marketplace.cards.create({
'number': data.card_number,
'expiration_year': data.expiry_year,
'expiration_month': data.expiry_month,
'cvv': data.cvv
}));
var associate = extractFromPromise(card.associate_to_customer(customerData.href).debit({
"amount": data.total_amount*100,
"appears_on_statement_as": "Trash Mountain" }));
});
As Andrew mentioned, you need to set the context for the method.
Here's the way you can do that with Async.wrap
Async.wrap(balanced.marketplace.customers, "create");

Resources