|
| 1 | +describe('UserInfo as params', function () { |
| 2 | + |
| 3 | + before(function(done){ |
| 4 | + this.searchTest = fixture.load('mock/searchTestResponse.json'); |
| 5 | + this.userInfo = fixture.load('mock/userInfo.json'); |
| 6 | + |
| 7 | + //setup document to hold search results |
| 8 | + document.body.innerHTML = __html__['index.html']; |
| 9 | + |
| 10 | + //initialize search |
| 11 | + this.searchobj = new window.Unbxd.setSearch(window.config); |
| 12 | + |
| 13 | + //stub search ajax call with mock response |
| 14 | + this.stub = sinon.stub(jQuery, 'ajax').yieldsTo('success',this.searchTest); |
| 15 | + //spy OnIntialResultLoad |
| 16 | + this.spyOnIntialResultLoad = sinon.spy(this.searchobj.options, |
| 17 | + 'onIntialResultLoad'); |
| 18 | + |
| 19 | + //stub getUserId |
| 20 | + this.stubGetUserId = sinon.stub(this.searchobj, |
| 21 | + 'getUserId'); |
| 22 | + this.stubGetUserId.returns(this.userInfo.uid); |
| 23 | + //stub getUserType |
| 24 | + this.stubGetUserType = sinon.stub(this.searchobj, |
| 25 | + 'getUserType'); |
| 26 | + this.stubGetUserType.returns(this.userInfo.userType); |
| 27 | + |
| 28 | + this.searchobj.callResults(this.searchobj.paintResultSet); |
| 29 | + done(); |
| 30 | + }); |
| 31 | + |
| 32 | + after(function(){ |
| 33 | + this.stub.restore(); |
| 34 | + fixture.cleanup(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('Should pass user-info as query params', function(){ |
| 38 | + var userInfoSent = { |
| 39 | + 'uid': this.userInfo.uid, |
| 40 | + 'user-type': this.userInfo.userType, |
| 41 | + 'unbxd-url': document.URL, |
| 42 | + 'unbxd-referrer': document.referrer, |
| 43 | + 'api-key': this.searchobj.options.APIKey |
| 44 | + }, |
| 45 | + userInfoInUrl = {}, |
| 46 | + searchApi = jQuery.ajax.getCall(0).args[0].url, |
| 47 | + params; |
| 48 | + if(searchApi.indexOf('search.unbxdapi.com')){ |
| 49 | + params = searchApi.split('?').length > 0 ? searchApi.split('?')[1] : ''; |
| 50 | + params.split('&').forEach(function(param){ |
| 51 | + var queryName = param.split('=')[0], |
| 52 | + queryValue = decodeURIComponent(param.split('=')[1]); |
| 53 | + |
| 54 | + if(queryName in userInfoSent){ |
| 55 | + userInfoInUrl[queryName] = queryValue; |
| 56 | + } |
| 57 | + }); |
| 58 | + } |
| 59 | + expect(userInfoSent).to.deep.equal(userInfoInUrl); |
| 60 | + }); |
| 61 | + |
| 62 | +}); |
0 commit comments