├── .github └── ISSUE_TEMPLATE │ ├── bug_report.md │ └── component.md ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── RELEASE.md ├── demo ├── .env ├── .gitignore ├── README.md ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ ├── index.html │ └── manifest.json └── src │ ├── BermudaTriangle.js │ ├── MyFeatureLayer.js │ └── index.js ├── package-lock.json ├── package.json ├── src └── ts │ ├── components │ ├── ArcBase.tsx │ ├── ArcComposites.tsx │ ├── ArcContainer.tsx │ ├── MapComposites.tsx │ └── WebComposites.tsx │ └── index.ts ├── test ├── doubles │ ├── ChildComponent.tsx │ ├── doubleTrouble.ts │ └── esriLoader.ts ├── harness.ts ├── mocha.opts ├── testSettings.js ├── ts │ ├── components │ │ ├── ArcBase.spec.tsx │ │ ├── ArcComposites.spec.tsx │ │ ├── MapComposites.spec.tsx │ │ └── WebComposites.spec.tsx │ └── test.tsx └── tsconfig.json ├── tsconfig.json ├── tslint.json └── webpack.config.js /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Found a bug specifically with react-arcgis 4 | title: '' 5 | labels: bug 6 | assignees: '' 7 | 8 | --- 9 | 10 | 22 | 23 | **Expected behavior** 24 | A clear and concise description of what you expected to happen. 25 | 26 | **Actual behavior** 27 | A clear and concise description of what the bug is. 28 | 29 | **To Reproduce** 30 | 31 | 34 | 35 | 1. Steps to reproduce the behavior 36 | 2. A link to an app where we can carry out those steps (either our example pages, your publicly facing app, or a Codesandbox, JSFiddle, JSBin, etc). Here is a Codesandbox template you can start with if the bug does not show in our example pages, or your app is not publicly accessible: https://codesandbox.io/s/react-arcgis-template-ic8kf 37 | 3. Relevant code snippet(s) (only if not easily obtained from the above link) 38 | 39 | 40 | **Error messages** 41 | Any error messages you see in the console. 42 | 43 | 44 | **Screenshots** 45 | If applicable, add screenshots to help explain your problem. 46 | 47 | **Versions (please complete the following information):** 48 | - react-arcgis version [e.g. 5.0.0] 49 | - esri-loader version [e.g. 2.11.0] 50 | - ArcGIS API for JavaScript version 51 | - Browser [e.g. chrome v77.0.3865.90] 52 | 53 | **Additional context** 54 | Add any other context about the problem here. 55 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/component.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Component 3 | about: I've created a react component and would like to share 4 | title: 'Sharing: COMPONENT NAME HERE' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | Anyone is welcome to contribute to this package. However, we do not plan to add any more components to this library. If you have created a component that you'd like to share, we encourage you to share it via [CodeSandbox](https://codesandbox.io/) or a [gist](https://gist.github.com/). Once you've done that, post the link here and we'll help spread the word. 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist 3 | examples 4 | src/**/*.js 5 | src/**/*.js.map 6 | .nyc_output 7 | test-results.xml 8 | coverage -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | env: 2 | global: 3 | - CC_TEST_REPORTER_ID=51a74a14a96931b5a22b9c6837bb61cd075eac815c77872a12963b0e05719ac1 4 | language: node_js 5 | sudo: false 6 | notifications: 7 | email: false 8 | node_js: 9 | - '11' 10 | cache: 11 | directories: 12 | - node_modules 13 | 14 | before_script: 15 | - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter 16 | - chmod +x ./cc-test-reporter 17 | - ./cc-test-reporter before-build 18 | 19 | script: 20 | - npm test && npm run report:lcov 21 | - npm run build 22 | 23 | after_script: 24 | - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT 25 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) 6 | and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). 7 | 8 | ### [Unreleased][HEAD] 9 | 10 | - none 11 | 12 | ## [5.2.0] - May 20th 2020 13 | 14 | - Add support of React 17 and React 18 15 | 16 | ## [5.1.0] - June 11th 2020 17 | 18 | ### Changed 19 | 20 | - Updated to esri-loader v2.14, `otherwise` vs `catch` issue with JS API ([#178](https://github.com/Esri/react-arcgis/pull/178)) 21 | 22 | ## [5.0.0] - October 14th 2019 23 | 24 | ### Changed 25 | 26 | - document how to configure esri-loader via `loaderOptions` 27 | 28 | ### Breaking 29 | 30 | - react-arcgis now declares React and esri-loader as peer dependencies 31 | - react-arcgis no longer exports `loadModules` nor `esriPromise` 32 | 33 | See the [v5 migration guide](./README.md#from-v4-to-v5) 34 | 35 | ## 4.0.0 - January 9th 2019 36 | 37 | ### Changed 38 | 39 | - `react-arcgis` has been moved into the @esri namespace on npm. 40 | 41 | ```shell 42 | npm install @esri/react-arcgis 43 | ``` 44 | 45 | ```js 46 | import { Map, Scene } from '@esri/react-arcgis'; 47 | ``` 48 | 49 | - React-arcgis will now load version 4.10 of the ArcGIS API for JavaScript by default 50 | 51 | ## 3.3.3 52 | 53 | - React-arcgis will now load version 4.9 of the ArcGIS JS API by default 54 | - Fixed issue with the `zoom` prop on `Scene` components 55 | 56 | ## 3.3.2 57 | 58 | - React-arcgis components now accept a `childrenAsFunction` prop: 59 | 60 | ```ts 61 | render() { 62 | return ( 63 | ( 66 | 67 | )} 68 | /> 69 | ); 70 | } 71 | ``` 72 | 73 | This is helpful for TypeScript users, as it allows the compiler to understand the relationship between parent and child components. If you are not using TypeScript, there is no benefit to using this prop over specifying children in the usual manner. 74 | 75 | ## 3.3.1 76 | 77 | - React-arcgis will now load version 4.8 by default 78 | - Issue with using `mapProperties` in the `WebMap` and `WebScene` components has been fixed 79 | 80 | ## 3.3.0 81 | 82 | - React-arcgis will now load version 4.7 by default 83 | - `loadModules` from [https://github.com/Esri/esri-loader](https://github.com/Esri/esri-loader) can now also be imported as `loadModules` instead of just `esriPromise` for consistency. For example: 84 | 85 | ```js 86 | import { loadModules, esriPromise } from 'react-arcgis'; 87 | 88 | loadModules === esriPromise // true 89 | ``` 90 | 91 | ## Version 3.2.0: 92 | 93 | - You can now include loader options directly in react-arcgis components. This provides an easy way to migrate to esri's newer async/await compatible promises in your react-arcgis application. For example: 94 | 95 | ```js 96 | render() { 97 | return ( 98 | 107 | ); 108 | } 109 | ``` 110 | [HEAD]: https://github.com/Esri/react-arcgis/compare/5.1.0...HEAD 111 | [5.2.0]: https://github.com/Esri/react-arcgis/compare/v5.1.0...5.2.0 112 | [5.1.0]: https://github.com/Esri/react-arcgis/compare/v5.0.0...5.1.0 113 | [5.0.0]: https://github.com/Esri/react-arcgis/compare/v4.0.0...5.0.0 114 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Code of conduct 2 | 3 | **TL;DR**
4 | We expect folks that participate in both our online and [IRL](https://www.urbandictionary.com/define.php?term=IRL) communities to be kind and considerate of others at all times. 5 | 6 | Esri's official CoC text can be found at http://www.esri.com/events/code-of-conduct -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Esri welcomes contributions from anyone and everyone. Please see our [guidelines for contributing](https://github.com/esri/contributing). 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright (c) 2017-2018 Esri Inc. 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # react-arcgis 2 | 3 | [![Version](https://img.shields.io/npm/v/@esri/react-arcgis.svg?style=flat-square)](https://www.npmjs.com/package/@esri/react-arcgis) 4 | [![build status][travis-img]][travis-url] 5 | [![Test Coverage][coverage-img]][coverage-url] 6 | 7 | [coverage-img]: https://api.codeclimate.com/v1/badges/940f84ed55f223c0bea8/test_coverage 8 | [coverage-url]: https://codeclimate.com/github/Esri/react-arcgis/test_coverage 9 | [travis-img]: https://img.shields.io/travis/Esri/react-arcgis/master.svg?style=flat-square 10 | [travis-url]: https://travis-ci.org/Esri/react-arcgis 11 | 12 | 13 | **The react-arcgis repository is now archived (July 2022)** 14 | 15 | This project provided a library with a few ready to use React components (``, ``, ``, and ``) to get you started using the [ArcGIS API for JavaScript](https://developers.arcgis.com/javascript/) in your React application. These components used [esri-loader](https://github.com/Esri/esri-loader) under the hood to lazy-load the ArcGIS API modules. 16 | 17 | **However, for a few years you have NOT needed `react-arcgis` to use the ArcGIS API in your React application. Instead it is best to use either [@arcgis/core](https://www.npmjs.com/package/@arcgis/core) or [esri-loader](https://github.com/esri/esri-loader) directly in your React application.** 18 | 19 | Please take a look at these modern alternatives to using react-arcgis: 20 | 21 | - [ArcGIS API for JavaScript: Build with ES Modules](https://developers.arcgis.com/javascript/latest/es-modules/) - how to use [@arcgis/core](https://www.npmjs.com/package/@arcgis/core) with modern build tools (example uses React) 22 | - [ArcGIS API for JavaScript with create-react-app](https://github.com/Esri/jsapi-resources/tree/master/esm-samples/jsapi-create-react-app) – An example of how to use [@arcgis/core](https://www.npmjs.com/package/@arcgis/core) in [Create React App](https://create-react-app.dev/). 23 | - [esri-loader React examples](https://github.com/Esri/esri-loader#react) 24 | 25 | Do you have a question related to JavaScript that isn’t specific to React? Post these questions in the [GeoNET](https://community.esri.com/community/developers/web-developers/arcgis-api-for-javascript) forum for the ArcGIS API for JavaScript. 26 | 27 | ## Installation 28 | 29 | **_IMPORTANT:_ This repository is now archived (July 2022) - please see above for how to use either [@arcgis/core](https://www.npmjs.com/package/@arcgis/core) or [esri-loader](https://github.com/esri/esri-loader) directly in your React application.** 30 | 31 | 1. Run `npm i --save esri-loader @esri/react-arcgis` in your React application 32 | 33 | *If you need to support browsers lacking a native promise implementation, you will have to add a global `Promise` constructor polyfill to your project, as react-arcgis does not include one. See [the esri-loader documentation](https://github.com/Esri/esri-loader#promises) for more details.* 34 | 35 | ## Basic Usage 36 | 37 | **_IMPORTANT:_ You _must_ load ArcGIS API styles before using the components in this library. The snippets below assume you've already used one of [the ways that `esri-loader` provides you with to load the ArcGIS API CSS](https://github.com/Esri/esri-loader#loading-styles).** 38 | 39 | Render a simple map in React: 40 | 41 | ```js 42 | import React from 'react'; 43 | import * as ReactDOM from 'react-dom'; 44 | import { Map } from '@esri/react-arcgis'; 45 | 46 | ReactDOM.render( 47 | , 48 | document.getElementById('container') 49 | ); 50 | ``` 51 | 52 | ![map](https://user-images.githubusercontent.com/16542714/27751340-a9e440b4-5d90-11e7-84bc-6281f2a2f59b.jpg) 53 | 54 | Or, render a 3D web-scene: 55 | 56 | ```js 57 | import React from 'react'; 58 | import * as ReactDOM from 'react-dom'; 59 | import { Scene } from '@esri/react-arcgis'; 60 | 61 | ReactDOM.render( 62 | , 63 | document.getElementById('container') 64 | ); 65 | ``` 66 | 67 | You can also add webmaps and webscenes from ArcGIS Online: 68 | 69 | ```js 70 | import React from 'react'; 71 | import * as ReactDOM from 'react-dom'; 72 | import { WebMap, WebScene } from '@esri/react-arcgis'; 73 | 74 | ReactDOM.render( 75 |
76 | 77 | 78 |
, 79 | document.getElementById('container') 80 | ); 81 | ``` 82 | ![webmap](https://user-images.githubusercontent.com/16542714/27751438-26e02b78-5d91-11e7-8e5d-9198cd390e57.jpg) 83 | 84 | If you want to change the style of the `Map` or `Scene`, just give it a class: 85 | 86 | ```js 87 | import React from 'react'; 88 | import * as ReactDOM from 'react-dom'; 89 | import { Scene } from '@esri/react-arcgis'; 90 | 91 | ReactDOM.render( 92 | , 93 | document.getElementById('container') 94 | ); 95 | ``` 96 | 97 | You can also pass properties into the `Map`, `MapView`, or `SceneView` via the viewProperties or mapProperties props: 98 | 99 | ```js 100 | import React from 'react'; 101 | import { Map } from '@esri/react-arcgis'; 102 | 103 | export default (props) => ( 104 | 108 | ) 109 | ``` 110 | ![map-properties](https://user-images.githubusercontent.com/16542714/27751672-3e31bcc8-5d92-11e7-8e2b-3afab88c7154.jpg) 111 | 112 | These properties are passed directly to the available properties on the corresponding [ArcGIS API classes](https://developers.arcgis.com/javascript/latest/api-reference/index.html): 113 | 114 | ```js 115 | import React from 'react'; 116 | import { Scene } from '@esri/react-arcgis'; 117 | 118 | export default (props) => ( 119 | 127 | ) 128 | ``` 129 | ![scene](https://user-images.githubusercontent.com/16542714/27750977-088b94ac-5d8f-11e7-997a-088a3c717cf6.jpg) 130 | 131 | If you want to access the `map` and `view` instances directly after they are loaded, pass in an `onLoad` handler: 132 | 133 | ```js 134 | import React from 'react'; 135 | import { Map } from '@esri/react-arcgis'; 136 | 137 | export default class MakeAMap extends React.Component { 138 | constructor(props) { 139 | super(props); 140 | this.state = { 141 | map: null, 142 | view: null 143 | }; 144 | 145 | this.handleMapLoad = this.handleMapLoad.bind(this) 146 | } 147 | 148 | render() { 149 | return ; 150 | } 151 | 152 | handleMapLoad(map, view) { 153 | this.setState({ map, view }); 154 | } 155 | } 156 | ``` 157 | 158 | Don't forget an `onFail` handler in case something goes wrong: 159 | 160 | ```js 161 | import React from 'react'; 162 | import { WebScene } from '@esri/react-arcgis'; 163 | 164 | export default class MakeAScene extends React.Component { 165 | constructor(props) { 166 | super(props); 167 | this.state = { 168 | status: 'loading' 169 | }; 170 | 171 | this.handleFail = this.handleFail.bind(this); 172 | } 173 | 174 | render() { 175 | return ; 176 | } 177 | 178 | handleFail(e) { 179 | console.error(e); 180 | this.setState({ status: 'failed' }); 181 | } 182 | } 183 | ``` 184 | 185 | ## Advanced Usage 186 | 187 | ### Configuring `esri-loader` 188 | 189 | By default, the components in this library will use `esri-loader`'s default options, which means they will lazy-load the modules from the CDN version of the ArcGIS API for JavaScript used by the version of `esri-loader` you have installed. See [the `esri-loader` configuration documentation](https://github.com/Esri/esri-loader/#configuring-esri-loader) for information on how you can customize `esri-loader`'s behavior. 190 | 191 | #### Using `setDefaultOptions()` 192 | 193 | The easiest way to do this is by calling `esri-loader`'s `setDefaultOptions()` once at application startup before any of the components in this library are rendered. 194 | 195 | ```jsx 196 | import React from 'react'; 197 | import * as ReactDOM from 'react-dom'; 198 | import { setDefaultOptions } from 'esri-loader'; 199 | // app contains react-arcgis components 200 | import { App } from './components/App.js'; 201 | 202 | // configure esri-loader to lazy load the CSS 203 | // the fisrt time any react-arcgis components are rendered 204 | setDefaultOptions({ css: true }); 205 | 206 | ReactDOM.render( 207 | , 208 | document.getElementById('app') 209 | ); 210 | ``` 211 | 212 | This requires `esri-loader@^2.12.0`. If you are using an older version of esri-loader you should probably upgrade. 213 | 214 | #### Using `loaderOptions` 215 | 216 | If you can't use `setDefaultOptions()`, you can use the `loaderOptions` prop provided by each of the components in this library. That will be [passed as `options` to `loadModules()`](https://github.com/Esri/esri-loader/#without-setdefaultoptions). Keep in mind that if you use `loaderOptions`, you must pass the same options to _all_ `react-arcgis` components in your application, as well as any places where your application calls `loadModules()` directly. 217 | 218 | ### Creating Your Own Components 219 | 220 | The functionality available through the [ArcGIS API for JavaScript](https://developers.arcgis.com/javascript/) goes well beyond just rendering maps, and if your application needs to do more with the map than simply show it, you will likely need to load and use additional classes from the ArcGIS API and provide the instances of those classes with references to the maps you've created with the components in this library. 221 | 222 | Fortunately you've already installed `esri-loader`, which allows you to [load any additional ArcGIS API modules your application might need](https://github.com/Esri/esri-loader#loading-modules-from-the-arcgis-api-for-javascript). Also, `react-arcgis` provides the children of ``, ``, ``, and `` with access to their parent's `map` and `view` instances through props. 223 | 224 | For example, let's convert a Bermuda Triangle graphic from [this example](https://developers.arcgis.com/javascript/latest/sample-code/sandbox/index.html?sample=intro-graphics) into a react component: 225 | 226 | ```js 227 | import { useState, useEffect } from 'react'; 228 | import { loadModules } from 'esri-loader'; 229 | 230 | const BermudaTriangle = (props) => { 231 | 232 | const [graphic, setGraphic] = useState(null); 233 | useEffect(() => { 234 | 235 | loadModules(['esri/Graphic']).then(([Graphic]) => { 236 | // Create a polygon geometry 237 | const polygon = { 238 | type: "polygon", // autocasts as new Polygon() 239 | rings: [ 240 | [-64.78, 32.3], 241 | [-66.07, 18.45], 242 | [-80.21, 25.78], 243 | [-64.78, 32.3] 244 | ] 245 | }; 246 | 247 | // Create a symbol for rendering the graphic 248 | const fillSymbol = { 249 | type: "simple-fill", // autocasts as new SimpleFillSymbol() 250 | color: [227, 139, 79, 0.8], 251 | outline: { // autocasts as new SimpleLineSymbol() 252 | color: [255, 255, 255], 253 | width: 1 254 | } 255 | }; 256 | 257 | // Add the geometry and symbol to a new graphic 258 | const graphic = new Graphic({ 259 | geometry: polygon, 260 | symbol: fillSymbol 261 | }); 262 | setGraphic(graphic); 263 | props.view.graphics.add(graphic); 264 | }).catch((err) => console.error(err)); 265 | 266 | return function cleanup() { 267 | props.view.graphics.remove(graphic); 268 | }; 269 | }, []); 270 | 271 | return null; 272 | 273 | } 274 | 275 | export default BermudaTriangle; 276 | ``` 277 | 278 | Now we can use the `` component within our ``, ``, ``, or ``, and the `map` and `view` props will automatically be supplied by react-arcgis: 279 | 280 | ```js 281 | import * as React from 'react'; 282 | import { Scene } from '@esri/react-arcgis'; 283 | import BermudaTriangle from './BermudaTriangle'; // The Graphic component we just made 284 | 285 | export default (props) => ( 286 | 287 | 288 | 289 | ) 290 | ``` 291 | ![bermuda-triangle](https://user-images.githubusercontent.com/16542714/27752141-5f000034-5d94-11e7-83bc-c88428f99053.jpg) 292 | 293 | ### Using the ArcGIS Types 294 | 295 | See the [`esri-loader` documentation on working with ArcGIS types](https://github.com/Esri/esri-loader#4x-types). 296 | 297 | ## Migrating 298 | 299 | ### From v4 to v5 300 | 301 | First, make sure esri-loader is installed as a dependency of your application: 302 | 303 | ```shell 304 | npm install --save esri-loader 305 | ``` 306 | 307 | Then, update any `import` statements for `loadModules()`: 308 | 309 | ```js 310 | // Replace old `loadModules` imports... 311 | // import { Map, loadModules } from '@esri/react-arcgis'; 312 | 313 | // With a new, separate esri-loader import: 314 | import { Map } from '@esri/react-arcgis'; 315 | import { loadModules } from 'esri-loader'; 316 | ``` 317 | 318 | ## Contributions 319 | 320 | Anyone is welcome to contribute to this package. However, we do not plan to add any more components to this library. If you have created a component that you'd like to share, we encourage you to share it via [CodeSandbox](https://codesandbox.io/) or a [gist](https://gist.github.com/). Once you've done that feel free to open an issue and we'll help spread the word. 321 | 322 | We gladly welcome bug fixes or improvements to documentation. 323 | 324 | Here are some commands that may be helpful for development: 325 | 326 | - `npm test`: Runs the unit tests 327 | - `npm run build`: Builds the application 328 | 329 | To run the demo application against the code you are developing, you'll need to run these commands: 330 | 331 | ```bash 332 | npm link 333 | npm run build 334 | cd demo 335 | npm i 336 | npm link @esri/react-arcgis 337 | npm start 338 | ``` 339 | 340 | Keep in mind that the `start` script only watches for changes to code in the demo app. You'll have to re-run `npm run build` each time you make changes in to the library and want to verify them in the demo app. 341 | 342 | ### License 343 | 344 | Copyright © 2017-2019 Esri 345 | 346 | Licensed under the Apache License, Version 2.0 (the "License"); 347 | you may not use this file except in compliance with the License. 348 | You may obtain a copy of the License at 349 | 350 | > http://www.apache.org/licenses/LICENSE-2.0 351 | 352 | Unless required by applicable law or agreed to in writing, software 353 | distributed under the License is distributed on an "AS IS" BASIS, 354 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 355 | See the License for the specific language governing permissions and 356 | limitations under the License. 357 | 358 | A copy of the license is available in the repository's [LICENSE](./LICENSE) file. 359 | -------------------------------------------------------------------------------- /RELEASE.md: -------------------------------------------------------------------------------- 1 | # Release Steps 2 | 3 | 1. Update `CHANGELOG.md` 4 | 1. Update verion in package.json 5 | 1. `npm install` 6 | 1. `npm test` 7 | 1. `npm run build` 8 | 1. Commit, push to master 9 | 1. Create tag for version, push tags to master 10 | 1. `npm whoami` and/or `npm login` 11 | 1. `npm publish` 12 | 1. Update release notes on the [releases page](https://github.com/Esri/react-arcgis/releases) (link to changelog). -------------------------------------------------------------------------------- /demo/.env: -------------------------------------------------------------------------------- 1 | SKIP_PREFLIGHT_CHECK=true -------------------------------------------------------------------------------- /demo/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /demo/README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `npm install && npm start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `npm run build` 16 | 17 | Builds the app for production to the `build` folder.
18 | It correctly bundles React in production mode and optimizes the build for the best performance. 19 | 20 | The build is minified and the filenames include the hashes.
21 | Your app is ready to be deployed! 22 | 23 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 24 | 25 | ## Learn More 26 | 27 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 28 | 29 | To learn React, check out the [React documentation](https://reactjs.org/). 30 | -------------------------------------------------------------------------------- /demo/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-arcgis-demo", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@esri/react-arcgis": "^4.0.0", 7 | "esri-loader": "^2.10.2", 8 | "react": "^18.1.0", 9 | "react-dom": "^18.1.0", 10 | "react-scripts": "^5.0.1" 11 | }, 12 | "scripts": { 13 | "start": "react-scripts start", 14 | "build": "react-scripts build", 15 | "test": "react-scripts test", 16 | "eject": "react-scripts eject" 17 | }, 18 | "eslintConfig": { 19 | "extends": "react-app" 20 | }, 21 | "browserslist": { 22 | "production": [ 23 | ">0.2%", 24 | "not dead", 25 | "not op_mini all" 26 | ], 27 | "development": [ 28 | "last 1 chrome version", 29 | "last 1 firefox version", 30 | "last 1 safari version" 31 | ] 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /demo/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Esri/react-arcgis/ba79e95057d43886fcf474c78dc1d119f1c2f418/demo/public/favicon.ico -------------------------------------------------------------------------------- /demo/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 19 | Esri React 20 | 21 | 22 | 25 |
26 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /demo/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | } 10 | ], 11 | "start_url": ".", 12 | "display": "standalone", 13 | "theme_color": "#000000", 14 | "background_color": "#ffffff" 15 | } 16 | -------------------------------------------------------------------------------- /demo/src/BermudaTriangle.js: -------------------------------------------------------------------------------- 1 | import { useState, useEffect } from 'react'; 2 | import { loadModules } from 'esri-loader'; 3 | 4 | const BermudaTriangle = (props) => { 5 | 6 | const [graphic, setGraphic] = useState(null); 7 | useEffect(() => { 8 | 9 | loadModules(['esri/Graphic']).then(([Graphic]) => { 10 | // Create a polygon geometry 11 | const polygon = { 12 | type: "polygon", // autocasts as new Polygon() 13 | rings: [ 14 | [-64.78, 32.3], 15 | [-66.07, 18.45], 16 | [-80.21, 25.78], 17 | [-64.78, 32.3] 18 | ] 19 | }; 20 | 21 | // Create a symbol for rendering the graphic 22 | const fillSymbol = { 23 | type: "simple-fill", // autocasts as new SimpleFillSymbol() 24 | color: [227, 139, 79, 0.8], 25 | outline: { // autocasts as new SimpleLineSymbol() 26 | color: [255, 255, 255], 27 | width: 1 28 | } 29 | }; 30 | 31 | // Add the geometry and symbol to a new graphic 32 | const graphic = new Graphic({ 33 | geometry: polygon, 34 | symbol: fillSymbol 35 | }); 36 | setGraphic(graphic); 37 | props.view.graphics.add(graphic); 38 | }).catch((err) => console.error(err)); 39 | 40 | return function cleanup() { 41 | props.view.graphics.remove(graphic); 42 | }; 43 | }, [ graphic, props ]); 44 | 45 | return null; 46 | 47 | } 48 | 49 | export default BermudaTriangle; -------------------------------------------------------------------------------- /demo/src/MyFeatureLayer.js: -------------------------------------------------------------------------------- 1 | import { useState, useEffect } from 'react'; 2 | import { loadModules } from 'esri-loader'; 3 | 4 | const MyFeatureLayer = (props) => { 5 | const [myFeatureLayer, setMyFeatureLayer] = useState(null); 6 | useEffect(() => { 7 | loadModules(['esri/layers/FeatureLayer']).then(([FeatureLayer]) => { 8 | const myFeatureLayer = new FeatureLayer({ 9 | url: props.featureLayerProperties.url 10 | }); 11 | 12 | setMyFeatureLayer(myFeatureLayer); 13 | props.map.add(myFeatureLayer); 14 | }).catch((err) => console.error(err)); 15 | 16 | return function cleanup() { 17 | props.map.remove(myFeatureLayer); 18 | } 19 | }, [ props ]); 20 | 21 | return null; 22 | } 23 | 24 | export default MyFeatureLayer; -------------------------------------------------------------------------------- /demo/src/index.js: -------------------------------------------------------------------------------- 1 | import { createRoot } from 'react-dom/client'; 2 | 3 | import { Map } from '@esri/react-arcgis'; 4 | import BermudaTriangle from './BermudaTriangle'; 5 | import MyFeatureLayer from './MyFeatureLayer'; 6 | 7 | const rootElement = document.getElementById('root'); 8 | const root = createRoot(rootElement); 9 | 10 | root.render( 11 | 17 | 18 | 23 | 24 | 25 | ); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "author": "Nick Senger", 3 | "version": "5.2.0", 4 | "bugs": { 5 | "url": "https://github.com/Esri/react-arcgis/issues" 6 | }, 7 | "description": "A few components to help you get started using the ArcGIS API for JavaScript and esri-loader with React", 8 | "devDependencies": { 9 | "@testing-library/react": "^13.1.1", 10 | "@types/arcgis-js-api": "4.15.0", 11 | "@types/glob": "^7.1.1", 12 | "@types/jsdom": "^12.2.4", 13 | "@types/mocha": "^5.2.7", 14 | "@types/react": "^18.0.6", 15 | "@types/react-dom": "^18.0.2", 16 | "@types/sinon": "^7.0.13", 17 | "chai": "^4.2.0", 18 | "es6-promise": "^4.2.8", 19 | "esri-loader": "2.14.0", 20 | "gh-release": "^3.5.0", 21 | "glob": "^7.1.5", 22 | "jsdom": "^15.1.1", 23 | "mocha": "^6.2.2", 24 | "mocha-junit-reporter": "^1.23.1", 25 | "mock-require": "^3.0.3", 26 | "npm-run-all": "^4.1.5", 27 | "nyc": "^14.1.1", 28 | "react": "^18.0.0", 29 | "react-dom": "^18.0.0", 30 | "react-test-renderer": "^16.10.0", 31 | "sinon": "^7.5.0", 32 | "source-map-loader": "^0.2.4", 33 | "ts-loader": "^8.4.0", 34 | "ts-node": "^8.4.1", 35 | "tslint": "^5.20.0", 36 | "tslint-react": "^4.1.0", 37 | "typescript": "^4.6.3", 38 | "webpack": "^4.46.0", 39 | "webpack-cli": "^3.3.9" 40 | }, 41 | "files": [ 42 | "dist/**/*" 43 | ], 44 | "homepage": "https://github.com/Esri/react-arcgis#readme", 45 | "keywords": [ 46 | "Esri", 47 | "ArcGIS", 48 | "TypeScript", 49 | "React", 50 | "Map" 51 | ], 52 | "license": "Apache-2.0", 53 | "main": "dist/umd/index.js", 54 | "module": "dist/esm/index.js", 55 | "name": "@esri/react-arcgis", 56 | "nyc": { 57 | "extension": [ 58 | ".tsx", 59 | ".ts" 60 | ] 61 | }, 62 | "peerDependencies": { 63 | "esri-loader": "^2.14", 64 | "react": "^16 || ^17 || ^18", 65 | "react-dom": "^16 || ^17 || ^18" 66 | }, 67 | "repository": { 68 | "type": "git", 69 | "url": "git+https://github.com/Esri/react-arcgis.git" 70 | }, 71 | "scripts": { 72 | "build": "run-s build:types build:scripts", 73 | "build:types": "tsc", 74 | "build:scripts": "webpack -p", 75 | "test": "nyc mocha", 76 | "test:junit": "nyc mocha --reporter mocha-junit-reporter", 77 | "test:all": "run-s test test:junit report:cobertura report:lcov", 78 | "report:cobertura": "nyc report --reporter cobertura", 79 | "report:lcov": "nyc report --reporter lcov", 80 | "lint": "tslint -p ." 81 | }, 82 | "types": "dist/esm/index.d.ts" 83 | } 84 | -------------------------------------------------------------------------------- /src/ts/components/ArcBase.tsx: -------------------------------------------------------------------------------- 1 | import { cloneElement, Component, Children } from 'react'; 2 | import { loadModules, ILoadScriptOptions } from 'esri-loader'; 3 | import ArcContainer from './ArcContainer'; 4 | 5 | export interface BaseProps { 6 | id?: string; 7 | children?: any; 8 | className?: string; 9 | style?: { 10 | [propName: string]: any 11 | }; 12 | mapProperties?: __esri.MapProperties | __esri.WebMapProperties | __esri.WebSceneProperties; 13 | viewProperties?: __esri.MapViewProperties | __esri.SceneViewProperties; 14 | onClick?: (e: EventProperties) => any; 15 | onDoubleClick?: (e: EventProperties) => any; 16 | onDrag?: (e: EventProperties) => any; 17 | onHold?: (e: EventProperties) => any; 18 | onKeyDown?: (e: EventProperties) => any; 19 | onKeyUp?: (e: EventProperties) => any; 20 | onLayerViewCreate?: (e: EventProperties) => any; 21 | onLayerViewDestroy?: (e: EventProperties) => any; 22 | onMouseWheel?: (e: EventProperties) => any; 23 | onPointerDown?: (e: EventProperties) => any; 24 | onPointerMove?: (e: EventProperties) => any; 25 | onPointerUp?: (e: EventProperties) => any; 26 | onResize?: (e: EventProperties) => any; 27 | onLoad?: (map: __esri.Map, view: __esri.MapView | __esri.SceneView) => any; 28 | onFail?: (e: any) => any; 29 | loadElement?: any; 30 | failElement?: any; 31 | loaderOptions?: ILoadScriptOptions; 32 | childrenAsFunction?: (map?: __esri.Map, view?: __esri.MapView | __esri.SceneView) => JSX.Element; 33 | } 34 | 35 | export interface ArcProps extends BaseProps { 36 | loadMap: (modules: any[], containerId: string) => Promise; 37 | scriptUri: string[]; 38 | } 39 | 40 | export interface EventProperties { 41 | [propName: string]: any; 42 | } 43 | 44 | export interface ComponentState { 45 | mapContainerId: string; 46 | status: string; 47 | map?: __esri.Map; 48 | view?: __esri.MapView | __esri.SceneView; 49 | } 50 | 51 | export class ArcView extends Component { 52 | constructor(props: ArcProps) { 53 | super(props); 54 | this.state = { 55 | mapContainerId: Math.random().toString(36).substring(0, 14), 56 | status: 'loading' 57 | }; 58 | } 59 | 60 | public render() { 61 | const centerStyle = { 62 | left: '50%', 63 | marginRight: '-50%', 64 | position: 'absolute', 65 | top: '50%', 66 | transform: 'translate(-50%, -50%)' 67 | }; 68 | 69 | const mapStyle = this.props.className ? 70 | this.props.style : 71 | { 72 | height: '100%', 73 | position: 'relative', 74 | width: '100%', 75 | ...this.props.style 76 | }; 77 | 78 | const loadElement = ( 79 | this.props.loadElement ? this.props.loadElement : 80 |

Loading...

81 | ); 82 | 83 | const failElement = ( 84 | this.props.failElement ? this.props.failElement : 85 |

The ArcGIS API failed to load.

86 | ); 87 | 88 | if (this.state.status === 'loaded') { 89 | if (!!this.props.childrenAsFunction) { 90 | return this.props.childrenAsFunction(this.state.map, this.state.view); 91 | } 92 | const childrenWithProps = Children.map(this.props.children, (child) => { 93 | const childEl = child as React.ReactElement; 94 | return cloneElement(childEl, { 95 | map: this.state.map, 96 | view: this.state.view 97 | } 98 | ); 99 | }); 100 | return ( 101 |
102 | 103 | {childrenWithProps} 104 |
105 | ); 106 | } else if (this.state.status === 'loading') { 107 | return ( 108 |
109 | 110 |
111 | {loadElement} 112 |
113 |
114 | ); 115 | } 116 | return ( 117 |
118 | 119 |
120 | {failElement} 121 |
122 |
123 | ); 124 | } 125 | 126 | public componentDidMount() { 127 | loadModules(this.props.scriptUri, this.props.loaderOptions) 128 | .then((modules: any) => ( 129 | this.props.loadMap(modules, this.state.mapContainerId) 130 | .then( 131 | ({ map, view }) => { 132 | this.setState({ 133 | map, 134 | view, 135 | status: 'loaded' 136 | }); 137 | if (this.props.onLoad) { 138 | this.props.onLoad(map, view); 139 | } 140 | }) 141 | .catch((e) => { 142 | throw e; 143 | }) 144 | )).catch((e: Error) => { 145 | this.setState({ status: 'failed' }); 146 | if (this.props.onFail) { 147 | this.props.onFail(e); 148 | } 149 | }); 150 | } 151 | } 152 | -------------------------------------------------------------------------------- /src/ts/components/ArcComposites.tsx: -------------------------------------------------------------------------------- 1 | import { ArcView, BaseProps } from './ArcBase'; 2 | 3 | export interface MapBaseProps extends BaseProps { 4 | scriptUri: string[]; 5 | 6 | [index: string]: any; 7 | } 8 | 9 | export interface WebBaseProps extends BaseProps { 10 | scriptUri: string[]; 11 | id: string; 12 | 13 | [index: string]: any; 14 | } 15 | 16 | export interface EventMap { 17 | onClick: 'click' | string; 18 | onDoubleClick: 'double-click' | string; 19 | onDrag: 'drag' | string; 20 | onHold: 'hold' | string; 21 | onKeyDown: 'key-down' | string; 22 | onKeyUp: 'key-up' | string; 23 | onLayerViewCreate: 'layerview-create' | string; 24 | onLayerViewDestroy: 'layerview-destroy' | string; 25 | onMouseWheel: 'mouse-wheel' | string; 26 | onPointerDown: 'pointer-down' | string; 27 | onPointerMove: 'pointer-move' | string; 28 | onPointerUp: 'pointer-up' | string; 29 | onResize: 'resize' | string; 30 | 31 | [index: string]: string; 32 | } 33 | 34 | const eventMap: EventMap = { 35 | onClick: 'click', 36 | onDoubleClick: 'double-click', 37 | onDrag: 'drag', 38 | onHold: 'hold', 39 | onKeyDown: 'key-down', 40 | onKeyUp: 'key-up', 41 | onLayerViewCreate: 'layerview-create', 42 | onLayerViewDestroy: 'layerview-destroy', 43 | onMouseWheel: 'mouse-wheel', 44 | onPointerDown: 'pointer-down', 45 | onPointerMove: 'pointer-move', 46 | onPointerUp: 'pointer-up', 47 | onResize: 'resize' 48 | }; 49 | 50 | export const MapBase = (props: MapBaseProps) => ( 51 | { 55 | const mapData = new Promise((resolve, reject) => { 56 | try { 57 | const map: __esri.Map = new Map(props.mapProperties); // Make the map 58 | const viewProperties: any = { 59 | map, 60 | container: containerId, 61 | ...props.viewProperties 62 | }; 63 | const view: __esri.View = new View(viewProperties); // Make the view 64 | const typedView = view as __esri.MapView | __esri.SceneView; 65 | Object.keys(eventMap).forEach((key) => { // Set view events to any user defined callbacks 66 | if (props[key]) { 67 | typedView.on(eventMap[key], props[key]); 68 | } 69 | }); 70 | view.when(() => { 71 | resolve({ map, view }); 72 | }, (err: Error) => { 73 | reject(err); 74 | }); 75 | } catch (err) { 76 | reject(err); 77 | } 78 | }); 79 | return mapData; 80 | } 81 | } 82 | /> 83 | ); 84 | 85 | export const WebBase = (props: WebBaseProps) => ( 86 | { 90 | const mapData = new Promise((resolve, reject) => { 91 | try { 92 | const map: __esri.WebMap | __esri.WebScene = new WebConstructor({ 93 | portalItem: { 94 | id: props.id 95 | }, 96 | ...props.mapProperties 97 | }); 98 | map.load() 99 | .then(() => { 100 | return map.basemap.load(); 101 | }) 102 | .then(() => { 103 | const allLayers = map.allLayers; 104 | const promises = allLayers.map((layer) => layer.load()); 105 | return all(promises.toArray()); 106 | }) 107 | .then(() => { 108 | const view = new ViewConstructor({ 109 | container: containerId, 110 | map, 111 | ...props.viewProperties 112 | }); 113 | Object.keys(eventMap).forEach((key) => { // Set view events to any user defined callbacks 114 | if (props[key]) { 115 | view.on(eventMap[key], props[key]); 116 | } 117 | }); 118 | resolve({ map, view }); 119 | }).catch((err) => { 120 | reject(err); 121 | }); 122 | } catch (err) { 123 | reject(err); 124 | } 125 | }); 126 | return mapData; 127 | } 128 | } 129 | /> 130 | ); 131 | -------------------------------------------------------------------------------- /src/ts/components/ArcContainer.tsx: -------------------------------------------------------------------------------- 1 | import { Component } from 'react'; 2 | 3 | export interface ArcContainerProps { 4 | id: string; 5 | style?: { 6 | [propName: string]: any; 7 | }; 8 | } 9 | 10 | export interface ArcContainerState {} 11 | 12 | export default class ArcContainer extends Component { 13 | constructor(props: ArcContainerProps) { 14 | super(props); 15 | } 16 | 17 | public render() { 18 | return
; 19 | } 20 | 21 | public shouldComponentUpdate() { 22 | return false; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/ts/components/MapComposites.tsx: -------------------------------------------------------------------------------- 1 | import { BaseProps } from './ArcBase'; 2 | import { MapBase } from './ArcComposites'; 3 | 4 | export interface MapProps extends BaseProps { 5 | mapProperties?: __esri.MapProperties; 6 | viewProperties?: __esri.MapViewProperties; 7 | } 8 | 9 | export interface SceneProps extends BaseProps { 10 | mapProperties?: __esri.MapProperties; 11 | viewProperties?: __esri.SceneViewProperties; 12 | } 13 | 14 | export const Map = (props: MapProps) => ( 15 | 28 | ); 29 | 30 | export const Scene = (props: SceneProps) => ( 31 | 45 | ); 46 | -------------------------------------------------------------------------------- /src/ts/components/WebComposites.tsx: -------------------------------------------------------------------------------- 1 | import { BaseProps } from './ArcBase'; 2 | import { WebBase } from './ArcComposites'; 3 | 4 | export interface MapProps extends BaseProps { 5 | id: string; 6 | mapProperties?: __esri.WebMapProperties; 7 | viewProperties?: __esri.MapViewProperties; 8 | } 9 | 10 | export interface SceneProps extends BaseProps { 11 | id: string; 12 | mapProperties?: __esri.WebSceneProperties; 13 | viewProperties?: __esri.SceneViewProperties; 14 | } 15 | 16 | export const WebMap = (props: MapProps) => ( 17 | 23 | ); 24 | 25 | export const WebScene = (props: SceneProps) => ( 26 | 32 | ); 33 | -------------------------------------------------------------------------------- /src/ts/index.ts: -------------------------------------------------------------------------------- 1 | import * as MapViews from './components/MapComposites'; 2 | import * as WebViews from './components/WebComposites'; 3 | 4 | const ReactArcGIS = { 5 | Map: MapViews.Map, 6 | Scene: MapViews.Scene, 7 | WebMap: WebViews.WebMap, 8 | WebScene: WebViews.WebScene 9 | }; 10 | 11 | export default ReactArcGIS; 12 | 13 | export const WebMap = ReactArcGIS.WebMap; 14 | export const WebScene = ReactArcGIS.WebScene; 15 | export const Map = ReactArcGIS.Map; 16 | export const Scene = ReactArcGIS.Scene; 17 | -------------------------------------------------------------------------------- /test/doubles/ChildComponent.tsx: -------------------------------------------------------------------------------- 1 | export interface ChildComponentProps { 2 | map?: __esri.Map; 3 | view?: __esri.View; 4 | } 5 | 6 | export default (props: ChildComponentProps) => ( 7 |

{props.map as any}{props.view as any}

8 | ); 9 | -------------------------------------------------------------------------------- /test/doubles/doubleTrouble.ts: -------------------------------------------------------------------------------- 1 | export function foo() { 2 | return 'bar'; 3 | } 4 | -------------------------------------------------------------------------------- /test/doubles/esriLoader.ts: -------------------------------------------------------------------------------- 1 | import * as sinon from 'sinon'; 2 | 3 | class GetterSetter { 4 | public set(key, value) { 5 | if (this[key]) { 6 | this[key] = value; 7 | } 8 | } 9 | 10 | public get(key) { 11 | if (this[key]) { 12 | return this[key]; 13 | } 14 | return undefined; 15 | } 16 | }; 17 | 18 | export function loadModules(modules) { 19 | if (global['asyncSuccess']) { 20 | if (global['generateMap']) { 21 | return Promise.resolve([ 22 | class Map { 23 | }, 24 | class View { 25 | on() { 26 | return null; 27 | }; 28 | when(callback, errback) { 29 | callback(); 30 | } 31 | } 32 | ]) 33 | } else if (global['generateBrokenMap']) { 34 | return Promise.resolve([ 35 | class Map { 36 | }, 37 | class View { 38 | on() { 39 | return null; 40 | }; 41 | when(callback, errback) { 42 | errback( new Error('failed')); 43 | } 44 | } 45 | ]) 46 | } else if (global['generateBadWebMap']) { 47 | return Promise.resolve([ 48 | class Map { 49 | load() { 50 | throw new Error('failed'); 51 | } 52 | } 53 | ]) 54 | } else if (global['generateWebMap']) { 55 | return Promise.resolve([ 56 | class WebMap { 57 | basemap: any; 58 | allLayers: any; 59 | 60 | constructor() { 61 | this.basemap = { 62 | load() { 63 | return null; 64 | } 65 | } 66 | this.allLayers = { 67 | map(callback) { 68 | callback({ load() {} }); 69 | return { 70 | toArray() { 71 | return null; 72 | } 73 | } 74 | } 75 | } 76 | } 77 | 78 | public load() { 79 | return ({ 80 | then(callback) { 81 | callback(); 82 | return { 83 | then(callback) { 84 | callback(); 85 | return { 86 | then(callback) { 87 | callback(); 88 | return { 89 | otherwise(errback) { 90 | errback(); 91 | } 92 | } 93 | } 94 | } 95 | } 96 | } 97 | } 98 | }) 99 | } 100 | }, 101 | class WebView { 102 | on() { 103 | return null; 104 | } 105 | }, 106 | () => null 107 | ]) 108 | } else if (global['generateGeometry']) { 109 | return Promise.resolve([ 110 | class Geometry extends GetterSetter { 111 | foo: string; 112 | 113 | constructor() { 114 | super(); 115 | this.foo = 'bar'; 116 | } 117 | } 118 | ]); 119 | } else if (global['generateGraphic']) { 120 | return Promise.resolve([ 121 | class Graphic extends GetterSetter { 122 | foo: string; 123 | 124 | constructor() { 125 | super(); 126 | this.foo = 'bar'; 127 | } 128 | 129 | add() {} 130 | } 131 | ]); 132 | } else if (global['generateLayer']) { 133 | return Promise.resolve([ 134 | class Layer extends GetterSetter { 135 | foo: string; 136 | 137 | constructor() { 138 | super(); 139 | this.foo = 'bar'; 140 | } 141 | 142 | on() {} 143 | } 144 | ]) 145 | } else if (global['generateSymbol']) { 146 | return Promise.resolve([ 147 | class Symbol extends GetterSetter { 148 | foo: string; 149 | 150 | constructor() { 151 | super(); 152 | this.foo = 'bar'; 153 | } 154 | } 155 | ]); 156 | } else if (global['generateWidget']) { 157 | return Promise.resolve([ 158 | class Widget extends GetterSetter { 159 | foo: string; 160 | on: any; 161 | 162 | constructor() { 163 | super(); 164 | this.foo = 'bar'; 165 | this.on = sinon.stub(); 166 | } 167 | 168 | } 169 | ]); 170 | } 171 | return Promise.resolve('success'); 172 | } 173 | return Promise.reject(new Error('failed')); 174 | }; 175 | -------------------------------------------------------------------------------- /test/harness.ts: -------------------------------------------------------------------------------- 1 | import { JSDOM } from "jsdom"; 2 | import * as mock from "mock-require"; 3 | 4 | const dom = new JSDOM(""); 5 | global["window"] = dom.window; 6 | global["navigator"] = dom.window.navigator; 7 | global["document"] = dom.window.document; 8 | global["asyncSuccess"] = true; 9 | 10 | mock("esri-loader", "./doubles/esriLoader"); 11 | 12 | import SampleTests from "./ts/test"; 13 | SampleTests(); 14 | 15 | import ArcBaseTests from "./ts/components/ArcBase.spec"; 16 | ArcBaseTests(); 17 | 18 | import { MapBaseTests, WebBaseTests } from "./ts/components/ArcComposites.spec"; 19 | MapBaseTests(); 20 | WebBaseTests(); 21 | 22 | import { MapTests, SceneTests } from "./ts/components/MapComposites.spec"; 23 | MapTests(); 24 | SceneTests(); 25 | 26 | import { WebMapTests, WebSceneTests } from "./ts/components/WebComposites.spec"; 27 | WebMapTests(); 28 | WebSceneTests(); 29 | -------------------------------------------------------------------------------- /test/mocha.opts: -------------------------------------------------------------------------------- 1 | --require test/testSettings.js 2 | --timeout 10000 3 | test/harness.ts 4 | -------------------------------------------------------------------------------- /test/testSettings.js: -------------------------------------------------------------------------------- 1 | require("es6-promise"); 2 | require("ts-node").register({ 3 | project: "test/tsconfig.json" 4 | }); 5 | 6 | global.IS_REACT_ACT_ENVIRONMENT = true; 7 | -------------------------------------------------------------------------------- /test/ts/components/ArcBase.spec.tsx: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import { render, act, RenderResult } from '@testing-library/react'; 3 | import * as sinon from 'sinon'; 4 | import ChildComponent from '../../doubles/ChildComponent'; 5 | import { ArcView } from '../../../src/ts/components/ArcBase'; 6 | 7 | function renderArcView({ 8 | loadMap = sinon.stub(), 9 | scriptUri = ['foo', 'bar'], 10 | ...restProps 11 | }: Partial> = {}): RenderResult & { instance: ArcView | null } { 12 | const renderResult = {} as RenderResult & { instance: ArcView | null }; 13 | 14 | const rendered = render( renderResult.instance = node} loadMap={loadMap} scriptUri={scriptUri} {...restProps} />); 15 | 16 | Object.assign(renderResult, rendered); 17 | 18 | return renderResult; 19 | }; 20 | 21 | export default () => ( 22 | describe('ArcBase', () => { 23 | describe('as a shallow component', () => { 24 | it('should exist', async () => { 25 | const { container } = renderArcView(); 26 | 27 | await act(() => { 28 | expect(container.querySelector('#base-container')).to.exist; 29 | }); 30 | }); 31 | 32 | it('should render the default loading component', async () => { 33 | const { container } = renderArcView(); 34 | 35 | await act(() => { 36 | expect(container.querySelector('#react-arcgis-loading-text')).to.exist; 37 | }); 38 | }); 39 | 40 | describe('the user has included a custom loading element', () => { 41 | const loadElement =

; 42 | 43 | it('should not render the default loading component', async () => { 44 | const { container } = renderArcView({ loadElement }); 45 | 46 | await act(() => { 47 | expect(container.querySelector('#react-arcgis-loading-text')).to.not.exist; 48 | }); 49 | }); 50 | 51 | it('should render the custom loading component', async () => { 52 | const { container } = renderArcView({ loadElement }); 53 | 54 | await act(() => { 55 | expect(container.querySelector('#custom-load-component')).to.exist; 56 | }); 57 | }); 58 | }); 59 | }); 60 | 61 | describe('as a mounted component', () => { 62 | beforeEach(() => { 63 | sinon.spy(ArcView.prototype, 'componentDidMount'); 64 | }); 65 | 66 | it('should call componentDidMount', async () => { 67 | renderArcView(); 68 | 69 | await act(async () => { 70 | expect(ArcView.prototype.componentDidMount['callCount']).to.equal(1); 71 | }); 72 | }); 73 | 74 | describe('the user has included a custom className', () => { 75 | it('should give that className to the container div', () => { 76 | const { container, unmount } = renderArcView({ className: 'foobar' }); 77 | 78 | expect(container.querySelector('#base-container').classList.contains('foobar')).to.be.true; 79 | 80 | unmount(); 81 | }); 82 | }); 83 | 84 | describe('esriPromise succeeds', () => { 85 | before(() => { 86 | global['asyncSuccess'] = true; 87 | }); 88 | 89 | it('should call loadMap with the result of esriPromise', async () => { 90 | let loadMap = sinon.stub(); 91 | 92 | renderArcView({ loadMap }); 93 | 94 | // Wait for implicit async state update in componentDidUpdate 95 | await act(async () => {}); 96 | 97 | expect(loadMap.calledWith('success')).to.be.true; 98 | expect(loadMap.callCount).to.equal(1); 99 | }); 100 | 101 | describe('the loadMap method succeeds', () => { 102 | let loadMap; 103 | let childrenAsFunction; 104 | 105 | beforeEach(() => { 106 | loadMap = () => (Promise.resolve({ map: 'foo', view: 'bar' })); 107 | }); 108 | 109 | it('should not display the failed state for the application', async () => { 110 | const { container } = renderArcView({ loadMap }); 111 | 112 | // Wait for implicit async state update in componentDidUpdate 113 | await act(async () => {}); 114 | 115 | expect(container.querySelector('#react-arcgis-fail-text')).to.not.exist 116 | }); 117 | 118 | it('should set the state of the map and view based on the result of loadMap', async () => { 119 | const { instance } = renderArcView({ loadMap }); 120 | 121 | // Wait for implicit async state update in componentDidUpdate 122 | await act(async () => {}); 123 | 124 | expect(instance.state.map).to.equal('foo'); 125 | expect(instance.state.view).to.equal('bar'); 126 | }); 127 | 128 | describe('the user has included "childrenAsFunction" prop', () => { 129 | beforeEach(() => { 130 | childrenAsFunction = (map: __esri.Map, view: __esri.MapView) => ( 131 | 132 | ) 133 | }); 134 | 135 | it('should render the children', async () => { 136 | const { container } = renderArcView({ loadMap, childrenAsFunction }) 137 | 138 | // Wait for implicit async state update in componentDidUpdate 139 | await act(async () => {}); 140 | 141 | expect(container.querySelector('#child')).to.exist; 142 | }); 143 | 144 | it('should give map and view props to the children', async () => { 145 | const { container } = renderArcView({ loadMap, childrenAsFunction }) 146 | 147 | // Wait for implicit async state update in componentDidUpdate 148 | await act(async () => {}); 149 | 150 | expect(container.querySelector('#child').textContent).to.equal('foobar'); 151 | }); 152 | }); 153 | 154 | describe('the user has included a child component', () => { 155 | it('should render the child component', async () => { 156 | const { container } = renderArcView({ loadMap, children: }) 157 | 158 | // Wait for implicit async state update in componentDidUpdate 159 | await act(() => {}); 160 | 161 | expect(container.querySelector('#child')).to.exist; 162 | }); 163 | 164 | it('should give map and view props to the child component', async () => { 165 | const { container } = renderArcView({ loadMap, children: }) 166 | 167 | // Wait for implicit async state update in componentDidUpdate 168 | await act(() => {}); 169 | 170 | expect(container.querySelector('#child').textContent).to.equal('foobar'); 171 | }); 172 | }); 173 | 174 | describe('the user has included an onLoad callback', () => { 175 | it('should call onLoad with a reference to the map and view', async () => { 176 | const onLoad = sinon.stub(); 177 | 178 | renderArcView({ loadMap, onLoad }) 179 | 180 | // Wait for implicit async state update in componentDidUpdate 181 | await act(async () => {}); 182 | 183 | expect(onLoad.callCount).to.equal(1); 184 | expect(onLoad.calledWith('foo', 'bar')).to.be.true; 185 | }); 186 | }); 187 | 188 | describe('the user updates the mapProperties or viewProperties', () => { 189 | const getterSetter = { 190 | set(key, value) { 191 | if (this[key]) { 192 | this[key] = value; 193 | } 194 | }, 195 | get(key) { 196 | if (this[key]) { 197 | return this[key]; 198 | } 199 | return undefined; 200 | } 201 | }; 202 | 203 | beforeEach(() => { 204 | loadMap = () => ({ 205 | then(callback, errback) { 206 | callback({ 207 | map: { 208 | ...getterSetter, 209 | foo: 'bar' 210 | }, 211 | view: { 212 | ...getterSetter, 213 | foo: 'bar', 214 | set(changeObj) { 215 | Object.keys(changeObj).forEach((key) => { 216 | if (this[key]) { 217 | this[key] = changeObj[key]; 218 | } 219 | }); 220 | } 221 | } 222 | }); 223 | } 224 | }); 225 | }); 226 | 227 | describe('this dataFlow is set to oneTime', () => { 228 | describe('the user updates the mapProperties with a valid key', () => { 229 | it('should make no change to the JS API', async () => { 230 | const { instance } = renderArcView({ loadMap }); 231 | 232 | // Wait for implicit async state update in componentDidUpdate 233 | await act(async () => {}); 234 | 235 | expect(instance.state.map.get('foo')).to.not.equal('banana'); 236 | }); 237 | }); 238 | 239 | describe('the user updates the mapProperties with an invalid key', () => { 240 | it('should make no change to the JS API', async () => { 241 | const { instance } = renderArcView({ loadMap }); 242 | 243 | // Wait for implicit async state update in componentDidUpdate 244 | await act(async () => {}); 245 | 246 | expect(instance.state.map.get('foo')).to.equal('bar'); 247 | expect(instance.state.map.get('bar')).to.not.exist; 248 | }); 249 | }); 250 | 251 | describe('the user updates the viewProperties with a valid key', () => { 252 | it('should update the JS API accordingly', async () => { 253 | const { instance } = renderArcView({ 254 | loadMap, 255 | ...{ userDefinedViewProperties: { foo: 'banana' } } 256 | }); 257 | 258 | // Wait for implicit async state update in componentDidUpdate 259 | await act(async () => {}); 260 | 261 | const { userDefinedViewProperties } = instance.props as any; 262 | expect(userDefinedViewProperties.foo).to.equal('banana'); 263 | expect(instance.state.view.get('foo')).to.not.equal('banana'); 264 | }); 265 | }); 266 | 267 | describe('the user updates the mapProperties with an invalid key', () => { 268 | it('should make no change to the JS API', async () => { 269 | const { instance } = renderArcView({ 270 | loadMap, 271 | ...{ userDefinedViewProperties:{ foo: 'bar', bar: 'foo' } } 272 | }); 273 | 274 | // Wait for implicit async state update in componentDidUpdate 275 | await act(async () => {}); 276 | 277 | const { userDefinedViewProperties } = instance.props as any; 278 | expect(userDefinedViewProperties.bar).to.equal('foo'); 279 | expect(instance.state.view.get('foo')).to.equal('bar'); 280 | expect(instance.state.view.get('bar')).to.not.exist; 281 | }); 282 | }); 283 | }); 284 | }); 285 | }); 286 | 287 | describe('the loadMap method fails', () => { 288 | let loadMap; 289 | 290 | beforeEach(() => { 291 | loadMap = () => (Promise.reject(new Error('failed'))); 292 | }); 293 | 294 | it ('should display the failed state for the application', async () => { 295 | const { container } = renderArcView({ loadMap }); 296 | 297 | // Wait for implicit async state update in componentDidUpdate 298 | await act(async () => {}); 299 | 300 | expect(container.querySelector('#react-arcgis-fail-text')).to.exist; 301 | }); 302 | }); 303 | 304 | after(() => { 305 | global['asyncSuccess'] = false; 306 | }); 307 | }); 308 | 309 | describe('esriPromise fails', () => { 310 | let loadMap; 311 | 312 | beforeEach(() => { 313 | global['asyncSuccess'] = false; 314 | loadMap = sinon.stub(); 315 | }); 316 | 317 | it('should not call loadMap', async () => { 318 | renderArcView({ loadMap }); 319 | 320 | // Wait for implicit async state update in componentDidUpdate 321 | await act(async () => {}); 322 | 323 | expect(loadMap.callCount).to.equal(0); 324 | }); 325 | 326 | it('should display the default failed state for the application', async () => { 327 | const { container } = renderArcView({ loadMap }); 328 | 329 | // Wait for implicit async state update in componentDidUpdate 330 | await act(async () => {}); 331 | 332 | expect(container.querySelector('#react-arcgis-fail-text')).to.exist; 333 | }); 334 | 335 | describe('the user has included a custom fail component', () => { 336 | const failElement =

; 337 | 338 | it('should not display the default failed state for the application', async () => { 339 | const { container } = renderArcView({ loadMap, failElement }); 340 | 341 | // Wait for implicit async state update in componentDidUpdate 342 | await act(async () => {}); 343 | 344 | expect(container.querySelector('#react-arcgis-fail-text')).to.not.exist; 345 | }); 346 | 347 | it('should display the custom fail component', async () => { 348 | const { container } = renderArcView({ loadMap, failElement }); 349 | 350 | // Wait for implicit async state update in componentDidUpdate 351 | await act(async () => {}); 352 | 353 | expect(container.querySelector('#custom-fail-component')).to.exist; 354 | }); 355 | }); 356 | 357 | describe('the user has included a custom fail callback', () => { 358 | let onFail = sinon.stub(); 359 | 360 | it('should call the failure callback', async () => { 361 | renderArcView({ onFail }); 362 | 363 | // Wait for implicit async state update in componentDidUpdate 364 | await act(async () => {}); 365 | 366 | expect(onFail.callCount).to.equal(1); 367 | }); 368 | }) 369 | }); 370 | 371 | afterEach(() => { 372 | ArcView.prototype.componentDidMount['restore'](); 373 | }); 374 | }); 375 | }) 376 | ); 377 | -------------------------------------------------------------------------------- /test/ts/components/ArcComposites.spec.tsx: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import { render, act, RenderResult } from '@testing-library/react'; 3 | import * as sinon from 'sinon'; 4 | import { ArcView } from '../../../src/ts/components/ArcBase'; 5 | import { MapBase, WebBase } from '../../../src/ts/components/ArcComposites'; 6 | 7 | function renderMapBase({ 8 | scriptUri = ['foo', 'bar'], 9 | ...restProps 10 | }: Partial> = {}): RenderResult { 11 | return render(); 12 | }; 13 | 14 | function renderWebBase({ 15 | id = "foobar", 16 | scriptUri = ['foo', 'bar'], 17 | ...restProps 18 | }: Partial> = {}): RenderResult { 19 | return render(); 20 | }; 21 | 22 | export const MapBaseTests = () => ( 23 | describe('MapBase', () => { 24 | 25 | describe('as a shallow component', () => { 26 | it('should exist', async () => { 27 | const { container } = renderMapBase(); 28 | 29 | await act(async () => { 30 | expect(container.querySelector('#base-container')).to.exist; 31 | }); 32 | }); 33 | }); 34 | 35 | describe('as a mounted component', () => { 36 | beforeEach(() => { 37 | sinon.spy(ArcView.prototype, 'componentDidMount'); 38 | }); 39 | 40 | it('should call componentDidMount', async () => { 41 | renderMapBase(); 42 | 43 | await act(async () => { 44 | expect(ArcView.prototype.componentDidMount['callCount']).to.equal(1); 45 | }); 46 | }); 47 | 48 | describe('esriPromise succeeds', () => { 49 | describe('loadMap successfully creates the map and view', () => { 50 | before(() => { 51 | global['asyncSuccess'] = true; 52 | global['generateMap'] = true; 53 | }); 54 | 55 | it('should display the loaded state of the application', async () => { 56 | const mapBase = renderMapBase(); 57 | 58 | // Wait for implicit async state update in componentDidUpdate 59 | await act(async () => {}); 60 | 61 | const { container } = mapBase; 62 | expect(container.querySelector('#react-arcgis-fail-text')).to.not.exist; 63 | expect(container.querySelector('#react-arcgis-loading-text')).to.not.exist; 64 | }); 65 | 66 | describe('the user has included custom event handlers', () => { 67 | const handler = () => 'foobar'; 68 | 69 | it('should display the loaded state of the application', async () => { 70 | const mapBase = renderMapBase({ onMouseWheel: handler }); 71 | 72 | // Wait for implicit async state update in componentDidUpdate 73 | await act(async () => {}); 74 | 75 | const { container } = mapBase; 76 | expect(container.querySelector('#react-arcgis-fail-text')).to.not.exist; 77 | expect(container.querySelector('#react-arcgis-loading-text')).to.not.exist; 78 | }); 79 | 80 | it('should pass the event handler to the ArcGIS JS API', (done) => { 81 | setTimeout(() => { 82 | done(); 83 | }, 1); 84 | }); 85 | }); 86 | 87 | after(() => { 88 | global['asyncSuccess'] = false; 89 | global['generateMap'] = false; 90 | }); 91 | }); 92 | 93 | describe('loadMap returns a map which fails to load', () => { 94 | before(() => { 95 | global['asyncSuccess'] = true; 96 | global['generateMap'] = false; 97 | global['generateBrokenMap'] = true; 98 | }); 99 | 100 | it('should display the failed state of the application', async () => { 101 | const mapBase = renderMapBase(); 102 | 103 | // Wait for implicit async state update in componentDidUpdate 104 | await act(async () => {}); 105 | 106 | const { container } = mapBase; 107 | expect(container.querySelector('#react-arcgis-fail-text')).to.exist; 108 | expect(container.querySelector('#react-arcgis-loading-text')).to.not.exist; 109 | }); 110 | 111 | after(() => { 112 | global['asyncSuccess'] = false; 113 | global['generateMap'] = false; 114 | global['generateBrokenMap'] = false; 115 | }); 116 | }); 117 | 118 | describe('loadMap fails before the view is instantiated', () => { 119 | before(() => { 120 | global['asyncSuccess'] = true; 121 | global['generateMap'] = false; 122 | }); 123 | 124 | it('should display the failed state for the application', async () => { 125 | const mapBase = renderMapBase(); 126 | 127 | // Wait for implicit async state update in componentDidUpdate 128 | await act(async () => {}); 129 | 130 | const { container } = mapBase; 131 | expect(container.querySelector('#react-arcgis-fail-text')).to.exist; 132 | }); 133 | 134 | after(() => { 135 | global['asyncSuccess'] = false; 136 | global['generateMap'] = false; 137 | }); 138 | }); 139 | }); 140 | 141 | afterEach(() => { 142 | ArcView.prototype.componentDidMount['restore'](); 143 | }); 144 | }); 145 | }) 146 | ); 147 | 148 | export const WebBaseTests = () => ( 149 | describe('WebBase', () => { 150 | 151 | describe('as a shallow component', () => { 152 | it('should exist', async () => { 153 | const { container } = renderWebBase(); 154 | 155 | await act(async () => { 156 | expect(container.querySelector('#base-container')).to.exist; 157 | }); 158 | }); 159 | }); 160 | 161 | describe('as a mounted component', () => { 162 | beforeEach(() => { 163 | sinon.spy(ArcView.prototype, 'componentDidMount'); 164 | }); 165 | 166 | it('should call componentDidMount', async () => { 167 | renderWebBase(); 168 | 169 | await act(async () => { 170 | expect(ArcView.prototype.componentDidMount['callCount']).to.equal(1); 171 | }); 172 | }); 173 | 174 | describe('loadMap successfully creates the map and view', () => { 175 | before(() => { 176 | global['asyncSuccess'] = true; 177 | global['generateWebMap'] = true; 178 | }); 179 | 180 | it('should display the loaded state of the application', async () => { 181 | const webBase = renderWebBase(); 182 | 183 | // Wait for implicit async state update in componentDidUpdate 184 | await act(async () => {}); 185 | 186 | const { container } = webBase; 187 | expect(container.querySelector('#react-arcgis-fail-text')).to.not.exist; 188 | expect(container.querySelector('#react-arcgis-loading-text')).to.not.exist; 189 | }); 190 | 191 | describe('the user has included custom event handlers', () => { 192 | const handler = () => 'foobar'; 193 | 194 | it('should display the loaded state of the application', async () => { 195 | const webBase = renderWebBase({ onMouseWheel: handler }); 196 | 197 | // Wait for implicit async state update in componentDidUpdate 198 | await act(async () => {}); 199 | 200 | const { container } = webBase; 201 | expect(container.querySelector('#react-arcgis-fail-text')).to.not.exist; 202 | expect(container.querySelector('#react-arcgis-loading-text')).to.not.exist; 203 | }); 204 | 205 | it('should pass the event handler to the ArcGIS JS API', (done) => { 206 | setTimeout(() => { 207 | done(); 208 | }, 1); 209 | }); 210 | }); 211 | 212 | after(() => { 213 | global['asyncSuccess'] = false; 214 | global['generateWebMap'] = false; 215 | }); 216 | }); 217 | 218 | describe('loadMap returns a map which fails to load', () => { 219 | before(() => { 220 | global['asyncSuccess'] = true; 221 | global['generateBadWebMap'] = true; 222 | }); 223 | 224 | it('should display the failed state for the application', async () => { 225 | const webBase = renderWebBase(); 226 | 227 | // Wait for implicit async state update in componentDidUpdate 228 | await act(async () => {}); 229 | 230 | const { container} = webBase; 231 | expect(container.querySelector('#react-arcgis-fail-text')).to.exist; 232 | }); 233 | 234 | after(() => { 235 | global['asyncSuccess'] = false; 236 | global['generateBadWebMap'] = false; 237 | }); 238 | }); 239 | 240 | describe('loadMap fails before the view is instantiated', () => { 241 | before(() => { 242 | global['asyncSuccess'] = true; 243 | global['generateMap'] = false; 244 | }); 245 | 246 | it('should display the failed state for the application', async () => { 247 | const webBase = renderWebBase(); 248 | 249 | // Wait for implicit async state update in componentDidUpdate 250 | await act(async () => {}); 251 | 252 | const { container} = webBase; 253 | expect(container.querySelector('#react-arcgis-fail-text')).to.exist; 254 | }); 255 | 256 | after(() => { 257 | global['asyncSuccess'] = false; 258 | global['generateMap'] = false; 259 | }); 260 | }); 261 | 262 | afterEach(() => { 263 | ArcView.prototype.componentDidMount['restore'](); 264 | }); 265 | }); 266 | }) 267 | ); 268 | -------------------------------------------------------------------------------- /test/ts/components/MapComposites.spec.tsx: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import { render, act } from '@testing-library/react'; 3 | import { Map, Scene } from '../../../src/ts/components/MapComposites'; 4 | 5 | export const MapTests = () => ( 6 | describe('Map', () => { 7 | it('should exist', async () => { 8 | const { container } = render(); 9 | 10 | await act(() => { 11 | expect(container.querySelector('#base-container')).to.exist; 12 | }); 13 | }); 14 | }) 15 | ); 16 | 17 | export const SceneTests = () => ( 18 | describe('Scene', () => { 19 | it('should exist', async () => { 20 | const { container } = render(); 21 | 22 | await act(() => { 23 | expect(container.querySelector('#base-container')).to.exist; 24 | }); 25 | }); 26 | }) 27 | ); 28 | -------------------------------------------------------------------------------- /test/ts/components/WebComposites.spec.tsx: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import { render, act } from '@testing-library/react'; 3 | import { WebMap, WebScene } from '../../../src/ts/components/WebComposites'; 4 | 5 | export const WebMapTests = () => ( 6 | describe('WebMap', () => { 7 | it('should exist', async () => { 8 | const { container } = render(); 9 | 10 | await act(() => { 11 | expect(container.querySelector('#base-container')).to.exist; 12 | }); 13 | }); 14 | }) 15 | ); 16 | 17 | export const WebSceneTests = () => ( 18 | describe('WebScene', () => { 19 | it('should exist', async () => { 20 | const { container } = render(); 21 | 22 | await act(() => { 23 | expect(container.querySelector('#base-container')).to.exist; 24 | }); 25 | }); 26 | }) 27 | ); 28 | -------------------------------------------------------------------------------- /test/ts/test.tsx: -------------------------------------------------------------------------------- 1 | import { expect } from 'chai'; 2 | import { ArcView } from '../../src/ts/components/ArcBase'; 3 | 4 | export default () => ( 5 | describe('The test suite', () => { 6 | it('should make assertions', () => { 7 | expect(1).to.equal(1); 8 | }); 9 | 10 | it ('should have access to modules', () => { 11 | expect(ArcView).to.exist; 12 | }); 13 | }) 14 | ); 15 | -------------------------------------------------------------------------------- /test/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "sourceMap": true, 4 | "noImplicitAny": false, 5 | "module": "umd", 6 | "moduleResolution": "node", 7 | "target": "es5", 8 | "experimentalDecorators": true, 9 | "jsx": "react-jsx", 10 | "lib": [ 11 | "dom", 12 | "es5", 13 | "es2015.promise" 14 | ] 15 | }, 16 | "include": [ 17 | "./**/*" 18 | ] 19 | } -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "outDir": "./dist/esm", 4 | "declaration": true, 5 | "noImplicitAny": true, 6 | "module": "es2015", 7 | "moduleResolution": "node", 8 | "target": "es5", 9 | "jsx": "react-jsx", 10 | "lib": [ 11 | "dom", 12 | "es5", 13 | "es2015.promise" 14 | ], 15 | "esModuleInterop": true, 16 | "noFallthroughCasesInSwitch": true, 17 | "noImplicitReturns": true, 18 | "noImplicitThis": true, 19 | "noUnusedLocals": true, 20 | "noUnusedParameters": true, 21 | "sourceMap": true, 22 | "strict": true, 23 | "skipLibCheck": true 24 | }, 25 | "include": [ 26 | "./index.ts", 27 | "./src/**/*" 28 | ] 29 | } -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": ["tslint-react"], 3 | "defaultSeverity": "warning", 4 | "rules": { 5 | "align": [ 6 | true, 7 | "parameters", 8 | "arguments", 9 | "statements" 10 | ], 11 | "ban": false, 12 | "class-name": true, 13 | "comment-format": [ 14 | true, 15 | "check-space" 16 | ], 17 | "curly": true, 18 | "eofline": false, 19 | "forin": true, 20 | "indent": [ true, "spaces" ], 21 | "interface-name": [true, "never-prefix"], 22 | "jsdoc-format": true, 23 | "jsx-no-lambda": false, 24 | "jsx-no-multiline-js": false, 25 | "label-position": true, 26 | "max-line-length": [ true, 120 ], 27 | "member-ordering": true, 28 | "no-any": true, 29 | "no-arg": true, 30 | "no-bitwise": true, 31 | "no-console": [ 32 | true, 33 | "log", 34 | "error", 35 | "debug", 36 | "info", 37 | "time", 38 | "timeEnd", 39 | "trace" 40 | ], 41 | "no-consecutive-blank-lines": true, 42 | "no-construct": true, 43 | "no-debugger": true, 44 | "no-duplicate-variable": true, 45 | "no-empty": true, 46 | "no-eval": true, 47 | "no-shadowed-variable": true, 48 | "no-string-literal": true, 49 | "no-switch-case-fall-through": true, 50 | "no-trailing-whitespace": false, 51 | "no-unused-expression": true, 52 | "one-line": [ 53 | true, 54 | "check-catch", 55 | "check-else", 56 | "check-open-brace", 57 | "check-whitespace" 58 | ], 59 | "quotemark": [true, "single", "jsx-double"], 60 | "radix": true, 61 | "semicolon": [true, "always"], 62 | "switch-default": true, 63 | 64 | "trailing-comma": false, 65 | 66 | "triple-equals": [ true, "allow-null-check" ], 67 | "typedef": [ 68 | true, 69 | "parameter", 70 | "property-declaration" 71 | ], 72 | "typedef-whitespace": [ 73 | true, 74 | { 75 | "call-signature": "nospace", 76 | "index-signature": "nospace", 77 | "parameter": "nospace", 78 | "property-declaration": "nospace", 79 | "variable-declaration": "nospace" 80 | } 81 | ], 82 | "variable-name": [true, "ban-keywords", "check-format", "allow-leading-underscore", "allow-pascal-case"], 83 | "whitespace": [ 84 | true, 85 | "check-branch", 86 | "check-decl", 87 | "check-module", 88 | "check-operator", 89 | "check-separator", 90 | "check-type", 91 | "check-typecast" 92 | ] 93 | } 94 | } -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const webpack = require('webpack'); 2 | 3 | module.exports = { 4 | entry: { 5 | index: [ 6 | './src/ts/index.ts' 7 | ] 8 | }, 9 | output: { 10 | path: __dirname + '/dist/umd', 11 | filename: '[name].js', 12 | library: 'react-arcgis', 13 | libraryTarget: 'umd', 14 | umdNamedDefine: true 15 | }, 16 | externals: { 17 | 'react': { 18 | root: 'React', 19 | commonjs2: 'react', 20 | commonjs: 'react', 21 | amd: 'react' 22 | } 23 | }, 24 | devtool: 'source-map', 25 | resolve: { 26 | extensions: ['.webpack.js', '.web.js', '.ts', '.tsx', '.js', '.jsx'] 27 | }, 28 | module: { 29 | rules: [ 30 | { test: /\.tsx?$/, loader: 'ts-loader' }, 31 | { test: /\.js$/, loader: 'source-map-loader', enforce: 'pre' } 32 | ] 33 | } 34 | }; 35 | --------------------------------------------------------------------------------