From 2d6b3f6dee1c4ed35dca5ec1ffd763fd6c5a9e3d Mon Sep 17 00:00:00 2001 From: Jeff Keen Date: Wed, 4 Mar 2020 09:27:52 -0600 Subject: [PATCH] WIP: force fastboot environment to be set to test unless otherwise specified --- index.js | 2 +- lib/helpers.js | 58 +++++++++++++++---- .../pods/examples/environment/controller.js | 5 ++ .../pods/examples/environment/template.hbs | 1 + tests/dummy/app/router.js | 2 + tests/dummy/config/environment.js | 2 +- tests/dummy/config/fastboot-testing.js | 1 + tests/fastboot/environment-test.js | 11 ++++ 8 files changed, 70 insertions(+), 12 deletions(-) create mode 100644 tests/dummy/app/pods/examples/environment/controller.js create mode 100644 tests/dummy/app/pods/examples/environment/template.hbs create mode 100644 tests/fastboot/environment-test.js diff --git a/index.js b/index.js index 0d0ff93f..d9b7cc9c 100644 --- a/index.js +++ b/index.js @@ -57,7 +57,7 @@ module.exports = { if (this.fastboot) { let options = makeFastbootTestingConfig({ distPath }, pkg); - reloadServer(this.fastboot, distPath, options); + reloadServer(this.fastboot, distPath, pkg, options); } else { this.fastboot = createServer(distPath, pkg); } diff --git a/lib/helpers.js b/lib/helpers.js index df48172d..6671c9d9 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -56,27 +56,64 @@ function createFastbootTest(app, callback) { callback({ req, res, options, urlToVisit }); }); } - + function createFastbootEcho(app) { app.post('/fastboot-testing/echo', bodyParser.text(), (req, res) => { res.send(req.body); }); } -function reloadServer(fastboot, distPath, options) { - fastboot.reload({ distPath }); +function reloadServer(fastboot, distPath, pkg, options) { + let config = makeFastbootTestingConfig({ distPath }, pkg); + setEnvironment(config.environment, () => fastboot.reload({ distPath })); options.setupFastboot(fastboot); } function createServer(distPath, pkg) { - let options = makeFastbootTestingConfig({ distPath }, pkg); - let fastboot = new FastBoot(options); + let options = makeFastbootTestingConfig({ distPath }, pkg); + let fastboot = setEnvironment(options.environment, () => new FastBoot(options)) options.setupFastboot(fastboot); return fastboot; } -function makeFastbootTestingConfig(config, pkg) { +function setEnvironment(environment, callback = function() {}) { + let oldConfig; + if (process.env.APP_CONFIG) { + oldConfig = process.env.APP_CONFIG; + } + // Get app's desired environment settings and set this ENV var + // which tells the Ember App when being built in fastboot to use + // these settings and not the defaults (which would be whatever + // environment this was being run from) + // https://github.com/embermap/ember-cli-fastboot-testing/issues/32 + + if (environment) { + process.env.APP_CONFIG = JSON.stringify(environment); + } + + let fastboot = callback() + + if (oldConfig) { + process.env.APP_CONFIG = oldConfig; + } + else { + delete process.env.APP_CONFIG + } + + return fastboot; +} + +function readEnvironment(pkg, name) { + let configPath = 'config'; + if (pkg['ember-addon'] && pkg['ember-addon']['configPath']) { + configPath = pkg['ember-addon']['configPath']; + } + let appEnvironment = require(path.resolve(configPath, 'environment.js')); + return appEnvironment(name); +} + +function makeFastbootTestingConfig(config, pkg, useEnvironment) { let defaults = { setupFastboot() {} }; @@ -88,12 +125,13 @@ function makeFastbootTestingConfig(config, pkg) { } let fastbootTestConfigPath = path.resolve(configPath, 'fastboot-testing.js'); - - let customized = fs.existsSync(fastbootTestConfigPath) ? + let overrides = fs.existsSync(fastbootTestConfigPath) ? require(fastbootTestConfigPath) : {}; - return Object.assign({}, config, defaults, customized); + overrides.environment = readEnvironment(pkg, overrides.useEnvironment || 'test'); + + return Object.assign({}, config, defaults, overrides); } module.exports = { @@ -104,4 +142,4 @@ module.exports = { reloadServer, createServer, makeFastbootTestingConfig -}; \ No newline at end of file +}; diff --git a/tests/dummy/app/pods/examples/environment/controller.js b/tests/dummy/app/pods/examples/environment/controller.js new file mode 100644 index 00000000..4c09b9c3 --- /dev/null +++ b/tests/dummy/app/pods/examples/environment/controller.js @@ -0,0 +1,5 @@ +import Controller from '@ember/controller'; +import config from '../../../config/environment'; +export default Controller.extend({ + env: config.environment, +}); diff --git a/tests/dummy/app/pods/examples/environment/template.hbs b/tests/dummy/app/pods/examples/environment/template.hbs new file mode 100644 index 00000000..7ea6770f --- /dev/null +++ b/tests/dummy/app/pods/examples/environment/template.hbs @@ -0,0 +1 @@ +

{{env}}

diff --git a/tests/dummy/app/router.js b/tests/dummy/app/router.js index 4faa3429..a05d58fd 100644 --- a/tests/dummy/app/router.js +++ b/tests/dummy/app/router.js @@ -20,6 +20,8 @@ Router.map(function() { }); this.route('examples', function() { + this.route('environment'); + this.route('redirects', function() { this.route('transition-to'); this.route('replace-with'); diff --git a/tests/dummy/config/environment.js b/tests/dummy/config/environment.js index 21bc4a9d..b2aa52f9 100644 --- a/tests/dummy/config/environment.js +++ b/tests/dummy/config/environment.js @@ -38,7 +38,7 @@ module.exports = function(environment) { // ENV.APP.LOG_VIEW_LOOKUPS = true; } - if (environment === 'test') { + if (environment === 'test' || environment == 'specified-by-user') { // Testem prefers this... ENV.locationType = 'none'; diff --git a/tests/dummy/config/fastboot-testing.js b/tests/dummy/config/fastboot-testing.js index 00927c03..4f4a4e24 100644 --- a/tests/dummy/config/fastboot-testing.js +++ b/tests/dummy/config/fastboot-testing.js @@ -3,6 +3,7 @@ module.exports = { sandboxGlobals: { SampleGlobal: `TestSampleGlobal` }, + useEnvironment: 'specified-by-user', setupFastboot: fastbootInstance => { // the modified SampleGlobal will be available in window.SampleGlobal in model of route fastbootInstance._app.sandbox.sandbox.SampleGlobal = 'Modified TestSampleGlobal'; diff --git a/tests/fastboot/environment-test.js b/tests/fastboot/environment-test.js new file mode 100644 index 00000000..953395d4 --- /dev/null +++ b/tests/fastboot/environment-test.js @@ -0,0 +1,11 @@ +import { module, test } from 'qunit'; +import { setup, visit } from 'ember-cli-fastboot-testing/test-support'; + +module('Fastboot | environment', function(hooks) { + setup(hooks); + + test('it renders the correct h1 title', async function(assert) { + await visit('/examples/environment'); + assert.dom('h1').includesText('specified-by-user'); + }); +});