-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create tests for compileClient file writing.
- Loading branch information
Showing
4 changed files
with
107 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/react-server-cli/src/__tests__/compileClient/compileClientSpec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |