From fdfd9d64f218caa4411d81022bbdb1aafeb693f2 Mon Sep 17 00:00:00 2001 From: chrisgarrity <chrisg@media.mit.edu> Date: Mon, 19 Nov 2018 09:18:38 -0500 Subject: [PATCH] re-refactor after fixing merge conflicts --- src/lib/app-state-hoc.jsx | 7 +---- src/lib/query-parser-hoc.jsx | 23 ++++++++-------- src/lib/tutorial-from-url.js | 4 +-- test/unit/util/tutorial-from-url.test.js | 35 ++++++++++-------------- 4 files changed, 28 insertions(+), 41 deletions(-) diff --git a/src/lib/app-state-hoc.jsx b/src/lib/app-state-hoc.jsx index 4387e0653..495a667d2 100644 --- a/src/lib/app-state-hoc.jsx +++ b/src/lib/app-state-hoc.jsx @@ -10,7 +10,6 @@ import {setPlayer, setFullScreen} from '../reducers/mode.js'; import locales from 'scratch-l10n'; import {detectLocale} from './detect-locale'; -import {detectTutorialId} from './tutorial-from-url'; const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose; @@ -65,11 +64,7 @@ const AppStateHOC = function (WrappedComponent, localesOnly) { initializedGui = initPlayer(initializedGui); } } else { - const tutorialId = detectTutorialId(); - if (tutorialId === null && props.showPreviewInfo) { - // Show preview info if requested and no tutorial ID found - initializedGui = initPreviewInfo(initializedGui); - } + initializedGui = initPreviewInfo(initializedGui); } reducers = { locales: localesReducer, diff --git a/src/lib/query-parser-hoc.jsx b/src/lib/query-parser-hoc.jsx index e23d4caa1..161b1a34a 100644 --- a/src/lib/query-parser-hoc.jsx +++ b/src/lib/query-parser-hoc.jsx @@ -6,7 +6,7 @@ import {connect} from 'react-redux'; import {detectTutorialId} from './tutorial-from-url'; import {activateDeck} from '../reducers/cards'; -import {openTipsLibrary} from '../reducers/modals'; +import {openTipsLibrary, closePreviewInfo} from '../reducers/modals'; /* Higher Order Component to get parameters from the URL query string and initialize redux state * @param {React.Component} WrappedComponent: component to render @@ -17,13 +17,8 @@ const QueryParserHOC = function (WrappedComponent) { constructor (props) { super(props); const queryParams = queryString.parse(location.search); - this.state = { - tutorial: false - }; - const tutorialId = detectTutorialId(queryParams); if (tutorialId) { - this.state.tutorial = true; if (tutorialId === 'all') { this.openTutorials(); } else { @@ -39,11 +34,10 @@ const QueryParserHOC = function (WrappedComponent) { } render () { const { - hideIntro: hideIntroProp, + onOpenTipsLibrary, // eslint-disable-line no-unused-vars + onUpdateReduxDeck, // eslint-disable-line no-unused-vars ...componentProps } = this.props; - // override hideIntro if there is a tutorial - componentProps.hideIntro = this.state.tutorial || hideIntroProp; return ( <WrappedComponent {...componentProps} @@ -52,13 +46,18 @@ const QueryParserHOC = function (WrappedComponent) { } } QueryParserComponent.propTypes = { - hideIntro: PropTypes.bool, onOpenTipsLibrary: PropTypes.func, onUpdateReduxDeck: PropTypes.func }; const mapDispatchToProps = dispatch => ({ - onOpenTipsLibrary: () => dispatch(openTipsLibrary()), - onUpdateReduxDeck: tutorialId => dispatch(activateDeck(tutorialId)) + onOpenTipsLibrary: () => { + dispatch(openTipsLibrary()); + dispatch(closePreviewInfo()); + }, + onUpdateReduxDeck: tutorialId => { + dispatch(activateDeck(tutorialId)); + dispatch(closePreviewInfo()); + } }); return connect( null, diff --git a/src/lib/tutorial-from-url.js b/src/lib/tutorial-from-url.js index fb2df2642..5e601fe33 100644 --- a/src/lib/tutorial-from-url.js +++ b/src/lib/tutorial-from-url.js @@ -5,7 +5,6 @@ import tutorials from './libraries/decks/index.jsx'; import analytics from './analytics'; -import queryString from 'query-string'; /** * Get the tutorial id from the given numerical id (representing the @@ -35,8 +34,7 @@ const getDeckIdFromUrlId = urlId => { * @return {string} The ID of the requested tutorial or null if no tutorial was * requested or found. */ -const detectTutorialId = () => { - const queryParams = queryString.parse(location.search); +const detectTutorialId = queryParams => { const tutorialID = Array.isArray(queryParams.tutorial) ? queryParams.tutorial[0] : queryParams.tutorial; diff --git a/test/unit/util/tutorial-from-url.test.js b/test/unit/util/tutorial-from-url.test.js index cfe2c14fe..454bc8725 100644 --- a/test/unit/util/tutorial-from-url.test.js +++ b/test/unit/util/tutorial-from-url.test.js @@ -8,45 +8,40 @@ jest.mock('../../../src/lib/libraries/decks/index.jsx', () => ({ noUrlIdSandwich: {} })); +import queryString from 'query-string'; import {detectTutorialId} from '../../../src/lib/tutorial-from-url.js'; -Object.defineProperty( - window.location, - 'search', - {value: '', writable: true} -); - test('returns the tutorial ID if the urlId matches', () => { - window.location.search = '?tutorial=one'; - expect(detectTutorialId()).toBe('foo'); + const queryParams = queryString.parse('?tutorial=one'); + expect(detectTutorialId(queryParams)).toBe('foo'); }); test('returns null if no matching urlId', () => { - window.location.search = '?tutorial=10'; - expect(detectTutorialId()).toBe(null); + const queryParams = queryString.parse('?tutorial=10'); + expect(detectTutorialId(queryParams)).toBe(null); }); test('returns null if empty template', () => { - window.location.search = '?tutorial='; - expect(detectTutorialId()).toBe(null); + const queryParams = queryString.parse('?tutorial='); + expect(detectTutorialId(queryParams)).toBe(null); }); test('returns null if no query param', () => { - window.location.search = ''; - expect(detectTutorialId()).toBe(null); + const queryParams = queryString.parse(''); + expect(detectTutorialId(queryParams)).toBe(null); }); test('returns null if unrecognized template', () => { - window.location.search = '?tutorial=asdf'; - expect(detectTutorialId()).toBe(null); + const queryParams = queryString.parse('?tutorial=asdf'); + expect(detectTutorialId(queryParams)).toBe(null); }); test('takes the first of multiple', () => { - window.location.search = '?tutorial=one&tutorial=two'; - expect(detectTutorialId()).toBe('foo'); + const queryParams = queryString.parse('?tutorial=one&tutorial=two'); + expect(detectTutorialId(queryParams)).toBe('foo'); }); test('returns all for the tutorial library shortcut', () => { - window.location.search = '?tutorial=all'; - expect(detectTutorialId()).toBe('all'); + const queryParams = queryString.parse('?tutorial=all'); + expect(detectTutorialId(queryParams)).toBe('all'); }); -- GitLab