Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensured that store from props is initialized with initialize from props. #136

Merged
merged 2 commits into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/LocalizeProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
getTranslationsForActiveLanguage,
type LocalizeState,
type Action,
initialize as initializeAC,
INITIALIZE,
InitializePayload
} from './localize';
Expand Down Expand Up @@ -58,23 +59,32 @@ export class LocalizeProvider extends Component<
};
}

static getDerivedStateFromProps(nextProps: any, nextState: any) {
return null;
componentDidMount() {
this.initExternalStore();
this.subscribeToExternalStore();
}

componentDidMount() {
if (this.props.store) {
componentWillUnmount() {
this.unsubscribeFromStore && this.unsubscribeFromStore();
}

initExternalStore() {
const { store, initialize } = this.props;
if (store && initialize) {
store.dispatch(initializeAC(initialize));
}
}

subscribeToExternalStore() {
const { store } = this.props;
if (store) {
this.unsubscribeFromStore = storeDidChange(
this.props.store,
store,
this.onStateDidChange.bind(this)
);
}
}

componentWillUnmount() {
this.unsubscribeFromStore && this.unsubscribeFromStore();
}

onStateDidChange(prevState: LocalizeProviderState) {
if (!this.props.store) {
return;
Expand Down
103 changes: 64 additions & 39 deletions tests/LocalizeProvider.test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
import React from 'react';
import ReactDOMServer from "react-dom/server";
import ReactDOMServer from 'react-dom/server';
import Enzyme, { shallow } from 'enzyme';
import { createStore, combineReducers } from 'redux';
import Adapter from 'enzyme-adapter-react-16';
import { Map } from 'immutable'
import { Map } from 'immutable';
import { LocalizeProvider } from '../src/LocalizeProvider';
import { localizeReducer } from '../src/localize';
import { getTranslate, getLanguages, initialize, withLocalize, Translate } from '../src';
import { getTranslate, getLanguages, withLocalize, Translate } from '../src';
import { defaultTranslateOptions } from '../src/localize';

Enzyme.configure({ adapter: new Adapter() });


describe('<LocalizeProvider />', () => {
const initialState = {
languages: [
{ code: 'en', active: true },
{ code: 'fr', active: false }
],
languages: [{ code: 'en', active: true }, { code: 'fr', active: false }],
translations: {
hello: ['Hello', 'Hello FR'],
bye: ['Goodbye', 'Goodbye FR'],
Expand All @@ -28,14 +24,16 @@ describe('<LocalizeProvider />', () => {
};

const getMockStore = () => {
return createStore(combineReducers({
localize: localizeReducer
}));
return createStore(
combineReducers({
localize: localizeReducer
})
);
};
const getImmutableStore = () => {
const reducer = (s, a) => Map({localize: localizeReducer(s, a)});
return createStore(reducer, Map({localize: initialState}));
}
const reducer = (s, a) => Map({ localize: localizeReducer(s, a) });
return createStore(reducer, Map({ localize: initialState }));
};

it('should set default values for localize state', () => {
const wrapper = shallow(
Expand All @@ -61,30 +59,31 @@ describe('<LocalizeProvider />', () => {
languages: getLanguages(initialState),
defaultLanguage: 'en',
activeLanguage: initialState.languages[0]
})
});
});

it('should not throw error when store prop when passed', () => {
const store = getMockStore();
const wrapper =

expect(() => {
const wrapper = expect(() => {
shallow(
<LocalizeProvider store={store}>
<div>Hello</div>
</LocalizeProvider>
)
);
}).not.toThrow();
});

it('should allow passing a custom function to access state', () => {
const store = getImmutableStore();
expect(() => {
shallow(
<LocalizeProvider store={store} getState={state => state.get('localize')}>
<div>Hello</div>
<LocalizeProvider
store={store}
getState={state => state.get('localize')}
>
<div>Hello</div>
</LocalizeProvider>
)
);
}).not.toThrow();
});

Expand All @@ -95,14 +94,14 @@ describe('<LocalizeProvider />', () => {

this.props.initialize({
languages: [
{ name: "English", code: "en" },
{ name: "French", code: "fr" }
{ name: 'English', code: 'en' },
{ name: 'French', code: 'fr' }
],
translation: {
hello: ['hello', 'alo']
},
options: {
defaultLanguage: "en",
defaultLanguage: 'en',
renderToStaticMarkup: ReactDOMServer.renderToStaticMarkup
}
});
Expand All @@ -120,23 +119,49 @@ describe('<LocalizeProvider />', () => {
const LocalizedApp = withLocalize(App);

const result = ReactDOMServer.renderToString(
<LocalizeProvider initialize={{
languages: [
{ name: "English", code: "en" },
{ name: "French", code: "fr" }
],
translation: {
hello: ['hello', 'alo']
},
options: {
defaultLanguage: "en",
renderToStaticMarkup: ReactDOMServer.renderToStaticMarkup
}
}}>
<LocalizeProvider
initialize={{
languages: [
{ name: 'English', code: 'en' },
{ name: 'French', code: 'fr' }
],
translation: {
hello: ['hello', 'alo']
},
options: {
defaultLanguage: 'en',
renderToStaticMarkup: ReactDOMServer.renderToStaticMarkup
}
}}
>
<LocalizedApp />
</LocalizeProvider>
);

expect(result).toEqual('<div>hello</div>');
});
});

it('should work when store and initialize are passed to Provider', () => {
const store = getMockStore();
const initializePayload = {
languages: [
{ name: 'English', code: 'en' },
{ name: 'French', code: 'fr' }
],
translation: {
hello: ['hello', 'alo']
},
options: {
defaultLanguage: 'en',
renderToStaticMarkup: ReactDOMServer.renderToStaticMarkup
}
};
shallow(
<LocalizeProvider store={store} initialize={initializePayload}>
<div>Hello</div>
</LocalizeProvider>
);
const translation = store.getState().localize.translations;
expect(translation).toEqual(initializePayload.translation);
});
});