| 1 | 'use strict'; |
| 2 | |
| 3 | var assert = require('assert') |
| 4 | var umd = require('../') |
| 5 | var src = umd('sentinel-prime', 'return "sentinel"') |
| 6 | var namespacedSrc = umd('sentinel.prime', 'return "sentinel"') |
| 7 | var multiNamespaces = umd('a.b.c.d.e', 'return "sentinel"') |
| 8 | var dollared = umd('$', 'return "sentinel"') |
| 9 | var number = umd('2sentinel', 'return "sentinel"') |
| 10 | var strip = umd('sentinel^', 'return "sentinel"') |
| 11 | |
| 12 | it('throws when no valid identifier is produced', function () { |
| 13 | assert.throws(umd.bind(null, '^!', ''), /Invalid JavaScript/) |
| 14 | }) |
| 15 | describe('with CommonJS', function () { |
| 16 | it('uses module.exports', function () { |
| 17 | var module = {exports: {}} |
| 18 | require('vm').runInNewContext(src, {module: module, exports: module.exports}) |
| 19 | assert(module.exports === 'sentinel') |
| 20 | }) |
| 21 | }) |
| 22 | describe('with amd', function () { |
| 23 | it('uses define', function () { |
| 24 | var defed |
| 25 | function define(d, fn) { |
| 26 | assert.deepEqual(d, [], 'You must pass an empty array of dependencies to amd to prevent dependency scanning.'); |
| 27 | defed = fn() |
| 28 | } |
| 29 | define.amd = true |
| 30 | require('vm').runInNewContext(src, {define: define}); |
| 31 | assert(defed === 'sentinel') |
| 32 | }) |
| 33 | }) |
| 34 | describe('in the absense of a module system', function () { |
| 35 | it('uses window', function () { |
| 36 | var glob = {} |
| 37 | require('vm').runInNewContext(src, {window: glob}) |
| 38 | assert(glob.sentinelPrime === 'sentinel') |
| 39 | }) |
| 40 | it('uses global', function () { |
| 41 | var glob = {} |
| 42 | require('vm').runInNewContext(src, {global: glob}) |
| 43 | assert(glob.sentinelPrime === 'sentinel') |
| 44 | }) |
| 45 | it('uses self', function () { |
| 46 | var glob = {} |
| 47 | require('vm').runInNewContext(src, {self: glob}) |
| 48 | assert(glob.sentinelPrime === 'sentinel') |
| 49 | }) |
| 50 | it('uses `this`', function () { |
| 51 | var glob = {} |
| 52 | require('vm').runInNewContext(src, glob) |
| 53 | assert(glob.sentinelPrime === 'sentinel') |
| 54 | }) |
| 55 | it('creates the proper namespaces', function() { |
| 56 | var glob = {} |
| 57 | Function('window', namespacedSrc)(glob) |
| 58 | assert(glob.sentinel.prime === 'sentinel') |
| 59 | }) |
| 60 | it('creates proper multiple namespaces', function() { |
| 61 | var glob = {} |
| 62 | Function('window', multiNamespaces)(glob) |
| 63 | assert(glob.a.b.c.d.e === 'sentinel') |
| 64 | }) |
| 65 | it('allows the name to be a dollar', function () { |
| 66 | var glob = {} |
| 67 | Function('window', dollared)(glob) |
| 68 | assert(glob.$ === 'sentinel') |
| 69 | }) |
| 70 | it('camelCases the name', function () { |
| 71 | var glob = {} |
| 72 | Function('window', src)(glob) |
| 73 | assert(glob.sentinelPrime === 'sentinel') |
| 74 | }) |
| 75 | it('strips invalid leading characters', function () { |
| 76 | var glob = {} |
| 77 | Function('window', number)(glob) |
| 78 | assert(glob.sentinel === 'sentinel') |
| 79 | }) |
| 80 | it('removes invalid characters', function () { |
| 81 | var glob = {} |
| 82 | Function('window', strip)(glob) |
| 83 | assert(glob.sentinel === 'sentinel') |
| 84 | }) |
| 85 | }) |