1+ import { WebCacheLocation , getPath , createOriginUrl , getLocation } from './'
2+ import { LocationStrategy } from '@angular/common'
3+ import { expect } from 'chai'
4+ import * as sinon from 'sinon'
5+ describe ( 'WebCacheLocation' , ( ) => {
6+ global [ 'location' ] = {
7+ pathname : '' ,
8+ search : ''
9+ }
10+ const mockPlatformStrategy = {
11+ getBaseHref : ( ) :string => {
12+ return ''
13+ } ,
14+ onPopState : ( ) => { } ,
15+ back : ( ) => { } ,
16+ forward : ( ) => { } ,
17+ prepareExternalUrl : ( internal : string ) : string => {
18+ return ''
19+ } ,
20+ path : ( includeHash ?: any ) : string => {
21+ return ''
22+ } ,
23+ pushState : ( ) => {
24+
25+ } ,
26+ replaceState : ( ) => {
27+
28+ }
29+ }
30+ let testcache :WebCacheLocation
31+ beforeEach ( ( ) => {
32+ testcache = new WebCacheLocation ( mockPlatformStrategy as LocationStrategy )
33+ } )
34+ describe ( 'Normalize' , ( ) => {
35+ it ( 'should return empty string when provided with empty ending url path' , ( ) => {
36+ const testUrl = 'http://webcache.googleusercontent.local:3000/search?q=cache:https://www.angularclass.com/'
37+ const normalizedUrl = testcache . normalize ( testUrl )
38+ expect ( normalizedUrl ) . to . be . equal ( '' )
39+ } )
40+ it ( 'should return string value after ending url slash' , ( ) => {
41+ const testUrl = 'http://webcache.googleusercontent.local:3000/search?q=cache:https://www.angularclass.com'
42+ const normalizedUrl = testcache . normalize ( testUrl )
43+ expect ( normalizedUrl ) . to . be . equal ( '//www.angularclass.com' )
44+ } )
45+ it ( 'should return string value equal to supplied ending url path of .com' , ( ) => {
46+ const testUrl = 'http://webcache.googleusercontent.local:3000/search?q=cache:https://www.angularclass.com/courses'
47+ const normalizedUrl = testcache . normalize ( testUrl )
48+ expect ( normalizedUrl ) . to . be . equal ( '/courses' )
49+ } )
50+ it ( 'should return string value equal to supplied ending url path of .io' , ( ) => {
51+ const testUrl = 'http://webcache.googleusercontent.local:3000/search?q=cache:https://www.angularclass.io/courses'
52+ const normalizedUrl = testcache . normalize ( testUrl )
53+ expect ( normalizedUrl ) . to . be . equal ( '/courses' )
54+ } )
55+ it ( 'should return string value equal to supplied ending url path of .gg' , ( ) => {
56+ const testUrl = 'http://webcache.googleusercontent.local:3000/search?q=cache:https://www.angularclass.gg/courses'
57+ const normalizedUrl = testcache . normalize ( testUrl )
58+ expect ( normalizedUrl ) . to . be . equal ( '/courses' )
59+ } )
60+ it ( 'should return string value equal to supplied ending url path of .net' , ( ) => {
61+ const testUrl = 'http://webcache.googleusercontent.local:3000/search?q=cache:https://www.angularclass.net/courses'
62+ const normalizedUrl = testcache . normalize ( testUrl )
63+ expect ( normalizedUrl ) . to . be . equal ( '/courses' )
64+ } )
65+ it ( 'should return string value equal to supplied ending url path of .gov' , ( ) => {
66+ const testUrl = 'http://webcache.googleusercontent.local:3000/search?q=cache:https://www.angularclass.gov/courses'
67+ const normalizedUrl = testcache . normalize ( testUrl )
68+ expect ( normalizedUrl ) . to . be . equal ( '/courses' )
69+ } )
70+ it ( 'should return string value equal to supplied ending url path of .tv' , ( ) => {
71+ const testUrl = 'http://webcache.googleusercontent.local:3000/search?q=cache:https://www.angularclass.tv/courses'
72+ const normalizedUrl = testcache . normalize ( testUrl )
73+ expect ( normalizedUrl ) . to . be . equal ( '/courses' )
74+ } )
75+ it ( 'should return string value equal to supplied ending url path of appended url site ' , ( ) => {
76+ const testUrl = 'http://webcache.googleusercontent.local:3000/search?q=cache:https://www.blog.angularclass.gov/courses'
77+ const normalizedUrl = testcache . normalize ( testUrl )
78+ expect ( normalizedUrl ) . to . be . equal ( '/courses' )
79+ } )
80+ it ( 'should return string value equal to supplied ending url path of query' , ( ) => {
81+ const testUrl = 'http://webcache.googleusercontent.local:3000/search?q=cache:https://www.blog.angularclass.com/search?aB32*#sj'
82+ const normalizedUrl = testcache . normalize ( testUrl )
83+ expect ( normalizedUrl ) . to . be . equal ( '/search?aB32*#sj' )
84+ } )
85+ } )
86+ describe ( 'Go' , ( ) => {
87+ it ( 'should update location pathname with input path' , ( ) => {
88+ global [ 'location' ] = {
89+ pathname : 'http://webcache.googleusercontent.local:3000/search?q=cache:https://www.angularclass.com/' ,
90+ search : ''
91+ }
92+
93+ const testPath = 'courses'
94+ const pushStateStub = sinon . stub ( mockPlatformStrategy , 'pushState' )
95+ . callsFake ( ( ...args ) => { } ) ;
96+
97+ testcache . go ( testPath )
98+ sinon . assert . calledWith ( pushStateStub , null , '' , `http://webcache.googleusercontent.local:3000/search?q=cache:https://www.angularclass.com/${ testPath } ` , '' )
99+ pushStateStub . restore ( )
100+ } )
101+
102+ it ( 'should update location pathname with input path special characters' , ( ) => {
103+ global [ 'location' ] = {
104+ pathname : 'http://webcache.googleusercontent.local:3000/search?q=cache:https://www.angularclass.com/' ,
105+ search : ''
106+ }
107+
108+ const testPath = 'courses/test123'
109+ const pushStateStub = sinon . stub ( mockPlatformStrategy , 'pushState' )
110+ . callsFake ( ( ...args ) => { } ) ;
111+
112+ testcache . go ( testPath )
113+ sinon . assert . calledWith ( pushStateStub , null , '' , `http://webcache.googleusercontent.local:3000/search?q=cache:https://www.angularclass.com/${ testPath } ` , '' )
114+ pushStateStub . restore ( )
115+ } )
116+
117+ } )
118+
119+ describe ( 'ReplaceState' , ( ) => {
120+ it ( 'should update location pathname with input path' , ( ) => {
121+ global [ 'location' ] = {
122+ pathname : 'http://webcache.googleusercontent.local:3000/search?q=cache:https://www.angularclass.com/' ,
123+ search : ''
124+ }
125+
126+ const testPath = 'courses'
127+ const replaceStateStub = sinon . stub ( mockPlatformStrategy , 'replaceState' )
128+ . callsFake ( ( ...args ) => { } ) ;
129+
130+ testcache . replaceState ( testPath )
131+ sinon . assert . calledWith ( replaceStateStub , null , '' , `http://webcache.googleusercontent.local:3000/search?q=cache:https://www.angularclass.com/${ testPath } ` , '' )
132+ replaceStateStub . restore ( )
133+ } )
134+
135+ it ( 'should update location pathname with input path special characters' , ( ) => {
136+ global [ 'location' ] = {
137+ pathname : 'http://webcache.googleusercontent.local:3000/search?q=cache:https://www.angularclass.com/' ,
138+ search : ''
139+ }
140+
141+ const testPath = 'courses/test123'
142+ const replaceStateStub = sinon . stub ( mockPlatformStrategy , 'replaceState' )
143+ . callsFake ( ( ...args ) => { } ) ;
144+
145+ testcache . replaceState ( testPath )
146+ sinon . assert . calledWith ( replaceStateStub , null , '' , `http://webcache.googleusercontent.local:3000/search?q=cache:https://www.angularclass.com/${ testPath } ` , '' )
147+ replaceStateStub . restore ( )
148+ } )
149+ } )
150+ } )
0 commit comments