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 #268 from aldryn/feature/unit-tests-update
Browse files Browse the repository at this point in the history
Added more unit tests
  • Loading branch information
Marketionist committed Jul 15, 2015
2 parents 67de520 + 6df20b6 commit aa7aae2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,27 @@ var Cl = window.Cl || {};
});
},

_handler: function (e) {
e.preventDefault();

var form = $(this);

$.ajax({
type: 'GET',
url: form.attr('action'),
data: form.serialize()
}).always(function (data) {
form.siblings('.js-search-results').html(data);
}).fail(function () {
alert('REQUEST TIMEOUT');
});
},

// container should be a jQuery object
_search: function (container) {
var form = container.find('form');

form.on('submit', function (e) {
e.preventDefault();

$.ajax({
type: 'GET',
url: form.prop('action'),
data: form.serialize()
}).always(function (data) {
form.siblings('.js-search-results').html(data);
}).fail(function () {
alert('REQUEST TIMEOUT');
});
});
form.on('submit', this._handler);
}

};
Expand Down
22 changes: 22 additions & 0 deletions aldryn_newsblog/tests/frontend/fixtures/search.html
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
<div class="js-aldryn-newsblog-article-search">
</div>

<div class="js-aldryn-newsblog-article-search">
<form action="/en/blog/search/" class="form-inline" method="get">
<input type="hidden" name="csrfmiddlewaretoken"
value="Ui0tGBmn0Thq5LUeUS2m4zAF20H0M8up">
<input type="hidden" name="max_articles" value="10">
<div class="input-group">
<label for="field-17989-keyword" class="sr-only">Keyword search
</label>
<input type="text" name="q" class="form-control"
id="field-17989-keyword" placeholder="Keyword">
<span class="input-group-btn">
<button type="submit" class="btn btn-primary">Go</button>
</span>
</div>
</form>
<div class="search-results js-search-results">
<ul class="results-list list-unstyled">
<h3>Most recent articles containing "<strong>test</strong>"</h3>
</ul>
</div>
</div>
33 changes: 33 additions & 0 deletions aldryn_newsblog/tests/frontend/unit/test.cl.newsblog.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,39 @@ describe('cl.newsblog.js:', function () {
expect(Cl.newsBlog.init()).toEqual(undefined);
});

it('runs _search()', function () {
spyOn(Cl.newsBlog, '_search');
Cl.newsBlog.init();

// validate that _search was called inside Cl.newsBlog.init()
expect(Cl.newsBlog._search).toHaveBeenCalled();
// validate 2 call as 2 js-aldryn-newsblog-article-search is
// specified in search.html
expect(Cl.newsBlog._search.calls.count()).toEqual(2);
});
});

describe('Cl.newsBlog._search: ', function () {
it('returns undefined', function () {
// validate the return of undefined
expect(Cl.newsBlog._search(
$('.js-aldryn-newsblog-article-search').eq(1)))
.toEqual(undefined);
});

it('has correct url 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 url
expect(callArgs.url).toEqual(
'/en/blog/search/'
);
});
});

});

0 comments on commit aa7aae2

Please sign in to comment.