Skip to content

Commit 31cb102

Browse files
committed
Merge pull request #4762 from spicyj/ts
Make TypeScript test work with ReactDOM
2 parents 9400b34 + 589c836 commit 31cb102

File tree

5 files changed

+61
-36
lines changed

5 files changed

+61
-36
lines changed

scripts/jest/preprocessor.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@ var coffee = require('coffee-script');
77

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

10-
var defaultLibraries = [
11-
require.resolve('./jest.d.ts'),
12-
require.resolve('../../src/isomorphic/modern/class/React.d.ts'),
13-
];
14-
15-
var ts = tsPreprocessor(defaultLibraries);
16-
1710
// This assumes the module map has been built. This might not be safe.
1811
// We should consider consuming this from a built fbjs module from npm.
1912
var moduleMap = require('fbjs/module-map');
@@ -26,7 +19,7 @@ module.exports = {
2619
return coffee.compile(src, {'bare': true});
2720
}
2821
if (filePath.match(/\.ts$/) && !filePath.match(/\.d\.ts$/)) {
29-
return ts.compile(src, filePath);
22+
return tsPreprocessor.compile(src, filePath);
3023
}
3124
// TODO: make sure this stays in sync with gulpfile
3225
if (!filePath.match(/\/node_modules\//) &&

scripts/jest/ts-preprocessor.js

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,37 @@ function formatErrorMessage(error) {
1515
);
1616
}
1717

18-
function compile(defaultLib, content, contentFilename) {
18+
function compile(content, contentFilename) {
1919
var output = null;
2020
var compilerHost = {
2121
getSourceFile: function(filename, languageVersion) {
22-
if (filename === contentFilename) {
23-
return ts.createSourceFile(filename, content, 'ES5', '0');
22+
var source;
23+
if (filename === 'lib.d.ts') {
24+
source = fs.readFileSync(
25+
require.resolve('typescript/bin/lib.d.ts')
26+
).toString();
27+
} else if (filename === 'jest.d.ts') {
28+
source = fs.readFileSync(
29+
path.join(__dirname, 'jest.d.ts')
30+
).toString();
31+
} else if (filename === contentFilename) {
32+
source = content;
33+
} else if (/\/(?:React|ReactDOM)(?:\.d)?\.ts$/.test(filename)) {
34+
// TypeScript will look for the .d.ts files in each ancestor directory,
35+
// so there may not be a file at the referenced path as it climbs the
36+
// hierarchy.
37+
try {
38+
source = fs.readFileSync(filename).toString();
39+
} catch (e) {
40+
if (e.code == 'ENOENT') {
41+
return undefined;
42+
}
43+
throw e;
44+
}
45+
} else {
46+
throw new Error('Unexpected filename ' + filename);
2447
}
25-
return defaultLib;
48+
return ts.createSourceFile(filename, source, 'ES5', '0');
2649
},
2750
writeFile: function(name, text, writeByteOrderMark) {
2851
if (output === null) {
@@ -41,7 +64,11 @@ function compile(defaultLib, content, contentFilename) {
4164
return '\n';
4265
},
4366
};
44-
var program = ts.createProgram([contentFilename], tsOptions, compilerHost);
67+
var program = ts.createProgram([
68+
'lib.d.ts',
69+
'jest.d.ts',
70+
contentFilename,
71+
], tsOptions, compilerHost);
4572
var errors = program.getDiagnostics();
4673
if (!errors.length) {
4774
var checker = program.getTypeChecker(true);
@@ -54,20 +81,6 @@ function compile(defaultLib, content, contentFilename) {
5481
return output;
5582
}
5683

57-
module.exports = function(defaultLibs) {
58-
var defaultLibSource = fs.readFileSync(
59-
path.join(path.dirname(require.resolve('typescript')), 'lib.d.ts')
60-
);
61-
62-
for (var i = 0; i < defaultLibs.length; i++) {
63-
defaultLibSource += '\n' + fs.readFileSync(defaultLibs[i]);
64-
}
65-
66-
var defaultLibSourceFile = ts.createSourceFile(
67-
'lib.d.ts', defaultLibSource, 'ES5'
68-
);
69-
70-
return {
71-
compile: compile.bind(null, defaultLibSourceFile),
72-
};
84+
module.exports = {
85+
compile: compile,
7386
};

src/isomorphic/modern/class/React.d.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,4 @@ declare module 'React' {
2626
}
2727
export var PropTypes : any;
2828
export function createElement(tag : any, props ?: any, ...children : any[]) : any
29-
export function render(element : any, container : any) : any
30-
export function unmountComponentAtNode(container : any) : void
31-
export function findDOMNode(instance : any) : any
3229
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*!
2+
* Copyright 2015, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
/**
11+
* TypeScript Definition File for React.
12+
*
13+
* Full type definitions are not yet officially supported. These are mostly
14+
* just helpers for the unit test.
15+
*/
16+
17+
declare module 'ReactDOM' {
18+
export function render(element : any, container : any) : any
19+
export function unmountComponentAtNode(container : any) : void
20+
export function findDOMNode(instance : any) : any
21+
}

src/isomorphic/modern/class/__tests__/ReactTypeScriptClass-test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*/
99

1010
import React = require('React');
11+
import ReactDOM = require('ReactDOM');
1112

1213
// Before Each
1314

@@ -27,7 +28,7 @@ class Inner extends React.Component {
2728
}
2829

2930
function test(element, expectedTag, expectedClassName) {
30-
var instance = React.render(element, container);
31+
var instance = ReactDOM.render(element, container);
3132
expect(container.firstChild).not.toBeNull();
3233
expect(container.firstChild.tagName).toBe(expectedTag);
3334
expect(container.firstChild.className).toBe(expectedClassName);
@@ -313,7 +314,7 @@ describe('ReactTypeScriptClass', function() {
313314
it('throws if no render function is defined', function() {
314315
spyOn(console, 'error');
315316

316-
expect(() => React.render(React.createElement(Empty), container)).toThrow();
317+
expect(() => ReactDOM.render(React.createElement(Empty), container)).toThrow();
317318

318319
expect((<any>console.error).argsForCall.length).toBe(1);
319320
expect((<any>console.error).argsForCall[0][0]).toBe(
@@ -425,7 +426,7 @@ describe('ReactTypeScriptClass', function() {
425426
'did-update', { value: 'foo' }, {}
426427
]);
427428
lifeCycles = []; // reset
428-
React.unmountComponentAtNode(container);
429+
ReactDOM.unmountComponentAtNode(container);
429430
expect(lifeCycles).toEqual([
430431
'will-unmount'
431432
]);
@@ -527,7 +528,7 @@ describe('ReactTypeScriptClass', function() {
527528
'DIV',
528529
'foo'
529530
);
530-
var node = React.findDOMNode(instance);
531+
var node = ReactDOM.findDOMNode(instance);
531532
expect(node).toBe(container.firstChild);
532533
});
533534

0 commit comments

Comments
 (0)