-
Notifications
You must be signed in to change notification settings - Fork 440
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
69 additions
and
61 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
packages/ember-cli-mirage/addon/initializers/ember-cli-mirage.js
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,62 @@ | ||
import getRfc232TestContext from 'ember-cli-mirage/get-rfc232-test-context'; | ||
import startMirageImpl from 'ember-cli-mirage/start-mirage'; | ||
import * as config from '../mirage/config'; | ||
const { default: makeServer } = config; | ||
|
||
// | ||
// This initializer does two things: | ||
// | ||
// 1. Pulls the mirage config objects from the application's config and | ||
// registers them in the container so `ember-cli-mirage/start-mirage` can | ||
// find them (since it doesn't have access to the app's namespace). | ||
// 2. Provides legacy support for auto-starting mirage in pre-rfc268 acceptance | ||
// tests. | ||
// | ||
export function initialize(application) { | ||
if (makeServer) { | ||
application.register('mirage:make-server', makeServer, { | ||
instantiate: false, | ||
}); | ||
} | ||
|
||
const ENV = application.resolveRegistration('config:environment'); | ||
|
||
ENV['ember-cli-mirage'] = ENV['ember-cli-mirage'] || {}; | ||
if (_shouldUseMirage(ENV.environment, ENV['ember-cli-mirage'])) { | ||
startMirage(application.__container__, ENV); | ||
} | ||
} | ||
|
||
export default { | ||
name: 'ember-cli-mirage', | ||
initialize, | ||
}; | ||
|
||
|
||
function startMirage(owner, env = ENV) { | ||
return startMirageImpl(owner, { env, makeServer }); | ||
} | ||
|
||
function _shouldUseMirage(env, addonConfig) { | ||
if (typeof FastBoot !== 'undefined') { | ||
return false; | ||
} | ||
if (getRfc232TestContext()) { | ||
return false; | ||
} | ||
let userDeclaredEnabled = typeof addonConfig.enabled !== 'undefined'; | ||
let defaultEnabled = _defaultEnabled(env, addonConfig); | ||
|
||
return userDeclaredEnabled ? addonConfig.enabled : defaultEnabled; | ||
} | ||
|
||
/* | ||
Returns a boolean specifying the default behavior for whether | ||
to initialize Mirage. | ||
*/ | ||
function _defaultEnabled(env, addonConfig) { | ||
let usingInDev = env === 'development' && !addonConfig.usingProxy; | ||
let usingInTest = env === 'test'; | ||
|
||
return usingInDev || usingInTest; | ||
} |
62 changes: 4 additions & 58 deletions
62
packages/ember-cli-mirage/app/initializers/ember-cli-mirage.js
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,58 +1,4 @@ | ||
import ENV from '../config/environment'; | ||
import getRfc232TestContext from 'ember-cli-mirage/get-rfc232-test-context'; | ||
import startMirageImpl from 'ember-cli-mirage/start-mirage'; | ||
import * as config from '../mirage/config'; | ||
const { default: makeServer } = config; | ||
|
||
// | ||
// This initializer does two things: | ||
// | ||
// 1. Pulls the mirage config objects from the application's config and | ||
// registers them in the container so `ember-cli-mirage/start-mirage` can | ||
// find them (since it doesn't have access to the app's namespace). | ||
// 2. Provides legacy support for auto-starting mirage in pre-rfc268 acceptance | ||
// tests. | ||
// | ||
export default { | ||
name: 'ember-cli-mirage', | ||
initialize(application) { | ||
if (makeServer) { | ||
application.register('mirage:make-server', makeServer, { | ||
instantiate: false, | ||
}); | ||
} | ||
|
||
ENV['ember-cli-mirage'] = ENV['ember-cli-mirage'] || {}; | ||
if (_shouldUseMirage(ENV.environment, ENV['ember-cli-mirage'])) { | ||
startMirage(application.__container__, ENV); | ||
} | ||
}, | ||
}; | ||
|
||
function startMirage(owner, env = ENV) { | ||
return startMirageImpl(owner, { env, makeServer }); | ||
} | ||
|
||
function _shouldUseMirage(env, addonConfig) { | ||
if (typeof FastBoot !== 'undefined') { | ||
return false; | ||
} | ||
if (getRfc232TestContext()) { | ||
return false; | ||
} | ||
let userDeclaredEnabled = typeof addonConfig.enabled !== 'undefined'; | ||
let defaultEnabled = _defaultEnabled(env, addonConfig); | ||
|
||
return userDeclaredEnabled ? addonConfig.enabled : defaultEnabled; | ||
} | ||
|
||
/* | ||
Returns a boolean specifying the default behavior for whether | ||
to initialize Mirage. | ||
*/ | ||
function _defaultEnabled(env, addonConfig) { | ||
let usingInDev = env === 'development' && !addonConfig.usingProxy; | ||
let usingInTest = env === 'test'; | ||
|
||
return usingInDev || usingInTest; | ||
} | ||
export { | ||
default, | ||
initialize, | ||
} from 'ember-cli-mirage/initializers/ember-cli-mirage'; |
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