2014-11-12 10:12:26 -05:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
describe('Newsletter Service unit test', function() {
|
|
|
|
var newsletter;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
angular.module('newsletter-test', ['woServices']);
|
2014-11-21 09:06:29 -05:00
|
|
|
angular.mock.module('newsletter-test');
|
|
|
|
angular.mock.inject(function($injector) {
|
2014-11-12 10:12:26 -05:00
|
|
|
newsletter = $injector.get('newsletter');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {});
|
|
|
|
|
|
|
|
describe('signup', function() {
|
|
|
|
var xhrMock, requests;
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
xhrMock = sinon.useFakeXMLHttpRequest();
|
|
|
|
requests = [];
|
|
|
|
|
|
|
|
xhrMock.onCreate = function(xhr) {
|
|
|
|
requests.push(xhr);
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function() {
|
|
|
|
xhrMock.restore();
|
|
|
|
});
|
|
|
|
|
2014-12-11 12:12:37 -05:00
|
|
|
it('should not signup if user has not agreed', function(done) {
|
2014-11-12 10:12:26 -05:00
|
|
|
newsletter.signup('text@example.com', false).then(function(result) {
|
|
|
|
expect(result).to.be.false;
|
2014-12-11 12:12:37 -05:00
|
|
|
expect(requests.length).to.equal(0);
|
|
|
|
done();
|
2014-11-12 10:12:26 -05:00
|
|
|
});
|
2014-12-11 12:12:37 -05:00
|
|
|
});
|
2014-11-12 10:12:26 -05:00
|
|
|
|
2014-12-11 12:12:37 -05:00
|
|
|
it('should not signup due to invalid email address', function(done) {
|
2014-11-12 10:12:26 -05:00
|
|
|
newsletter.signup('textexample.com', true).catch(function(err) {
|
|
|
|
expect(err.message).to.contain('Invalid');
|
2014-12-11 12:12:37 -05:00
|
|
|
expect(requests.length).to.equal(0);
|
|
|
|
done();
|
2014-11-12 10:12:26 -05:00
|
|
|
});
|
2014-12-11 12:12:37 -05:00
|
|
|
});
|
2014-11-12 10:12:26 -05:00
|
|
|
|
2014-12-11 12:12:37 -05:00
|
|
|
it('should fail', function(done) {
|
2014-11-12 10:12:26 -05:00
|
|
|
newsletter.signup('text@example.com', true).catch(function(err) {
|
|
|
|
expect(err).to.exist;
|
2014-12-11 12:12:37 -05:00
|
|
|
expect(requests.length).to.equal(1);
|
|
|
|
done();
|
2014-11-12 10:12:26 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
requests[0].onerror('err');
|
2014-12-11 12:12:37 -05:00
|
|
|
});
|
2014-11-12 10:12:26 -05:00
|
|
|
|
2014-12-11 12:12:37 -05:00
|
|
|
it('should work', function(done) {
|
2014-11-12 10:12:26 -05:00
|
|
|
newsletter.signup('text@example.com', true).then(function(result) {
|
|
|
|
expect(result).to.exist;
|
2014-12-11 12:12:37 -05:00
|
|
|
expect(requests.length).to.equal(1);
|
|
|
|
done();
|
2014-11-12 10:12:26 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
requests[0].respond(200, {
|
|
|
|
"Content-Type": "text/plain"
|
|
|
|
}, 'foobar!');
|
2014-12-11 12:12:37 -05:00
|
|
|
});
|
2014-11-12 10:12:26 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
});
|