Skip to content

Commit

Permalink
Create tests for compileClient file writing.
Browse files Browse the repository at this point in the history
  • Loading branch information
drewpc committed Jan 15, 2017
1 parent a5d6a4e commit 49f1740
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 2 deletions.
19 changes: 18 additions & 1 deletion packages/react-server-cli/gulpfile.babel.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import eslint from "gulp-eslint";
import gulp from "gulp";
import babel from "gulp-babel";
import jasmine from "gulp-jasmine";
import logging from "react-server-gulp-module-tagger";

function getSpecGlob (prefix) {
// add a wildcard onto the end if no file extension or wildcard
// currently present
let specGlob = "*[Ss]pec.js";
if (!specGlob.endsWith(".js") && !specGlob.endsWith("*")) {
specGlob += "*";
}

const specs = prefix + specGlob;
return specs;
}

gulp.task("default", () => {
return gulp.src("src/**/*.js")
.pipe(logging())
Expand All @@ -24,7 +37,11 @@ gulp.task("eslint", [], () => {
});

// there are no tests for this project :(
gulp.task("test", ["eslint"]);
gulp.task("test", ["default", "eslint"], () => {
process.env.NODE_ENV = "__react-server-cli-unit-test__"; // eslint-disable-line no-process-env
return gulp.src(getSpecGlob("target/__tests__/**/"))
.pipe(jasmine({verbose:true, includeStackTrace: true}));
});

gulp.task("watch", () => {
gulp.watch("src/*.js", ['default']);
Expand Down
2 changes: 2 additions & 0 deletions packages/react-server-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
"gulp": "^3.9.1",
"gulp-babel": "^6.1.2",
"gulp-eslint": "^3.0.1",
"gulp-jasmine": "^2.4.2",
"memory-stream": "0.0.3",
"nsp": "^2.6.2",
"react-hot-loader": "^1.3.1",
"react-server-gulp-module-tagger": "^0.4.10",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import fs from "fs";
const MemoryStream = require('memory-stream');
import path from "path";

import { writeWebpackCompatibleRoutesFile } from "../../compileClient";

describe("compileClient", () => {
let mockFs;

describe("writes client routes file for Webpack", () => {
const pathStringTests = [
{
title: "apostrophes",
path: "PathWith'InIt.js",
},
{
title: "double quotes",
path: 'PathWith"InIt.js',
},
{
title: "windows style",
path: 'c:\\Path\\With\\InIt.js',
},
{
title: "spaces",
path: 'Path With Spaces.js',
},
];

beforeEach(() => {
mockFs = new MemoryStream();
});

afterEach(() => {
mockFs = null;
});

pathStringTests.map((test) => {
it("handles file paths with " + test.title, (finishTest) => {
spyOn(fs, 'writeFileSync').and.callFake((path, data) => {
mockFs.write(data);
});

const filePath = test.path;
const routes = {
"routes": {
"Homepage": {
"path": "/",
"page": filePath,
},
},
};

writeWebpackCompatibleRoutesFile(routes, ".", path.normalize("."), null, true, null);

const coreMiddlewareStringified = JSON.stringify(require.resolve("react-server-core-middleware"));
const filePathStringified = JSON.stringify(filePath);
const filePathRegexStrings = [
"var coreJsMiddleware = require(" + coreMiddlewareStringified + ").coreJsMiddleware;",
"var coreCssMiddleware = require(" + coreMiddlewareStringified + ").coreCssMiddleware;",
"require.ensure(" + filePathStringified + ", function() {",
"cb(unwrapEs6Module(require(" + filePathStringified + ")));",
];

const fileData = mockFs.toString();
filePathRegexStrings.map((regex) => {
expect(fileData).toMatch(regex.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"));
});

finishTest();
});
});
});
});
14 changes: 13 additions & 1 deletion packages/react-server-cli/src/callerDependency.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import lookup from "look-up";
import path from "path";

export default function callerDependency(dep) {
// TODO: We should grab stuff based on what the routes file would get out
// of `require.resolve(dep)`. Using `process.cwd()` instead for now.
return lookup("node_modules/" + dep, {cwd: process.cwd()});
let cwd = process.cwd(),
lookupResult;

if (process.env.NODE_ENV === '__react-server-cli-unit-test__') { // eslint-disable-line no-process-env
cwd = path.resolve(cwd + '/..');
lookupResult = lookup("packages/" + dep, {cwd: cwd});
} else {
lookupResult = lookup("node_modules/" + dep, {cwd: cwd});
}
console.log('cwd: ', cwd);
console.log(lookupResult);
return lookupResult;
}

0 comments on commit 49f1740

Please sign in to comment.