a zip code crypto-currency system good for red ONLY

mock-providers.js 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. import { NgZone } from '@angular/core';
  2. import { App } from '../components/app/app';
  3. import { Config } from '../config/config';
  4. import { Content } from '../components/content/content';
  5. import { DeepLinker } from '../navigation/deep-linker';
  6. import { DomController } from '../platform/dom-controller';
  7. import { GestureController } from '../gestures/gesture-controller';
  8. import { Haptic } from '../tap-click/haptic';
  9. import { IonicApp } from '../components/app/app-root';
  10. import { Menu } from '../components/menu/menu';
  11. import { NavControllerBase } from '../navigation/nav-controller-base';
  12. import { OverlayPortal } from '../components/app/overlay-portal';
  13. import { PageTransition } from '../transitions/page-transition';
  14. import { Platform } from '../platform/platform';
  15. import { QueryParams } from '../platform/query-params';
  16. import { Tab } from '../components/tabs/tab';
  17. import { Tabs } from '../components/tabs/tabs';
  18. import { TransitionController } from '../transitions/transition-controller';
  19. import { UrlSerializer } from '../navigation/url-serializer';
  20. import { ViewController } from '../navigation/view-controller';
  21. import { ModuleLoader } from './module-loader';
  22. import { NgModuleLoader } from './ng-module-loader';
  23. import { STATE_INITIALIZED } from '../navigation/nav-util';
  24. import { Ion } from '../components/ion';
  25. import { Item } from '../components/item/item';
  26. import { Form } from './form';
  27. export function mockConfig(config, _url = '/', platform) {
  28. const c = new Config();
  29. const p = platform || mockPlatform();
  30. c.init(config, p);
  31. return c;
  32. }
  33. export function mockQueryParams(url = '/') {
  34. const qp = new QueryParams();
  35. qp.parseUrl(url);
  36. return qp;
  37. }
  38. export function mockPlatform() {
  39. return new MockPlatform();
  40. }
  41. export class MockPlatform extends Platform {
  42. constructor() {
  43. super();
  44. this.timeoutIds = 0;
  45. this.timeouts = [];
  46. this.rafIds = 0;
  47. this.timeStamps = 0;
  48. this.rafs = [];
  49. const doc = document.implementation.createHTMLDocument('');
  50. this.setWindow(window);
  51. this.setDocument(doc);
  52. this.setCssProps(doc.documentElement);
  53. }
  54. timeout(callback, timeout) {
  55. const timeoutId = ++this.timeoutIds;
  56. this.timeouts.push({
  57. callback: callback,
  58. timeout: timeout,
  59. timeoutId: timeoutId
  60. });
  61. return timeoutId;
  62. }
  63. cancelTimeout(timeoutId) {
  64. for (var i = 0; i < this.timeouts.length; i++) {
  65. if (timeoutId === this.timeouts[i].timeoutId) {
  66. this.timeouts.splice(i, 1);
  67. break;
  68. }
  69. }
  70. }
  71. flushTimeouts(done) {
  72. setTimeout(() => {
  73. this.timeouts.sort(function (a, b) {
  74. if (a.timeout < b.timeout)
  75. return -1;
  76. if (a.timeout > b.timeout)
  77. return 1;
  78. return 0;
  79. }).forEach(t => {
  80. t.callback();
  81. });
  82. this.timeouts.length = 0;
  83. done();
  84. });
  85. }
  86. flushTimeoutsUntil(timeout, done) {
  87. setTimeout(() => {
  88. this.timeouts.sort(function (a, b) {
  89. if (a.timeout < b.timeout)
  90. return -1;
  91. if (a.timeout > b.timeout)
  92. return 1;
  93. return 0;
  94. });
  95. const keepers = [];
  96. this.timeouts.forEach(t => {
  97. if (t.timeout < timeout) {
  98. t.callback();
  99. }
  100. else {
  101. keepers.push(t);
  102. }
  103. });
  104. this.timeouts = keepers;
  105. done();
  106. });
  107. }
  108. raf(callback) {
  109. const rafId = ++this.rafIds;
  110. this.rafs.push({
  111. callback: callback,
  112. rafId: rafId
  113. });
  114. return rafId;
  115. }
  116. cancelRaf(rafId) {
  117. for (var i = 0; i < this.rafs.length; i++) {
  118. if (rafId === this.rafs[i].rafId) {
  119. this.rafs.splice(i, 1);
  120. break;
  121. }
  122. }
  123. }
  124. flushRafs(done) {
  125. const timestamp = ++this.timeStamps;
  126. setTimeout(() => {
  127. this.rafs.forEach(raf => {
  128. raf.callback(timestamp);
  129. });
  130. this.rafs.length = 0;
  131. done(timestamp);
  132. });
  133. }
  134. }
  135. export function mockDomController(platform) {
  136. platform = platform || mockPlatform();
  137. return new MockDomController(platform);
  138. }
  139. export class MockDomController extends DomController {
  140. constructor(mockedPlatform) {
  141. super(mockedPlatform);
  142. this.mockedPlatform = mockedPlatform;
  143. }
  144. flush(done) {
  145. this.mockedPlatform.flushTimeouts(() => {
  146. this.mockedPlatform.flushRafs((timeStamp) => {
  147. done(timeStamp);
  148. });
  149. });
  150. }
  151. flushUntil(timeout, done) {
  152. this.mockedPlatform.flushTimeoutsUntil(timeout, () => {
  153. this.mockedPlatform.flushRafs((timeStamp) => {
  154. done(timeStamp);
  155. });
  156. });
  157. }
  158. }
  159. export function mockApp(config, platform) {
  160. platform = platform || mockPlatform();
  161. config = config || mockConfig(null, '/', platform);
  162. let app = new App(config, platform);
  163. mockIonicApp(app, config, platform);
  164. return app;
  165. }
  166. export function mockIonicApp(app, config, plt) {
  167. let appRoot = new IonicApp(null, null, mockElementRef(), mockRenderer(), config, plt, app);
  168. appRoot._loadingPortal = mockOverlayPortal(app, config, plt);
  169. appRoot._toastPortal = mockOverlayPortal(app, config, plt);
  170. appRoot._overlayPortal = mockOverlayPortal(app, config, plt);
  171. appRoot._modalPortal = mockOverlayPortal(app, config, plt);
  172. return appRoot;
  173. }
  174. export const mockTrasitionController = function (config) {
  175. let platform = mockPlatform();
  176. platform.raf = function (callback) {
  177. callback();
  178. };
  179. let trnsCtrl = new TransitionController(platform, config);
  180. trnsCtrl.get = (trnsId, enteringView, leavingView, opts) => {
  181. let trns = new PageTransition(platform, enteringView, leavingView, opts);
  182. trns.trnsId = trnsId;
  183. return trns;
  184. };
  185. return trnsCtrl;
  186. };
  187. export function mockContent() {
  188. const platform = mockPlatform();
  189. return new Content(mockConfig(), platform, mockDomController(platform), mockElementRef(), mockRenderer(), null, null, mockZone(), null, null);
  190. }
  191. export function mockZone() {
  192. return new NgZone({ enableLongStackTrace: false });
  193. }
  194. export function mockChangeDetectorRef() {
  195. let cd = {
  196. reattach: () => { },
  197. detach: () => { },
  198. detectChanges: () => { }
  199. };
  200. return cd;
  201. }
  202. export function mockGestureController(app) {
  203. if (!app) {
  204. app = mockApp();
  205. }
  206. return new GestureController(app);
  207. }
  208. export class MockElementRef {
  209. constructor(ele) {
  210. this.nativeElement = ele;
  211. }
  212. }
  213. export class MockElement {
  214. constructor() {
  215. this.children = [];
  216. this.classList = new ClassList();
  217. this.attributes = {};
  218. this.style = {};
  219. this.nodeName = 'ION-MOCK';
  220. this.clientWidth = 0;
  221. this.clientHeight = 0;
  222. this.clientTop = 0;
  223. this.clientLeft = 0;
  224. this.offsetWidth = 0;
  225. this.offsetHeight = 0;
  226. this.offsetTop = 0;
  227. this.offsetLeft = 0;
  228. this.scrollTop = 0;
  229. this.scrollHeight = 0;
  230. }
  231. get className() {
  232. return this.classList.classes.join(' ');
  233. }
  234. set className(val) {
  235. this.classList.classes = val.split(' ');
  236. }
  237. hasAttribute(name) {
  238. return !!this.attributes[name];
  239. }
  240. getAttribute(name) {
  241. return this.attributes[name];
  242. }
  243. setAttribute(name, val) {
  244. this.attributes[name] = val;
  245. }
  246. addEventListener(_type, _listener, _options) { }
  247. removeEventListener(_type, _listener, _options) { }
  248. removeAttribute(name) {
  249. delete this.attributes[name];
  250. }
  251. }
  252. export class ClassList {
  253. constructor() {
  254. this.classes = [];
  255. }
  256. add(className) {
  257. if (!this.contains(className)) {
  258. this.classes.push(className);
  259. }
  260. }
  261. remove(className) {
  262. const index = this.classes.indexOf(className);
  263. if (index > -1) {
  264. this.classes.splice(index, 1);
  265. }
  266. }
  267. toggle(className) {
  268. if (this.contains(className)) {
  269. this.remove(className);
  270. }
  271. else {
  272. this.add(className);
  273. }
  274. }
  275. contains(className) {
  276. return this.classes.indexOf(className) > -1;
  277. }
  278. }
  279. export function mockElementRef() {
  280. return new MockElementRef(new MockElement());
  281. }
  282. export function mockElementRefEle(ele) {
  283. return new MockElementRef(ele);
  284. }
  285. export class MockRenderer {
  286. setElementAttribute(renderElement, name, val) {
  287. if (name === null) {
  288. renderElement.removeAttribute(name);
  289. }
  290. else {
  291. renderElement.setAttribute(name, val);
  292. }
  293. }
  294. setElementClass(renderElement, className, isAdd) {
  295. if (isAdd) {
  296. renderElement.classList.add(className);
  297. }
  298. else {
  299. renderElement.classList.remove(className);
  300. }
  301. }
  302. setElementStyle(renderElement, styleName, styleValue) {
  303. renderElement.style[styleName] = styleValue;
  304. }
  305. }
  306. export function mockRenderer() {
  307. const renderer = new MockRenderer();
  308. return renderer;
  309. }
  310. export function mockLocation() {
  311. let location = {
  312. path: () => { return ''; },
  313. subscribe: () => { },
  314. go: () => { },
  315. back: () => { },
  316. prepareExternalUrl: () => { }
  317. };
  318. return location;
  319. }
  320. export function mockView(component, data) {
  321. if (!component) {
  322. component = MockView;
  323. }
  324. let view = new ViewController(component, data);
  325. view.init(mockComponentRef());
  326. return view;
  327. }
  328. export function mockViews(nav, views) {
  329. nav._views = views;
  330. views.forEach(v => {
  331. v._setNav(nav);
  332. });
  333. }
  334. export function mockComponentRef() {
  335. let componentRef = {
  336. location: mockElementRef(),
  337. changeDetectorRef: mockChangeDetectorRef(),
  338. destroy: () => { }
  339. };
  340. return componentRef;
  341. }
  342. export function mockDeepLinker(linkConfig = null, app) {
  343. app = app || mockApp(mockConfig(), mockPlatform());
  344. let serializer = new UrlSerializer(app, linkConfig);
  345. let location = mockLocation();
  346. return new DeepLinker(app || mockApp(), serializer, location, null, null);
  347. }
  348. export function mockNavController() {
  349. let platform = mockPlatform();
  350. let config = mockConfig(null, '/', platform);
  351. let app = mockApp(config, platform);
  352. let zone = mockZone();
  353. let dom = mockDomController(platform);
  354. let elementRef = mockElementRef();
  355. let renderer = mockRenderer();
  356. let componentFactoryResolver = null;
  357. let gestureCtrl = new GestureController(app);
  358. let linker = mockDeepLinker(null, app);
  359. let trnsCtrl = mockTrasitionController(config);
  360. let nav = new NavControllerBase(null, app, config, platform, elementRef, zone, renderer, componentFactoryResolver, gestureCtrl, trnsCtrl, linker, dom, null);
  361. nav._viewInit = function (enteringView) {
  362. enteringView.init(mockComponentRef());
  363. enteringView._state = STATE_INITIALIZED;
  364. };
  365. nav._orgViewInsert = nav._viewAttachToDOM;
  366. nav._viewAttachToDOM = function (view, componentRef, _viewport) {
  367. let mockedViewport = {
  368. insert: () => { }
  369. };
  370. nav._orgViewInsert(view, componentRef, mockedViewport);
  371. };
  372. return nav;
  373. }
  374. export function mockOverlayPortal(app, config, plt) {
  375. let zone = mockZone();
  376. let dom = mockDomController(plt);
  377. let elementRef = mockElementRef();
  378. let renderer = mockRenderer();
  379. let componentFactoryResolver = null;
  380. let gestureCtrl = new GestureController(app);
  381. let serializer = new UrlSerializer(app, null);
  382. let location = mockLocation();
  383. let deepLinker = new DeepLinker(app, serializer, location, null, null);
  384. return new OverlayPortal(app, config, plt, elementRef, zone, renderer, componentFactoryResolver, gestureCtrl, null, deepLinker, null, dom, null);
  385. }
  386. export function mockTab(parentTabs, overrideLoad = true) {
  387. let platform = mockPlatform();
  388. let config = mockConfig(null, '/', platform);
  389. let app = parentTabs._app || mockApp(config, platform);
  390. let zone = mockZone();
  391. let dom = mockDomController(platform);
  392. let elementRef = mockElementRef();
  393. let renderer = mockRenderer();
  394. let changeDetectorRef = mockChangeDetectorRef();
  395. let compiler = null;
  396. let gestureCtrl = new GestureController(app);
  397. let linker = mockDeepLinker(null, app);
  398. let tab = new Tab(parentTabs, app, config, platform, elementRef, zone, renderer, compiler, changeDetectorRef, gestureCtrl, null, linker, dom, null);
  399. if (overrideLoad) {
  400. tab.load = (_opts) => {
  401. return Promise.resolve();
  402. };
  403. }
  404. return tab;
  405. }
  406. export function mockForm() {
  407. return new Form();
  408. }
  409. export function mockIon() {
  410. const config = mockConfig();
  411. const elementRef = mockElementRef();
  412. const renderer = mockRenderer();
  413. return new Ion(config, elementRef, renderer, 'ion');
  414. }
  415. export function mockItem() {
  416. const form = mockForm();
  417. const config = mockConfig();
  418. const elementRef = mockElementRef();
  419. const renderer = mockRenderer();
  420. return new Item(form, config, elementRef, renderer, null);
  421. }
  422. export function mockTabs(app) {
  423. let platform = mockPlatform();
  424. let config = mockConfig(null, '/', platform);
  425. app = app || mockApp(config, platform);
  426. let elementRef = mockElementRef();
  427. let renderer = mockRenderer();
  428. let linker = mockDeepLinker();
  429. return new Tabs(null, null, app, config, elementRef, platform, renderer, linker);
  430. }
  431. export function mockMenu(platform = null) {
  432. let app = mockApp();
  433. let gestureCtrl = new GestureController(app);
  434. let dom = mockDomController();
  435. let elementRef = mockElementRef();
  436. let renderer = mockRenderer();
  437. let plt = platform === null ? mockPlatform() : platform;
  438. return new Menu(null, elementRef, null, plt, renderer, null, gestureCtrl, dom, app);
  439. }
  440. export function mockDeepLinkConfig(links) {
  441. return {
  442. links: links || [
  443. { component: MockView1, name: 'viewone' },
  444. { component: MockView2, name: 'viewtwo' },
  445. { component: MockView3, name: 'viewthree' },
  446. { component: MockView4, name: 'viewfour' },
  447. { component: MockView5, name: 'viewfive' }
  448. ]
  449. };
  450. }
  451. export function mockHaptic() {
  452. return new Haptic(mockPlatform());
  453. }
  454. export class MockView {
  455. }
  456. export class MockView1 {
  457. }
  458. export class MockView2 {
  459. }
  460. export class MockView3 {
  461. }
  462. export class MockView4 {
  463. }
  464. export class MockView5 {
  465. }
  466. export function noop() { return 'noop'; }
  467. export function mockModuleLoader(ngModuleLoader) {
  468. ngModuleLoader = ngModuleLoader || mockNgModuleLoader();
  469. return new ModuleLoader(ngModuleLoader, null);
  470. }
  471. export function mockNgModuleLoader() {
  472. return new NgModuleLoader(null);
  473. }
  474. export function mockOverlay() {
  475. return {
  476. present: (_opts) => { return Promise.resolve(); },
  477. dismiss: (_data, _role, _navOptions) => { return Promise.resolve(); },
  478. onDidDismiss: (_callback) => { },
  479. onWillDismiss: (_callback) => { }
  480. };
  481. }
  482. //# sourceMappingURL=mock-providers.js.map