mirror of
https://github.com/moparisthebest/mail
synced 2024-11-10 11:15:01 -05:00
cf1f60fbf9
* add spinners to all login pages * use inline error messages in all form instead of scope.onError * create newsletter service
76 lines
2.2 KiB
JavaScript
76 lines
2.2 KiB
JavaScript
'use strict';
|
|
|
|
var mocks = angular.mock;
|
|
require('../../src/js/service/newsletter');
|
|
|
|
describe('Newsletter Service unit test', function() {
|
|
var newsletter;
|
|
|
|
beforeEach(function() {
|
|
angular.module('newsletter-test', ['woServices']);
|
|
mocks.module('newsletter-test');
|
|
mocks.inject(function($injector) {
|
|
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();
|
|
});
|
|
|
|
it('should not signup if user has not agreed', inject(function($rootScope) {
|
|
newsletter.signup('text@example.com', false).then(function(result) {
|
|
expect(result).to.be.false;
|
|
});
|
|
|
|
$rootScope.$apply();
|
|
expect(requests.length).to.equal(0);
|
|
}));
|
|
|
|
it('should not signup due to invalid email address', inject(function($rootScope) {
|
|
newsletter.signup('textexample.com', true).catch(function(err) {
|
|
expect(err.message).to.contain('Invalid');
|
|
});
|
|
|
|
$rootScope.$apply();
|
|
expect(requests.length).to.equal(0);
|
|
}));
|
|
|
|
it('should fail', inject(function($rootScope) {
|
|
newsletter.signup('text@example.com', true).catch(function(err) {
|
|
expect(err).to.exist;
|
|
});
|
|
|
|
requests[0].onerror('err');
|
|
$rootScope.$apply();
|
|
expect(requests.length).to.equal(1);
|
|
}));
|
|
|
|
it('should work', inject(function($rootScope) {
|
|
newsletter.signup('text@example.com', true).then(function(result) {
|
|
expect(result).to.exist;
|
|
});
|
|
|
|
requests[0].respond(200, {
|
|
"Content-Type": "text/plain"
|
|
}, 'foobar!');
|
|
$rootScope.$apply();
|
|
expect(requests.length).to.equal(1);
|
|
}));
|
|
});
|
|
|
|
}); |