Skip to content

Commit

Permalink
Redux Injector (Enables bindings between redux and dojo 2 widget-core) (
Browse files Browse the repository at this point in the history
#2)

* redux injector

* add dojo/interfaces dependency

* add tests for redux injector

* Add redux to intern config

* add other config for intern

* move redux to optional dependency
  • Loading branch information
agubler committed Sep 13, 2017
1 parent 24faa62 commit bce86c5
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 3 deletions.
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,27 @@
"prepublish": "grunt peerDepInstall",
"test": "grunt test"
},
"peerDependencies": {
"@dojo/widget-core": "next",
"@dojo/core": "next",
"@dojo/has": "next",
"@dojo/i18n": "next",
"@dojo/shim": "next"
},
"devDependencies": {
"@dojo/interfaces": "next",
"@dojo/loader": "next",
"@types/chai": "4.0.*",
"@types/glob": "5.0.*",
"@types/grunt": "0.4.*",
"@types/sinon": "^1.16.31",
"codecov.io": "0.1.6",
"@dojo/loader": "next",
"grunt": "~1.0.1",
"grunt-dojo2": "latest",
"intern": "^3.4.6",
"typescript": "~2.4.2"
},
"optionalDependencies": {
"redux": "^3.7.2"
}
}
37 changes: 37 additions & 0 deletions src/redux/ReduxInjector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Store } from 'redux';
import { Base } from '@dojo/widget-core/Injector';

/**
* Injector for redux store
*/
export class ReduxInjector<S = any> extends Base<Store<S>> {

/**
* Injected redux store
*/
protected store: Store<S>;

/**
* Sets the store and attaches the injectors invalidate to the redux
* stores bind.
*
* @param store The store for the injector
*/
constructor(store: Store<S>) {
super();
this.store = store;
this.store.subscribe(this.invalidate.bind(this));
}

/**
* Returns the value to be injected, in this case the Store
* that was passed when creating the Injector.
*
* @returns Returns the store
*/
public toInject(): Store<S> {
return this.store;
}
}

export default ReduxInjector;
20 changes: 18 additions & 2 deletions tests/intern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,25 @@ export const loaderOptions = {
{ name: '@dojo', location: 'node_modules/@dojo' },
{ name: 'dojo', location: 'node_modules/intern/node_modules/dojo' },
{ name: 'grunt-dojo2', location: 'node_modules/grunt-dojo2' },
{ name: 'redux', location: 'node_modules/redux/dist', main: 'redux' },
{ name: 'src', location: '_build/src' },
{ name: 'tests', location: '_build/tests' }
]
{ name: 'tests', location: '_build/tests' },
{ name: 'cldr-data', location: 'node_modules/cldr-data' },
{ name: 'cldrjs', location: 'node_modules/cldrjs' },
{ name: 'globalize', location: 'node_modules/globalize', main: 'dist/globalize' },
{ name: 'maquette', location: 'node_modules/maquette/dist', main: 'maquette' },
{ name: 'pepjs', location: 'node_modules/pepjs/dist', main: 'pep' },
{ name: 'grunt-dojo2', location: 'node_modules/grunt-dojo2'},
{ name: 'sinon', location: 'node_modules/sinon/pkg', main: 'sinon' }
],
map: {
globalize: {
'cldr': 'cldrjs/dist/cldr',
'cldr/event': 'cldrjs/dist/cldr/event',
'cldr/supplemental': 'cldrjs/dist/cldr/supplemental',
'cldr/unresolved': 'cldrjs/dist/cldr/unresolved'
}
}
};

// Non-functional test suite(s) to run in each browser
Expand Down
1 change: 1 addition & 0 deletions tests/unit/all.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
import './main';
import './redux/ReduxInjector';
23 changes: 23 additions & 0 deletions tests/unit/redux/ReduxInjector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as registerSuite from 'intern!object';
import * as assert from 'intern/chai!assert';
import { ReduxInjector } from './../../../src/redux/ReduxInjector';
import { createStore } from 'redux';

registerSuite({
name: 'ReduxInjector',
reduxInjector() {
const store = createStore((state) => state);
const injector = new ReduxInjector(store);
let injectorInvalidated = false;
injector.on('invalidated', () => {
injectorInvalidated = true;
});
store.dispatch({ type: 'TEST' });
assert.isTrue(injectorInvalidated);
},
toInject() {
const store = createStore((state) => state);
const injector = new ReduxInjector(store);
assert.strictEqual(injector.toInject(), store);
}
});

0 comments on commit bce86c5

Please sign in to comment.