@samitouri / QOS-React-2 / commits / 3dc1074908

Test that React loads properly in a web worker

This should catch top-level uses of `window` and `document`, while lint rules catch `global`.

Ben Alpert committed Sep 25, 2013 at 14:56 UTC 3dc10749080a460e48bee46d769763ec7191ac76
6 files changed +114 -37
Gruntfile.js
+1 -1
@@ -79,7 +79,7 @@ module.exports = function(grunt) {
79 'populist:test'
80 ]);
81
82 - grunt.registerTask('test', ['build:test', 'phantom:run']);
82 + grunt.registerTask('test', ['build:test', 'build:basic', 'phantom:run']);
83 grunt.registerTask('npm:test', ['build', 'npm:pack']);
84
85 // Optimized build task that does all of our builds. The subtasks will be run
src/environment/__tests__/ReactWebWorker-test.js new
+50
@@ -0,0 +1,50 @@
1 +/**
2 + * Copyright 2013 Facebook, Inc.
3 + *
4 + * Licensed under the Apache License, Version 2.0 (the "License");
5 + * you may not use this file except in compliance with the License.
6 + * You may obtain a copy of the License at
7 + *
8 + * http://www.apache.org/licenses/LICENSE-2.0
9 + *
10 + * Unless required by applicable law or agreed to in writing, software
11 + * distributed under the License is distributed on an "AS IS" BASIS,
12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 + * See the License for the specific language governing permissions and
14 + * limitations under the License.
15 + *
16 + * @jsx React.DOM
17 + * @emails react-core
18 + */
19 +
20 +/*jslint evil: true */
21 +
22 +"use strict";
23 +
24 +describe('ReactWebWorker', function() {
25 + it('can run React in a web worker', function() {
26 + var done = false;
27 + var error;
28 +
29 + var worker = new Worker('/worker.js');
30 + worker.addEventListener('message', function(e) {
31 + var data = JSON.parse(e.data);
32 + if (data.type == 'error') {
33 + error = data.message + "\n" + data.stack;
34 + } else {
35 + expect(data.type).toBe('done');
36 + done = true;
37 + }
38 + });
39 +
40 + waitsFor(function() {
41 + return done;
42 + });
43 + runs(function() {
44 + if (error) {
45 + console.log(error);
46 + throw new Error(error);
47 + }
48 + });
49 + });
50 +});
src/test/all.js
+1 -32
@@ -2,38 +2,7 @@
2 // modules in src/test and to specify an ordering on those modules, since
3 // some still have implicit dependencies on others.
4
5 -var Ap = Array.prototype;
6 -var slice = Ap.slice;
7 -var Fp = Function.prototype;
8 -
9 -if (!Fp.bind) {
10 - // PhantomJS doesn't support Function.prototype.bind natively, so
11 - // polyfill it whenever this module is required.
12 - Fp.bind = function(context) {
13 - var func = this;
14 - var args = slice.call(arguments, 1);
15 -
16 - function bound() {
17 - var invokedAsConstructor = func.prototype && (this instanceof func);
18 - return func.apply(
19 - // Ignore the context parameter when invoking the bound function
20 - // as a constructor. Note that this includes not only constructor
21 - // invocations using the new keyword but also calls to base class
22 - // constructors such as BaseClass.call(this, ...) or super(...).
23 - !invokedAsConstructor && context || this,
24 - args.concat(slice.call(arguments))
25 - );
26 - }
27 -
28 - // The bound function must share the .prototype of the unbound
29 - // function so that any object created by one constructor will count
30 - // as an instance of both constructors.
31 - bound.prototype = func.prototype;
32 -
33 - return bound;
34 - };
35 -}
36 -
5 +require("./phantomjs-shims");
6 require("ReactTestUtils");
7 require("reactComponentExpect");
8 require("mocks");
src/test/phantomjs-shims.js new
+35
@@ -0,0 +1,35 @@
1 +(function() {
2 +
3 +var Ap = Array.prototype;
4 +var slice = Ap.slice;
5 +var Fp = Function.prototype;
6 +
7 +if (!Fp.bind) {
8 + // PhantomJS doesn't support Function.prototype.bind natively, so
9 + // polyfill it whenever this module is required.
10 + Fp.bind = function(context) {
11 + var func = this;
12 + var args = slice.call(arguments, 1);
13 +
14 + function bound() {
15 + var invokedAsConstructor = func.prototype && (this instanceof func);
16 + return func.apply(
17 + // Ignore the context parameter when invoking the bound function
18 + // as a constructor. Note that this includes not only constructor
19 + // invocations using the new keyword but also calls to base class
20 + // constructors such as BaseClass.call(this, ...) or super(...).
21 + !invokedAsConstructor && context || this,
22 + args.concat(slice.call(arguments))
23 + );
24 + }
25 +
26 + // The bound function must share the .prototype of the unbound
27 + // function so that any object created by one constructor will count
28 + // as an instance of both constructors.
29 + bound.prototype = func.prototype;
30 +
31 + return bound;
32 + };
33 +}
34 +
35 +})();
src/test/worker.js new
+20
@@ -0,0 +1,20 @@
1 +/* jshint worker: true */
2 +"use strict";
3 +
4 +// The UMD wrapper tries to store on `global` if `window` isn't available
5 +var global = {};
6 +importScripts("phantomjs-shims.js");
7 +
8 +try {
9 + importScripts("react.js");
10 +} catch (e) {
11 + postMessage(JSON.stringify({
12 + type: 'error',
13 + message: e.message,
14 + stack: e.stack
15 + }));
16 +}
17 +
18 +postMessage(JSON.stringify({
19 + type: 'done'
20 +}));
test/phantom-harness.js
+7 -4
@@ -50,16 +50,19 @@ server.listen(port, function(req, res) {
50 var content;
51
52 switch (file) {
53 + case "jasmine.js":
54 + case "react.js":
55 case "react-test.js":
56 file = "../build/" + file;
57 break;
58
57 - case "jasmine.css":
58 - file = "../vendor/jasmine/" + file;
59 + case "phantomjs-shims.js":
60 + case "worker.js":
61 + file = "../src/test/" + file;
62 break;
63
61 - case "jasmine.js":
62 - file = "../build/" + file;
64 + case "jasmine.css":
65 + file = "../vendor/jasmine/" + file;
66 break;
67
68 case "":