From 839769bc2e10f1c437457d66189c70f290b18e16 Mon Sep 17 00:00:00 2001 From: bobrimperator Date: Thu, 26 Dec 2024 04:40:09 +0100 Subject: [PATCH] refactor(ember-simple-auth): adaptive store to es6 --- .../src/session-stores/adaptive.js | 63 +++++++++-------- .../tests/helpers/create-adaptive-store.js | 17 ----- .../unit/session-stores/adaptive-test.js | 68 +++++++++---------- .../shared/cookie-store-behavior.js | 25 ++++--- 4 files changed, 80 insertions(+), 93 deletions(-) delete mode 100644 packages/test-esa/tests/helpers/create-adaptive-store.js diff --git a/packages/ember-simple-auth/src/session-stores/adaptive.js b/packages/ember-simple-auth/src/session-stores/adaptive.js index bbf35973e..faa65869c 100644 --- a/packages/ember-simple-auth/src/session-stores/adaptive.js +++ b/packages/ember-simple-auth/src/session-stores/adaptive.js @@ -38,7 +38,7 @@ const proxyToInternalStore = function () { @extends BaseStore @public */ -export default Base.extend({ +export default class AdaptiveStore extends Base { /** The `localStorage` key the store persists data in if `localStorage` is available. @@ -49,7 +49,7 @@ export default Base.extend({ @default 'ember_simple_auth-session' @public */ - localStorageKey: 'ember_simple_auth-session', + localStorageKey = 'ember_simple_auth-session'; /** The domain to use for the cookie if `localStorage` is not available, e.g., @@ -63,8 +63,9 @@ export default Base.extend({ @default null @public */ - _cookieDomain: null, - cookieDomain: proxyToInternalStore(), + _cookieDomain = null; + @proxyToInternalStore() + cookieDomain; /** Allows servers to assert that a cookie ought not to be sent along with @@ -81,8 +82,9 @@ export default Base.extend({ @default null @public */ - _sameSite: null, - sameSite: proxyToInternalStore(), + _sameSite = null; + @proxyToInternalStore() + sameSite; /** Allows servers to assert that a cookie should opt in to partitioned storage, @@ -99,8 +101,9 @@ export default Base.extend({ @default null @public */ - _partitioned: null, - partitioned: proxyToInternalStore(), + _partitioned = null; + @proxyToInternalStore() + partitioned; /** The name of the cookie to use if `localStorage` is not available. @@ -111,8 +114,9 @@ export default Base.extend({ @default ember_simple_auth-session @public */ - _cookieName: 'ember_simple_auth-session', - cookieName: proxyToInternalStore(), + _cookieName = 'ember_simple_auth-session'; + @proxyToInternalStore() + cookieName; /** The path to use for the cookie, e.g., "/", "/something". @@ -123,8 +127,9 @@ export default Base.extend({ @default '/' @public */ - _cookiePath: '/', - cookiePath: proxyToInternalStore(), + _cookiePath = '/'; + @proxyToInternalStore() + cookiePath; /** The expiration time for the cookie in seconds if `localStorage` is not @@ -137,20 +142,20 @@ export default Base.extend({ @type Integer @public */ - _cookieExpirationTime: null, - cookieExpirationTime: proxyToInternalStore(), + _cookieExpirationTime = null; + @proxyToInternalStore() + cookieExpirationTime; - _cookies: service('cookies'), + @service('cookies') _cookies; - _isLocalStorageAvailable: computed({ - get() { + __isLocalStorageAvailable = null; + get _isLocalStorageAvailable() { + if (this.__isLocalStorageAvailable !== null) { + return this.__isLocalStorageAvailable; + } else { return testLocalStorageAvailable(); - }, - - set(key, value) { - return value; - }, - }), + } + } init() { this._super(...arguments); @@ -188,14 +193,14 @@ export default Base.extend({ this.set('_store', store); this._setupStoreEvents(store); - }, + } _setupStoreEvents(store) { store.on('sessionDataUpdated', ({ detail: data }) => { this.trigger('sessionDataUpdated', data); }); return store; - }, + } /** Persists the `data` in the `localStorage` if it is available or in a cookie @@ -209,7 +214,7 @@ export default Base.extend({ */ persist() { return this.get('_store').persist(...arguments); - }, + } /** Returns all data currently stored in the `localStorage` if that is @@ -222,7 +227,7 @@ export default Base.extend({ */ restore() { return this.get('_store').restore(); - }, + } /** Clears the store by deleting the @@ -236,8 +241,8 @@ export default Base.extend({ */ clear() { return this.get('_store').clear(); - }, -}); + } +} function testLocalStorageAvailable() { try { diff --git a/packages/test-esa/tests/helpers/create-adaptive-store.js b/packages/test-esa/tests/helpers/create-adaptive-store.js deleted file mode 100644 index 754130702..000000000 --- a/packages/test-esa/tests/helpers/create-adaptive-store.js +++ /dev/null @@ -1,17 +0,0 @@ -import Adaptive from 'ember-simple-auth/session-stores/adaptive'; - -export default function createAdaptiveStore(cookiesService, options = {}, owner) { - owner.register( - 'session-store:adaptive', - Adaptive.extend( - Object.assign( - { - _isLocalStorageAvailable: false, - }, - options - ) - ) - ); - - return owner.lookup('session-store:adaptive'); -} diff --git a/packages/test-esa/tests/unit/session-stores/adaptive-test.js b/packages/test-esa/tests/unit/session-stores/adaptive-test.js index 19188c786..75a064868 100644 --- a/packages/test-esa/tests/unit/session-stores/adaptive-test.js +++ b/packages/test-esa/tests/unit/session-stores/adaptive-test.js @@ -5,7 +5,7 @@ import sinonjs from 'sinon'; import itBehavesLikeAStore from './shared/store-behavior'; import itBehavesLikeACookieStore from './shared/cookie-store-behavior'; import FakeCookieService from '../../helpers/fake-cookie-service'; -import createAdaptiveStore from '../../helpers/create-adaptive-store'; +import AdaptiveStore from 'ember-simple-auth/session-stores/adaptive'; module('AdaptiveStore', function (hooks) { setupTest(hooks); @@ -14,20 +14,19 @@ module('AdaptiveStore', function (hooks) { itBehavesLikeAStore({ hooks, store(sinon, owner) { - let store; let cookieService; owner.register('service:cookies', FakeCookieService); cookieService = owner.lookup('service:cookies'); sinon.spy(cookieService, 'read'); sinon.spy(cookieService, 'write'); - store = createAdaptiveStore( - cookieService, - { - _isLocalStorageAvailable: true, - }, - owner + + owner.register( + 'session-store:adaptive', + class TestAdaptiveStorage extends AdaptiveStore { + __isLocalStorageAvailable = true; + } ); - return store; + return owner.lookup('session-store:adaptive'); }, }); }); @@ -36,21 +35,19 @@ module('AdaptiveStore', function (hooks) { itBehavesLikeAStore({ hooks, store(sinon, owner) { - let store; let cookieService; owner.register('service:cookies', FakeCookieService); cookieService = owner.lookup('service:cookies'); sinon.spy(cookieService, 'read'); sinon.spy(cookieService, 'write'); - store = createAdaptiveStore( - cookieService, - { - _isLocalStorageAvailable: false, - _cookieName: 'test:session', - }, - owner + owner.register( + 'session-store:adaptive', + class TestAdaptiveStorage extends AdaptiveStore { + __isLocalStorageAvailable = false; + _cookieName = 'test:session'; + } ); - return store; + return owner.lookup('session-store:adaptive'); }, }); }); @@ -59,23 +56,20 @@ module('AdaptiveStore', function (hooks) { module('Behaviour', function (hooks) { itBehavesLikeACookieStore({ hooks, - store(sinon, owner, { adaptive: storeOptions } = {}) { + store(sinon, owner, { adaptive: klass } = {}) { owner.register('service:cookies', FakeCookieService); let cookieService = owner.lookup('service:cookies'); sinon.spy(cookieService, 'read'); sinon.spy(cookieService, 'write'); - let store = createAdaptiveStore( - cookieService, - Object.assign( - { - _isLocalStorageAvailable: false, - _cookieName: 'test:session', - }, - storeOptions - ), - owner + owner.register( + 'session-store:adaptive', + klass || + class TestAdaptiveStorage extends AdaptiveStore { + __isLocalStorageAvailable = false; + _cookieName = 'test:session'; + } ); - return store; + return owner.lookup('session-store:adaptive'); }, renew(store, data) { return store.get('_store')._renew(data); @@ -102,14 +96,14 @@ module('AdaptiveStore', function (hooks) { sinon.spy(cookieService, 'write'); run(() => { now = new Date(); - store = createAdaptiveStore( - cookieService, - { - _isLocalStorageAvailable: false, - _cookieName: 'test:session', - }, - this.owner + this.owner.register( + 'session-store:adaptive', + class TestAdaptiveStorage extends AdaptiveStore { + __isLocalStorageAvailable = false; + _cookieName = 'test:session'; + } ); + store = this.owner.lookup('session-store:adaptive'); store.setProperties({ cookieName: 'test:session', diff --git a/packages/test-esa/tests/unit/session-stores/shared/cookie-store-behavior.js b/packages/test-esa/tests/unit/session-stores/shared/cookie-store-behavior.js index 009a72af2..2f60c94dc 100644 --- a/packages/test-esa/tests/unit/session-stores/shared/cookie-store-behavior.js +++ b/packages/test-esa/tests/unit/session-stores/shared/cookie-store-behavior.js @@ -4,6 +4,7 @@ import { registerWarnHandler } from '@ember/debug'; import sinonjs from 'sinon'; import FakeCookieService from '../../../helpers/fake-cookie-service'; import CookieStore from 'ember-simple-auth/session-stores/cookie'; +import AdaptiveStore from 'ember-simple-auth/session-stores/adaptive'; let warnings; registerWarnHandler((message, options, next) => { @@ -178,9 +179,10 @@ export default function (options) { cookieName = 'test-session'; cookieExpirationTime = 60; }, - adaptive: { - cookieName: 'test-session', - cookieExpirationTime: 60, + adaptive: class TestAdaptiveStore extends AdaptiveStore { + __isLocalStorageAvailable = false; + cookieName = 'test-session'; + cookieExpirationTime = 60; }, }); cookieService = store.get('_cookies'); @@ -217,8 +219,9 @@ export default function (options) { cookie: class TestRenewCookieStore extends CookieStore { cookieName = 'ember_simple_auth-session'; }, - adaptive: { - cookieName: 'ember_simple_auth-session', + adaptive: class TestAdaptiveStore extends AdaptiveStore { + __isLocalStorageAvailable = false; + cookieName = 'ember_simple_auth-session'; }, }); triggered = false; @@ -287,9 +290,10 @@ export default function (options) { _cookieName = 'session-foo'; _cookieExpirationTime = 1000; }, - adaptive: { - _cookieName: 'session-foo', - _cookieExpirationTime: 1000, + adaptive: class TestAdaptiveStore extends AdaptiveStore { + __isLocalStorageAvailable = false; + _cookieName = 'session-foo'; + _cookieExpirationTime = 1000; }, }); cookieService = store.get('_cookies') || store.get('_store._cookies'); @@ -444,8 +448,9 @@ export default function (options) { cookie: class TestRenewCookieStore extends CookieStore { cookieExpirationTime = expirationTime; }, - adaptive: { - cookieExpirationTime: expirationTime, + adaptive: class TestAdaptiveStore extends AdaptiveStore { + _isLocalStorageAvailable = false; + cookieExpirationTime = expirationTime; }, }); cookieService = store.get('_cookies');