Skip to content

Commit

Permalink
feat: explicitly lookup session and store services
Browse files Browse the repository at this point in the history
  • Loading branch information
BobrImperator committed Jun 8, 2021
1 parent 1cca7aa commit b8fd72f
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 79 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import Ember from 'ember';
import InternalSession from '../internal-session';
import Ephemeral from '../session-stores/ephemeral';
import inject from '../utils/inject';

export default function setupSession(registry) {
registry.register('session:main', InternalSession);

let store = 'session-store:application';
if (Ember.testing) {
store = 'session-store:test';
registry.register(store, Ephemeral);
registry.register('session-store:test', Ephemeral);
}

inject(registry, 'session:main', 'store', store);
}
11 changes: 8 additions & 3 deletions packages/ember-simple-auth/addon/internal-session.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import { isEmpty, isNone } from '@ember/utils';
import ObjectProxy from '@ember/object/proxy';
import Evented from '@ember/object/evented';
import { merge, assign as emberAssign } from '@ember/polyfills';
import { set } from '@ember/object';
import { set, computed } from '@ember/object';
import { debug, assert } from '@ember/debug';
import { getOwner, setOwner } from '@ember/application';
const assign = emberAssign || merge;

export default ObjectProxy.extend(Evented, {
authenticator: null,
store: null,
isAuthenticated: false,
attemptedTransition: null,

Expand All @@ -21,6 +20,12 @@ export default ObjectProxy.extend(Evented, {
this._bindToStoreEvents();
},

store: computed(function() {
const owner = getOwner(this);
const store = Ember.testing ? 'session-store:test' : 'session-store:application';
return owner.lookup(store);
}),

authenticate(authenticatorFactory, ...args) {
this._busy = true;
assert(`Session#authenticate requires the authenticator to be specified, was "${authenticatorFactory}"!`, !isEmpty(authenticatorFactory));
Expand Down Expand Up @@ -166,7 +171,7 @@ export default ObjectProxy.extend(Evented, {
},

_bindToStoreEvents() {
this.store.on('sessionDataUpdated', (content) => {
this.get('store').on('sessionDataUpdated', (content) => {
if (!this._busy) {
this._busy = true;
let { authenticator: authenticatorFactory } = (content.authenticated || {});
Expand Down
5 changes: 5 additions & 0 deletions packages/ember-simple-auth/addon/services/session.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { alias, oneWay } from '@ember/object/computed';
import { A } from '@ember/array';
import Service from '@ember/service';
import { computed } from '@ember/object';
import Evented from '@ember/object/evented';
import { getOwner } from '@ember/application';
import { assert } from '@ember/debug';
Expand Down Expand Up @@ -137,6 +138,10 @@ export default Service.extend(Evented, {
*/
attemptedTransition: alias('session.attemptedTransition'),

session: computed(function () {
return getOwner(this).lookup('session:main');
}),

init() {
this._super(...arguments);
this._forwardSessionEvents();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import ENV from '../config/environment';
import Configuration from 'ember-simple-auth/configuration';
import setupSession from 'ember-simple-auth/initializers/setup-session';
import setupSessionService from 'ember-simple-auth/initializers/setup-session-service';
import setupSessionRestoration from 'ember-simple-auth/initializers/setup-session-restoration';

export default {
Expand All @@ -13,7 +12,6 @@ export default {
Configuration.load(config);

setupSession(registry);
setupSessionService(registry);
setupSessionRestoration(registry);
}
};

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -42,29 +42,5 @@ describe('setupSession', () => {

expect(registry.register).to.have.been.calledWith('session-store:test', Ephemeral);
});

it('injects the test session store into the session', function() {
sinon.spy(registry, 'injection');
setupSession(registry);

expect(registry.injection).to.have.been.calledWith('session:main', 'store', 'session-store:test');
});
});

describe('when Ember.testing is false', function() {
beforeEach(function() {
Ember.testing = false; // eslint-disable-line ember/no-ember-testing-in-module-scope
});

afterEach(function() {
Ember.testing = true; // eslint-disable-line ember/no-ember-testing-in-module-scope
});

it('injects the application session store into the session', function() {
sinon.spy(registry, 'injection');
setupSession(registry);

expect(registry.injection).to.have.been.calledWith('session:main', 'store', 'session-store:application');
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import Ember from 'ember';
import { describe, beforeEach, it } from 'mocha';
import { setupTest } from 'ember-mocha';
import { expect } from 'chai';
import sinonjs from 'sinon';
import InternalSession from 'ember-simple-auth/internal-session';
import EphemeralStore from 'ember-simple-auth/session-stores/ephemeral';

describe('InternalSession store injection', () => {
setupTest();

let sinon;
let session;

beforeEach(function() {
sinon = sinonjs.createSandbox();
});

afterEach(function() {
sinon.restore();
});

describe('session store injection', function () {
afterEach(function () {
Ember.testing = true;
});

it('looks up the test session store when Ember.testing true', function() {
Ember.testing = true;

this.owner.register('session-store:test', EphemeralStore);
session = InternalSession.create(this.owner.ownerInjection());

expect(session.get('store')).to.eql(this.owner.lookup('session-store:test'));
});

it('looks up the application session store when Ember.testing false', function() {
Ember.testing = false;
sinon = sinonjs.createSandbox();

this.owner.register('session-store:test', EphemeralStore);
session = InternalSession.create(this.owner.ownerInjection());

expect(session.get('store')).to.eql(this.owner.lookup('session-store:application'));
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ describe('InternalSession', () => {
beforeEach(function() {
sinon = sinonjs.createSandbox();

store = EphemeralStore.create();
authenticator = Authenticator.create();
this.owner.register('authenticator:test', authenticator, { instantiate: false });
this.owner.register('session-store:test', EphemeralStore);
store = this.owner.lookup('session-store:test');

session = InternalSession.create({ store });
session = InternalSession.create(this.owner.ownerInjection());
setOwner(session, this.owner);
});

Expand Down Expand Up @@ -810,8 +811,7 @@ describe('InternalSession', () => {
});

it('does not share the content object between multiple instances', function() {
let session2 = InternalSession.create({ store });
setOwner(session2, this.owner);
let session2 = InternalSession.create(this.owner.ownerInjection());

expect(session2.get('content')).to.not.equal(session.get('content'));
});
Expand Down
12 changes: 6 additions & 6 deletions packages/ember-simple-auth/tests/unit/services/session-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ describe('SessionService', () => {

beforeEach(function() {
sinon = sinonjs.createSandbox();
session = ObjectProxy.extend(Evented, {
this.owner.register('authorizer:custom', EmberObject.extend({
authorize() {}
}));
this.owner.register('session:main', ObjectProxy.extend(Evented, {
init() {
this._super(...arguments);
this.content = {};
}
}).create();

this.owner.register('authorizer:custom', EmberObject.extend({
authorize() {}
}));

sessionService = Session.create({ session });
session = this.owner.lookup('session:main');
sessionService = Session.create(this.owner.ownerInjection());
setOwner(sessionService, this.owner);
this.owner.register('service:session', sessionService, { instantiate: false });
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ export default function(options) {
expect(triggered).to.be.true;
});

it('does not trigger the "sessionDataUpdated" event when the data in the browser storage has not changed', function() {
it('does not trigger the "sessionDataUpdated" event when the data in the browser storage has not changed', async function() {
let triggered = false;
store = options.store();
store.on('sessionDataUpdated', () => {
triggered = true;
});

store.persist({ key: 'value' }); // this data will be read again when the event is handled so that no change will be detected
await store.persist({ key: 'value' }); // this data will be read again when the event is handled so that no change will be detected
window.dispatchEvent(new StorageEvent('storage', { key: store.get('key') }));

expect(triggered).to.be.false;
Expand Down

0 comments on commit b8fd72f

Please sign in to comment.