-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: incorporates export-application-global & removes dependency
- declares export-application-global internally instead of using external archived dependency
- Loading branch information
Showing
9 changed files
with
189 additions
and
21 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import Application from '@ember/application' | ||
import { classify } from '@ember/string' | ||
|
||
export function initialize(application: Application): void { | ||
const config:any = application.resolveRegistration('config:environment') | ||
const mustExportApplicationGlobal = config.embedded?.delegateStart === true && config.exportApplicationGlobal !== false | ||
|
||
if (mustExportApplicationGlobal) { | ||
let theGlobal | ||
|
||
if (typeof window !== 'undefined') { | ||
theGlobal = window | ||
} else if (typeof global !== 'undefined') { | ||
theGlobal = global | ||
} else if (typeof self !== 'undefined') { | ||
theGlobal = self | ||
} else { | ||
return | ||
} | ||
|
||
const value = config.exportApplicationGlobal | ||
|
||
let globalName | ||
|
||
if (typeof value === 'string') { | ||
globalName = value | ||
} else { | ||
globalName = classify(config.modulePrefix) | ||
} | ||
|
||
// @ts-ignore: until there's a way to access a dynamic propertyName of window in TS ? | ||
if (!theGlobal[globalName]) { | ||
// @ts-ignore: until there's a way to set a dynamic propertyName on the window in TS ? | ||
theGlobal[globalName] = application | ||
} | ||
} | ||
} | ||
|
||
export default { | ||
name: 'export-application-global', | ||
initialize | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default, initialize } from 'ember-cli-embedded/initializers/export-application-global' |
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
Empty file.
121 changes: 121 additions & 0 deletions
121
tests/unit/initializers/export-application-global-test.ts
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 |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import Application from '@ember/application' | ||
import { initialize } from 'dummy/initializers/export-application-global' | ||
import { module, test } from 'qunit' | ||
import Resolver from 'ember-resolver' | ||
import { classify } from '@ember/string' | ||
import { run } from '@ember/runloop' | ||
|
||
type TestApplication = Application & { | ||
// Public types are currently incomplete, these 2 properties exist: | ||
// https://github.com/emberjs/ember.js/blob/v3.26.1/packages/@ember/application/lib/application.js#L376-L377 | ||
_booted: boolean | ||
_readinessDeferrals: number | ||
} | ||
|
||
// How an app would look like with our Initializer `embedded` | ||
interface EmbeddedApp extends TestApplication { | ||
start?: (config?: Record<string, unknown>) => void | ||
} | ||
|
||
interface Context { | ||
TestApplication: typeof Application | ||
application: EmbeddedApp | ||
} | ||
|
||
module('Unit | Initializer | export-application-global', function (hooks) { | ||
hooks.beforeEach(function (this: Context) { | ||
this.TestApplication = class TestApplication extends Application { | ||
modulePrefix = 'whatever' | ||
} | ||
|
||
this.TestApplication.initializer({ | ||
name: 'export application global initializer', | ||
initialize, | ||
}) | ||
|
||
// @ts-ignore: temporarily required as public types are incomplete | ||
this.application = this.TestApplication.create({ | ||
autoboot: false, | ||
Resolver | ||
}) | ||
|
||
this.application.register('config:environment', {}) | ||
}) | ||
|
||
hooks.afterEach(function (this: Context) { | ||
const config:any = this.application.resolveRegistration('config:environment') | ||
const exportedApplicationGlobal:string = classify(config.modulePrefix) | ||
// @ts-ignore: because TS doesn't like window[dynamicPropertyName] | ||
delete window[exportedApplicationGlobal] | ||
run(this.application, 'destroy') | ||
}) | ||
|
||
// @ts-ignore: because QUnit is not set up with TS propertly and does not like .each() | ||
test.each('it adds expected application global to window if config.embedded.delegateStart is true', [ | ||
['something-random', 'SomethingRandom'], | ||
['something_more-random', 'SomethingMoreRandom'], | ||
['something-', 'Something'], | ||
['something', 'Something'] | ||
], async function (this: Context, assert: Record<string, unknown>, testData: Array<Array<string>>) { | ||
const [modulePrefix, exportedApplicationGlobal] = testData | ||
|
||
this.application.register('config:environment', { | ||
modulePrefix, | ||
embedded: { | ||
delegateStart: true | ||
} | ||
}) | ||
|
||
await this.application.boot() | ||
|
||
// @ts-ignore: because TS doesn't like modulePrefix | ||
assert.strictEqual(classify(modulePrefix), exportedApplicationGlobal, 'it "classifies" module prefix') | ||
|
||
// @ts-ignore: because TS doesn't like window[dynamicPropertyName] | ||
assert.deepEqual(window[exportedApplicationGlobal], this.application, 'it creates expected application global on window') | ||
}) | ||
|
||
test('it does not add application global to window if config.embedded.delegateStart is not true', async function (this: Context, assert) { | ||
this.application.register('config:environment', { | ||
modulePrefix: 'something-random' | ||
}) | ||
|
||
await this.application.boot() | ||
|
||
// @ts-ignore: because TS doesn't like window[dynamicPropertyName] | ||
assert.notOk(window.SomethingRandom) | ||
}) | ||
|
||
test('it does not create application global on window if config.exportApplicationGlobal is false', async function (this: Context, assert) { | ||
this.application.register('config:environment', { | ||
modulePrefix: 'something-random', | ||
embedded: { | ||
delegateStart: true | ||
}, | ||
exportApplicationGlobal: false | ||
}) | ||
|
||
await this.application.boot() | ||
|
||
// @ts-ignore: because TS doesn't like window[dynamicPropertyName] | ||
assert.notOk(window.SomethingRandom) | ||
}) | ||
|
||
test('it adds application global to window using value of config.exportApplicationGlobal, if it is a String', async function (this: Context, assert) { | ||
this.application.register('config:environment', { | ||
modulePrefix: 'something-random', | ||
embedded: { | ||
delegateStart: true | ||
}, | ||
exportApplicationGlobal: 'SomethingElse' | ||
}) | ||
|
||
await this.application.boot() | ||
|
||
// @ts-ignore: because TS doesn't like window.PropertyName ? | ||
assert.deepEqual(window.SomethingElse, this.application, 'name set in config is used for exported application global, instead of original module prefix') | ||
|
||
// @ts-ignore: because TS doesn't like window.PropertyName ? | ||
assert.notOk(window.SomethingRandom, 'original module prefix is not used in exported application global') | ||
}) | ||
}) |
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,3 +1,5 @@ | ||
declare module 'dummy/app' | ||
declare module 'dummy/initializers/embedded' | ||
declare module 'dummy/initializers/export-application-global' | ||
declare module 'dummy/instance-initializers/embedded' | ||
declare module 'dummy/config/environment' |
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