Skip to content

Commit

Permalink
fix(persist): improve isNeeded method
Browse files Browse the repository at this point in the history
make improvement to isNeeded method
add dependency to eg.agent
add unit tests for isNeeded
update isNeeded jsdoc
add persist custom event jsdoc
merge jsdoc into one method definition

FIX #382
FIX #387
  • Loading branch information
happyhj authored Nov 24, 2016
1 parent 6039277 commit e22a8f1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 74 deletions.
101 changes: 41 additions & 60 deletions src/plugin/persist.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
*/

// jscs:disable maximumLineLength
eg.module("persist", ["jQuery", window, document], function($, global, doc) {
eg.module("persist", ["jQuery", eg, window, document], function($, ns, global, doc) {
"use strict";

// jscs:enable maximumLineLength
var wp = global.performance;
var history = global.history;
var agent = ns.agent();

var isNeeded = (function() {
var ua = global.navigator.userAgent;
var version = ua ? ua.match(/Android\s([^\;]*)/i) : null;
var version = parseFloat(agent.os.version);

/*
* a isNeeded value is
* - iOS: false,
* - Android 4.4+: true
* - Android 4.4 and less: false
*/
return !(/iPhone|iPad/.test(ua) || (version ? parseFloat(version.pop()) < 4.4 : true));
return !(
agent.os.name === "ios" ||
(agent.os.name === "mac" && agent.browser.name === "safari") ||
(agent.os.name === "android" &&
(version <= 4.3 && agent.browser.webview === true || version < 3))
);
})();

var JSON = global.JSON;
Expand Down Expand Up @@ -76,6 +76,20 @@ eg.module("persist", ["jQuery", window, document], function($, global, doc) {

// jscs:enable maximumLineLength

/**
* This jQuery custom event is fired when device rotates.
*
* @ko 기기가 회전할 때 발생하는 jQuery 커스텀 이벤트
* @name jQuery#persist
* @event
* @deprecated since version 1.2.0
* @example
* $(window).on("persist",function(){
* var state = $.persist("KEY");
* // Restore state
* });
*
*/
function onPageshow(e) {
isPersisted = isPersisted || (e.originalEvent && e.originalEvent.persisted);
if (!isPersisted && isBackForwardNavigated) {
Expand Down Expand Up @@ -177,58 +191,23 @@ eg.module("persist", ["jQuery", window, document], function($, global, doc) {
setState(beforeData);
}
/**
* Stores the current state of the web page in a default key using JSON.
* @ko 웹 페이지의 현재 상태를 기본 키에 JSON 형식으로 저장한다.
* @method jQuery.persist
* @deprecated since version 1.2.0
* @support {"ie": "9+", "ch" : "latest", "ff" : "1.5+", "sf" : "latest", "edge" : "latest", "ios" : "7+", "an" : "2.2+ (except 3.x)"}
* @param {Object} state The state information of the web page written in JSON <ko>JSON 객체로 정의한 웹 페이지의 상태 정보</ko>
* @example
$("a").on("click",function(e){
e.preventdefault();
// save state
$.persist(state);
});
*/
/**
* Return stored state in global namespace as object
* @ko 디폴트 키에 저장된 상태를 반환한다.
` * @method jQuery.persist
* @deprecated since version 1.2.0
* @return {Object}
* @example
$("a").on("click",function(e){
e.preventdefault();
// get state
var state = $.persist();
});
*/
/**
* Stores the current state of the web page using JSON.
* @ko 웹 페이지의 현재 상태를 JSON 형식으로 저장한다
* Get or store the current state of the web page using JSON.
* @ko 웹 페이지의 현재 상태를 JSON 형식으로 저장하거나 읽는다.
* @method jQuery.persist
* @param {String} key The key of the state information to be stored <ko>저장할 상태 정보의 키</ko>
* @param {Object} state The value to be stored in a given key<ko>키에 저장할 값</ko>
* @param {Object} [state] The value to be stored in a given key <ko>키에 저장할 값</ko>
* @example
$("a").on("click",function(e){
e.preventdefault();
// save state
$.persist("KEY",state);
});
*/
/**
* Returns the state of stored web pages.
* @ko 저장된 웹 페이지의 상태를 반환한다
* @method jQuery.persist
* @param {String} key The name of the key to be checked<ko>값을 확인할 키의 이름</ko>
* @return {Object} The value of the key <ko>키의 값</ko>
// when 'key' and 'value' are given, it saves state object
$.persist("KEY",state);
// when only 'key' is given, it loads state object
var state = $.persist("KEY");
* @example
$("a").on("click",function(e){
e.preventdefault();
// get state
var state = $.persist("KEY");
});
*/
// this is deprecated API
// save state without Key
$.persist(state);
// get state without Key
var state = $.persist();
*/
$.persist = function(state, data) {
var key;

Expand All @@ -249,9 +228,11 @@ eg.module("persist", ["jQuery", window, document], function($, global, doc) {
/**
* Return whether you need "Persist" module by checking the bfCache support of the current browser
* @ko 현재 브라우저의 bfCache 지원여부에 따라 persist 모듈의 필요여부를 반환한다.
* @method $.persist.isApplicable
* @group jQuery Extension
* @namespace
* @property {function} isNeeded
* @example
$.persist.isApplicable();
$.persist.isNeeded();
*/
$.persist.isNeeded = function() {
return isNeeded;
Expand Down
64 changes: 50 additions & 14 deletions test/unit/js/persist.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module("persist: mock", {
this.state = state;
};

this.method = eg.invoke("persist",[null, this.fakeWindow, this.fakeDocument]);
this.method = eg.invoke("persist",[null, eg, this.fakeWindow, this.fakeDocument]);
this.GLOBALKEY = this.method.GLOBALKEY;
},
teardown: function() {
Expand Down Expand Up @@ -142,7 +142,7 @@ test("onPageshow : when bfCache miss and not BF navigated, _reset method must be
var ht = {};
ht[this.GLOBALKEY] = this.data;
this.fakeWindow.performance.navigation.type = 2; // navigation
eg.invoke("persist",[null, this.fakeWindow, this.fakeDocument]);
eg.invoke("persist",[null, eg, this.fakeWindow, this.fakeDocument]);
$.persist(this.data)
deepEqual($.persist(), this.data);

Expand All @@ -160,7 +160,7 @@ test("onPageshow : when bfCache miss and not BF navigated, _reset method must be
// When
this.fakeWindow.performance.navigation.type = 0; // enter url...
this.fakeWindow.history.state = JSON.stringify(ht);
eg.invoke("persist",[null, this.fakeWindow, this.fakeDocument]);
eg.invoke("persist",[null, eg, this.fakeWindow, this.fakeDocument]);

// Then
equal($.persist(), null); // must reset
Expand All @@ -179,7 +179,7 @@ test("onPageshow : when bfCache miss and not BF navigated, _reset method must be
// When
this.fakeWindow.performance.navigation.type = 1;
this.fakeWindow.history.state = JSON.stringify(ht);
eg.invoke("persist",[null, this.fakeWindow, this.fakeDocument]);
eg.invoke("persist",[null, eg, this.fakeWindow, this.fakeDocument]);

// Then
equal($.persist(), null);
Expand All @@ -206,7 +206,7 @@ test("onPageshow : when bfCache miss and BF navigated, persist event must be tri
TYPE_RESERVED: 255
};
this.fakeWindow.performance.navigation.type = 2;
eg.invoke("persist",[null, this.fakeWindow, this.fakeDocument]);
eg.invoke("persist",[null, eg, this.fakeWindow, this.fakeDocument]);

var restoredState = null;
$(this.fakeWindow).on("persist", function(e) {
Expand All @@ -232,7 +232,7 @@ test("Test not throwing error for legacy browsers", function() {
delete this.fakeWindow.sessionStorage;

// When
var method = eg.invoke("persist",[null, this.fakeWindow, this.fakeDocument]);
var method = eg.invoke("persist", [null, eg, this.fakeWindow, this.fakeDocument]);

// Then
ok(!method, "If browser don't have history.state neither web storage, persist shouldn't be defined.");
Expand All @@ -248,7 +248,7 @@ test("Test for browsers which don't have JSON object", function() {
}

// When
var method = eg.invoke("persist",[null, this.fakeWindow, this.fakeDocument]);
var method = eg.invoke("persist",[null, eg, this.fakeWindow, this.fakeDocument]);

// Then
ok(!method, "If browser don't have JSON object, persist shouldn't be defined.");
Expand All @@ -262,7 +262,7 @@ module("persist: native", {
this.data = {
"scrollTop": 100
};
this.method = eg.invoke("persist",[null, window, document]);
this.method = eg.invoke("persist",[null, eg, window, document]);
this.GLOBALKEY = this.method.GLOBALKEY;
},
teardown: function() {
Expand Down Expand Up @@ -319,7 +319,7 @@ test("Test not throwing error for legacy browsers", function() {
"sessionStorage" in window || "localStorage" in window;

// When
var persist = eg.invoke("persist",[null, window, document]);
var persist = eg.invoke("persist",[null, eg, window, document]);

// Then
ok(isPersistAvailable ? persist : !persist,
Expand All @@ -336,7 +336,7 @@ test("Test for browsers which don't have JSON object", function() {
}

// When
var persist = eg.invoke("persist",[null, window, document]);
var persist = eg.invoke("persist",[null, eg, window, document]);

// Then
ok(isSupportJSON ? persist : !persist, "If browser don't have JSON object, persist shouldn't be defined.");
Expand All @@ -347,19 +347,54 @@ test("Test for browsers which don't have JSON object", function() {

var ua = [
{
"device": "Android 4.3.0",
"device": "Android 2.3.6 SamsungBrowser",
"ua": "Mozilla/5.0 (Linux;U;Android 2.3.6;ko-kr;SHV-E160S Build/GINGERBREAD) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1",
"isNeeded": false
},
{
"device": "Android 4.3.0 chrome42",
"ua": "Mozilla/5.0 (Linux; Android 4.3.0; SM-G900S Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.108 Mobile Safari/537.36",
"isNeeded": true
},
{
"device": "Android 4.3.0(galaxy s3) SamsungBrowser",
"ua": "Mozilla/5.0 (Linux; Android 4.3; ko-kr; SHW-M440S Build/JSS15J) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30",
"isNeeded": true
},
{
"device": "Android 4.0.4 webview",
"ua": "Mozilla/5.0 (Linux; U; Android 4.0.4; SM-E210S Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 NAVER(inapp; search; 490; 7.4.1)",
"isNeeded": false
},
{
"device": "Android 5.1.1",
"device": "Android 5.1.1 SamsungBrowser",
"ua": "Mozilla/5.0 (Linux; Android 5.1.1; SAMSUNG SM-G925S Build/LMY47X) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/3.2 Chrome/38.0.2125.102 Mobile Safari/537.36",
"isNeeded": true
},
{
"device": "iOS 8.0",
"ua": "Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12B440",
"isNeeded": false
},
{
"device": "iOS 7.0",
"ua": "Mozilla/5.0 (iPhone;CPU iPhone OS 7_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10B146 Safari/8536.25",
"isNeeded": false
},
{
"device": "IE10",
"ua": "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)",
"isNeeded": true
},
{
"device": "IE9",
"ua": "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
"isNeeded": true
},
{
"device": "Chrome",
"ua": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/53.0.2785.143 Chrome/53.0.2785.143 Safari/537.36",
"isNeeded": true
}
];

Expand Down Expand Up @@ -402,10 +437,11 @@ module("extend Agent Test", {
});

$.each(ua, function(i, v) {
test("$.persist.isNeeded : "+ v.device, function() {
test("$.persist.isNeeded : "+ v.device + "(" +v.isNeeded+ ")", function() {
// Given
this.fakeWindow.navigator.userAgent = v.ua;
eg.invoke("persist", [null, this.fakeWindow]);
eg.invoke("eg",[null, null, this.fakeWindow]);
eg.invoke("persist", [null, eg, this.fakeWindow]);
equal($.persist.isNeeded(), v.isNeeded, "isNeeded");
});
});
3 changes: 3 additions & 0 deletions test/unit/persist.test.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ <h2 id="qunit-userAgent"></h2>
<script type="text/javascript">
eg.debug = true;
</script>
<script src="../../src/agent.js"></script>
<script src="../../src/eg.js"></script>
<script src="../../src/plugin/persist.js"></script>

<!-- user script files end -->
<!-- test script files start -->
<script src="js/persist.test.js"></script>
Expand Down

0 comments on commit e22a8f1

Please sign in to comment.