Skip to content

Commit

Permalink
feat: send hostname (#9)
Browse files Browse the repository at this point in the history
* feat: send hostname

* test: add test for send hostname
  • Loading branch information
kyuwoo-choi authored Apr 12, 2018
1 parent be6674a commit 799c0af
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 83 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ You can also see the older versions of API page on the [releases page](https://g


## 🔩 Dependency
* [tui-code-snippet](https://github.com/nhnent/tui.code-snippet) >=1.2.5
* [tui-code-snippet](https://github.com/nhnent/tui.code-snippet) >=1.3.0
* [js-cookie](https://github.com/js-cookie/js-cookie) >=1.2.0 (If use 1.3.0 or more, You need to include the [JSON-js polyfill](https://github.com/douglascrockford/JSON-js))
* [jquery](https://github.com/jquery/jquery) >=1.11.0

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
"dependencies": {
"jquery" : "^1.11.0",
"js-cookie": "^1.2.0",
"tui-code-snippet" : "^1.2.8"
"tui-code-snippet" : "^1.3.0"
}
}
2 changes: 1 addition & 1 deletion docs/getting-started.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
## Dependencies
* [tui-code-snippet](https://github.com/nhnent/tui.code-snippet) >=1.2.5
* [tui-code-snippet](https://github.com/nhnent/tui.code-snippet) >=1.3.0
* [js-cookie](https://github.com/js-cookie/js-cookie) >=1.2.0 (If use 1.3.0 or more, You need to include the [JSON-js polyfill](https://github.com/douglascrockford/JSON-js))
* [jquery](https://github.com/jquery/jquery) >=1.11.0

Expand Down
2 changes: 1 addition & 1 deletion examples/example01-basic.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ <h4> See the real services. </h4>
</div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/1.5.1/js.cookie.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/nhnent/tui.code-snippet/v1.2.5/dist/tui-code-snippet.js"></script>
<script type="text/javascript" src="https://uicdn.toast.com/tui.code-snippet/v1.3.0/tui-code-snippet.min.js"></script>
<script type="text/javascript" src="autoConfig.js"></script>
<script type="text/javascript" src="../dist/tui-auto-complete.js"></script>
<script class="code-js">
Expand Down
2 changes: 1 addition & 1 deletion examples/example02-toggle-autocompletion.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ <h4> See the real services. </h4>
</div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/1.5.1/js.cookie.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/nhnent/tui.code-snippet/v1.2.5/dist/tui-code-snippet.js"></script>
<script type="text/javascript" src="https://uicdn.toast.com/tui.code-snippet/v1.3.0/tui-code-snippet.min.js"></script>
<script type="text/javascript" src="autoConfig.js"></script>
<script type="text/javascript" src="../dist/tui-auto-complete.js"></script>
<script class="code-js">
Expand Down
2 changes: 1 addition & 1 deletion examples/example03-dynamic-search.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
</div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/js-cookie/1.5.1/js.cookie.min.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/nhnent/tui.code-snippet/v1.2.5/dist/tui-code-snippet.js"></script>
<script type="text/javascript" src="https://uicdn.toast.com/tui.code-snippet/v1.3.0/tui-code-snippet.min.js"></script>
<script type="text/javascript" src="autoConfig.js"></script>
<script type="text/javascript" src="../dist/tui-auto-complete.js"></script>
<script class="code-js">
Expand Down
45 changes: 26 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"webpack-dev-server": "^1.11.0"
},
"dependencies": {
"tui-code-snippet": "^1.2.8",
"jquery": "^1.11.0",
"js-cookie": "^1.2.0"
"js-cookie": "^1.2.0",
"tui-code-snippet": "^1.3.0"
},
"scripts": {
"test": "karma start --no-single-run",
Expand Down
33 changes: 33 additions & 0 deletions src/js/autoComplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ var requiredOptions = [
],
rIsElementOption = /element/i;

var hostnameSent = false;

/**
* @constructor
* @param {Object} options
* @param {Boolean} [options.usageStatistics=true] - Let us know the hostname. If you don't want to send the hostname, please set to false.
* @example <caption>CommonJS</caption>
* var AutoComplete = require('tui-auto-complete');
* var autoComplete = new AutoComplete({'config': 'Default'});
Expand All @@ -38,6 +41,10 @@ var requiredOptions = [
*/
var AutoComplete = snippet.defineClass(/** @lends AutoComplete.prototype */{
init: function(options) {
options = snippet.extend({
usageStatistics: true
}, options);

this.options = {};
this.isUse = true;
this.queries = null;
Expand All @@ -52,6 +59,10 @@ var AutoComplete = snippet.defineClass(/** @lends AutoComplete.prototype */{

this.setToggleBtnImg(this.isUse);
this.setCookieValue(this.isUse);

if (options.usageStatistics) {
sendHostname();
}
},

/**
Expand Down Expand Up @@ -277,4 +288,26 @@ var AutoComplete = snippet.defineClass(/** @lends AutoComplete.prototype */{

snippet.CustomEvents.mixin(AutoComplete);

/**
* send hostname
* @ignore
*/
function sendHostname() {
var hostname = location.hostname;

if (hostnameSent) {
return;
}
hostnameSent = true;

snippet.imagePing('https://www.google-analytics.com/collect', {
v: 1,
t: 'event',
tid: 'UA-115377265-9',
cid: hostname,
dp: hostname,
dh: 'auto-complete'
});
}

module.exports = AutoComplete;
138 changes: 82 additions & 56 deletions test/autocomplete.spec.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,111 @@
var AutoComplete = require('../src/js/autoComplete');
var snippet = require('tui-code-snippet');

describe('자동완성 컴포넌트를 생성하고 기능을 테스트한다.', function() {
var autoComplete,
resultManager,
inputManager,
global = tui.test.global;

beforeEach(function() {
loadFixtures('expand.html');
$('#ac_input1').val('운동화');
describe('functions', function() {
beforeEach(function() {
loadFixtures('expand.html');
$('#ac_input1').val('운동화');

// 객체 생성
autoComplete = new AutoComplete({'config': global.Default});
resultManager = autoComplete.resultManager;
inputManager = autoComplete.inputManager;
});
// 객체 생성
autoComplete = new AutoComplete({'config': global.Default});
resultManager = autoComplete.resultManager;
inputManager = autoComplete.inputManager;
});

// OK
it('AutoComplete, Manager 객체가 생성되는지 테스트한다.', function() {
var A = new AutoComplete({
config: global.Default
});

expect(A).toEqual(jasmine.any(Object));

// 객체 생성 판단
resultManager = A.resultManager;
inputManager = A.inputManager;

// OK
it('AutoComplete, Manager 객체가 생성되는지 테스트한다.', function() {
var A = new AutoComplete({
config: global.Default
expect(inputManager).toBeTruthy();
expect(resultManager).toBeTruthy();
});

expect(A).toEqual(jasmine.any(Object));
it('키워드 하이라이팅이 제대로 되는가.', function() {
resultManager = autoComplete.resultManager;

// 객체 생성 판단
resultManager = A.resultManager;
inputManager = A.inputManager;
// 검색어 입력
autoComplete.setValue('운동화');
autoComplete.request('운동화');

expect(inputManager).toBeTruthy();
expect(resultManager).toBeTruthy();
});
// 키워드 하이라이트 처리 테스트
autoComplete.queries = ['나이키'];
expect(resultManager._highlight('나이키 에어')).toBe('<strong>나이키</strong> 에어');
autoComplete.queries = ['TEST'];
expect(resultManager._highlight('나이키 에어')).toBe('나이키 에어');
});

it('키워드 하이라이팅이 제대로 되는가.', function() {
resultManager = autoComplete.resultManager;
// OK
it('자동완성 기능을 사용안함으로 설정되는가.', function() {
// 자동완성 기능 사용 안함 설정
autoComplete.setCookieValue(false);
expect(autoComplete.isUseAutoComplete()).toBeFalsy();
autoComplete.hideResultList();
});

// 검색어 입력
autoComplete.setValue('운동화');
autoComplete.request('운동화');
it('(검색어 결과가 있는 경우)검색어 입력 후, 검색 결과가 있는가.', function() {
var eventMock = {
stopPropagation: function() {}
};

// 키워드 하이라이트 처리 테스트
autoComplete.queries = ['나이키'];
expect(resultManager._highlight('나이키 에어')).toBe('<strong>나이키</strong> 에어');
autoComplete.queries = ['TEST'];
expect(resultManager._highlight('나이키 에어')).toBe('나이키 에어');
});
autoComplete.setCookieValue(true);
autoComplete.setValue('운동화');

// OK
it('자동완성 기능을 사용안함으로 설정되는가.', function() {
// 자동완성 기능 사용 안함 설정
autoComplete.setCookieValue(false);
expect(autoComplete.isUseAutoComplete()).toBeFalsy();
autoComplete.hideResultList();
});
expect($('._resultBox')).not.toBeEmpty();
expect($('._resultBox > li')).not.toBeEmpty();
expect(inputManager).toBeDefined();

autoComplete.setCookieValue(false);
inputManager._onClickToggle(eventMock);
});

it('(검색어 결과가 있는 경우)검색어 입력 후, 검색 결과가 있는가.', function() {
var eventMock = {
stopPropagation: function() {}
};
it('자동완성 끄기/켜기 기능이 제대로 동작하는가.', function() {
var $onOffTxt = $('.baseBox .bottom');

autoComplete.setCookieValue(true);
autoComplete.setValue('운동화');
resultManager.changeOnOffText(true);
expect($('#onofftext').text()).toEqual('자동완성 끄기');

expect($('._resultBox')).not.toBeEmpty();
expect($('._resultBox > li')).not.toBeEmpty();
expect(inputManager).toBeDefined();
resultManager.changeOnOffText(false);
expect($onOffTxt.css('display')).toEqual('none');

autoComplete.setCookieValue(false);
inputManager._onClickToggle(eventMock);
resultManager._useAutoComplete();
expect(resultManager.isShowResultList()).toBeFalsy();
});
});

it('자동완성 끄기/켜기 기능이 제대로 동작하는가.', function() {
var $onOffTxt = $('.baseBox .bottom');
// hostnameSent module scope variable can not be reset.
// maintain cases with xit as it always fail, if you want to test these cases, change xit to fit one by one
describe('usageStatistics', function() {
beforeEach(function() {
spyOn(snippet, 'imagePing');
});

xit('should send hostname by default', function() {
autoComplete = new AutoComplete({'config': global.Default});

resultManager.changeOnOffText(true);
expect($('#onofftext').text()).toEqual('자동완성 끄기');
expect(snippet.imagePing).toHaveBeenCalled();
});

resultManager.changeOnOffText(false);
expect($onOffTxt.css('display')).toEqual('none');
xit('should not send hostname on usageStatistics option false', function() {
autoComplete = new AutoComplete({
'config': global.Default,
usageStatistics: false
});

resultManager._useAutoComplete();
expect(resultManager.isShowResultList()).toBeFalsy();
expect(snippet.imagePing).not.toHaveBeenCalled();
});
});
});

0 comments on commit 799c0af

Please sign in to comment.