Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
U
utils
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
EIT-SWE
ux
utils
Commits
845be4e2
Commit
845be4e2
authored
Dec 10, 2019
by
Matt Teeter
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(browser, rx, security): incorporate browser-utils, rx-utils, and a part of angular-security
parent
c5e07045
Pipeline
#73981
failed with stages
in 7 minutes and 22 seconds
Changes
56
Pipelines
1
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
56 changed files
with
2127 additions
and
376 deletions
+2127
-376
angular.json
angular.json
+90
-9
libs/.gitkeep
libs/.gitkeep
+0
-0
libs/utils/README.md
libs/utils/README.md
+20
-4
libs/utils/browser/README.md
libs/utils/browser/README.md
+71
-0
libs/utils/browser/jest.config.js
libs/utils/browser/jest.config.js
+9
-0
libs/utils/browser/ng-package.json
libs/utils/browser/ng-package.json
+5
-0
libs/utils/browser/src/index.ts
libs/utils/browser/src/index.ts
+13
-0
libs/utils/browser/src/lib/cache/cache.interceptor.spec.ts
libs/utils/browser/src/lib/cache/cache.interceptor.spec.ts
+105
-0
libs/utils/browser/src/lib/cache/cache.interceptor.ts
libs/utils/browser/src/lib/cache/cache.interceptor.ts
+21
-0
libs/utils/browser/src/lib/focus/focus.directive.spec.ts
libs/utils/browser/src/lib/focus/focus.directive.spec.ts
+50
-0
libs/utils/browser/src/lib/focus/focus.directive.ts
libs/utils/browser/src/lib/focus/focus.directive.ts
+15
-0
libs/utils/browser/src/lib/focus/focus.module.ts
libs/utils/browser/src/lib/focus/focus.module.ts
+10
-0
libs/utils/browser/src/lib/http-error/http-error.service.spec.ts
...ils/browser/src/lib/http-error/http-error.service.spec.ts
+131
-0
libs/utils/browser/src/lib/http-error/http-error.service.ts
libs/utils/browser/src/lib/http-error/http-error.service.ts
+19
-0
libs/utils/browser/src/lib/loading-screen/loading-events.ts
libs/utils/browser/src/lib/loading-screen/loading-events.ts
+31
-0
libs/utils/browser/src/lib/local-storage/local-storage.service.spec.ts
...owser/src/lib/local-storage/local-storage.service.spec.ts
+98
-0
libs/utils/browser/src/lib/local-storage/local-storage.service.ts
...ls/browser/src/lib/local-storage/local-storage.service.ts
+129
-0
libs/utils/browser/src/lib/tracing/request-tracing.config.ts
libs/utils/browser/src/lib/tracing/request-tracing.config.ts
+7
-0
libs/utils/browser/src/lib/tracing/request-tracing.interceptor.spec.ts
...owser/src/lib/tracing/request-tracing.interceptor.spec.ts
+82
-0
libs/utils/browser/src/lib/tracing/request-tracing.interceptor.ts
...ls/browser/src/lib/tracing/request-tracing.interceptor.ts
+47
-0
libs/utils/browser/src/lib/url-builder/url-builder.spec.ts
libs/utils/browser/src/lib/url-builder/url-builder.spec.ts
+131
-0
libs/utils/browser/src/lib/url-builder/url-builder.ts
libs/utils/browser/src/lib/url-builder/url-builder.ts
+20
-0
libs/utils/browser/src/lib/window/window.service.spec.ts
libs/utils/browser/src/lib/window/window.service.spec.ts
+14
-0
libs/utils/browser/src/lib/window/window.service.ts
libs/utils/browser/src/lib/window/window.service.ts
+23
-0
libs/utils/browser/src/test-setup.ts
libs/utils/browser/src/test-setup.ts
+1
-0
libs/utils/browser/tsconfig.json
libs/utils/browser/tsconfig.json
+7
-0
libs/utils/browser/tsconfig.lib.json
libs/utils/browser/tsconfig.lib.json
+20
-0
libs/utils/browser/tsconfig.spec.json
libs/utils/browser/tsconfig.spec.json
+10
-0
libs/utils/browser/tslint.json
libs/utils/browser/tslint.json
+7
-0
libs/utils/rx/README.md
libs/utils/rx/README.md
+11
-0
libs/utils/rx/jest.config.js
libs/utils/rx/jest.config.js
+9
-0
libs/utils/rx/ng-package.json
libs/utils/rx/ng-package.json
+5
-0
libs/utils/rx/src/index.ts
libs/utils/rx/src/index.ts
+2
-0
libs/utils/rx/src/lib/http-backoff.ts
libs/utils/rx/src/lib/http-backoff.ts
+64
-0
libs/utils/rx/src/lib/reducer-utils.spec.ts
libs/utils/rx/src/lib/reducer-utils.spec.ts
+11
-0
libs/utils/rx/src/lib/reducer-utils.ts
libs/utils/rx/src/lib/reducer-utils.ts
+9
-0
libs/utils/rx/src/test-setup.ts
libs/utils/rx/src/test-setup.ts
+1
-0
libs/utils/rx/tsconfig.json
libs/utils/rx/tsconfig.json
+7
-0
libs/utils/rx/tsconfig.lib.json
libs/utils/rx/tsconfig.lib.json
+20
-0
libs/utils/rx/tsconfig.spec.json
libs/utils/rx/tsconfig.spec.json
+10
-0
libs/utils/rx/tslint.json
libs/utils/rx/tslint.json
+7
-0
libs/utils/security/README.md
libs/utils/security/README.md
+3
-0
libs/utils/security/jest.config.js
libs/utils/security/jest.config.js
+9
-0
libs/utils/security/ng-package.json
libs/utils/security/ng-package.json
+5
-0
libs/utils/security/src/index.ts
libs/utils/security/src/index.ts
+1
-0
libs/utils/security/src/lib/constants.spec.ts
libs/utils/security/src/lib/constants.spec.ts
+5
-0
libs/utils/security/src/lib/constants.ts
libs/utils/security/src/lib/constants.ts
+1
-0
libs/utils/security/src/test-setup.ts
libs/utils/security/src/test-setup.ts
+1
-0
libs/utils/security/tsconfig.json
libs/utils/security/tsconfig.json
+7
-0
libs/utils/security/tsconfig.lib.json
libs/utils/security/tsconfig.lib.json
+20
-0
libs/utils/security/tsconfig.spec.json
libs/utils/security/tsconfig.spec.json
+10
-0
libs/utils/security/tslint.json
libs/utils/security/tslint.json
+7
-0
nx.json
nx.json
+9
-0
package.json
package.json
+39
-35
tsconfig.json
tsconfig.json
+4
-1
yarn.lock
yarn.lock
+664
-327
No files found.
angular.json
View file @
845be4e2
...
...
@@ -73,10 +73,7 @@
"lint"
:
{
"builder"
:
"@angular-devkit/build-angular:tslint"
,
"options"
:
{
"tsConfig"
:
[
"apps/demo/tsconfig.app.json"
,
"apps/demo/tsconfig.spec.json"
],
"tsConfig"
:
[
"apps/demo/tsconfig.app.json"
,
"apps/demo/tsconfig.spec.json"
],
"exclude"
:
[
"**/node_modules/**"
]
}
},
...
...
@@ -132,10 +129,7 @@
"lint"
:
{
"builder"
:
"@angular-devkit/build-angular:tslint"
,
"options"
:
{
"tsConfig"
:
[
"libs/utils/tsconfig.lib.json"
,
"libs/utils/tsconfig.spec.json"
],
"tsConfig"
:
[
"libs/utils/tsconfig.lib.json"
,
"libs/utils/tsconfig.spec.json"
],
"exclude"
:
[
"**/node_modules/**"
]
}
},
...
...
@@ -153,6 +147,90 @@
"styleext"
:
"scss"
}
}
},
"utils-security"
:
{
"projectType"
:
"library"
,
"root"
:
"libs/utils/security"
,
"sourceRoot"
:
"libs/utils/security/src"
,
"prefix"
:
"psu"
,
"architect"
:
{
"lint"
:
{
"builder"
:
"@angular-devkit/build-angular:tslint"
,
"options"
:
{
"tsConfig"
:
[
"libs/utils/security/tsconfig.lib.json"
,
"libs/utils/security/tsconfig.spec.json"
],
"exclude"
:
[
"**/node_modules/**"
,
"!libs/utils/security/**"
]
}
},
"test"
:
{
"builder"
:
"@nrwl/jest:jest"
,
"options"
:
{
"jestConfig"
:
"libs/utils/security/jest.config.js"
,
"tsConfig"
:
"libs/utils/security/tsconfig.spec.json"
,
"setupFile"
:
"libs/utils/security/src/test-setup.ts"
}
}
},
"schematics"
:
{
"@nrwl/angular:component"
:
{
"styleext"
:
"scss"
}
}
},
"utils-rx"
:
{
"projectType"
:
"library"
,
"root"
:
"libs/utils/rx"
,
"sourceRoot"
:
"libs/utils/rx/src"
,
"prefix"
:
"psu"
,
"architect"
:
{
"lint"
:
{
"builder"
:
"@angular-devkit/build-angular:tslint"
,
"options"
:
{
"tsConfig"
:
[
"libs/utils/rx/tsconfig.lib.json"
,
"libs/utils/rx/tsconfig.spec.json"
],
"exclude"
:
[
"**/node_modules/**"
,
"!libs/utils/rx/**"
]
}
},
"test"
:
{
"builder"
:
"@nrwl/jest:jest"
,
"options"
:
{
"jestConfig"
:
"libs/utils/rx/jest.config.js"
,
"tsConfig"
:
"libs/utils/rx/tsconfig.spec.json"
,
"setupFile"
:
"libs/utils/rx/src/test-setup.ts"
}
}
},
"schematics"
:
{
"@nrwl/angular:component"
:
{
"styleext"
:
"scss"
}
}
},
"utils-browser"
:
{
"projectType"
:
"library"
,
"root"
:
"libs/utils/browser"
,
"sourceRoot"
:
"libs/utils/browser/src"
,
"prefix"
:
"psu"
,
"architect"
:
{
"lint"
:
{
"builder"
:
"@angular-devkit/build-angular:tslint"
,
"options"
:
{
"tsConfig"
:
[
"libs/utils/browser/tsconfig.lib.json"
,
"libs/utils/browser/tsconfig.spec.json"
],
"exclude"
:
[
"**/node_modules/**"
,
"!libs/utils/browser/**"
]
}
},
"test"
:
{
"builder"
:
"@nrwl/jest:jest"
,
"options"
:
{
"jestConfig"
:
"libs/utils/browser/jest.config.js"
,
"tsConfig"
:
"libs/utils/browser/tsconfig.spec.json"
,
"setupFile"
:
"libs/utils/browser/src/test-setup.ts"
}
}
},
"schematics"
:
{
"@nrwl/angular:component"
:
{
"styleext"
:
"scss"
}
}
}
},
"cli"
:
{
...
...
@@ -169,7 +247,10 @@
"e2eTestRunner"
:
"cypress"
},
"@nrwl/angular:library"
:
{
"unitTestRunner"
:
"jest"
"unitTestRunner"
:
"jest"
,
"directory"
:
"utils"
,
"simpleModuleName"
:
true
,
"style"
:
"scss"
},
"@psu/schematics:ng-new"
:
{
"workspaceType"
:
"library"
...
...
libs/.gitkeep
deleted
100644 → 0
View file @
c5e07045
libs/utils/README.md
View file @
845be4e2
#
utils
#
Utils Library
This library was generated with
[
Nx
](
https://nx.dev
)
.
Monorepo for building-block type utilities that don't have dependencies on any other
`@psu`
libraries
.
## Running unit tests
-
[
Browser
](
https://git.psu.edu/ais-swe/ux/utils/tree/develop/libs/utils/browser
)
-
Cache Interceptor
-
Autofocus Directive
-
Error Service
-
Loading Events Service
-
Local Storage Service
-
Request Tracing Interceptor
-
UrlBuilder
-
Window Service
-
[
Rx
](
https://git.psu.edu/ais-swe/ux/utils/tree/develop/libs/utils/rx
)
-
Http Retry Backoff Operator
-
[
Security
](
https://git.psu.edu/ais-swe/ux/utils/tree/develop/libs/utils/security
)
-
`REQUIRE_AUTH_HEADER`
string constant
Run
`ng test utils`
to execute the unit tests.
# Contributing
Write small, portable utils, and try to keep the docs up to date as you add things.
See the README here for how to create a new entry point https://git.psu.edu/ais-swe/ux/components/blob/develop/README.md
libs/utils/browser/README.md
0 → 100644
View file @
845be4e2
# Browser Utils
## Cache Interceptor
Adds an always different
`_d`
param to any request which has the
`NO_CACHE_HEADER`
present. This header is only used by the client as a marker, and is deleted within this interceptor
Must be registered at the same level as
`HttpClientModule`
### Exports
-
`CacheInterceptor`
-
`const NO_CACHE_HEADER = 'X-No-Cache'`
## Focus Directive
Autofocuses the host element
```
html
<input
type=
"text"
utAutoFocus
/>
```
## Error Service
Provides a facade for handling http errors
```
typescript
import
{
HttpErrorService
}
from
'
@psu/utils/browser
'
;
http
.
get
(
'
some-url
'
).
pipe
(
map
(()
=>
{}),
catchError
(
e
=>
this
.
httpErrorService
.
handleError
(
e
))
);
```
## Loading Events Service
Provides method to trigger a DOM event outside of angular that can be picked up by a loading screen defined in index.html.
```
typescript
import
{
LoadingEvents
}
from
'
@psu/utils/browser
'
;
```
## Local Storage Service
Provides an abstraction layer over various local storage areas
```
typescript
import
{
LocalStorageService
}
from
'
@psu/utils/browser
'
;
```
## Request Tracing Interceptor
Provides a client-only header,
`TRACE_HEADER`
that will enable server side request tracing on any request with said header attached.
Must be registered at the same level as
`HttpClientModule`
## UrlBuilder
A class which normalizes alot of the pain around consistently building URLs
```
typescript
import
{
UrlBuilder
}
from
'
@psu/utils/browser
'
;
```
## Window Service
An abstraction over the browser's native window object.
```
typescript
import
{
WindowService
}
from
'
@psu/utils/browser
'
;
```
libs/utils/browser/jest.config.js
0 → 100644
View file @
845be4e2
module
.
exports
=
{
name
:
'
utils-browser
'
,
preset
:
'
../../../jest.config.js
'
,
coverageDirectory
:
'
../../../coverage/libs/utils/browser
'
,
snapshotSerializers
:
[
'
jest-preset-angular/AngularSnapshotSerializer.js
'
,
'
jest-preset-angular/HTMLCommentSerializer.js
'
]
};
libs/utils/browser/ng-package.json
0 → 100644
View file @
845be4e2
{
"lib"
:
{
"entryFile"
:
"src/index.ts"
}
}
libs/utils/browser/src/index.ts
0 → 100644
View file @
845be4e2
export
{
CacheInterceptor
}
from
'
./lib/cache/cache.interceptor
'
;
export
{
FocusDirective
}
from
'
./lib/focus/focus.directive
'
;
export
{
FocusModule
}
from
'
./lib/focus/focus.module
'
;
export
{
HttpErrorModule
}
from
'
./lib/http-error/http-error.module
'
;
export
{
HttpErrorService
}
from
'
./lib/http-error/http-error.service
'
;
export
{
LoadingEvents
}
from
'
./lib/loading-screen/loading-events
'
;
export
{
LoadingEventsModule
}
from
'
./lib/loading-screen/loading-events.module
'
;
export
{
LocalStorageModule
}
from
'
./lib/local-storage/local-storage.module
'
;
export
{
LocalStorageService
}
from
'
./lib/local-storage/local-storage.service
'
;
export
{
RequestTracingConfig
}
from
'
./lib/tracing/request-tracing.config
'
;
export
{
RequestTracingInterceptor
}
from
'
./lib/tracing/request-tracing.interceptor
'
;
export
{
UrlBuilder
}
from
'
./lib/url-builder/url-builder
'
;
export
{
WindowService
}
from
'
./lib/window/window.service
'
;
libs/utils/browser/src/lib/cache/cache.interceptor.spec.ts
0 → 100644
View file @
845be4e2
import
{
CacheInterceptor
,
NO_CACHE_HEADER
}
from
'
./cache.interceptor
'
;
import
*
as
MockDate
from
'
mockdate
'
;
import
{
TestBed
}
from
'
@angular/core/testing
'
;
import
{
HttpTestingController
,
HttpClientTestingModule
}
from
'
@angular/common/http/testing
'
;
import
{
HTTP_INTERCEPTORS
,
HttpClient
,
HttpRequest
,
HttpHeaders
}
from
'
@angular/common/http
'
;
import
{
Injectable
}
from
'
@angular/core
'
;
import
{
Observable
}
from
'
rxjs
'
;
@
Injectable
()
class
TestService
{
private
headers
:
HttpHeaders
;
constructor
(
private
httpClient
:
HttpClient
)
{
this
.
headers
=
new
HttpHeaders
().
append
(
NO_CACHE_HEADER
,
'
true
'
);
}
public
getNoCache
():
Observable
<
boolean
>
{
return
this
.
httpClient
.
get
<
boolean
>
(
'
busted
'
,
{
headers
:
this
.
headers
.
append
(
'
Another
'
,
'
abc
'
)
});
}
public
getNoCache2
():
Observable
<
boolean
>
{
return
this
.
httpClient
.
get
<
boolean
>
(
'
busted2
'
,
{
headers
:
this
.
headers
.
append
(
'
Content-Type
'
,
'
junk
'
),
params
:
{
a
:
'
a
'
,
b
:
'
b
'
}
});
}
public
get
():
Observable
<
boolean
>
{
return
this
.
httpClient
.
get
<
boolean
>
(
'
normal
'
);
}
}
describe
(
'
CacheInterceptor
'
,
()
=>
{
let
backend
:
HttpTestingController
;
let
service
:
TestService
;
let
now
:
Date
;
beforeEach
(()
=>
{
now
=
new
Date
();
MockDate
.
set
(
now
);
TestBed
.
configureTestingModule
({
imports
:
[
HttpClientTestingModule
],
providers
:
[{
provide
:
HTTP_INTERCEPTORS
,
useClass
:
CacheInterceptor
,
multi
:
true
},
TestService
]
});
service
=
TestBed
.
get
(
TestService
);
backend
=
TestBed
.
get
(
HttpTestingController
);
});
describe
(
'
intercept
'
,
()
=>
{
describe
(
'
when request has cache bust header
'
,
()
=>
{
beforeEach
(()
=>
{
service
.
getNoCache
().
subscribe
(
r
=>
{
expect
(
r
).
toBeTruthy
();
});
});
it
(
'
should add cache busting query param
'
,
()
=>
{
const
req
=
backend
.
expectOne
((
r
:
HttpRequest
<
any
>
)
=>
{
return
r
.
url
===
'
busted
'
;
});
expect
(
req
.
request
.
params
.
get
(
'
_d
'
)).
toEqual
(
`
${
now
.
getTime
()}
`
);
expect
(
req
.
request
.
headers
.
has
(
'
X-No-Cache
'
)).
toBe
(
false
);
expect
(
req
.
request
.
headers
.
get
(
'
Another
'
)).
toBe
(
'
abc
'
);
});
});
describe
(
'
when request has cache bust header and other query params
'
,
()
=>
{
beforeEach
(()
=>
{
service
.
getNoCache2
().
subscribe
(
r
=>
{
expect
(
r
).
toBeTruthy
();
});
});
it
(
'
should add cache busting query param
'
,
()
=>
{
const
req
=
backend
.
expectOne
((
r
:
HttpRequest
<
any
>
)
=>
{
return
r
.
url
===
'
busted2
'
;
});
expect
(
req
.
request
.
params
.
get
(
'
_d
'
)).
toEqual
(
`
${
now
.
getTime
()}
`
);
expect
(
req
.
request
.
params
.
get
(
'
a
'
)).
toEqual
(
'
a
'
);
expect
(
req
.
request
.
params
.
get
(
'
b
'
)).
toEqual
(
'
b
'
);
expect
(
req
.
request
.
headers
.
has
(
'
X-No-Cache
'
)).
toBe
(
false
);
expect
(
req
.
request
.
headers
.
get
(
'
Content-Type
'
)).
toBe
(
'
junk
'
);
});
});
describe
(
'
when request does not have cache bust header
'
,
()
=>
{
beforeEach
(()
=>
{
service
.
get
().
subscribe
(
r
=>
{
expect
(
r
).
toBeTruthy
();
});
});
it
(
'
should not add cache busting query param
'
,
()
=>
{
const
req
=
backend
.
expectOne
(
'
normal
'
);
expect
(
req
.
request
.
params
.
has
(
'
_d
'
)).
toBe
(
false
);
expect
(
req
.
request
.
headers
.
has
(
'
X-No-Cache
'
)).
toBe
(
false
);
});
});
});
});
libs/utils/browser/src/lib/cache/cache.interceptor.ts
0 → 100644
View file @
845be4e2
import
{
HttpEvent
,
HttpHandler
,
HttpInterceptor
,
HttpRequest
}
from
'
@angular/common/http
'
;
import
{
Injectable
}
from
'
@angular/core
'
;
import
{
Observable
}
from
'
rxjs
'
;
export
const
NO_CACHE_HEADER
=
'
X-No-Cache
'
;
@
Injectable
({
providedIn
:
'
root
'
})
export
class
CacheInterceptor
implements
HttpInterceptor
{
public
intercept
(
req
:
HttpRequest
<
any
>
,
next
:
HttpHandler
):
Observable
<
HttpEvent
<
any
>>
{
if
(
req
.
headers
.
has
(
NO_CACHE_HEADER
))
{
const
busted
=
req
.
clone
({
setParams
:
{
_d
:
`
${
new
Date
().
getTime
()}
`
},
headers
:
req
.
headers
.
delete
(
NO_CACHE_HEADER
)
});
return
next
.
handle
(
busted
);
}
return
next
.
handle
(
req
);
}
}
libs/utils/browser/src/lib/focus/focus.directive.spec.ts
0 → 100644
View file @
845be4e2
import
{
CommonModule
}
from
'
@angular/common
'
;
import
{
Component
,
PLATFORM_ID
}
from
'
@angular/core
'
;
import
{
ComponentFixture
,
TestBed
}
from
'
@angular/core/testing
'
;
import
{
FocusModule
}
from
'
./focus.module
'
;
@
Component
({
selector
:
'
ut-test-comp
'
,
template
:
`
<input id="inputId" utAutoFocus />
`
})
class
TestComponent
{}
describe
(
'
FocusDirective
'
,
()
=>
{
let
fixture
:
ComponentFixture
<
TestComponent
>
;
describe
(
'
when platform is browser
'
,
()
=>
{
beforeEach
(()
=>
{
TestBed
.
configureTestingModule
({
imports
:
[
CommonModule
,
FocusModule
],
declarations
:
[
TestComponent
],
providers
:
[{
provide
:
PLATFORM_ID
,
useValue
:
'
browser
'
}]
}).
compileComponents
();
fixture
=
TestBed
.
createComponent
(
TestComponent
);
fixture
.
detectChanges
();
});
it
(
'
should set focus
'
,
()
=>
{
expect
(
document
.
activeElement
.
id
).
toEqual
(
'
inputId
'
);
});
});
describe
(
'
when platform is server
'
,
()
=>
{
beforeEach
(()
=>
{
TestBed
.
configureTestingModule
({
imports
:
[
CommonModule
,
FocusModule
],
declarations
:
[
TestComponent
],
providers
:
[{
provide
:
PLATFORM_ID
,
useValue
:
'
server
'
}]
}).
compileComponents
();
fixture
=
TestBed
.
createComponent
(
TestComponent
);
fixture
.
detectChanges
();
});
it
(
'
should not set focus
'
,
()
=>
{
expect
(
document
.
activeElement
.
id
).
not
.
toBe
(
'
inputId
'
);
});
});
});
libs/utils/browser/src/lib/focus/focus.directive.ts
0 → 100644
View file @
845be4e2
import
{
isPlatformBrowser
}
from
'
@angular/common
'
;
import
{
AfterViewInit
,
Directive
,
ElementRef
,
Inject
,
PLATFORM_ID
}
from
'
@angular/core
'
;
@
Directive
({
selector
:
'
[utAutoFocus]
'
})
export
class
FocusDirective
implements
AfterViewInit
{
constructor
(
private
element
:
ElementRef
,
@
Inject
(
PLATFORM_ID
)
private
platform
:
Object
)
{}
public
ngAfterViewInit
():
void
{
if
(
isPlatformBrowser
(
this
.
platform
))
{
this
.
element
.
nativeElement
.
focus
();
}
}
}
libs/utils/browser/src/lib/focus/focus.module.ts
0 → 100644
View file @
845be4e2
import
{
NgModule
}
from
'
@angular/core
'
;
import
{
FocusDirective
}
from
'
./focus.directive
'
;
import
{
CommonModule
}
from
'
@angular/common
'
;
@
NgModule
({
imports
:
[
CommonModule
],
declarations
:
[
FocusDirective
],
exports
:
[
FocusDirective
]
})
export
class
FocusModule
{}
libs/utils/browser/src/lib/http-error/http-error.service.spec.ts
0 → 100644
View file @
845be4e2
import
{
HttpErrorResponse
}
from
'
@angular/common/http
'
;
import
{
TestBed
}
from
'
@angular/core/testing
'
;
import
{
HttpErrorService
}
from
'
./http-error.service
'
;
describe
(
'
HttpErrorService
'
,
()
=>
{
let
service
:
HttpErrorService
;
beforeEach
(()
=>
{
TestBed
.
configureTestingModule
({
providers
:
[
HttpErrorService
]
});
service
=
TestBed
.
get
(
HttpErrorService
);
});
it
(
'
should be created
'
,
()
=>
{
expect
(
service
).
toBeTruthy
();
});
describe
(
'
handleError
'
,
()
=>
{
let
result
:
string
;
let
emitted
:
boolean
;
beforeEach
(()
=>
{
emitted
=
false
;
});
describe
(
'
when error message is a string
'
,
()
=>
{
beforeEach
(()
=>
{
service
.
handleError
(
'
some error
'
).
subscribe
(()
=>
(
emitted
=
true
),
e
=>
(
result
=
e
));
});
it
(
'
should not emit
'
,
()
=>
{
expect
(
emitted
).
toBe
(
false
);
});
it
(
'
should error with some error
'
,
()
=>
{
expect
(
result
).
toBe
(
'
some error
'
);
});
});
describe
(
'
when error message is specified
'
,
()
=>
{
beforeEach
(()
=>
{
service
.
handleError
(
new
HttpErrorResponse
({
status
:
404
,
statusText
:
'
Not Found
'
,
error
:
{
errorMessage
:
'
oh no
'
}
})
)
.
subscribe
(()
=>
(
emitted
=
true
),
e
=>
(
result
=
e
));
});
it
(
'
should not emit
'
,
()
=>
{
expect
(
emitted
).
toBe
(
false
);
});
it
(
'
should error with oh no
'
,
()
=>
{
expect
(
result
).
toBe
(
'
oh no
'
);
});
});
describe
(
'
when error message is not specified
'
,
()
=>
{
beforeEach
(()
=>
{
service
.
handleError
(
new
HttpErrorResponse
({
status
:
404
,
statusText
:
'
Not Found
'
,
error
:
{}
})
)
.
subscribe
(()
=>
(
emitted
=
true
),
e
=>
(
result
=
e
));
});
it
(
'
should not emit
'
,
()
=>
{
expect
(
emitted
).
toBe
(
false
);
});
it
(
'
should error with oops
'
,
()
=>
{
expect
(
result
).
toBe
(
'
Oops, something went wrong!
'
);
});
});
describe
(
'
when error message is not specified with default
'
,
()
=>
{
beforeEach
(()
=>
{
service
.
handleError
(
new
HttpErrorResponse
({
status
:
404
,
statusText
:
'
Not Found
'
,
error
:
{}
}),
'
my message
'
)
.
subscribe
(()
=>
(
emitted
=
true
),
e
=>
(
result
=
e
));
});
it
(
'
should not emit
'
,
()
=>
{
expect
(
emitted
).
toBe
(
false
);
});
it
(
'
should error with custom message
'
,
()
=>
{
expect
(
result
).
toBe
(
'
my message
'
);
});
});