Skip to content

Commit

Permalink
Merge pull request mainmatter#2881 from mainmatter/migrate-adaptive-t…
Browse files Browse the repository at this point in the history
…o-es6

refactor(ember-simple-auth): adaptive store to es6
  • Loading branch information
BobrImperator authored Dec 26, 2024
2 parents b1ee36f + 839769b commit db45580
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 93 deletions.
63 changes: 34 additions & 29 deletions packages/ember-simple-auth/src/session-stores/adaptive.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.,
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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.
Expand All @@ -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".
Expand All @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -222,7 +227,7 @@ export default Base.extend({
*/
restore() {
return this.get('_store').restore();
},
}

/**
Clears the store by deleting the
Expand All @@ -236,8 +241,8 @@ export default Base.extend({
*/
clear() {
return this.get('_store').clear();
},
});
}
}

function testLocalStorageAvailable() {
try {
Expand Down
17 changes: 0 additions & 17 deletions packages/test-esa/tests/helpers/create-adaptive-store.js

This file was deleted.

68 changes: 31 additions & 37 deletions packages/test-esa/tests/unit/session-stores/adaptive-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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');
},
});
});
Expand All @@ -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');
},
});
});
Expand All @@ -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);
Expand All @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit db45580

Please sign in to comment.