Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7f2481a

Browse files
committedAug 13, 2021
feat: put internal-session into DI system
1 parent e797c0a commit 7f2481a

11 files changed

+65
-129
lines changed
 

‎packages/ember-simple-auth/addon/initializers/setup-session-service.js

-5
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
import Ember from 'ember';
22
import InternalSession from '../internal-session';
33
import Ephemeral from '../session-stores/ephemeral';
4-
import inject from '../utils/inject';
54

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

9-
let store = 'session-store:application';
108
if (Ember.testing) {
11-
store = 'session-store:test';
12-
registry.register(store, Ephemeral);
9+
registry.register('session-store:test', Ephemeral);
1310
}
14-
15-
inject(registry, 'session:main', 'store', store);
1611
}

‎packages/ember-simple-auth/addon/internal-session.js

+7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import Ember from 'ember';
12
import RSVP from 'rsvp';
23
import { isEmpty, isNone } from '@ember/utils';
34
import ObjectProxy from '@ember/object/proxy';
@@ -17,6 +18,12 @@ export default ObjectProxy.extend(Evented, {
1718
init() {
1819
this._super(...arguments);
1920
this.set('content', { authenticated: {} });
21+
let storeFactory = 'session-store:application';
22+
if (Ember.testing) {
23+
storeFactory = 'session-store:test';
24+
}
25+
26+
this.set('store', getOwner(this).lookup(storeFactory));
2027
this._busy = false;
2128
this._bindToStoreEvents();
2229
},

‎packages/ember-simple-auth/addon/services/session.js

+3
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,11 @@ export default Service.extend(Evented, {
137137
*/
138138
attemptedTransition: alias('session.attemptedTransition'),
139139

140+
session: null,
141+
140142
init() {
141143
this._super(...arguments);
144+
this.set('session', getOwner(this).lookup('session:main'));
142145
this._forwardSessionEvents();
143146
},
144147

‎packages/ember-simple-auth/app/initializers/ember-simple-auth.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import ENV from '../config/environment';
22
import Configuration from 'ember-simple-auth/configuration';
33
import setupSession from 'ember-simple-auth/initializers/setup-session';
4-
import setupSessionService from 'ember-simple-auth/initializers/setup-session-service';
54
import setupSessionRestoration from 'ember-simple-auth/initializers/setup-session-restoration';
65
import Adaptive from 'ember-simple-auth/session-stores/adaptive';
76
import LocalStorage from 'ember-simple-auth/session-stores/local-storage';
@@ -20,7 +19,6 @@ export default {
2019
registry.register('session-store:local-storage', LocalStorage);
2120

2221
setupSession(registry);
23-
setupSessionService(registry);
2422
setupSessionRestoration(registry);
2523
}
2624
};
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
import { describe, beforeEach, it } from 'mocha';
22
import { expect } from 'chai';
33
import sinonjs from 'sinon';
4-
import setupSessionService from 'ember-simple-auth/initializers/setup-session-service';
4+
import { setupTest } from 'ember-mocha';
55

66
describe('setupSessionService', () => {
7+
setupTest();
78
let sinon;
8-
let registry;
99

1010
beforeEach(function() {
1111
sinon = sinonjs.createSandbox();
12-
registry = {
13-
injection() {}
14-
};
1512
});
1613

1714
afterEach(function() {
1815
sinon.restore();
1916
});
2017

2118
it('injects the session into the session service', function() {
22-
sinon.spy(registry, 'injection');
23-
setupSessionService(registry);
24-
25-
expect(registry.injection).to.have.been.calledWith('service:session', 'session', 'session:main');
19+
expect(this.owner.lookup('service:session').session).eq(this.owner.lookup('session:main'));
2620
});
2721
});

‎packages/ember-simple-auth/tests/unit/initializers/setup-session-test.js

-70
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Ember from 'ember';
2+
import { describe, beforeEach, it } from 'mocha';
3+
import { setupApplicationTest } from 'ember-mocha';
4+
import { expect } from 'chai';
5+
import sinonjs from 'sinon';
6+
7+
describe('InternalSession store injection', () => {
8+
setupApplicationTest();
9+
10+
let sinon;
11+
let session;
12+
13+
beforeEach(function() {
14+
sinon = sinonjs.createSandbox();
15+
});
16+
17+
afterEach(function() {
18+
sinon.restore();
19+
});
20+
21+
describe('session store injection', function() {
22+
afterEach(function() {
23+
Ember.testing = true; // eslint-disable-line ember/no-ember-testing-in-module-scope
24+
});
25+
26+
it('looks up the test session store when Ember.testing true', function() {
27+
Ember.testing = true; // eslint-disable-line ember/no-ember-testing-in-module-scope
28+
29+
session = this.owner.lookup('session:main');
30+
expect(session.get('store')).to.eql(this.owner.lookup('session-store:test'));
31+
});
32+
33+
it('looks up the application session store when Ember.testing false', function() {
34+
Ember.testing = false; // eslint-disable-line ember/no-ember-testing-in-module-scope
35+
36+
session = this.owner.lookup('session:main');
37+
expect(session.get('store')).to.eql(this.owner.lookup('session-store:application'));
38+
});
39+
});
40+
});

‎packages/ember-simple-auth/tests/unit/internal-session-test.js

+4-15
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import { describe, beforeEach, it } from 'mocha';
55
import { setupTest } from 'ember-mocha';
66
import { expect } from 'chai';
77
import sinonjs from 'sinon';
8-
import InternalSession from 'ember-simple-auth/internal-session';
9-
import EphemeralStore from 'ember-simple-auth/session-stores/ephemeral';
108
import Authenticator from 'ember-simple-auth/authenticators/base';
119

1210
describe('InternalSession', () => {
@@ -20,12 +18,10 @@ describe('InternalSession', () => {
2018
beforeEach(function() {
2119
sinon = sinonjs.createSandbox();
2220

23-
store = EphemeralStore.create();
24-
authenticator = Authenticator.create();
25-
this.owner.register('authenticator:test', authenticator, { instantiate: false });
26-
27-
session = InternalSession.create({ store });
28-
setOwner(session, this.owner);
21+
this.owner.register('authenticator:test', Authenticator);
22+
authenticator = this.owner.lookup('authenticator:test');
23+
session = this.owner.lookup('session:main');
24+
store = session.get('store');
2925
});
3026

3127
afterEach(function() {
@@ -808,11 +804,4 @@ describe('InternalSession', () => {
808804
});
809805
});
810806
});
811-
812-
it('does not share the content object between multiple instances', function() {
813-
let session2 = InternalSession.create({ store });
814-
setOwner(session2, this.owner);
815-
816-
expect(session2.get('content')).to.not.equal(session.get('content'));
817-
});
818807
});

‎packages/ember-simple-auth/tests/unit/mixins/application-route-mixin-test.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import { setupTest } from 'ember-mocha';
88
import { expect } from 'chai';
99
import sinonjs from 'sinon';
1010
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
11-
import InternalSession from 'ember-simple-auth/internal-session';
12-
import EphemeralStore from 'ember-simple-auth/session-stores/ephemeral';
1311
import * as LocationUtil from 'ember-simple-auth/utils/location';
1412

1513
describe('ApplicationRouteMixin', () => {
@@ -24,8 +22,7 @@ describe('ApplicationRouteMixin', () => {
2422
beforeEach(function() {
2523
sinon = sinonjs.createSandbox();
2624

27-
session = InternalSession.create({ store: EphemeralStore.create() });
28-
this.owner.register('session:main', session, { instantiate: false });
25+
session = this.owner.lookup('session:main');
2926
sessionService = this.owner.lookup('service:session');
3027

3128
this.owner.register('service:cookies', Service.extend({

‎packages/ember-simple-auth/tests/unit/services/session-test.js

+6-18
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
11
import Ember from 'ember';
2-
import ObjectProxy from '@ember/object/proxy';
32
import Service from '@ember/service';
4-
import { setOwner } from '@ember/application';
5-
import Evented from '@ember/object/evented';
63
import { next } from '@ember/runloop';
74
import EmberObject, { set } from '@ember/object';
85
import { registerDeprecationHandler } from '@ember/debug';
96
import { describe, beforeEach, it } from 'mocha';
107
import { setupTest } from 'ember-mocha';
118
import { expect } from 'chai';
129
import sinonjs from 'sinon';
13-
import Session from 'ember-simple-auth/services/session';
1410
import * as LocationUtil from 'ember-simple-auth/utils/location';
1511

1612
describe('SessionService', () => {
@@ -22,20 +18,12 @@ describe('SessionService', () => {
2218

2319
beforeEach(function() {
2420
sinon = sinonjs.createSandbox();
25-
session = ObjectProxy.extend(Evented, {
26-
init() {
27-
this._super(...arguments);
28-
this.content = {};
29-
}
30-
}).create();
31-
3221
this.owner.register('authorizer:custom', EmberObject.extend({
3322
authorize() {}
3423
}));
3524

36-
sessionService = Session.create({ session });
37-
setOwner(sessionService, this.owner);
38-
this.owner.register('service:session', sessionService, { instantiate: false });
25+
sessionService = this.owner.lookup('service:session');
26+
session = sessionService.get('session');
3927
});
4028

4129
afterEach(function() {
@@ -147,19 +135,19 @@ describe('SessionService', () => {
147135
it("is read from the session's content", function() {
148136
session.set('some', 'data');
149137

150-
expect(sessionService.get('data')).to.eql({ some: 'data' });
138+
expect(sessionService.get('data')).to.eql({ some: 'data', authenticated: {} });
151139
});
152140

153141
it("is written back to the session's content", function() {
154142
sessionService.set('data.some', { other: 'data' });
155143

156-
expect(session.content).to.eql({ some: { other: 'data' } });
144+
expect(session.content).to.eql({ some: { other: 'data' }, authenticated: {} });
157145
});
158146

159147
it('can be set with Ember.set', function() {
160148
set(sessionService, 'data.emberSet', 'ember-set-data');
161149

162-
expect(session.content).to.eql({ emberSet: 'ember-set-data' });
150+
expect(session.content).to.eql({ emberSet: 'ember-set-data', authenticated: {} });
163151
});
164152

165153
it('is read-only', function() {
@@ -325,7 +313,7 @@ describe('SessionService', () => {
325313
it("does not set the session's 'attemptedTransition' property", function() {
326314
sessionService.requireAuthentication(null, 'login');
327315

328-
expect(sessionService.get('attemptedTransition')).to.be.undefined;
316+
expect(sessionService.get('attemptedTransition')).to.be.null;
329317
});
330318

331319
it('does not set the redirectTarget cookie in fastboot', function() {

0 commit comments

Comments
 (0)
Please sign in to comment.