Skip to content

Commit

Permalink
Add Basic TypeScript Class Test
Browse files Browse the repository at this point in the history
As part of the new class effort it is now possible to define React
Components using any type of generic JavaScript class syntax.

This includes TypeScript classes. This test ensures that we don't regress
that support, and also serves as an example for using React in TypeScript.
TypeScript provides a good demo of where we think property initializers
are going.

We don't have any official *type* support for TypeScript yet.

This test trails the ReactES6Class-test file. Some manual tweaking is
required when converting tests.
  • Loading branch information
sebmarkbage committed Jan 27, 2015
1 parent d750cf7 commit 6c145c3
Show file tree
Hide file tree
Showing 6 changed files with 620 additions and 2 deletions.
71 changes: 71 additions & 0 deletions jest/jest.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
declare var jasmine: any;

declare function afterEach(fn: any): any;
declare function beforeEach(fn: any): any;
declare function describe(name: string, fn: any): void;
declare var it: {
(name: string, fn: any): void;
only: (name: string, fn: any) => void;
}
declare function expect(val: any): Expect;
declare var jest: Jest;
declare function pit(name: string, fn: any): void;
declare function spyOn(obj: any, key: string): any;
declare function xdescribe(name: string, fn: any): void;
declare function xit(name: string, fn: any): void;

interface Expect {
not: Expect
toThrow(message?: string): void
toBe(value: any): void
toEqual(value: any): void
toBeFalsy(): void
toBeTruthy(): void
toBeNull(): void
toBeUndefined(): void
toBeDefined(): void
toMatch(regexp: RegExp): void
toContain(string: string): void
toBeCloseTo(number: number, delta: number): void
toBeGreaterThan(number: number): void
toBeLessThan(number: number): void
toBeCalled(): void
toBeCalledWith(...arguments): void
lastCalledWith(...arguments): void
}

interface Jest {
autoMockOff(): void
autoMockOn(): void
clearAllTimers(): void
dontMock(moduleName: string): void
genMockFromModule(moduleObj: Object): Object
genMockFunction(): MockFunction
genMockFn(): MockFunction
mock(moduleName: string): void
runAllTicks(): void
runAllTimers(): void
runOnlyPendingTimers(): void
setMock(moduleName: string, moduleExports: Object): void
}

interface MockFunction {
(...arguments): any
mock: {
calls: Array<Array<any>>
instances: Array<Object>
}
mockClear(): void
mockImplementation(fn: Function): MockFunction
mockImpl(fn: Function): MockFunction
mockReturnThis(): MockFunction
mockReturnValue(value: any): MockFunction
mockReturnValueOnce(value: any): MockFunction
}

// Allow importing jasmine-check
declare module 'jasmine-check' {
export function install(global?: any): void;
}
declare var check: any;
declare var gen: any;
17 changes: 17 additions & 0 deletions jest/preprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,29 @@
var ReactTools = require('../main.js');

var coffee = require('coffee-script');
var ts = require('ts-compiler');

module.exports = {
process: function(src, path) {
if (path.match(/\.coffee$/)) {
return coffee.compile(src, {'bare': true});
}
if (path.match(/\.ts$/) && !path.match(/\.d\.ts$/)) {
ts.compile([path], {
skipWrite: true,
module: 'commonjs'
}, function(err, results) {
if (err) {
throw err;
}
results.forEach(function(file) {
// This is gross, but jest doesn't provide an asynchronous way to
// process a module, and ts currently runs syncronously.
src = file.text;
});
});
return src;
}
return ReactTools.transform(src, {harmony: true});
}
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"populist": "~0.1.6",
"recast": "^0.9.11",
"sauce-tunnel": "~1.1.0",
"ts-compiler": "^2.0.0",
"tmp": "~0.0.18",
"uglify-js": "~2.4.0",
"uglifyify": "^2.4.0",
Expand All @@ -82,7 +83,8 @@
"setupEnvScriptFile": "jest/environment.js",
"testFileExtensions": [
"js",
"coffee"
"coffee",
"ts"
],
"modulePathIgnorePatterns": [
"/build/",
Expand Down
7 changes: 6 additions & 1 deletion src/modern/class/__tests__/ReactClassEquivalence-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,14 @@ describe('ReactClassEquivalence', function() {

var es6 = () => require('./ReactES6Class-test.js');
var coffee = () => require('./ReactCoffeeScriptClass-test.coffee');
var ts = () => require('./ReactTypeScriptClass-test.ts');

it('tests the same thing for es6 classes and coffee script', function() {
it('tests the same thing for es6 classes and CoffeeScript', function() {
expect(coffee).toEqualSpecsIn(es6);
});

it('tests the same thing for es6 classes and TypeScript', function() {
expect(ts).toEqualSpecsIn(es6);
});

});
Loading

0 comments on commit 6c145c3

Please sign in to comment.