|
| 1 | +/* global moduleRequire:true, pluginRequire:true, testGlobal:true */ |
| 2 | +define([ |
| 3 | + "intern!object", |
| 4 | + "intern/chai!assert" |
| 5 | +], function (registerSuite, assert) { |
| 6 | + |
| 7 | + registerSuite({ |
| 8 | + name: "has - module", |
| 9 | + setup: function () { |
| 10 | + moduleRequire = require.config({ |
| 11 | + context: "module", |
| 12 | + baseUrl: "../../", |
| 13 | + config: { |
| 14 | + has: { |
| 15 | + "config-feature": true, |
| 16 | + "config-feature2": false |
| 17 | + } |
| 18 | + } |
| 19 | + }); |
| 20 | + }, |
| 21 | + beforeEach: function () {}, |
| 22 | + "Cache setup from config": function () { |
| 23 | + var dfd = this.async(); |
| 24 | + |
| 25 | + moduleRequire(["has"], dfd.callback(function (has) { |
| 26 | + assert.isTrue(has.cache["config-feature"], "import feature from config"); |
| 27 | + assert.isFalse(has.cache["config-feature2"], "import feature from config"); |
| 28 | + assert.isUndefined(has.cache["undefined feature"], "import feature from config"); |
| 29 | + })); |
| 30 | + }, |
| 31 | + "has function": function () { |
| 32 | + var dfd = this.async(); |
| 33 | + |
| 34 | + moduleRequire(["has"], dfd.callback(function (has) { |
| 35 | + assert.isTrue(has("config-feature"), "import feature from config"); |
| 36 | + assert.isFalse(has("config-feature2"), "import feature from config"); |
| 37 | + assert.isUndefined(has("undefined feature"), "import feature from config"); |
| 38 | + })); |
| 39 | + }, |
| 40 | + "add feature": function () { |
| 41 | + var dfd = this.async(); |
| 42 | + |
| 43 | + moduleRequire(["has"], dfd.callback(function (has) { |
| 44 | + has.add("add-feature", function () { |
| 45 | + return true; |
| 46 | + }); |
| 47 | + has.add("add-feature2", function () { |
| 48 | + return false; |
| 49 | + }); |
| 50 | + assert.isTrue(has("add-feature"), "added a true feature."); |
| 51 | + assert.isFalse(has("add-feature2"), "added a false feature"); |
| 52 | + })); |
| 53 | + }, |
| 54 | + "add feature, lazy evaluation": function () { |
| 55 | + var dfd = this.async(); |
| 56 | + |
| 57 | + moduleRequire(["has"], dfd.callback(function (has) { |
| 58 | + testGlobal = false; |
| 59 | + has.add("lazy-feature", function (global) { |
| 60 | + return global.testGlobal; |
| 61 | + }); |
| 62 | + has.add("lazy-feature2", function (global) { |
| 63 | + return global.testGlobal; |
| 64 | + }, true); |
| 65 | + testGlobal = true; |
| 66 | + |
| 67 | + assert.isTrue(has("lazy-feature"), |
| 68 | + "should be true as the test function should be lazily evaluated."); |
| 69 | + assert.isFalse(has("lazy-feature2"), |
| 70 | + "should be false as the test function should be evaluated immediately."); |
| 71 | + })); |
| 72 | + }, |
| 73 | + "add feature, force": function () { |
| 74 | + var dfd = this.async(); |
| 75 | + |
| 76 | + moduleRequire(["has"], dfd.callback(function (has) { |
| 77 | + has.add("config-feature", function () { |
| 78 | + return false; |
| 79 | + }, false, true); |
| 80 | + has.add("config-feature", function () { |
| 81 | + return true; |
| 82 | + }, false); |
| 83 | + |
| 84 | + has.add("config-feature2", function () { |
| 85 | + return true; |
| 86 | + }, true, true); |
| 87 | + has.add("config-feature2", function () { |
| 88 | + return false; |
| 89 | + }, true); |
| 90 | + |
| 91 | + assert.isFalse(has("config-feature"), "should be erased by false"); |
| 92 | + assert.isTrue(has("config-feature2"), "should be erased by true"); |
| 93 | + })); |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + registerSuite({ |
| 98 | + name: "has - plugin", |
| 99 | + setup: function () { |
| 100 | + pluginRequire = require.config({ |
| 101 | + context: "plugin", |
| 102 | + baseUrl: "../../", |
| 103 | + packages: [{ |
| 104 | + name: "modules", |
| 105 | + location: "tests/testModules/" |
| 106 | + }], |
| 107 | + config: { |
| 108 | + has: { |
| 109 | + "config-feature": true, |
| 110 | + "config-feature2": false |
| 111 | + } |
| 112 | + } |
| 113 | + }); |
| 114 | + }, |
| 115 | + "basic load": function () { |
| 116 | + var dfd = this.async(); |
| 117 | + |
| 118 | + pluginRequire([ |
| 119 | + "has!config-feature?modules/module1:modules/module2", |
| 120 | + "has!config-feature2?modules/module1:modules/module2" |
| 121 | + ], dfd.callback(function (module1, module2) { |
| 122 | + assert.strictEqual(module1.msg, "module 1", |
| 123 | + "config-feature is true so has should resolve to module1"); |
| 124 | + assert.strictEqual(module2.msg, "module 2", |
| 125 | + "config-feature2 is false so has should resolve to module2"); |
| 126 | + })); |
| 127 | + }, |
| 128 | + "ternary variation": function () { |
| 129 | + var dfd = this.async(); |
| 130 | + |
| 131 | + pluginRequire([ |
| 132 | + "has!config-feature?modules/module1", |
| 133 | + "has!config-feature?:modules/module2", |
| 134 | + "has!config-feature2?modules/module1", |
| 135 | + "has!config-feature2?:modules/module2" |
| 136 | + ], dfd.callback(function (module1, undefined2, undefined1, module2) { |
| 137 | + assert.strictEqual(module1.msg, "module 1", |
| 138 | + "config-feature is true so has should resolve to module1"); |
| 139 | + assert.strictEqual(module2.msg, "module 2", |
| 140 | + "config-feature2 is false so has should resolve to module2"); |
| 141 | + assert.isUndefined(undefined2); |
| 142 | + assert.isUndefined(undefined1); |
| 143 | + })); |
| 144 | + }, |
| 145 | + "chained ternary": function () { |
| 146 | + var dfd = this.async(); |
| 147 | + |
| 148 | + pluginRequire([ |
| 149 | + "has!config-feature?modules/module1:config-feature2?modules/module2:modules/module3", |
| 150 | + "has!config-feature2?modules/module1:config-feature?modules/module2:modules/module3", |
| 151 | + "has!config-feature2?modules/module1:config-feature2?modules/module2:modules/module3" |
| 152 | + ], dfd.callback(function (module1, module2, module3) { |
| 153 | + assert.strictEqual(module1.msg, "module 1"); |
| 154 | + assert.strictEqual(module2.msg, "module 2"); |
| 155 | + assert.strictEqual(module3.msg, "module 3"); |
| 156 | + })); |
| 157 | + }, |
| 158 | + "undefined feature": function () { |
| 159 | + var dfd = this.async(); |
| 160 | + |
| 161 | + pluginRequire([ |
| 162 | + "has!undef-feature?modules/module1:modules/module2" |
| 163 | + ], dfd.callback(function (module2) { |
| 164 | + assert.strictEqual(module2.msg, "module 2"); |
| 165 | + })); |
| 166 | + }, |
| 167 | + "normalization": function () { |
| 168 | + var dfd = this.async(); |
| 169 | + |
| 170 | + pluginRequire([ |
| 171 | + "has!config-feature?./tests/testModules/module1", |
| 172 | + "has!config-feature?./tests/testModules/plugin!./resources!test" |
| 173 | + ], dfd.callback(function (module1, plugin) { |
| 174 | + assert.strictEqual(module1.msg, "module 1"); |
| 175 | + assert.strictEqual(plugin.res, "./resources!test"); |
| 176 | + })); |
| 177 | + } |
| 178 | + |
| 179 | + }); |
| 180 | +}); |
0 commit comments