Skip to content
This repository has been archived by the owner on Dec 30, 2022. It is now read-only.

Commit

Permalink
fix(GoogleMapsLoader): inline the import to scriptjs (#1427)
Browse files Browse the repository at this point in the history
* fix(GoogleMapsLoader): inline require

* refactor(GoogleMapsLoader): add .jsdom to the test file

* chore(babel): add dynamic-import-node

* fix(GoogleMapsLoader): use import rather than require

* chore(maps): avoid to transpile dyanmic import with Rollup

* style(GoogleMapsLoader): use destructuring for dynamic import
  • Loading branch information
samouss committed Jul 11, 2018
1 parent 1b416ca commit 8019416
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 37 deletions.
31 changes: 28 additions & 3 deletions packages/react-instantsearch-dom-maps/.babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"stage-2"
],
"plugins": [
"lodash"
"lodash",
"dynamic-import-node"
]
},
"test": {
Expand All @@ -37,7 +38,8 @@
"stage-2"
],
"plugins": [
"lodash"
"lodash",
"dynamic-import-node"
]
},
"production": {
Expand All @@ -57,10 +59,33 @@
"stage-2"
],
"plugins": [
"lodash"
"lodash",
"dynamic-import-node"
]
},
"es": {
"presets": [
[
"env",
{
"targets": {
"browsers": [
"last 2 versions",
"ie >= 9"
]
},
"modules": false
}
],
"react",
"stage-2"
],
"plugins": [
"lodash",
"dynamic-import-node"
]
},
"rollup": {
"presets": [
[
"env",
Expand Down
3 changes: 2 additions & 1 deletion packages/react-instantsearch-dom-maps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"build": "yarn build:cjs && yarn build:es && yarn build:umd",
"build:cjs": "babel src --out-dir dist/cjs --ignore __tests__,__mocks__ --quiet",
"build:es": "BABEL_ENV=es babel src --out-dir dist/es --ignore __tests__,__mocks__ --quiet",
"build:umd": "BABEL_ENV=es rollup -c rollup.config.js",
"build:umd": "BABEL_ENV=rollup rollup -c rollup.config.js",
"test": "jest",
"release": "yarn clean && yarn build && yarn publish --new-version $VERSION",
"release:beta": "yarn clean && yarn build && yarn publish --tag beta --new-version $VERSION"
Expand All @@ -50,6 +50,7 @@
"devDependencies": {
"babel-cli": "6.26.0",
"babel-core": "6.26.0",
"babel-plugin-dynamic-import-node": "2.0.0",
"babel-plugin-external-helpers": "6.22.0",
"babel-plugin-lodash": "3.3.4",
"babel-preset-env": "1.7.0",
Expand Down
26 changes: 15 additions & 11 deletions packages/react-instantsearch-dom-maps/src/GoogleMapsLoader.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Component } from 'react';
import PropTypes from 'prop-types';
import injectScript from 'scriptjs';

class GoogleMapsLoader extends Component {
static propTypes = {
Expand All @@ -20,16 +19,21 @@ class GoogleMapsLoader extends Component {
isUnmounting = false;

componentDidMount() {
const { apiKey, endpoint } = this.props;
const operator = endpoint.indexOf('?') !== -1 ? '&' : '?';
const endpointWithCredentials = `${endpoint}${operator}key=${apiKey}`;

injectScript(endpointWithCredentials, () => {
if (!this.isUnmounting) {
this.setState(() => ({
google: window.google,
}));
}
// Inline the import to avoid to run the module on the server (rely on `document`)
// Under the hood we use `dynamic-import-node` to transpile the `import` to `require`
// see: https://github.com/algolia/react-instantsearch/issues/1425
return import('scriptjs').then(({ default: injectScript }) => {
const { apiKey, endpoint } = this.props;
const operator = endpoint.indexOf('?') !== -1 ? '&' : '?';
const endpointWithCredentials = `${endpoint}${operator}key=${apiKey}`;

injectScript(endpointWithCredentials, () => {
if (!this.isUnmounting) {
this.setState(() => ({
google: window.google,
}));
}
});
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ describe('GoogleMapsLoader', () => {
apiKey: 'API_KEY',
};

const flushPendingMicroTasks = () =>
new Promise(resolve => setImmediate(resolve));

it('expect to call Google Maps API', () => {
const children = jest.fn(x => x);

Expand All @@ -22,10 +25,12 @@ describe('GoogleMapsLoader', () => {

shallow(<GoogleMapsLoader {...props}>{children}</GoogleMapsLoader>);

expect(injectScript).toHaveBeenLastCalledWith(
'https://maps.googleapis.com/maps/api/js?v=3.31&key=API_KEY',
expect.any(Function)
);
return flushPendingMicroTasks().then(() => {
expect(injectScript).toHaveBeenLastCalledWith(
'https://maps.googleapis.com/maps/api/js?v=3.31&key=API_KEY',
expect.any(Function)
);
});
});

it('expect to call Google Maps API with a custom API Key', () => {
Expand All @@ -38,10 +43,12 @@ describe('GoogleMapsLoader', () => {

shallow(<GoogleMapsLoader {...props}>{children}</GoogleMapsLoader>);

expect(injectScript).toHaveBeenLastCalledWith(
'https://maps.googleapis.com/maps/api/js?v=3.31&key=CUSTOM_API_KEY',
expect.any(Function)
);
return flushPendingMicroTasks().then(() => {
expect(injectScript).toHaveBeenLastCalledWith(
'https://maps.googleapis.com/maps/api/js?v=3.31&key=CUSTOM_API_KEY',
expect.any(Function)
);
});
});

it('expect to call Google Maps API with a custom endpoint', () => {
Expand All @@ -55,10 +62,12 @@ describe('GoogleMapsLoader', () => {

shallow(<GoogleMapsLoader {...props}>{children}</GoogleMapsLoader>);

expect(injectScript).toHaveBeenLastCalledWith(
'https://maps.googleapis.com/maps/api/js?v=3.32&places,geometry&key=API_KEY',
expect.any(Function)
);
return flushPendingMicroTasks().then(() => {
expect(injectScript).toHaveBeenLastCalledWith(
'https://maps.googleapis.com/maps/api/js?v=3.32&places,geometry&key=API_KEY',
expect.any(Function)
);
});
});

it("expect to render nothing when it's loading", () => {
Expand Down Expand Up @@ -87,7 +96,7 @@ describe('GoogleMapsLoader', () => {
...defaultProps,
};

injectScript.mockImplementationOnce((endpoint, callback) => {
injectScript.mockImplementationOnce((_, callback) => {
global.google = google;
callback();
});
Expand All @@ -96,11 +105,13 @@ describe('GoogleMapsLoader', () => {
<GoogleMapsLoader {...props}>{children}</GoogleMapsLoader>
);

expect(wrapper.type).not.toBe(null);
expect(children).toHaveBeenCalledTimes(1);
expect(children).toHaveBeenCalledWith(google);
return flushPendingMicroTasks().then(() => {
expect(wrapper.type).not.toBe(null);
expect(children).toHaveBeenCalledTimes(1);
expect(children).toHaveBeenCalledWith(google);

delete global.google;
delete global.google;
});
});

it('expect to not call setState when we unmount before loading is complete', () => {
Expand All @@ -119,13 +130,15 @@ describe('GoogleMapsLoader', () => {
<GoogleMapsLoader {...props}>{children}</GoogleMapsLoader>
);

expect(wrapper.type).not.toBe(null);
expect(children).not.toHaveBeenCalled();
return flushPendingMicroTasks().then(() => {
expect(wrapper.type).not.toBe(null);
expect(children).not.toHaveBeenCalled();

wrapper.unmount();
wrapper.unmount();

triggerLoadingComplete();
triggerLoadingComplete();

expect(children).not.toHaveBeenCalled();
expect(children).not.toHaveBeenCalled();
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* @jest-environment node
*/

describe('GoogleMapsLoader', () => {
it('expect to require the file in a Node environment', () => {
expect(() => require('../GoogleMapsLoader')).not.toThrowError();
});
});
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1827,6 +1827,13 @@ babel-plugin-dynamic-import-node@1.1.0:
babel-template "^6.26.0"
babel-types "^6.26.0"

babel-plugin-dynamic-import-node@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/babel-plugin-dynamic-import-node/-/babel-plugin-dynamic-import-node-2.0.0.tgz#d6fc3f6c5e3bdc34e49c15faca7ce069755c0a57"
dependencies:
babel-plugin-syntax-dynamic-import "^6.18.0"
object.assign "^4.1.0"

babel-plugin-external-helpers@6.22.0, babel-plugin-external-helpers@^6.22.0:
version "6.22.0"
resolved "https://registry.yarnpkg.com/babel-plugin-external-helpers/-/babel-plugin-external-helpers-6.22.0.tgz#2285f48b02bd5dede85175caf8c62e86adccefa1"
Expand Down

0 comments on commit 8019416

Please sign in to comment.