Skip to content
This repository was archived by the owner on Feb 17, 2024. It is now read-only.

Commit 293f773

Browse files
committed
💣 Added test files. Included build tools.
1 parent 5acd2a2 commit 293f773

11 files changed

+232
-30
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ node_modules
22
*.log
33
*.tmp
44
DS*
5+
.DS_Store
56
typings
67
dist
78
.vscode

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
"version": "0.0.1",
44
"description": "Starter to make Angular libs",
55
"main": "bundle.js",
6-
"jsnext:main": "lib.module.js",
7-
"module": "lib.module.js",
6+
"jsnext:main": "angular-web-cached-view.module.js",
7+
"module": "angular-web-cached-view.module.js",
88
"types": "index.d.ts",
99
"scripts": {
1010
"test": "rimraf ./tmp/mocha-webpack && mocha-webpack --opts 'mocha.opts'",
@@ -30,6 +30,7 @@
3030
"@types/sinon": "^2.2.2",
3131
"angular2-template-loader": "^0.6.2",
3232
"awesome-typescript-loader": "^3.1.3",
33+
"chai": "^3.5.0",
3334
"core-js": "^2.4.1",
3435
"jsdom": "^10.1.0",
3536
"mocha": "^3.4.1",
@@ -38,6 +39,7 @@
3839
"raw-loader": "^0.5.1",
3940
"rimraf": "^2.6.1",
4041
"rxjs": "^5.4.0",
42+
"sinon": "^2.2.0",
4143
"source-map-support": "^0.4.15",
4244
"typescript": "^2.3.2",
4345
"webpack": "^2.5.1",

src/angular-web-cached-view.module.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Location, LocationStrategy } from '@angular/common'
2+
import { NgModule } from '@angular/core'
3+
import { getLocation } from './angular-web-cached-view.service'
4+
5+
@NgModule({})
6+
export class WebCachedModule {
7+
static forRoot() {
8+
return {
9+
ngModule: WebCachedModule,
10+
providers: [
11+
{
12+
provide: Location,
13+
useFactory: getLocation,
14+
deps: [ LocationStrategy ]
15+
}
16+
]
17+
}
18+
}
19+
}
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { Location, LocationStrategy } from '@angular/common'
2+
import { Injectable } from '@angular/core'
3+
4+
export const GOOGLE_WEBCACHE_URL = /(.+)(q=cache:(https?:\/\/)?(.+?)\/)(.*?)(&.+|$|\+&.+|\+)/;
5+
export const GOOGLE_WEBCACHE = /webcache/;
6+
7+
export function createOriginUrl(url: string) {
8+
const _url = url.replace(/^\//, '');
9+
const originUrl = location.pathname + location.search;
10+
return originUrl.replace(GOOGLE_WEBCACHE_URL, '$1$2' + _url + '$6');
11+
}
12+
13+
export function getPath (url: string) {
14+
return url
15+
.replace(/%3A/g, ':')
16+
.replace(/%2F/g, '/')
17+
.replace(GOOGLE_WEBCACHE_URL, '/$5');
18+
}
19+
20+
@Injectable()
21+
export class WebCacheLocation extends Location {
22+
_platformStrategy: LocationStrategy;
23+
constructor(platformStrategy: LocationStrategy) {
24+
super(platformStrategy);
25+
}
26+
27+
normalize(url: string) {
28+
const _url = getPath(url);
29+
return super.normalize(_url);
30+
}
31+
32+
go(path: string, query: string = '') {
33+
const url = createOriginUrl(path);
34+
this._platformStrategy.pushState(null, '', url, query);
35+
}
36+
37+
replaceState(path: string, query: string = '') {
38+
const url = createOriginUrl(path);
39+
this._platformStrategy.replaceState(null, '', url, query);
40+
}
41+
42+
}
43+
44+
export function getLocation(platformStrategy: any) {
45+
const host = location.host;
46+
if (GOOGLE_WEBCACHE.test(host)) {
47+
return new WebCacheLocation(platformStrategy);
48+
}
49+
return new Location(platformStrategy);
50+
}
51+
52+
function webCacheUrl(url: string) {
53+
if (!GOOGLE_WEBCACHE.test(url)) { return url; }
54+
return getPath(url);
55+
}

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export * from './lib.module'
1+
export * from './angular-web-cached-view.service'
2+
export * from './angular-web-cached-view.module'

src/lib.component.html

-3
This file was deleted.

src/lib.component.ts

-7
This file was deleted.

src/lib.module.ts

-11
This file was deleted.

src/lib.spec.ts

-5
This file was deleted.

src/test.spec.ts

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
})

webpack.testing.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const config = {
88
extensions: ['.ts', '.js', '.html', '.scss', '.css']
99
},
1010
resolveLoader: {
11-
moduleExtensions: ['-loader']
11+
moduleExtensions: ['-loader']
1212
},
1313
target: 'node',
1414
externals: [nodeExternals()],

0 commit comments

Comments
 (0)