test.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
  7. <title>JSAPI Loader</title>
  8. <style>
  9. html,
  10. body,
  11. #container {
  12. height: 100%;
  13. width: 100%;
  14. margin: 0;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="mocha"></div>
  20. <link rel="stylesheet" href="./libs/mocha.min.css" />
  21. <script src="./libs/chai.min.js"></script>
  22. <script src="./libs/mocha.min.js"></script>
  23. <script>
  24. mocha.setup('bdd');
  25. mocha.traceIgnores = ['mocha.min.js', 'chai.min.js'];
  26. expect = chai.expect;
  27. </script>
  28. <div id="container" tabindex="0"></div>
  29. <script src="../dist/index.js"></script>
  30. <script>
  31. // 请把 key 写在这里
  32. const JSAPIKey = '';
  33. const chai = window.chai;
  34. const mocha = window.mocha;
  35. mocha.setup({
  36. timeout: '5000',
  37. ui: 'bdd',
  38. });
  39. const expect = chai.expect;
  40. describe('JSAPI', () => {
  41. beforeEach(function () {
  42. AMapLoader.reset()
  43. });
  44. it('Simple', (done) => {
  45. AMapLoader.load({
  46. key: JSAPIKey,//首次load必填
  47. version: '2.0',
  48. plugins: ['AMap.Scale'],
  49. AMapUI: {
  50. plugins: ['overlay/SimpleMarker']
  51. }
  52. }).then(() => {
  53. expect(Boolean(window.AMap)).eq(true)
  54. expect(Boolean(window.AMapUI)).eq(true)
  55. expect(Boolean(window.AMapUI.SimpleMarker)).eq(true)
  56. expect(Boolean(window.Loca)).eq(false)
  57. done();
  58. });
  59. });
  60. it('Request with AMapUI Loca', (done) => {
  61. AMapLoader.load({
  62. key: JSAPIKey,//首次load必填
  63. version: '1.4.15',
  64. plugins: ['AMap.Scale'],
  65. AMapUI: {
  66. plugins: ['overlay/SimpleMarker']
  67. },
  68. Loca: {
  69. version: '1.3.2'
  70. }
  71. }).then(() => {
  72. expect(Boolean(window.AMap)).eq(true)
  73. expect(Boolean(window.AMapUI)).eq(true)
  74. expect(Boolean(window.AMapUI.SimpleMarker)).eq(true)
  75. expect(Boolean(window.Loca)).eq(true)
  76. done();
  77. });
  78. });
  79. it('fail with multi version', (done) => {
  80. AMapLoader.load({
  81. key: JSAPIKey,//首次load必填
  82. version: '2.0',
  83. }).then(() => {
  84. done()
  85. });
  86. AMapLoader.load({
  87. key: JSAPIKey,//首次load必填
  88. version: '1.4.15',
  89. }).then(() => {
  90. done(Error('should not here'))
  91. })
  92. .catch(err => {
  93. try {
  94. expect(err).eq('不允许多个版本 JSAPI 混用')
  95. } catch (error) {
  96. done(error)
  97. }
  98. })
  99. });
  100. it('fail with multi key', (done) => {
  101. AMapLoader.load({
  102. key: JSAPIKey,//首次load必填
  103. version: '2.0',
  104. }).then(() => {
  105. done()
  106. });
  107. AMapLoader.load({
  108. key: 'asdfas',//首次load必填
  109. }).then(() => {
  110. done(Error('should not here'))
  111. })
  112. .catch(err => {
  113. try {
  114. expect(err).eq('多个不一致的 key')
  115. } catch (error) {
  116. done(error)
  117. }
  118. })
  119. });
  120. })
  121. describe('AMapUI', () => {
  122. beforeEach(function () {
  123. AMapLoader.reset()
  124. });
  125. it('version', (done) => {
  126. AMapLoader.load({
  127. key: JSAPIKey,//首次load必填
  128. version: '1.4.15',
  129. AMapUI: {
  130. version: '1.1',
  131. plugins: ['misc/PathSimplifier']
  132. }
  133. }).then(() => {
  134. expect(Boolean(window.AMap)).eq(true)
  135. expect(Boolean(window.AMapUI)).eq(true)
  136. expect(Boolean(window.AMapUI.PathSimplifier)).eq(true)
  137. expect(Boolean(window.AMapUI.version.startsWith('1.1'))).eq(true)
  138. done();
  139. }).catch(done);
  140. });
  141. it('async', (done) => {
  142. console.log(window.AMap);
  143. AMapLoader.load({
  144. key: JSAPIKey,//首次load必填
  145. version: '1.4.15',
  146. AMapUI: {
  147. version: '1.0',
  148. plugins: ['overlay/SimpleMarker']
  149. }
  150. }).then(() => {
  151. AMapLoader.load({
  152. AMapUI: {
  153. plugins: ['overlay/SimpleInfoWindow']
  154. }
  155. }).then(() => {
  156. expect(Boolean(window.AMap)).eq(true)
  157. expect(Boolean(window.AMapUI.version.startsWith('1.0'))).eq(true)
  158. expect(Boolean(window.AMapUI.SimpleMarker)).eq(true)
  159. expect(Boolean(window.AMapUI.SimpleInfoWindow)).eq(true)
  160. done()
  161. });
  162. }).catch(done);
  163. })
  164. });
  165. describe('Loca', () => {
  166. beforeEach(function () {
  167. AMapLoader.reset()
  168. });
  169. it('version', (done) => {
  170. AMapLoader.load({
  171. key: JSAPIKey,//首次load必填
  172. version: '1.4.15',
  173. Loca: {
  174. version: '1.3.1'
  175. }
  176. }).then(() => {
  177. expect(Boolean(window.AMap)).eq(true)
  178. expect(Boolean(window.Loca)).eq(true)
  179. expect(window.Loca.version).eq('1.3.1')
  180. done()
  181. }).catch(done);
  182. });
  183. it('fail with jsapi2.0', (done) => {
  184. AMapLoader.load({
  185. key: JSAPIKey,//首次load必填
  186. version: '2.0',
  187. Loca: {
  188. version: '1.3.1'
  189. }
  190. }).then(() => {
  191. done(Error('should not here'))
  192. }).catch(err => {
  193. try {
  194. expect(err).eq("Loca 暂不适配 JSAPI 2.0,请使用 1.4.15")
  195. done()
  196. } catch (error) {
  197. done(error)
  198. }
  199. });
  200. });
  201. it('async', (done) => {
  202. AMapLoader.load({
  203. key: JSAPIKey,//首次load必填
  204. version: '1.4.15',
  205. Loca: {
  206. version: '1.2.1'
  207. }
  208. }).then(() => {
  209. expect(Boolean(window.AMap)).eq(true)
  210. expect(Boolean(window.Loca)).eq(true)
  211. expect(window.Loca.version).eq('1.2.1')
  212. AMapLoader.load({
  213. Loca: {
  214. version: '1.2.1'
  215. }
  216. }).then(() => {
  217. expect(Boolean(window.AMap)).eq(true)
  218. expect(Boolean(window.Loca)).eq(true)
  219. expect(window.Loca.version).eq('1.2.1')
  220. done()
  221. }).catch(done);
  222. });
  223. })
  224. });
  225. describe('Advance', () => {
  226. beforeEach(function () {
  227. AMapLoader.reset()
  228. });
  229. it('Multi', (done) => {
  230. AMapLoader.load({ key: JSAPIKey, version: '1.4.15' }).then(() => {
  231. expect(Boolean(window.AMap)).eq(true);
  232. expect(window.AMap.v).eq('1.4.15');
  233. expect(Boolean(window.AMapUI)).eq(false);
  234. expect(Boolean(window.Loca)).eq(false);
  235. AMapLoader.load({ AMapUI: {} }).then(() => {
  236. expect(Boolean(window.AMap)).eq(true);
  237. expect(window.AMap.v).eq('1.4.15');
  238. expect(Boolean(window.AMapUI)).eq(true);
  239. expect(window.AMapUI.version.startsWith('1.1')).eq(true);
  240. expect(Boolean(window.Loca)).eq(false);
  241. AMapLoader.load({ Loca: {} }).then(() => {
  242. expect(Boolean(window.AMap)).eq(true);
  243. expect(window.AMap.v).eq('1.4.15');
  244. expect(Boolean(window.AMapUI)).eq(true);
  245. expect(window.AMapUI.version.startsWith('1.1')).eq(true);
  246. expect(Boolean(window.Loca)).eq(true);
  247. expect(window.Loca.version).eq('1.3.2');
  248. done()
  249. })
  250. })
  251. }).catch(done)
  252. })
  253. })
  254. mocha.run()
  255. </script>
  256. </script>
  257. </body>
  258. </html>