Skip to content

Commit

Permalink
complete testing process function
Browse files Browse the repository at this point in the history
  • Loading branch information
tompascall committed Feb 25, 2017
1 parent 74253da commit f0aba5e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 28 deletions.
75 changes: 53 additions & 22 deletions src/__tests__/main.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,27 @@ const configPath = `${path.resolve('.riot-jest-tranformer')}`;

describe('riot-jest-transformer', function() {

describe('getCompiled', () => {
it('gives back the compiled tag', function() {
expect(transformer.getCompiled(hello).search(/riot.tag2\(['|"]hello['|"]/)).not.toEqual(-1);
});
});

describe('isertRiot', () => {
it('should insert riot dependency into compiled tag', () => {
const hello2 = `
import { someMethod } from 'someModule';
<hello>
<h1>{ opts.name }</h1>
</hello>
`;

let compiled = transformer.getCompiled(hello2);
let completed = transformer.insertRiot(compiled);
expect(completed.indexOf('const riot = require("riot")')).not.toEqual(-1);
});
});

describe('getDefaultConfig', () => {
it('should use babel transformer optioned with filename', () => {
const config = transformer.getDefaultConfig({ filename: 'fakeFile'});
Expand All @@ -31,10 +52,10 @@ describe('riot-jest-transformer', function() {

if (fs.existsSync(configPath)) {
fs.unlinkSync(configPath);
expect(callGetConfig).not.toThrow();
const config = transformer.getConfig({ filename: 'fakeFile' });
expect(config).toEqual(transformer.getDefaultConfig({ filename: 'fakeFile' }));
}
expect(callGetConfig).not.toThrow();
const config = transformer.getConfig({ filename: 'fakeFile' });
expect(config).toEqual(transformer.getDefaultConfig({ filename: 'fakeFile' }));
});

it('should use .riot-jest-transformer file as config if it exists', () => {
Expand All @@ -46,6 +67,7 @@ describe('riot-jest-transformer', function() {

fs.writeFileSync(configPath, JSON.stringify(config), {encoding: 'utf8'});
expect(transformer.getConfig({ filename: 'fakeFile'})).toEqual(config);
fs.unlinkSync(configPath);
});
});

Expand Down Expand Up @@ -81,28 +103,27 @@ describe('riot-jest-transformer', function() {
});
});

describe('isertRiot', () => {
it('should insert riot dependency into compiled tag', () => {
const hello2 = `
import { transform } from 'babel-core';
<hello>
<h1>{ opts.name }</h1>
</hello>
`;

let compiled = transformer.getCompiled(hello2);
let completed = transformer.insertRiot(compiled);
expect(completed.indexOf('const riot = require("riot")')).not.toEqual(-1);
});
});

describe('process', () => {
let compiled,
completedWithRiot,
config,
transformed;

it('should be a function', function() {
expect(typeof process).toBe('function');
});

it('gives back the compiled tag', function() {
expect(process(hello).search(/riot.tag2\(['|"]hello['|"]/)).not.toEqual(-1);
it('should call getCompiled with tag source', () => {
spyOn(transformer, 'getCompiled').and.callThrough();
process(hello, 'fakeFile');
expect(transformer.getCompiled).toHaveBeenCalledWith(hello);
});

it('should call insertRiot with compiled tag', () => {
compiled = transformer.getCompiled(hello);
spyOn(transformer, 'insertRiot').and.callThrough();
process(hello, 'fakeFile');
expect(transformer.insertRiot).toHaveBeenCalledWith(compiled);
});

it('should call getConfig with the second argument (provided by jest as filename)', () => {
Expand All @@ -111,9 +132,19 @@ describe('riot-jest-transformer', function() {
expect(transformer.getConfig).toHaveBeenCalledWith({ filename: 'fakeFile'});
});

xit('should insert riot import into transformed tag in order to be able to run riot tag', () => {
it('should call getTreansformed with compiled tag and config', () => {
completedWithRiot = transformer.insertRiot(compiled);
config = transformer.getConfig({ filename: 'fakeFile' });

spyOn(transformer, 'getTransformed').and.callThrough();
process(hello, 'fakeFile');
expect(transformer.getTransformed).toHaveBeenCalledWith({ compiled: completedWithRiot, ...config});
});
});

it('returns code attribute of transformed tag', () => {
transformed = transformer.getTransformed({ compiled: completedWithRiot, ...config});
expect(process(hello, 'fakeFile')).toEqual(transformed.code);
});
});
});

13 changes: 7 additions & 6 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ const { transform } = require('babel-core');
const path = require('path');
const fs = require('fs');
const CONFIG_PATH = `${path.resolve('.riot-jest-tranformer')}`;
const RIOT_PROCESSOR = 'riot.tag2';

const transformer = {
getCompiled (source) {
return riot.compile(source);
},

insertRiot (compiled) {
let completed;
const [ header, tag ] = compiled.split('riot.tag2');
const [ header, tag ] = compiled.split(RIOT_PROCESSOR);
if (header.search(/from\s*['|"]riot['|"]/) == -1 &&
header.search(/require\s*\(['|"]riot['|"]\)/)
) {
Expand Down Expand Up @@ -38,20 +40,19 @@ const transformer = {
},

getTransformed ({ compiled, transformer, method, args = [] } = {}) {
args.unshift(compiled);
if (transformer && method) {
const transformerByParam = require(transformer);
return transformerByParam[method](...args);
return transformerByParam[method](compiled, ...args);
}
return transform(...args);
return transform(compiled, ...args);
}
}

exports.process = function (source, filename) {
let compiled = transformer.getCompiled(source);
let appendedCompiled = transformer.insertRiot(compiled);
let completedWithRiot = transformer.insertRiot(compiled);
const config = transformer.getConfig({ filename });
let transformed = transformer.getTransformed({ compiled: appendedCompiled, ...config });
let transformed = transformer.getTransformed({ compiled: completedWithRiot, ...config });
return transformed.code;
}

Expand Down

0 comments on commit f0aba5e

Please sign in to comment.