karma-fixture
Advanced tools
Comparing version 0.1.0 to 0.2.1-1
@@ -24,3 +24,3 @@ // Generated by CoffeeScript 1.7.1 | ||
Fixture.prototype.load = function() { | ||
var append, err, filename, filenames, index, json, results, string, _i, _j, _len; | ||
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++]; | ||
@@ -34,5 +34,8 @@ if (append == null) { | ||
} | ||
if (append === false) { | ||
this.cleanup(); | ||
} | ||
results = []; | ||
for (index = _j = 0, _len = filenames.length; _j < _len; index = ++_j) { | ||
filename = filenames[index]; | ||
for (_j = 0, _len = filenames.length; _j < _len; _j++) { | ||
filename = filenames[_j]; | ||
string = (typeof __html__ !== "undefined" && __html__ !== null ? __html__["" + this.base + "/" + filename] : void 0) || ''; | ||
@@ -48,5 +51,8 @@ if (filename.indexOf('.json') !== -1) { | ||
} else { | ||
results.push(this._add_fixture(string, append || index > 0)); | ||
results.push(this._add_fixture(string)); | ||
} | ||
} | ||
if (results.length === 1) { | ||
results = results[0]; | ||
} | ||
return results; | ||
@@ -56,3 +62,3 @@ }; | ||
Fixture.prototype.set = function() { | ||
var append, index, string, strings, _i, _j, _len, _results; | ||
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++]; | ||
@@ -66,6 +72,9 @@ if (append == null) { | ||
} | ||
if (append === false) { | ||
this.cleanup(); | ||
} | ||
_results = []; | ||
for (index = _j = 0, _len = strings.length; _j < _len; index = ++_j) { | ||
string = strings[index]; | ||
_results.push(this._add_fixture(string, append || index > 0)); | ||
for (_j = 0, _len = strings.length; _j < _len; _j++) { | ||
string = strings[_j]; | ||
_results.push(this._add_fixture(string)); | ||
} | ||
@@ -80,10 +89,4 @@ return _results; | ||
Fixture.prototype._add_fixture = function(html_string, append) { | ||
Fixture.prototype._add_fixture = function(html_string) { | ||
var i, results, temp_div; | ||
if (append == null) { | ||
append = false; | ||
} | ||
if (append === false) { | ||
this.cleanup(); | ||
} | ||
temp_div = document.createElement('div'); | ||
@@ -90,0 +93,0 @@ temp_div.innerHTML = html_string; |
{ | ||
"name": "karma-fixture", | ||
"description": "Fixtures for Karma", | ||
"description": "A plugin for the Karma test runner that loads .html and .json fixtures", | ||
"keywords": [ | ||
"Karma", | ||
"karma", | ||
"karma-plugin", | ||
"fixtures", | ||
"fixture" | ||
], | ||
"version": "0.1.0", | ||
"version": "0.2.1-1", | ||
"author": "Bill Trikalinos <billtrik@gmail.com>", | ||
@@ -10,0 +12,0 @@ "homepage": "https://github.com/billtrik/karma-fixture", |
202
README.md
@@ -1,4 +0,202 @@ | ||
karma-fixture | ||
karma-fixture [![Build Status](https://travis-ci.org/billtrik/karma-fixture.svg?branch=master)](https://travis-ci.org/billtrik/karma-fixture) | ||
============= | ||
A Karma fixtures plugin | ||
A plugin for the Karma test runner that loads `.html` and `.json` fixtures. | ||
It provides the same API as the teaspoon fixture package. | ||
Installation | ||
------------ | ||
Install the plugin from npm: | ||
```sh | ||
$ npm install karma-fixture --save-dev | ||
``` | ||
Add `fixture` to the `frameworks` array in your Karma configuration: | ||
```javascript | ||
module.exports = function(config){ | ||
config.set({ | ||
frameworks: ['mocha', 'fixture'], | ||
// ... | ||
``` | ||
You also have to register any/all fixtures in your Karma configuration: | ||
(defaults to `spec/fixtures`) | ||
```javascript | ||
module.exports = function(config){ | ||
config.set({ | ||
files: [ | ||
{ | ||
pattern: 'spec/fixtures/**/*', | ||
}, | ||
// ... | ||
], | ||
// ... | ||
], | ||
``` | ||
Finally you have to add the html2js karma preprocessor: | ||
```sh | ||
$ npm install karma-html2js-preprocessor --save-dev | ||
``` | ||
and then configure it Karma configuration to process all html and JSON files: | ||
```javascript | ||
module.exports = function(config){ | ||
config.set({ | ||
preprocessors: { | ||
'**/*.html' : ['html2js'], | ||
'**/*.json' : ['html2js'] | ||
}, | ||
// ... | ||
``` | ||
Implementation details | ||
----- | ||
All fixtures files are pre-loaded as strings as-well, 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. | ||
Usage | ||
------ | ||
Lets say you have the following fixture files: | ||
- `spec/fixtures/test1.html` | ||
```html | ||
<p>p</p> | ||
<a href='#'> | ||
<span>link</span> | ||
</a> | ||
``` | ||
- `spec/fixtures/json/test1.json` | ||
```javascript | ||
"{"test":true}" | ||
``` | ||
So you can use `fixture` inside your tests. | ||
```javascript | ||
describe('some test that needs a fixture', function(){ | ||
beforeEach(function(){ | ||
this.result = fixture.load('html_fixture', 'json_fixture'); | ||
}); | ||
afterEach(function(){ | ||
fixture.cleanup() | ||
}); | ||
it('plays with the html fixture', function(){ | ||
expect(fixture.el.firstChild).to.equal(this.result[0][0]); | ||
}); | ||
// ... | ||
}); | ||
``` | ||
API | ||
------- | ||
* `fixture.el` | ||
Reference to the container element. Inside this container element, all html fixture files get appended, after creation. | ||
* `fixture.json` | ||
An array of all json objects created from fixture templates. | ||
* `fixture.load(files..., append = false)` | ||
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 a boolean argument with default value `false`. | ||
If `false`, it empties the `window.fixture.el` container element and clears the `window.fixture.json` array. | ||
Scenarios: | ||
**html fixture** | ||
It returns an array of all the first-level nodes created by the fixture file: | ||
```javascript | ||
html_fixture = fixture.load('test1.html'); | ||
// then | ||
expect(html_fixture[0].innerHTML).to.equal('<p>p</p>') | ||
// and | ||
expect(html_fixture[1].innerHTML).to.equal('<a href="#"><span>link</span></a>') | ||
``` | ||
**JSON fixture** | ||
It returns a valid object by JSON.parsing the passed json fixture file. | ||
Also all JSON files loaded get appended to the `window.fixture.json` array: | ||
```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}) | ||
``` | ||
**Multiple files** | ||
The result will be an array containing results of each loaded template: | ||
```javascript | ||
loaded_fixtures = fixture.load('test1.html', 'json/test1.json') | ||
// then | ||
expect(loaded_fixtures[0][0].innerHTML).to.equal('<p>p</p>') | ||
// 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}) | ||
``` | ||
* `fixture.set(html_strings, append=false)` | ||
It takes multiple html_strings as arguments and load them. | ||
It returns the loaded result, or an array of more than one loaded results | ||
It takes a boolean argument with default value `false`. | ||
If `false`, it empties the `window.fixture.el` container element and clears the `window.fixture.json` array. | ||
```javascript | ||
result = fixture.set('<h1>test</h1>') | ||
// then | ||
expect(result[0].innerHTML).to.equal('<h1>test</h1>') | ||
``` | ||
* `fixture.clear()` | ||
It empties the `window.fixture.el` container element and clears the `window.fixture.json` array. | ||
License | ||
------- | ||
The MIT License (MIT) |
10955
8
134
203