loopback-boot/test/browser.test.js

141 lines
4.2 KiB
JavaScript
Raw Normal View History

2019-05-07 13:12:54 +00:00
// Copyright IBM Corp. 2014,2019. All Rights Reserved.
2016-05-06 04:52:36 +00:00
// Node module: loopback-boot
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
2017-05-08 21:48:48 +00:00
'use strict';
var boot = require('../');
var exportBrowserifyToFile = require('./helpers/browserify').exportToSandbox;
var fs = require('fs');
var path = require('path');
var expect = require('chai').expect;
var browserify = require('browserify');
var sandbox = require('./helpers/sandbox');
var vm = require('vm');
var createBrowserLikeContext = require('./helpers/browser').createContext;
var printContextLogs = require('./helpers/browser').printContextLogs;
var compileStrategies = {
2018-07-23 18:26:47 +00:00
default: function(appDir) {
var b = browserify({
basedir: appDir,
debug: true,
});
2017-05-08 21:48:48 +00:00
b.require('./app.js', {expose: 'browser-app'});
return b;
},
2018-07-23 18:26:47 +00:00
coffee: function(appDir) {
var b = browserify({
basedir: appDir,
extensions: ['.coffee'],
debug: true,
});
b.transform('coffeeify');
2017-05-08 21:48:48 +00:00
b.require('./app.coffee', {expose: 'browser-app'});
return b;
},
};
describe('browser support', function() {
2014-08-18 12:35:19 +00:00
this.timeout(60000); // 60s to give browserify enough time to finish
beforeEach(sandbox.reset);
it('has API for bundling and executing boot instructions', function(done) {
var appDir = path.resolve(__dirname, './fixtures/browser-app');
browserifyTestApp(appDir, function(err, bundlePath) {
if (err) return done(err);
var app = executeBundledApp(bundlePath, function(err) {
if (err) return done(err);
// configured in fixtures/browser-app/boot/configure.js
expect(app.settings).to.have.property('custom-key', 'custom-value');
expect(Object.keys(app.models)).to.include('Customer');
2018-07-23 18:26:47 +00:00
expect(app.models.Customer.settings).to.have.property(
'_customized',
'Customer'
);
// configured in fixtures/browser-app/component-config.json
// and fixtures/browser-app/components/dummy-component.js
2017-05-08 21:48:48 +00:00
expect(app.dummyComponentOptions).to.eql({option: 'value'});
done();
});
});
});
it('loads mixins', function(done) {
var appDir = path.resolve(__dirname, './fixtures/browser-app');
var options = {
appRootDir: appDir,
};
browserifyTestApp(options, function(err, bundlePath) {
if (err) return done(err);
var app = executeBundledApp(bundlePath, function(err) {
var modelBuilder = app.registry.modelBuilder;
var registry = modelBuilder.mixins.mixins;
expect(Object.keys(registry)).to.eql(['TimeStamps']);
expect(app.models.Customer.timeStampsMixin).to.eql(true);
done();
});
});
});
it('supports coffee-script files', function(done) {
// add coffee-script to require.extensions
2018-07-23 18:26:47 +00:00
require('coffeescript/register');
var appDir = path.resolve(__dirname, './fixtures/coffee-app');
browserifyTestApp(appDir, 'coffee', function(err, bundlePath) {
if (err) return done(err);
var app = executeBundledApp(bundlePath, function(err) {
// configured in fixtures/browser-app/boot/configure.coffee
expect(app.settings).to.have.property('custom-key', 'custom-value');
expect(Object.keys(app.models)).to.include('Customer');
2018-07-23 18:26:47 +00:00
expect(app.models.Customer.settings).to.have.property(
'_customized',
'Customer'
);
done();
});
});
});
});
function browserifyTestApp(options, strategy, next) {
// set default args
2018-07-23 18:26:47 +00:00
if (typeof strategy === 'function' && !next) {
next = strategy;
strategy = undefined;
}
2018-07-23 18:26:47 +00:00
if (!strategy) strategy = 'default';
2018-07-23 18:26:47 +00:00
var appDir = typeof options === 'object' ? options.appRootDir : options;
var b = compileStrategies[strategy](appDir);
boot.compileToBrowserify(options, b, function(err) {
exportBrowserifyToFile(b, 'browser-app-bundle.js', next);
});
}
function executeBundledApp(bundlePath, done) {
var code = fs.readFileSync(bundlePath);
var context = createBrowserLikeContext();
vm.runInContext(code, context, bundlePath);
var app = vm.runInContext('require("browser-app")', context);
app.once('booted', function(err) {
printContextLogs(context);
done(err, app);
});
return app;
}