karma-fixture
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.7.1 | ||
// Generated by CoffeeScript 1.9.1 | ||
(function() { | ||
@@ -3,0 +3,0 @@ this.fixture = new Fixture(); |
@@ -1,26 +0,24 @@ | ||
// Generated by CoffeeScript 1.7.1 | ||
// Generated by CoffeeScript 1.9.1 | ||
(function() { | ||
var Fixture, | ||
__slice = [].slice; | ||
slice = [].slice; | ||
Fixture = (function() { | ||
Fixture.el_id = 'fixture_container'; | ||
function Fixture(base) { | ||
if (base == null) { | ||
base = 'spec/fixtures'; | ||
} | ||
this.base = base; | ||
function Fixture(base, id) { | ||
this.base = base != null ? base : 'spec/fixtures'; | ||
this.id = id != null ? id : 'fixture_container'; | ||
this.json = []; | ||
this.el = window.fixture_container || (function() { | ||
var container; | ||
container = document.createElement('div'); | ||
container.setAttribute('id', Fixture.el_id); | ||
return document.body.appendChild(container); | ||
})(); | ||
this.el = window[this.id] || ((function(_this) { | ||
return function() { | ||
var container; | ||
container = document.createElement('div'); | ||
container.setAttribute('id', _this.id); | ||
return document.body.appendChild(container); | ||
}; | ||
})(this))(); | ||
} | ||
Fixture.prototype.load = function() { | ||
var append, err, filename, filenames, json, results, string, _i, _j, _len; | ||
filenames = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), append = arguments[_i++]; | ||
var append, err, filename, filenames, j, json, k, len, results, string; | ||
filenames = 2 <= arguments.length ? slice.call(arguments, 0, j = arguments.length - 1) : (j = 0, []), append = arguments[j++]; | ||
if (append == null) { | ||
@@ -37,5 +35,8 @@ append = false; | ||
results = []; | ||
for (_j = 0, _len = filenames.length; _j < _len; _j++) { | ||
filename = filenames[_j]; | ||
string = (typeof __html__ !== "undefined" && __html__ !== null ? __html__["" + this.base + "/" + filename] : void 0) || ''; | ||
for (k = 0, len = filenames.length; k < len; k++) { | ||
filename = filenames[k]; | ||
string = typeof __html__ !== "undefined" && __html__ !== null ? __html__[this.base + "/" + filename] : void 0; | ||
if (string == null) { | ||
this._throwNoFixture(); | ||
} | ||
if (filename.indexOf('.json') !== -1) { | ||
@@ -50,3 +51,3 @@ try { | ||
} else { | ||
results.push(this._add_fixture(string)); | ||
results.push(this._appendFixture(string)); | ||
} | ||
@@ -61,4 +62,4 @@ } | ||
Fixture.prototype.set = function() { | ||
var append, string, strings, _i, _j, _len, _results; | ||
strings = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), append = arguments[_i++]; | ||
var append, j, k, len, results, string, strings; | ||
strings = 2 <= arguments.length ? slice.call(arguments, 0, j = arguments.length - 1) : (j = 0, []), append = arguments[j++]; | ||
if (append == null) { | ||
@@ -74,8 +75,11 @@ append = false; | ||
} | ||
_results = []; | ||
for (_j = 0, _len = strings.length; _j < _len; _j++) { | ||
string = strings[_j]; | ||
_results.push(this._add_fixture(string)); | ||
results = []; | ||
for (k = 0, len = strings.length; k < len; k++) { | ||
string = strings[k]; | ||
results.push(this._appendFixture(string)); | ||
} | ||
return _results; | ||
if (results.length === 1) { | ||
results = results[0]; | ||
} | ||
return results; | ||
}; | ||
@@ -88,3 +92,3 @@ | ||
Fixture.prototype._add_fixture = function(html_string) { | ||
Fixture.prototype._appendFixture = function(html_string) { | ||
var i, results, temp_div; | ||
@@ -94,3 +98,3 @@ temp_div = document.createElement('div'); | ||
results = []; | ||
while ((i = temp_div.firstChild)) { | ||
while (i = temp_div.firstChild) { | ||
if (i.nodeType !== 1) { | ||
@@ -109,2 +113,6 @@ temp_div.removeChild(i); | ||
Fixture.prototype._throwNoFixture = function() { | ||
throw new ReferenceError('Fixture not found'); | ||
}; | ||
return Fixture; | ||
@@ -114,5 +122,5 @@ | ||
if (typeof exports === "object") { | ||
if (typeof exports === 'object') { | ||
module.exports = Fixture; | ||
} else if (typeof define === "function" && define.amd) { | ||
} else if (typeof define === 'function' && define.amd) { | ||
define('fixture', [], function() { | ||
@@ -119,0 +127,0 @@ return Fixture; |
@@ -1,2 +0,2 @@ | ||
// Generated by CoffeeScript 1.7.1 | ||
// Generated by CoffeeScript 1.9.1 | ||
(function() { | ||
@@ -3,0 +3,0 @@ var adapter_path, framework, framework_path, path, pattern; |
{ | ||
"name": "karma-fixture", | ||
"description": "A plugin for Karma test runner that enables loading fixture html and json files", | ||
"description": "A plugin for the Karma test runner that loads .html and .json fixtures", | ||
"keywords": [ | ||
@@ -10,3 +10,3 @@ "karma", | ||
], | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"author": "Bill Trikalinos <billtrik@gmail.com>", | ||
@@ -30,3 +30,3 @@ "homepage": "https://github.com/billtrik/karma-fixture", | ||
"grunt": "^0.4.5", | ||
"grunt-bump": "0.0.14", | ||
"grunt-bump": "0.1.0", | ||
"grunt-cli": "^0.1.13", | ||
@@ -38,5 +38,5 @@ "karma": "^0.12.16", | ||
"karma-mocha": "^0.1.3", | ||
"karma-mocha-reporter": "^0.2.5", | ||
"karma-mocha-reporter": "^0.3.2", | ||
"karma-phantomjs-launcher": "^0.1.4" | ||
} | ||
} |
132
README.md
@@ -1,2 +0,2 @@ | ||
karma-fixture [![Build Status](https://travis-ci.org/billtrik/karma-fixture.svg?branch=master)](https://travis-ci.org/billtrik/karma-fixture) | ||
karma-fixture [![Build Status](https://travis-ci.org/billtrik/karma-fixture.svg?branch=master)](https://travis-ci.org/billtrik/karma-fixture) [![NPM version](https://badge.fury.io/js/karma-fixture.svg)](http://badge.fury.io/js/karma-fixture) | ||
============= | ||
@@ -19,10 +19,8 @@ | ||
```coffee | ||
module.exports = (config) -> | ||
config.set | ||
```javascript | ||
module.exports = function(config){ | ||
config.set({ | ||
frameworks: ['mocha', 'fixture'], | ||
# frameworks to use | ||
frameworks: ['mocha', 'fixture'] | ||
# ... | ||
// ... | ||
``` | ||
@@ -35,9 +33,13 @@ | ||
```coffee | ||
files: [ | ||
{ | ||
```javascript | ||
module.exports = function(config){ | ||
config.set({ | ||
files: [ | ||
{ | ||
pattern: 'spec/fixtures/**/*', | ||
}, | ||
}, | ||
// ... | ||
], | ||
# ... | ||
// ... | ||
], | ||
@@ -52,18 +54,38 @@ ``` | ||
and then configure it Karma configuration to process all html and JSON files: | ||
and then configure Karma to load all html and JSON fixture files: | ||
```coffee | ||
preprocessors: { | ||
'**/*.html' : ['html2js'], | ||
'**/*.json' : ['html2js'] | ||
} | ||
```javascript | ||
module.exports = function(config){ | ||
config.set({ | ||
preprocessors: { | ||
'**/*.html' : ['html2js'], | ||
'**/*.json' : ['html2js'] | ||
}, | ||
// ... | ||
``` | ||
*(optional)* If the plugin won't get loaded by karma, you might have to declare it inside the `plugins` array in your Karma configuration | ||
*(and maybe load `karma-html2js-preprocessor` as well)*: | ||
```javascript | ||
module.exports = function(config){ | ||
config.set({ | ||
// ... | ||
plugins: [ | ||
'karma-fixture' | ||
'karma-html2js-preprocessor' | ||
// ... | ||
], | ||
// ... | ||
``` | ||
Implementation details | ||
----- | ||
All fixtures files are pre-loaded as strings as-well, and placed inside the Karma-created `window.__html__` array. | ||
All fixture files are pre-loaded as strings and placed inside the Karma-created `window.__html__` array. | ||
The fixture plugin is exposed in the `window.fixture` object on every test run. It loads fixture files from that array and appends the created html inside the `window.fixture.el` element that gets created on start-up. | ||
The fixture plugin is exposed in the `window.fixture` object on every test run. | ||
It loads fixture files from that array and appends the created html inside the `window.fixture.el` element that gets created on start-up. | ||
@@ -90,15 +112,21 @@ | ||
So you can use `fixture` inside your tests. | ||
You can use `fixture` inside your tests to handle the fixtures: | ||
```coffee | ||
describe 'some test that needs a fixture', -> | ||
beforeEach -> | ||
@result = fixture.load('html_fixture', 'json_fixture') | ||
```javascript | ||
describe('some test that needs a fixture', function(){ | ||
beforeEach(function(){ | ||
this.result = fixture.load('html_fixture', 'json_fixture'); | ||
}); | ||
afterEach -> | ||
afterEach(function(){ | ||
fixture.cleanup() | ||
}); | ||
it 'play with the html fixture', -> | ||
expect(fixture.el.firstChild).to.equal(@result[0][0]) | ||
it('plays with the html fixture', function(){ | ||
expect(fixture.el.firstChild).to.equal(this.result[0][0]); | ||
}); | ||
// ... | ||
}); | ||
``` | ||
@@ -111,7 +139,7 @@ | ||
Reference to the container element. Inside this container element, all html fixture files get appended, after creation. | ||
Reference to the container element. Every html fixture loaded gets appended inside this container. | ||
* `fixture.json` | ||
An array of all json objects created from fixture templates. | ||
An array of all json objects imported from fixture templates. | ||
@@ -121,7 +149,9 @@ | ||
It takes multiple filenames as arguments and load them. | ||
It returns the loaded result, or an array of more than one loaded results | ||
It takes multiple filenames as arguments loads and appends them inside the fixtures container element. | ||
It returns an array with references to the newly created first level html elements. | ||
When more than one are loaded, it returns an array of the above described format, for each loaded fixture. | ||
It takes a boolean argument with default value `false`. | ||
It takes an optional boolean argument which defaults to `false`. | ||
If `false`, it empties the `window.fixture.el` container element and clears the `window.fixture.json` array. | ||
If `true`, it just appends the requested fixture to the container. | ||
@@ -134,8 +164,8 @@ Scenarios: | ||
```coffee | ||
html_fixture = fixture.load('test1.html') | ||
```javascript | ||
html_fixture = fixture.load('test1.html'); | ||
// then | ||
expect(html_fixture[0].innerHTML).to.equal('<p>p</p>') | ||
expect(html_fixture[1].innerHTML).to.equal('<a href='#'><span>link</span></a>') | ||
// and | ||
expect(html_fixture[1].innerHTML).to.equal('<a href="#"><span>link</span></a>') | ||
``` | ||
@@ -148,7 +178,7 @@ | ||
```coffee | ||
```javascript | ||
json_fixture = fixture.load('json/test1.json') | ||
// then | ||
expect(json_fixture).to.eql({"test":true}) | ||
// and | ||
expect(fixture.json[0]).to.eql({"test":true}) | ||
@@ -161,11 +191,11 @@ ``` | ||
```coffee | ||
```javascript | ||
loaded_fixtures = fixture.load('test1.html', 'json/test1.json') | ||
// then | ||
expect(loaded_fixtures[0][0].innerHTML).to.equal('<p>p</p>') | ||
expect(loaded_fixtures[0][1].innerHTML).to.equal('<a href='_'><span>link</span></a>') | ||
// and | ||
expect(loaded_fixtures[0][1].innerHTML).to.equal('<a href="#"><span>link</span></a>') | ||
// and | ||
expect(loaded_fixtures[1]).to.eql({"test":true}) | ||
// and | ||
expect(fixture.json[0]).to.eql({"test":true}) | ||
@@ -184,9 +214,9 @@ ``` | ||
```coffee | ||
```javascript | ||
result = fixture.set('<h1>test</h1>') | ||
# and | ||
// then | ||
expect(result[0].innerHTML).to.equal('<h1>test</h1>') | ||
``` | ||
* `fixture.clear()` | ||
* `fixture.cleanup()` | ||
@@ -193,0 +223,0 @@ It empties the `window.fixture.el` container element and clears the `window.fixture.json` array. |
13459
9
142
221