Skip to content

Commit

Permalink
Closes #7. Use 'karma-json-fixtures-preprocessor' to load json fixtures.
Browse files Browse the repository at this point in the history
  • Loading branch information
billtrik committed May 26, 2015
1 parent dbd395e commit 68bb717
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 27 deletions.
32 changes: 23 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ Add `fixture` to the `frameworks` array in your Karma configuration:
```javascript
module.exports = function(config){
config.set({
// ...
frameworks: ['mocha', 'fixture'],

// ...
```
Expand All @@ -32,37 +32,50 @@ all files of interest under this base path.
```javascript
module.exports = function(config){
config.set({
// ...
files: [
{
pattern: 'fixtures/base/path/**/*',
},
// ...
],

// ...
],
```
Finally you have to add the html2js karma preprocessor:
Finally you have to add the `html2js` and `karma-json-fixtures-preprocessor` karma preprocessors:
```sh
$ npm install karma-html2js-preprocessor --save-dev
$ npm install karma-json-fixtures-preprocessor --save-dev
```
and then configure Karma to load all html and JSON fixture files:
, configure Karma to load all html and JSON fixture files via those preprocessors:
```javascript
module.exports = function(config){
config.set({
// ...
preprocessors: {
'**/*.html' : ['html2js'],
'**/*.json' : ['html2js']
'**/*.json' : ['json_fixtures']
},
// ...
```
and then setup the `karma-json-fixtures-preprocessor` plugin:
```javascript
module.exports = function(config){
config.set({
// ...
jsonFixturesPreprocessor: {
variableName: '__json__'
},
// ...
```
*(optional)* If the plugin won't get loaded by karma, you might have to declare it inside the `plugins` array in your Karma configuration
*(optional)* If the plugins won't get loaded by karma, you might have to declare them inside the `plugins` array in your Karma configuration
*(and maybe load `karma-html2js-preprocessor` as well)*:
```javascript
Expand All @@ -72,19 +85,20 @@ module.exports = function(config){
plugins: [
'karma-fixture'
'karma-html2js-preprocessor'
'karma-json-fixtures-preprocessor'
// ...
],

// ...
```
Implementation details
-----
All fixture files are pre-loaded as strings and placed inside the Karma-created `window.__html__` array.
All html fixture files are pre-loaded as strings and placed inside the Karma-created `window.__html__` object and all json fixtures are loaded inside
`window.__json__`.
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.
It loads fixture files from these objects and appends the created html inside the `window.fixture.el` element that gets created on start-up. It appends loaded JSONs inside the `fixture.json` array.
Usage
Expand Down
41 changes: 31 additions & 10 deletions spec/fixture_spec.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
json_data = {
json_data =
test1: 'check'
test2: 'ok'
}

json_template = JSON.stringify(json_data)
json_template = JSON.stringify json_data
escaped_json_template = JSON.stringify
"myLink": "<a href=\"link.url\">Link</a>"

html_template1 = '<h1 id="tmpl">test</h1>'
html_template2 = '<h2 id="tmpl">test</h2><p>multiple</p>'
html_template3 = '<script>window.test_a_test = true</script>'
Expand All @@ -12,6 +14,10 @@ html_template5 = '<script type="application/javascript">window.test_c_test = tru
html_template6 = '<script type="text/x-custom">window.test_d_test = true</script>'
fixture_base = 'spec/fixtures'

load_template_as_karma_json_fixures = (name, string, base = fixture_base)->
window.__json__ ?= {}
window.__json__["#{base}/#{name}"] = string

load_template_as_karma_html2js = (name, string, base = fixture_base)->
window.__html__ ?= {}
window.__html__["#{base}/#{name}"] = string
Expand Down Expand Up @@ -82,15 +88,16 @@ describe 'Fixture', ->
@instance = null
delete @instance

describe 'load', ->
describe '#load()', ->
beforeEach ->
load_template_as_karma_html2js 'html1', html_template1
load_template_as_karma_html2js 'html2', html_template2
load_template_as_karma_html2js 'html3', html_template3
load_template_as_karma_html2js 'html4', html_template4
load_template_as_karma_html2js 'html5', html_template5
load_template_as_karma_html2js 'html6', html_template6
load_template_as_karma_html2js 'json.json', json_template
load_template_as_karma_json_fixures 'json.json', json_template
load_template_as_karma_json_fixures 'escaped_json.json', escaped_json_template

