Skip to content

Commit

Permalink
Add infrastructure for easy direct testing of SVG output
Browse files Browse the repository at this point in the history
(Run bootstrap-tests.js with e.g. babel-node to generate a new fixtures.js)
  • Loading branch information
motiz88 committed Apr 13, 2016
1 parent f9cd645 commit 8629649
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 0 deletions.
10 changes: 10 additions & 0 deletions __tests__/fixtures.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This file is auto-updated by ../bootstrap-tests.js

import React from 'react';
import {Sparklines, SparklinesLine} from '../src/Sparklines';

export default {
// AUTO-GENERATED PART STARTS HERE
'SimpleLine': {jsx: (<Sparklines data={[1, 2, 3]}><SparklinesLine /></Sparklines>), svg: ''}
// AUTO-GENERATED PART ENDS HERE
};
14 changes: 14 additions & 0 deletions __tests__/graphical-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import fixtures from './fixtures';
import {render} from 'enzyme';
import { expect } from 'chai';

describe('Graphical tests from fixtures.js', function() {
for (let key of Object.keys(fixtures)) {
describe(`${key}`, function() {
it('should render as specified', function() {
const wrapper = render(fixtures[key].jsx);
expect(wrapper.html()).to.eq(fixtures[key].svg);
});
});
}
});
78 changes: 78 additions & 0 deletions bootstrap-tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import path from 'path';
import {render} from 'enzyme';
import LineByLineReader from 'line-by-line';
import reactElementToJsx from 'react-element-to-jsx-string';

const fixturesFile = path.resolve(__dirname, './__tests__/fixtures.js');
const dynamicPartStartSignal = '// AUTO-GENERATED PART STARTS HERE';
const dynamicPartEndSignal = '// AUTO-GENERATED PART ENDS HERE';

const fixtures = require(fixturesFile).default;

// Output control
let outData = '';
const write = content => { outData += content + '\n'; }
const save = () => console.log(outData);
function writeFixtures() {
for (let key of Object.keys(fixtures)) {
const jsx = fixtures[key].jsx;
const wrapper = render(jsx);
const jsxCode = `(${toOneLine(reactElementToJsx(jsx))})`;
const htmlCode = JSON.stringify(wrapper.html());

write(`\t${JSON.stringify(key)}: {jsx: ${jsxCode}, html: ${htmlCode}},`);
}
}
function toOneLine(code) {
return code.replace(/\s*[\r\n]\s*/g, ' ').replace(/\s+/g, ' ');
}

// Input control
const lr = new LineByLineReader(fixturesFile, {skipEmptyLines: false});
let inDynamicPart = false, dynamicPartCount = 0, lineCount = 0;

lr.on('line', line => {
++lineCount;
if (line === dynamicPartStartSignal) {
if (inDynamicPart)
throw new LineError('Dynamic part opened again');
++dynamicPartCount;
if (dynamicPartCount > 1)
throw new LineError('Multiple dynamic parts found');
inDynamicPart = true;
write(line);
try {
writeFixtures();
} catch(e) {
throw new LineError(e);
}
}
else if (line === dynamicPartEndSignal) {
if (!inDynamicPart)
throw new LineError('Dynamic part closed again');
inDynamicPart = false;
write(line);
}
else if (!inDynamicPart)
write(line);
});

lr.on('end', () => {
if (inDynamicPart) {
throw new LineError('Dynamic part not closed');
}
if (!dynamicPartCount) {
throw new LineError('No dynamic part found in file!');
}
save();
});

lr.on('error', function (err) {
throw new LineError(err);
});

class LineError extends Error {
constructor(message) {
super(`${fixturesFile}:${lineCount}: ${message}`);
}
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,10 @@
"chai": "^3.5.0",
"enzyme": "^2.1.0",
"jsdom": "^8.1.0",
"line-by-line": "^0.1.4",
"mocha": "^2.4.5",
"react-addons-test-utils": "^0.14.7",
"react-element-to-jsx-string": "^2.5.0",
"webpack": "^2.1.0-beta.4",
"webpack-dev-server": "^2.0.0-beta"
},
Expand Down

0 comments on commit 8629649

Please sign in to comment.