Skip to content

Commit

Permalink
fix naive global ReactOnRails references (#569)
Browse files Browse the repository at this point in the history
This fixes some test running issues. This should not change the current production code.
  • Loading branch information
Judahmeek authored and justin808 committed Oct 22, 2016
1 parent 106b2ed commit e53c0a0
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 14 deletions.
2 changes: 0 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
---
parser: babel-eslint

extends: eslint-config-shakacode

plugins:
Expand Down
1 change: 1 addition & 0 deletions .jscsrc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
".jsx"
],
"excludeFiles": [
"**/.c9/**",
"**/build/**",
"**/node_modules/**",
"**/assets/webpack/**",
Expand Down
20 changes: 17 additions & 3 deletions node_package/src/clientStartup.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,25 @@ import isRouterResult from './isRouterResult';
const REACT_ON_RAILS_COMPONENT_CLASS_NAME = 'js-react-on-rails-component';
const REACT_ON_RAILS_STORE_CLASS_NAME = 'js-react-on-rails-store';

function findContext() {
if (typeof window.ReactOnRails !== 'undefined') {
return window;
} else if (typeof ReactOnRails !== 'undefined') {
return global;
}

throw new Error(`\
ReactOnRails is undefined in both global and window namespaces.
`);
}

function debugTurbolinks(...msg) {
if (!window) {
return;
}

if (ReactOnRails.option('traceTurbolinks')) {
const context = findContext();
if (context.ReactOnRails.option('traceTurbolinks')) {
console.log('TURBO:', ...msg);
}
}
Expand All @@ -34,11 +47,12 @@ function forEachComponent(fn, railsContext) {
}

function initializeStore(el, railsContext) {
const context = findContext();
const name = el.getAttribute('data-store-name');
const props = JSON.parse(el.getAttribute('data-props'));
const storeGenerator = ReactOnRails.getStoreGenerator(name);
const storeGenerator = context.ReactOnRails.getStoreGenerator(name);
const store = storeGenerator(props, railsContext);
ReactOnRails.setStore(name, store);
context.ReactOnRails.setStore(name, store);
}

function forEachStore(railsContext) {
Expand Down
1 change: 0 additions & 1 deletion node_package/tests/Authenticity.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import test from 'tape';

import ReactOnRails from '../src/ReactOnRails.js';

test('authenticityToken and authenticityHeaders', (assert) => {
Expand Down
7 changes: 0 additions & 7 deletions node_package/tests/ReactOnRails.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,9 @@
import test from 'tape';
import { createStore } from 'redux';
import React from 'react';
import { canUseDOM } from 'fbjs/lib/ExecutionEnvironment';
import JsDom from 'jsdom';

import ReactOnRails from '../src/ReactOnRails';

if (!canUseDOM) {
global.document = JsDom.jsdom('<div id="root"></div>');
global.window = document.defaultView;
}

test('ReactOnRails render returns a virtual DOM element for component', (assert) => {
assert.plan(1);
const R1 = React.createClass({
Expand Down
10 changes: 10 additions & 0 deletions node_package/tests/helpers/test_helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import JsDom from 'jsdom';

global.document = JsDom.jsdom('<div id="root"></div>');
global.window = document.defaultView;

Object.keys(window).forEach((key) => {
if (!(key in global)) {
global[key] = window[key];
}
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"node_package/lib"
],
"scripts": {
"test": "babel-tape-runner node_package/tests/*.js | tap-spec",
"test": "babel-tape-runner -r node_package/tests/helpers/test_helper.js node_package/tests/*.js | tap-spec",
"clean": "rm -rf node_package/lib",
"prepublish": "npm run build",
"babel": "babel --out-dir node_package/lib node_package/src",
Expand Down

0 comments on commit e53c0a0

Please sign in to comment.