afterEach ->
cleanup_karma_html2js_templates()
Expand All @@ -99,7 +106,7 @@ describe 'Fixture', ->
@instance.load 'html1'
expect(@fixture_cont.children.length).to.equal 1

it 'accepts an append boolean as second param', ->
it 'accepts an "append" boolean as second param', ->
@instance.load 'html1', true
expect(@fixture_cont.children.length).to.equal 1

Expand Down Expand Up @@ -203,18 +210,32 @@ describe 'Fixture', ->
result = @instance.load 'json.json'
expect(result).to.include json_data

it 'retrieves json templates from window.__json__', ->
result = @instance.load 'json.json'
expect(result).to.eql(JSON.parse(window.__json__["#{fixture_base}/json.json"]))

it 'loads the json template into fixture.json', ->
@instance.load 'json.json'
expect(@instance.json[0]).to.include json_data

it 'returns multipe json objects', ->
it 'returns multiple json objects', ->
result = @instance.load 'json.json', 'json.json'
expect(result.length).to.equal 2

it 'loads multiple json templates into fixture.json', ->
result = @instance.load 'json.json', 'json.json'
expect(@instance.json.length).to.equal 2

context 'when it loads json template with escaped characters', ->
it 'does not throw', ->
func = -> @instance.load 'escaped_json.json'
expect(func).to.not.throw

it 'creates properly unescaped data', ->
result = @instance.load 'escaped_json.json'
expect(result.myLink).to.eql('<a href=\"link.url\">Link</a>')
expect(result.myLink.indexOf('\\')).to.equal(-1);

context 'json and html templates', ->
beforeEach ->
@result = @instance.load 'html1', 'json.json'
Expand All @@ -225,7 +246,7 @@ describe 'Fixture', ->
it 'pushes the json obj to fixture.json', ->
expect(@instance.json[0]).to.include json_data

describe 'set', ->
describe '#set()', ->
it 'accepts a string as first param and creates a dom element', ->
@instance.set html_template1
expect(@fixture_cont.innerHTML).to.equal html_template1
Expand Down Expand Up @@ -295,7 +316,7 @@ describe 'Fixture', ->
expect(@result[1][1])
.to.equal @fixture_cont.children[2]

describe 'cleanup', ->
describe '#cleanup()', ->
beforeEach ->
load_template_as_karma_html2js 'html1', html_template1
load_template_as_karma_html2js 'html2', html_template2
Expand All @@ -315,7 +336,7 @@ describe 'Fixture', ->
it 'empties fixture container', ->
expect(@fixture_cont.innerHTML).to.equal ''

describe 'setBase', ->
describe '#setBase()', ->
beforeEach ->
@lastBase = @instance.base
@testBase = 'test_base'
Expand Down
28 changes: 20 additions & 8 deletions src/fixture.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class Fixture
)()

load: (filenames..., append = false) ->
__html__ = window.__html__ || {}
__json__ = window.__json__ || {}

unless typeof append is 'boolean'
filenames.push append
append = false
Expand All @@ -42,17 +45,26 @@ class Fixture
else
fixture_path = "#{@base}/#{filename}"

string = __html__?[fixture_path]
@_throwNoFixture(fixture_path) unless string?

# JSON
if filename.indexOf('.json') isnt -1
json = __json__[filename.replace('.json', '')] or
__json__[filename] or
__json__[fixture_path] or
__json__["#{@base}/#{filename.replace('.json', '')}"]

@_throwNoFixture(fixture_path) unless json?

# Is this needed?
try
json = JSON.parse string
@json.push json
results.push json
catch err
json = JSON.parse json
catch ignore

@json.push json
results.push json
else if __html__[fixture_path]
results.push @_appendFixture __html__[fixture_path]
else
results.push @_appendFixture string
@_throwNoFixture(fixture_path) unless string?

results = results[0] if results.length is 1
return results
Expand Down

0 comments on commit 68bb717

Please sign in to comment.