github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/server/camlistored/ui/navigator_test.js (about) 1 /* 2 Copyright 2014 Google Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 goog.require('goog.Uri'); 18 goog.require('goog.events.EventTarget'); 19 var assert = require('assert'); 20 21 goog.require('cam.Navigator'); 22 23 var MockLocation = function() { 24 goog.base(this); 25 this.href = ''; 26 this.reloadCount = 0; 27 }; 28 goog.inherits(MockLocation, goog.events.EventTarget); 29 MockLocation.prototype.reload = function() { 30 this.reloadCount++; 31 }; 32 33 var MockHistory = function() { 34 this.states = []; 35 }; 36 MockHistory.prototype.pushState = function(a, b, url) { 37 this.states.push(url); 38 }; 39 40 var Handler = function() { 41 this.lastURL = null; 42 this.returnsTrue = false; 43 this.handle = this.handle.bind(this); 44 }; 45 Handler.prototype.handle = function(url) { 46 this.lastURL = url; 47 return this.returnsTrue; 48 }; 49 50 describe('cam.Navigator', function() { 51 var mockWindow, mockLocation, mockHistory, handler, navigator; 52 var url = new goog.Uri('http://www.camlistore.org/foobar'); 53 54 beforeEach(function() { 55 mockWindow = new goog.events.EventTarget(); 56 mockLocation = new MockLocation(); 57 mockHistory = new MockHistory(); 58 handler = new Handler(); 59 navigator = new cam.Navigator(mockWindow, mockLocation, mockHistory); 60 navigator.onNavigate = handler.handle; 61 }); 62 63 it('#navigate - no handler', function() { 64 // We should do network navigation. 65 navigator.onNavigate = function(){}; 66 navigator.navigate(url); 67 assert.equal(mockLocation.href, url.toString()); 68 assert.equal(mockHistory.states.length, 0); 69 }); 70 71 it('#navigate - handler returns false', function() { 72 // Both handlers should get called, we should do network navigation. 73 navigator.navigate(url); 74 assert.equal(handler.lastURL, url); 75 assert.equal(mockLocation.href, url.toString()); 76 assert.equal(mockHistory.states, 0); 77 }); 78 79 it('#navigate - handler returns true', function() { 80 // Both handlers should get called, we should do pushState() navigation. 81 handler.returnsTrue = true; 82 navigator.navigate(url); 83 assert.equal(handler.lastURL, url); 84 assert.equal(mockLocation.href, ''); 85 assert.deepEqual(mockHistory.states, [url.toString()]); 86 }); 87 88 it('#handleClick_ - handled', function() { 89 handler.returnsTrue = true; 90 var ev = new goog.events.Event('click'); 91 ev.button = 0; 92 ev.target = { 93 nodeName: 'A', 94 href: url.toString() 95 }; 96 mockWindow.dispatchEvent(ev); 97 assert.equal(mockLocation.href, ''); 98 assert.deepEqual(mockHistory.states, [url.toString()]); 99 }); 100 101 it('#handleClick_ - not handled', function() { 102 var ev = new goog.events.Event('click'); 103 ev.button = 0; 104 ev.target = { 105 nodeName: 'A', 106 href: url.toString() 107 }; 108 mockWindow.dispatchEvent(ev); 109 assert.equal(mockLocation.href, ''); 110 assert.deepEqual(mockHistory.states, []); 111 assert.equal(ev.defaultPrevented, false); 112 }); 113 114 it('#handlePopState_ - handled', function() { 115 handler.returnsTrue = true; 116 mockWindow.dispatchEvent('popstate'); 117 assert.equal(mockLocation.reloadCount, 0); 118 assert.deepEqual(mockHistory.states, []); 119 }); 120 121 it('#handlePopState_ - not handled', function() { 122 mockWindow.dispatchEvent('popstate'); 123 assert.equal(mockLocation.reloadCount, 1); 124 assert.deepEqual(mockHistory.states, []); 125 }); 126 });