-
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. * Apply fixes for various path issues in compileClient. Remove console.log instrumentation. Update gulpfile and test spec. * Remove out of date comments regarding tests not being present. * Restore callerDependency to it's original state. Create a mock callerDependency and add mockery to handle properly requiring the mock instead of the original callerDependency. * Removed unnecessary getSpecGlob() function.
- Loading branch information
Showing
7 changed files
with
119 additions
and
14 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
92 changes: 92 additions & 0 deletions
92
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,92 @@ | ||
import fs from "fs"; | ||
const MemoryStream = require('memory-stream'); | ||
import path from "path"; | ||
import mockery from "mockery"; | ||
|
||
describe("compileClient", () => { | ||
let mockFs, | ||
writeWebpackCompatibleRoutesFile; | ||
|
||
beforeAll(() => { | ||
mockery.registerSubstitute('./callerDependency', './callerDependency-Mock'); | ||
mockery.enable({ | ||
useCleanCache: true, | ||
warnOnUnregistered: false, | ||
}); | ||
|
||
writeWebpackCompatibleRoutesFile = require("../../compileClient").writeWebpackCompatibleRoutesFile; | ||
}); | ||
|
||
afterAll(() => { | ||
mockery.disable(); | ||
}); | ||
|
||
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); | ||
|
||
// These four strings are important when using multiple platforms or strings with weird characters in them. | ||
// If we're going to output something to a file and then import that file later, we'd better be darn sure | ||
// it's all correctly formatted! Apostrophes, quotes, and windows-style file path characters \ vs / are the worst! | ||
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
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("packages/" + dep, {cwd: path.resolve(process.cwd() + '/..')}); | ||
} |
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
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