Permalink
Browse files

Added unit tests and fixed a bug-ish behavior

  • Loading branch information...
1 parent c6c7577 commit 67200c4c4adaea9fd76d88a1eeaf67219bb8bb25 @young-steveo young-steveo committed Mar 15, 2013
Showing with 75 additions and 1 deletion.
  1. +3 −1 src/companion.js
  2. +1 −0 test/fixture/foo.js
  3. +1 −0 test/fixture/test.js
  4. +70 −0 test/spec/companion.spec.js
View
4 src/companion.js
@@ -1,14 +1,16 @@
/* global require */
(function(module, undefined){
"use strict";
- var fs, vm, load;
+ var fs, vm, path, load;
fs = require('fs');
vm = require('vm');
+ path = require('path');
load = function(filename, context, callback){
var poly = typeof context === 'function';
+ filename = path.resolve(path.dirname(module.parent.filename), filename);
callback = poly ? context : callback;
context = poly ? {} : (context || {});
View
1 test/fixture/foo.js
@@ -0,0 +1 @@
+var Foo = true;
View
1 test/fixture/test.js
@@ -0,0 +1 @@
+var Test = true;
View
70 test/spec/companion.spec.js
@@ -0,0 +1,70 @@
+(function(define, expect, it, runs, waitsFor, undefined){
+ describe('companion', function(){
+ console.log(__dirname);
+ var companion = require('../../src/companion.js');
+
+ it('can load a file synchronously', function(){
+ var context = companion.require('../fixture/test.js');
+ expect(context.Test).toBe(true);
+ });
+
+ it('can load a file asynchronously', function(){
+ var context;
+ runs(function(){
+ companion.require('../fixture/foo.js', function(err, data){
+ context = data;
+ });
+ });
+ waitsFor(function(){
+ return !!context;
+ });
+ runs(function(){
+ expect(context.Foo).toBe(true);
+ });
+ });
+
+ it('can pass a context synchronously', function(){
+ var context = { baz : 42 };
+
+ companion.require('../fixture/test.js', context);
+ expect(context.Test).toBe(true);
+ expect(context.baz).toBe(42);
+ });
+
+ it('can pass a context synchronously', function(){
+ var context;
+ runs(function(){
+ companion.require('../fixture/foo.js', { biff : true }, function(err, data){
+ context = data;
+ });
+ });
+ waitsFor(function(){
+ return !!context;
+ });
+ runs(function(){
+ expect(context.Foo).toBe(true);
+ expect(context.biff).toBe(true);
+ });
+ });
+ it('still returns the context when calling asynchronously', function(){
+ var context, precontext;
+ runs(function(){
+ precontext = companion.require('../fixture/foo.js', { zing : false }, function(err, data){
+ context = data;
+ });
+ expect(precontext.Foo).toBeUndefined();
+ expect(precontext.zing).toBe(false);
+
+ });
+ waitsFor(function(){
+ return !!context;
+ });
+ runs(function(){
+ expect(context.Foo).toBe(true);
+ expect(context.zing).toBe(false);
+ expect(precontext.Foo).toBe(true);
+ expect(precontext.zing).toBe(false);
+ });
+ });
+ });
+}(describe, expect, it, runs, waitsFor));

0 comments on commit 67200c4

Please sign in to comment.