Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make TypeScript test work with ReactDOM #4762

Merged
merged 1 commit into from
Sep 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 1 addition & 8 deletions scripts/jest/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,6 @@ var coffee = require('coffee-script');

var tsPreprocessor = require('./ts-preprocessor');

var defaultLibraries = [
require.resolve('./jest.d.ts'),
require.resolve('../../src/isomorphic/modern/class/React.d.ts'),
];

var ts = tsPreprocessor(defaultLibraries);

// This assumes the module map has been built. This might not be safe.
// We should consider consuming this from a built fbjs module from npm.
var moduleMap = require('fbjs/module-map');
Expand All @@ -26,7 +19,7 @@ module.exports = {
return coffee.compile(src, {'bare': true});
}
if (filePath.match(/\.ts$/) && !filePath.match(/\.d\.ts$/)) {
return ts.compile(src, filePath);
return tsPreprocessor.compile(src, filePath);
}
// TODO: make sure this stays in sync with gulpfile
if (!filePath.match(/\/node_modules\//) &&
Expand Down
55 changes: 34 additions & 21 deletions scripts/jest/ts-preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,37 @@ function formatErrorMessage(error) {
);
}

function compile(defaultLib, content, contentFilename) {
function compile(content, contentFilename) {
var output = null;
var compilerHost = {
getSourceFile: function(filename, languageVersion) {
if (filename === contentFilename) {
return ts.createSourceFile(filename, content, 'ES5', '0');
var source;
if (filename === 'lib.d.ts') {
source = fs.readFileSync(
require.resolve('typescript/bin/lib.d.ts')
).toString();
} else if (filename === 'jest.d.ts') {
source = fs.readFileSync(
path.join(__dirname, 'jest.d.ts')
).toString();
} else if (filename === contentFilename) {
source = content;
} else if (/\/(?:React|ReactDOM)(?:\.d)?\.ts$/.test(filename)) {
// TypeScript will look for the .d.ts files in each ancestor directory,
// so there may not be a file at the referenced path as it climbs the
// hierarchy.
try {
source = fs.readFileSync(filename).toString();
} catch (e) {
if (e.code == 'ENOENT') {
return undefined;
}
throw e;
}
} else {
throw new Error('Unexpected filename ' + filename);
}
return defaultLib;
return ts.createSourceFile(filename, source, 'ES5', '0');
},
writeFile: function(name, text, writeByteOrderMark) {
if (output === null) {
Expand All @@ -41,7 +64,11 @@ function compile(defaultLib, content, contentFilename) {
return '\n';
},
};
var program = ts.createProgram([contentFilename], tsOptions, compilerHost);
var program = ts.createProgram([
'lib.d.ts',
'jest.d.ts',
contentFilename,
], tsOptions, compilerHost);
var errors = program.getDiagnostics();
if (!errors.length) {
var checker = program.getTypeChecker(true);
Expand All @@ -54,20 +81,6 @@ function compile(defaultLib, content, contentFilename) {
return output;
}

module.exports = function(defaultLibs) {
var defaultLibSource = fs.readFileSync(
path.join(path.dirname(require.resolve('typescript')), 'lib.d.ts')
);

for (var i = 0; i < defaultLibs.length; i++) {
defaultLibSource += '\n' + fs.readFileSync(defaultLibs[i]);
}

var defaultLibSourceFile = ts.createSourceFile(
'lib.d.ts', defaultLibSource, 'ES5'
);

return {
compile: compile.bind(null, defaultLibSourceFile),
};
module.exports = {
compile: compile,
};
3 changes: 0 additions & 3 deletions src/isomorphic/modern/class/React.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,4 @@ declare module 'React' {
}
export var PropTypes : any;
export function createElement(tag : any, props ?: any, ...children : any[]) : any
export function render(element : any, container : any) : any
export function unmountComponentAtNode(container : any) : void
export function findDOMNode(instance : any) : any
}
21 changes: 21 additions & 0 deletions src/isomorphic/modern/class/ReactDOM.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*!
* Copyright 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

/**
* TypeScript Definition File for React.
*
* Full type definitions are not yet officially supported. These are mostly
* just helpers for the unit test.
*/

declare module 'ReactDOM' {
export function render(element : any, container : any) : any
export function unmountComponentAtNode(container : any) : void
export function findDOMNode(instance : any) : any
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import React = require('React');
import ReactDOM = require('ReactDOM');

// Before Each

Expand All @@ -27,7 +28,7 @@ class Inner extends React.Component {
}

function test(element, expectedTag, expectedClassName) {
var instance = React.render(element, container);
var instance = ReactDOM.render(element, container);
expect(container.firstChild).not.toBeNull();
expect(container.firstChild.tagName).toBe(expectedTag);
expect(container.firstChild.className).toBe(expectedClassName);
Expand Down Expand Up @@ -313,7 +314,7 @@ describe('ReactTypeScriptClass', function() {
it('throws if no render function is defined', function() {
spyOn(console, 'error');

expect(() => React.render(React.createElement(Empty), container)).toThrow();
expect(() => ReactDOM.render(React.createElement(Empty), container)).toThrow();

expect((<any>console.error).argsForCall.length).toBe(1);
expect((<any>console.error).argsForCall[0][0]).toBe(
Expand Down Expand Up @@ -425,7 +426,7 @@ describe('ReactTypeScriptClass', function() {
'did-update', { value: 'foo' }, {}
]);
lifeCycles = []; // reset
React.unmountComponentAtNode(container);
ReactDOM.unmountComponentAtNode(container);
expect(lifeCycles).toEqual([
'will-unmount'
]);
Expand Down Expand Up @@ -527,7 +528,7 @@ describe('ReactTypeScriptClass', function() {
'DIV',
'foo'
);
var node = React.findDOMNode(instance);
var node = ReactDOM.findDOMNode(instance);
expect(node).toBe(container.firstChild);
});

Expand Down