-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Navegg UserID Submodule: conform with pub storage configuration (#12032)
* dev/ simple solution * dev: working script and working tests * dev/ final adjustments * dev: minor-changes * hotfix/ importing only the necessary functions
- Loading branch information
Showing
3 changed files
with
211 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,155 @@ | ||
import { naveggIdSubmodule, storage } from 'modules/naveggIdSystem.js'; | ||
import { naveggIdSubmodule, storage, getIdFromAPI } from 'modules/naveggIdSystem.js'; | ||
import { server } from 'test/mocks/xhr.js'; | ||
import * as ajaxLib from 'src/ajax.js'; | ||
|
||
describe('naveggId', function () { | ||
let sandbox; | ||
beforeEach(() => { | ||
sandbox = sinon.sandbox.create(); | ||
sandbox.stub(storage, 'getDataFromLocalStorage'); | ||
const NAVEGGID_CONFIG_COOKIE_HTML5 = { | ||
storage: { | ||
name: 'nvggid', | ||
type: 'cookie&html5', | ||
expires: 8 | ||
} | ||
} | ||
|
||
const MOCK_RESPONSE = { | ||
nvggid: 'test_nvggid' | ||
} | ||
|
||
const MOCK_RESPONSE_NULL = { | ||
nvggid: null | ||
} | ||
|
||
function mockResponse(responseText, isSuccess = true) { | ||
return function(url, callbacks) { | ||
if (isSuccess) { | ||
callbacks.success(responseText) | ||
} else { | ||
callbacks.error(new Error('Mock Error')) | ||
} | ||
} | ||
} | ||
|
||
function deleteAllCookies() { | ||
document.cookie.split(';').forEach(cookie => { | ||
const eqPos = cookie.indexOf('='); | ||
const name = eqPos > -1 ? cookie.substring(0, eqPos) : cookie; | ||
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:00 GMT'; | ||
}); | ||
} | ||
|
||
function setLocalStorage() { | ||
storage.setDataInLocalStorage('nvggid', 'localstorage_value'); | ||
} | ||
|
||
describe('getId', function () { | ||
let ajaxStub, ajaxBuilderStub; | ||
|
||
beforeEach(function() { | ||
ajaxStub = sinon.stub(); | ||
ajaxBuilderStub = sinon.stub(ajaxLib, 'ajaxBuilder').returns(ajaxStub); | ||
}); | ||
|
||
afterEach(function() { | ||
ajaxBuilderStub.restore(); | ||
deleteAllCookies(); | ||
storage.removeDataFromLocalStorage('nvggid'); | ||
}); | ||
|
||
it('should get the value from the existing localstorage', function() { | ||
setLocalStorage(); | ||
|
||
const callback = sinon.spy(); | ||
const apiCallback = naveggIdSubmodule.getId(NAVEGGID_CONFIG_COOKIE_HTML5).callback; | ||
|
||
ajaxStub.callsFake((url, successCallbacks, errorCallback, options) => { | ||
if (successCallbacks && typeof successCallbacks === 'function') { | ||
successCallbacks(JSON.stringify(MOCK_RESPONSE_NULL)); | ||
} | ||
}); | ||
apiCallback(callback) | ||
|
||
expect(callback.calledOnce).to.be.true; | ||
expect(callback.calledWith('localstorage_value')).to.be.true; | ||
}); | ||
afterEach(() => { | ||
sandbox.restore(); | ||
|
||
it('should get the value from a nid cookie', function() { | ||
storage.setCookie('nid', 'old_nid_cookie', storage.expires) | ||
|
||
const callback = sinon.spy(); | ||
const apiCallback = naveggIdSubmodule.getId(NAVEGGID_CONFIG_COOKIE_HTML5).callback; | ||
|
||
ajaxStub.callsFake((url, successCallbacks, errorCallback, options) => { | ||
if (successCallbacks && typeof successCallbacks === 'function') { | ||
successCallbacks(JSON.stringify(MOCK_RESPONSE_NULL)); | ||
} | ||
}); | ||
apiCallback(callback) | ||
|
||
expect(callback.calledOnce).to.be.true; | ||
expect(callback.calledWith('old_nid_cookie')).to.be.true; | ||
}); | ||
|
||
it('should NOT find navegg id', function () { | ||
let id = naveggIdSubmodule.getId(); | ||
it('should get the value from a nav cookie', function() { | ||
storage.setCookie('navId', 'old_nav_cookie', storage.expires) | ||
|
||
const callback = sinon.spy(); | ||
const apiCallback = naveggIdSubmodule.getId(NAVEGGID_CONFIG_COOKIE_HTML5).callback; | ||
|
||
expect(id).to.be.undefined; | ||
ajaxStub.callsFake((url, successCallbacks, errorCallback, options) => { | ||
if (successCallbacks && typeof successCallbacks === 'function') { | ||
successCallbacks(JSON.stringify(MOCK_RESPONSE_NULL)); | ||
} | ||
}); | ||
apiCallback(callback) | ||
|
||
expect(callback.calledOnce).to.be.true; | ||
expect(callback.calledWith('old_nav_cookie')).to.be.true; | ||
}); | ||
|
||
it('getId() should return "test-nid" id from cookie OLD_NAVEGG_ID', function() { | ||
sinon.stub(storage, 'getCookie').withArgs('nid').returns('test-nid'); | ||
let id = naveggIdSubmodule.getId(); | ||
expect(id).to.be.deep.equal({id: 'test-nid'}) | ||
}) | ||
it('should get the value from an old nvg cookie', function() { | ||
storage.setCookie('nvgid', 'old_nvg_cookie', storage.expires) | ||
|
||
const callback = sinon.spy(); | ||
const apiCallback = naveggIdSubmodule.getId(NAVEGGID_CONFIG_COOKIE_HTML5).callback; | ||
|
||
ajaxStub.callsFake((url, successCallbacks, errorCallback, options) => { | ||
if (successCallbacks && typeof successCallbacks === 'function') { | ||
successCallbacks(JSON.stringify(MOCK_RESPONSE_NULL)); | ||
} | ||
}); | ||
apiCallback(callback) | ||
|
||
it('getId() should return "test-nvggid" id from local storage NAVEGG_ID', function() { | ||
storage.getDataFromLocalStorage.callsFake(() => 'test-ninvggidd') | ||
expect(callback.calledOnce).to.be.true; | ||
expect(callback.calledWith('old_nvg_cookie')).to.be.true; | ||
}); | ||
|
||
let id = naveggIdSubmodule.getId(); | ||
expect(id).to.be.deep.equal({id: 'test-ninvggidd'}) | ||
}) | ||
it('should return correct value from API response', function(done) { | ||
const callback = sinon.spy(); | ||
const apiCallback = naveggIdSubmodule.getId(NAVEGGID_CONFIG_COOKIE_HTML5).callback; | ||
|
||
it('getId() should return "test-nvggid" id from local storage NAV0', function() { | ||
storage.getDataFromLocalStorage.callsFake(() => 'nvgid-nav0') | ||
ajaxStub.callsFake((url, successCallbacks, errorCallback, options) => { | ||
if (successCallbacks && typeof successCallbacks === 'function') { | ||
successCallbacks(JSON.stringify(MOCK_RESPONSE)); | ||
} | ||
}); | ||
apiCallback(callback) | ||
|
||
let id = naveggIdSubmodule.getId(); | ||
expect(id).to.be.deep.equal({id: 'nvgid-nav0'}) | ||
}) | ||
expect(callback.calledOnce).to.be.true; | ||
expect(callback.calledWith('test_nvggid')).to.be.true; | ||
done(); | ||
}); | ||
|
||
it('getId() should return "test-nvggid" id from local storage NVG0', function() { | ||
storage.getDataFromLocalStorage.callsFake(() => 'nvgid-nvg0') | ||
it('should return no value from API response', function(done) { | ||
const callback = sinon.spy(); | ||
const apiCallback = naveggIdSubmodule.getId(NAVEGGID_CONFIG_COOKIE_HTML5).callback; | ||
|
||
let id = naveggIdSubmodule.getId(); | ||
expect(id).to.be.deep.equal({id: 'nvgid-nvg0'}) | ||
}) | ||
ajaxStub.callsFake((url, successCallbacks, errorCallback, options) => { | ||
if (successCallbacks && typeof successCallbacks === 'function') { | ||
successCallbacks(JSON.stringify(MOCK_RESPONSE_NULL)); | ||
} | ||
}); | ||
apiCallback(callback) | ||
expect(callback.calledOnce).to.be.true; | ||
expect(callback.calledWith(undefined)).to.be.true; | ||
done(); | ||
}); | ||
}); |