Skip to content

Commit

Permalink
tests: added tests for netteForms.js
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 8, 2015
1 parent fcdb7e9 commit c469a59
Show file tree
Hide file tree
Showing 23 changed files with 4,428 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tests/netteForms.js/SpecRunner.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Jasmine Spec Runner v2.2.0</title>

<link rel="shortcut icon" href="lib/jasmine-2.2.0/jasmine_favicon.png">
<link rel="stylesheet" href="lib/jasmine-2.2.0/jasmine.css">

<script src="lib/jasmine-2.2.0/jasmine.js"></script>
<script src="lib/jasmine-2.2.0/jasmine-html.js"></script>
<script src="lib/jasmine-2.2.0/boot.js"></script>
<script src="lib/fixtures/fixtures.js"></script>

<script src="../../src/assets/netteForms.js"></script>

<script src="spec/Nette.validateRuleSpec.js"></script>
</head>

<body>
</body>
</html>
20 changes: 20 additions & 0 deletions tests/netteForms.js/lib/fixtures/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2012 Duncan Wong

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
85 changes: 85 additions & 0 deletions tests/netteForms.js/lib/fixtures/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
## Fixtures

The fixtures module allows you to load HTML content to be used by your tests. Try using this in conjunction with a powerful assertion library such as [chai-jquery](https://github.com/chaijs/chai-jquery).

The code was completely refactored from the awesome jasmine-jquery with all jasmine and jquery dependencies removed, specs written with Chai + Mocha, and using an iframe implementation as a sandbox. This allows the fixtures to be more portable and minimizes side effects with the test runner.

## Installation

```
npm install js-fixtures // if you use npm
bower install fixtures // or if you prefer bower
```

then within your test runner:

```
<script src="fixtures.js"></script>
// or if you prefer AMD:
define(['fixtures'], function(fixtures){
...
})
```

## Usage

Use `fixtures.load('your-fixture.html')` in your specs. Fixtures will load from `/spec/javascripts/fixtures/your-fixture.html` (see below to change this path). Once that is setup, you should be able to write specs that look like this:

```javascript
describe('my jquery plugin', function(){
var $$;
beforeEach(function(){
fixtures.load('fixture-with-jquery-and-plugin.html');
$$ = fixtures.window().jQuery; // access the jquery instance from within the fixtures context
$$('body').pizzazz();
});
it('it throws a lot of pizzazz on the screen', function(){
$$('body').should.have.wonderful.colors;
$$('body').should.contain(1000000).children;
// etc.. etc..
});
afterEach(function(){
fixtures.cleanUp(); // cleans up the fixture for the next test
});
});
```

## Documentation
Your fixture is being loaded into an iframe container that is automatically added to the DOM. Fixtures are internally cached, so you can load the same fixture file in several tests without penalty to your test suite's speed.

Several methods for loading fixtures are provided:

- `load(fixtureUrl[, fixtureUrl, ...], cb)`
- Loads fixture(s) from one or more files and automatically appends them to the DOM (to the fixtures container).
- If cb is provided, it will fire upon iframe ready
- `set(html)`
- Same as `load` except you may load markup directly without specifying a path
- `sandbox(jsObject)`
- Creates a quick fixture from the js object you provide (ex. `sandbox({id: 'foo-fixture', class: 'cool'})` )
- `read(fixtureUrl[, fixtureUrl, ...])`
- Loads fixture(s) from one or more files but instead of appending them to the DOM returns them as a string (useful in combination with `set` if you want to use a templating engine).
- `cache(fixtureUrl[, fixtureUrl, ...])`
- Pre-loads fixture(s) from one or more files and stores them into cache, without returning them or appending them to the DOM.

Additionally, two clean up methods are provided:

- `clearCache()`
- purges internal cache
- `cleanUp()`
- cleans-up fixtures container

Finally, there are two convenience methods to access the contents of the sandboxed iframe:
- `body`
- returns the html contents of the body. Use it to assert various values on the body of the iframe DOM.
- `window`
- returns the global window reference of the iframe, giving you the ability to use the global variables injected into that context.

Options:
- `fixtures.containerId`
- change the ID of the iframe that gets injected into the page
- `fixtures.path`
- change the path to look for fixtures (default: `spec/javascripts/fixtures`)

## Executing Tests
Do an `npm install` to grab the test dependencies. Then point your browser to the test/index.html file.
130 changes: 130 additions & 0 deletions tests/netteForms.js/lib/fixtures/fixtures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
"use strict";
(function(fixtures){
if (typeof require === 'function' && typeof exports === 'object' && typeof module === 'object') {
// NodeJS
module.exports = fixtures;
} else if (typeof define === 'function' && define.amd){
define(function(){
return fixtures;
});
} else{
var global = (false || eval)('this');
global.fixtures = fixtures;
}

}(new function(){
var fixturesCache = {};
var self = this;

self.containerId = 'js-fixtures';
self.path = 'spec/javascripts/fixtures';
self.window = function(){
var iframe = document.getElementById(self.containerId);
if (!iframe) return null;

return iframe.contentWindow || iframe.contentDocument;
};
self.body = function(){
if (!self.window()) return null;

var content = self.window().document.body.innerHTML;
return content;
};
self.load = function(html){
var cb = typeof arguments[arguments.length - 1] === 'function' ? arguments[arguments.length -1] : null;
addToContainer(self.read.apply(self, arguments), cb);
};
self.set = function(html){
addToContainer(html);
};
self.cache = function(){
self.read.apply(self, arguments);
};
self.sandbox = function(obj){
addToContainer(objToHTML(obj));
};
self.read = function(){
var htmlChunks = '';

Array.prototype.slice.call(arguments, 0).forEach(function(argument){
if (typeof argument === 'string') htmlChunks += getFixtureHtml(argument);
});
return htmlChunks;
};
self.clearCache = function(){
fixturesCache = {};
};
self.cleanUp = function(){
var iframe = document.getElementById(self.containerId);
if(!iframe) return null;

iframe.parentNode.removeChild(iframe);
};
var createContainer = function(html){
var cb = typeof arguments[arguments.length - 1] === 'function' ? arguments[arguments.length -1] : null;
var iframe = document.createElement('iframe');
iframe.setAttribute('id', self.containerId);
iframe.style.opacity = 0;
iframe.style.filter = 'alpha(0)';

document.body.appendChild(iframe);
var doc = iframe.contentWindow || iframe.contentDocument;
doc = doc.document ? doc.document : doc;

if (cb){
var iframeReady = setInterval(function(){
if (doc.readyState === 'complete'){
clearInterval(iframeReady);
cb();
}
}, 0);
}

doc.open();
doc.defaultView.onerror = captureErrors;
doc.write(html);
doc.close();
};
var addToContainer = function(html, cb){
var container = document.getElementById(self.containerId);
if (!container) createContainer.apply(self, arguments);
else self.window().document.body.innerHTML += html;
};
var captureErrors = function(){
if (window.onerror){
// Rewrite the message prefix to indicate that the error
// occurred in the fixture.
arguments[0] = arguments[0].replace(/^[^:]*/, "Uncaught fixture error");
window.onerror.apply(window, arguments);
}
return true;
};
var getFixtureHtml = function(url){
if (typeof fixturesCache[url] === 'undefined'){
loadFixtureIntoCache(url);
}
return fixturesCache[url];
};
var loadFixtureIntoCache = function(relativeUrl){
var url = makeFixtureUrl(relativeUrl);
var request = new XMLHttpRequest();
request.open('GET', url + '?' + new Date().getTime(), false);
request.send(null);
fixturesCache[relativeUrl] = request.responseText;
};
var makeFixtureUrl = function(relativeUrl){
return self.path.match('/$') ? self.path + relativeUrl : self.path + '/' + relativeUrl;
};
var objToHTML = function(obj){
var divElem = document.createElement('div');
for (var key in obj){
if (key === 'class'){ // IE < 9 compatibility
divElem.className = obj[key];
continue;
}
divElem.setAttribute(key, obj[key]);
}
return divElem.outerHTML;
};
}
));
20 changes: 20 additions & 0 deletions tests/netteForms.js/lib/jasmine-2.2.0/MIT.LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright (c) 2008-2014 Pivotal Labs

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Loading

0 comments on commit c469a59

Please sign in to comment.