fastly/epoch — pull request #109 by rsandor (2014-07-07 09:49:50+00:00)
Title: Closes #24; Added Core Unit Tests

Added core functionality unit testing with [Mocha](http://visionmedia.github.io/mocha/), [Chai](http://chaijs.com/), and [jsDom](https://github.com/tmpvar/jsdom). Decided to leave individual chart testing for later as this is mostly covered by the current rendering tests. Note that rendering tests are now under the `render-test/` directory, and the new unit tests are under `test/`.

### How it works

To run the tests, from the command-line execute `cake test`. This task will first build the JavaScript and CSS for the Project and then invoke mocha.

The `test/` directory currently contains only two files, the first being a directory containing the core test suites, and the second an initialization script. The script (`test/init.coffee`) sets up the testing environment in the following way:

1. Sets the Time Zone to PST - useful for testing date related functionality
2. Loads `chai` and sets it to `global.assert` - This allows each of the suites to simply call `assert.*` without having to load the library indivitually.
3. Registers a mocha `before` handler that sets up a jsDom environment which includes d3 and the current working version of the javascript.
4. Adds a few helper globals - Namely: `Epoch`, `d3`, and `doc`
5. Sets a global device pixel ratio to 2 - extremely useful for testing `Epoch.Chart.Canvas`
6. Adds a global method named `addStyleSheet` - When given a string of CSS this method will insert a new CSS style tag into the head of the jsDom document.

### New Issues

While building out the core test suites I ran across a couple of unknown issues in the library itself, they were:

- `Epoch.Util.dasherize` didn't work with multiple spaces (missing `/g` on the regular expression).
- `Epoch.Events.off` flat out didn't work after the changes to the type functions in #62
- Added a new `draw` event that is triggered by `Epoch.Chart.Base`. It is used to inform listeners that drawing has completed. Each of the individual chart implementations now calls `super` up to the base chart to ensure the event fires.

### Dummy Rendering Context

Finally, jsDom has very limited `` functionality. While the authors suggest using `node-canvas`, I found the installation process to be less than desirable. Instead of having everyone go through the pain of installing it just to run tests I opted abstract out the way we get rendering contexts.

To this end I added a new `Epoch.Util.getContext` method that, when given a node, attempts to get a context from that node. If it is unable to do so it simply returns a dummy context which implements a pretty big portion of the HTML5 `CanvasRenderingContext` methods (include all the ones we use in the real-time charts). The new dummy context is named `Epoch.TestContext`, and in the future we might be able to use it to do operation-by-operation unit testing for each of the real-time chart classes.