Skip to content
This repository has been archived by the owner on Oct 29, 2019. It is now read-only.

Commit

Permalink
Merge pull request #269 from aldryn/feature/unit-tests
Browse files Browse the repository at this point in the history
Added more unit tests
  • Loading branch information
Marketionist committed Jul 16, 2015
2 parents a4c5429 + c71e8f1 commit 2da5e50
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion aldryn_newsblog/tests/frontend/unit/test.cl.newsblog.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('cl.newsblog.js:', function () {
.toEqual(undefined);
});

it('has correct url in ajax request', function () {
it('has correct url parameter in ajax request', function () {
spyOn($, 'ajax').and.callThrough();
Cl.newsBlog._handler.call(
$('.js-aldryn-newsblog-article-search .form-inline')[0],
Expand All @@ -65,6 +65,62 @@ describe('cl.newsblog.js:', function () {
'/en/blog/search/'
);
});

it('has correct data parameter in ajax request', function () {
spyOn($, 'ajax').and.callThrough();
Cl.newsBlog._handler.call(
$('.js-aldryn-newsblog-article-search .form-inline')[0],
this.preventEvent);

var callArgs = $.ajax.calls.allArgs()[0][0];

// validate ajax request data
expect(callArgs.data).toEqual(
'csrfmiddlewaretoken=Ui0tGBmn0Thq5LUeUS2m4zAF20H0M8up&' +
'max_articles=10&q='
);
});

it('has ajax request with "always" function adding results data ' +
'correctly', function () {
// emulate always after ajax call
spyOn($, 'ajax').and.returnValue({
always: function (callback) {
callback('<h2>Test results</h2>');

return { fail: function () {} };
}
});

Cl.newsBlog._handler.call(
$('.js-aldryn-newsblog-article-search .form-inline')[0],
this.preventEvent);

// validate search results got updated with new info
expect($('.js-search-results')[0].innerHTML).toEqual(
'<h2>Test results</h2>');
});

it('has ajax request with "fail" function alert working ' +
'correctly', function () {
// emulate fail after always after ajax call
spyOn($, 'ajax').and.returnValue({
always: function () {
return { fail: function (callback) {
callback();
}};
}
});

spyOn(window, 'alert');

Cl.newsBlog._handler.call(
$('.js-aldryn-newsblog-article-search .form-inline')[0],
this.preventEvent);

// validate alert text
expect(window.alert).toHaveBeenCalledWith('REQUEST TIMEOUT');
});
});

});

0 comments on commit 2da5e50

Please sign in to comment.