Skip to content

Commit

Permalink
WIP (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBuchholz committed Oct 4, 2017
1 parent 59b9a06 commit e5bfe9f
Showing 1 changed file with 96 additions and 23 deletions.
119 changes: 96 additions & 23 deletions test/require.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,109 @@ describe("require", function () {

describe("gpf.require", function () {

it("loads JSON file as an object", function (done) {
gpf.require({
data: "data.json"
}, function (require) {
try {
assert("object" === typeof require.data);
assert("value" === require.data.member);
done();
} catch (e) {
done(e);
}
function validateData (data) {
assert("object" === typeof data);
assert("value" === data.member);
}

function validateModule (module, type, dataExpected) {
assert("object" === typeof module);
assert(type === module.type);
if (dataExpected) {
validateData(module.data);
}
}

describe("Synchronous loading", function () {

beforeEach(function () {
gpf.require.cache = {};
});

it("loads JSON file as an object", function () {
validateData(gpf.require("data.json"));
});

it("supports CommonJS format", function () {
validateModule(gpf.require("commonjs.js"), "commonjs", true);
});

it("supports AMD format (named with factory)", function () {
validateModule(gpf.require("amd.js"), "amd", true);
});

it("supports AMD format (anonymous static)", function () {
var amd = gpf.require("anonymous_amd.js");
validateModule(amd, "amd", false);
assert("anonymous" === amd.name);
});

});

it("handles NodeJS modules", function (done) {
gpf.require({
node: "nodejs.js"
}, function (require) {
try {
assert("object" === typeof require.node);
done();
} catch (e) {
done(e);
}
describe("Asynchronous loading", function () {

beforeEach(function () {
gpf.require.cache = {};
});

it("loads JSON file as an object", function (done) {
gpf.require({
data: "data.json"
}, function (require) {
try {
validateData(require.data);
done();
} catch (e) {
done(e);
}
});
});

it("supports CommonJS format", function (done) {
gpf.require({
commonjs: "commonjs.js"
}, function (require) {
try {
validateModule(require.commonjs, "commonjs", true);
done();
} catch (e) {
done(e);
}
});
});

it("supports AMD format (named with factory)", function (done) {
gpf.require({
amd: "amd.js"
}, function (require) {
try {
validateModule(require.amd, "amd", true);
done();
} catch (e) {
done(e);
}
});
});

it("supports AMD format (anonymous static)", function (done) {
gpf.require({
amd: "anonymous_amd.js"
}, function (require) {
try {
validateModule(require.amd, "amd", false);
assert("anonymous" === require.amd.name);
done();
} catch (e) {
done(e);
}
});
});
});


it("handles gpf.require modules", function (done) {
gpf.require({
test: "require.js"
test: "gpf.js"
}, function (require) {
try {
assert("object" === typeof require.test);
Expand Down Expand Up @@ -92,7 +165,7 @@ describe("require", function () {
var fakeData = {
member: "value2"
};
gpf.require.cache(gpf.require.resolve("data.json"), fakeData);
gpf.require.cache[gpf.require.resolve("data.json")] = fakeData;
gpf.require({
data: "data.json"
}, function (require) {
Expand Down

0 comments on commit e5bfe9f

Please sign in to comment.