Skip to content
This repository has been archived by the owner on May 29, 2023. It is now read-only.

Commit

Permalink
chore(comments): fix 'em
Browse files Browse the repository at this point in the history
  • Loading branch information
waterplea committed Mar 5, 2021
1 parent e873246 commit 8ec86c4
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 125 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ import '@ng-web-apis/universal/mocks';

## Tokens

You can provide tokens from this package into your `app.server.module.ts`
to have type safe mocks for global objects on server side:

```ts
@NgModule({
imports: [AppBrowserModule, ServerModule],
bootstrap: [AppComponent],
providers: [UNIVERSAL_WINDOW],
})
export class AppServerModule {}
```

- `WINDOW` — add `UNIVERSAL_WINDOW` to provide type-safe mock object, effectively mocking all `navigator` related entities
- `NAVIGATOR` — add `UNIVERSAL_NAVIGATOR` to provide type-safe mock object, effectively mocking all `navigator` related entities
- `NETWORK_INFORMATION` — no need to do anything
Expand Down
4 changes: 2 additions & 2 deletions projects/universal/src/constants/universal-location.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {FactoryProvider} from '@angular/core';
import {USER_AGENT} from '@ng-web-apis/common';
import {LOCATION} from '@ng-web-apis/common';
import {SSR_LOCATION} from '../tokens/ssr-location';
import {identity} from '../utils/functions';

export const UNIVERSAL_LOCATION: FactoryProvider = {
provide: USER_AGENT,
provide: LOCATION,
deps: [SSR_LOCATION],
useFactory: identity,
};
187 changes: 96 additions & 91 deletions projects/universal/src/constants/universal-navigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,100 +50,105 @@ interface NavigatorLike extends Navigator {
[key: string]: any;
}

export const NAVIGATOR_MOCK: NavigatorLike = {
appCodeName: '',
appName: '',
appVersion: '',
platform: '',
product: '',
productSub: '',
userAgent: '',
vendor: '',
vendorSub: '',

onLine: false,

confirmSiteSpecificTrackingException: alwaysFalse,
confirmWebWideTrackingException: alwaysFalse,
removeSiteSpecificTrackingException: emptyFunction,
removeWebWideTrackingException: emptyFunction,
storeSiteSpecificTrackingException: emptyFunction,
storeWebWideTrackingException: emptyFunction,

msSaveBlob: alwaysFalse,
msSaveOrOpenBlob: alwaysFalse,

sendBeacon: alwaysFalse,

hardwareConcurrency: 0,

getDisplayMedia: alwaysRejected,

language: '',
languages: [],

storage: {
estimate: alwaysRejected,
persist: alwaysRejected,
persisted: alwaysRejected,
},

activeVRDisplays: [],
authentication: {
getAssertion: alwaysRejected,
makeCredential: alwaysRejected,
},
clipboard: {
...EVENT_TARGET,
readText: alwaysRejected,
writeText: alwaysRejected,
},
cookieEnabled: false,
doNotTrack: null,
gamepadInputEmulation: 'keyboard',
geolocation: {
clearWatch: emptyFunction,
getCurrentPosition: emptyFunction,
watchPosition: alwaysZero,
},
maxTouchPoints: 0,
mediaDevices: {
...EVENT_TARGET,
ondevicechange: null,
enumerateDevices: alwaysRejected,
getSupportedConstraints: emptyObject,
getUserMedia: alwaysRejected,
},
mimeTypes: MIME_TYPES,
msManipulationViewsEnabled: false,
msMaxTouchPoints: 0,
msPointerEnabled: false,
permissions: {
query: alwaysRejected,
},
plugins: MIME_TYPES,
pointerEnabled: false,
serviceWorker: {
...EVENT_TARGET,
controller: null,
oncontrollerchange: null,
onmessage: null,
onmessageerror: null,
ready: alwaysRejected(),
getRegistration: alwaysRejected,
getRegistrations: alwaysRejected,
register: alwaysRejected,
startMessages: emptyFunction,
},
webdriver: false,
getGamepads: emptyArray,
getUserMedia: emptyFunction,
getVRDisplays: alwaysRejected,
javaEnabled: alwaysFalse,
msLaunchUri: emptyFunction,
requestMediaKeySystemAccess: alwaysRejected,
vibrate: alwaysFalse,
};

export function navigatorFactory(userAgent: string | null): NavigatorLike {
return {
appCodeName: '',
appName: '',
appVersion: '',
platform: '',
product: '',
productSub: '',
...NAVIGATOR_MOCK,
userAgent: userAgent || '',
vendor: '',
vendorSub: '',

onLine: false,

confirmSiteSpecificTrackingException: alwaysFalse,
confirmWebWideTrackingException: alwaysFalse,
removeSiteSpecificTrackingException: emptyFunction,
removeWebWideTrackingException: emptyFunction,
storeSiteSpecificTrackingException: emptyFunction,
storeWebWideTrackingException: emptyFunction,

msSaveBlob: alwaysFalse,
msSaveOrOpenBlob: alwaysFalse,

sendBeacon: alwaysFalse,

hardwareConcurrency: 0,

getDisplayMedia: alwaysRejected,

language: '',
languages: [],

storage: {
estimate: alwaysRejected,
persist: alwaysRejected,
persisted: alwaysRejected,
},

activeVRDisplays: [],
authentication: {
getAssertion: alwaysRejected,
makeCredential: alwaysRejected,
},
clipboard: {
...EVENT_TARGET,
readText: alwaysRejected,
writeText: alwaysRejected,
},
cookieEnabled: false,
doNotTrack: null,
gamepadInputEmulation: 'keyboard',
geolocation: {
clearWatch: emptyFunction,
getCurrentPosition: emptyFunction,
watchPosition: alwaysZero,
},
maxTouchPoints: 0,
mediaDevices: {
...EVENT_TARGET,
ondevicechange: null,
enumerateDevices: alwaysRejected,
getSupportedConstraints: emptyObject,
getUserMedia: alwaysRejected,
},
mimeTypes: MIME_TYPES,
msManipulationViewsEnabled: false,
msMaxTouchPoints: 0,
msPointerEnabled: false,
permissions: {
query: alwaysRejected,
},
plugins: MIME_TYPES,
pointerEnabled: false,
serviceWorker: {
...EVENT_TARGET,
controller: null,
oncontrollerchange: null,
onmessage: null,
onmessageerror: null,
ready: alwaysRejected(),
getRegistration: alwaysRejected,
getRegistrations: alwaysRejected,
register: alwaysRejected,
startMessages: emptyFunction,
},
webdriver: false,
getGamepads: emptyArray,
getUserMedia: emptyFunction,
getVRDisplays: alwaysRejected,
javaEnabled: alwaysFalse,
msLaunchUri: emptyFunction,
requestMediaKeySystemAccess: alwaysRejected,
vibrate: alwaysFalse,
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {ValueProvider} from '@angular/core';
import {SPEECH_SYNTHESIS} from '@ng-web-apis/common';
import {alwaysFalse, emptyArray, emptyFunction} from '../utils/functions';

const MOCK: SpeechSynthesis = {
export const SPEECH_SYNTHESIS_MOCK: SpeechSynthesis = {
paused: false,
pending: false,
speaking: false,
Expand All @@ -19,5 +19,5 @@ const MOCK: SpeechSynthesis = {

export const UNIVERSAL_SPEECH_SYNTHESIS: ValueProvider = {
provide: SPEECH_SYNTHESIS,
useValue: MOCK,
useValue: SPEECH_SYNTHESIS_MOCK,
};
46 changes: 16 additions & 30 deletions projects/universal/src/constants/universal-window.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
import {DOCUMENT} from '@angular/common';
import {FactoryProvider} from '@angular/core';
import {
LOCAL_STORAGE,
LOCATION,
NAVIGATOR,
PERFORMANCE,
SESSION_STORAGE,
SPEECH_SYNTHESIS,
WINDOW,
} from '@ng-web-apis/common';
import {FactoryProvider, Optional} from '@angular/core';
import {WINDOW} from '@ng-web-apis/common';
import {BlobMock} from '../classes/blob-mock';
import {StorageMock} from '../classes/storage-mock';
import {SSR_LOCATION} from '../tokens/ssr-location';
import {SSR_USER_AGENT} from '../tokens/ssr-user-agent';
import {EVENT_TARGET} from '../utils/event-target';
import {
alwaysFalse,
Expand All @@ -19,6 +14,9 @@ import {
emptyFunction,
identity,
} from '../utils/functions';
import {NAVIGATOR_MOCK} from './universal-navigator';
import {performanceFactory} from './universal-performance';
import {SPEECH_SYNTHESIS_MOCK} from './universal-speech-synthesis';

const COMPUTED_STYLES: Partial<CSSStyleDeclaration> = {
getPropertyPriority: () => '',
Expand Down Expand Up @@ -65,22 +63,18 @@ const WINDOW_HANDLER: ProxyHandler<Window> = {

export function windowFactory(
document: Document,
navigator: Navigator,
localStorage: Storage,
location: Location,
performance: Performance,
sessionStorage: Storage,
speechSynthesis: SpeechSynthesis,
userAgent: string,
): Window {
const windowMock: Window = {
...EVENT_TARGET,
document,
localStorage,
location,
navigator,
performance,
sessionStorage,
speechSynthesis,
localStorage: new StorageMock(),
location: location || {},
navigator: {...NAVIGATOR_MOCK, userAgent: userAgent || ''},
performance: performanceFactory(),
sessionStorage: new StorageMock(),
speechSynthesis: SPEECH_SYNTHESIS_MOCK,
URL,
URLSearchParams,
setTimeout,
Expand Down Expand Up @@ -205,14 +199,6 @@ export function windowFactory(

export const UNIVERSAL_WINDOW: FactoryProvider = {
provide: WINDOW,
deps: [
DOCUMENT,
LOCAL_STORAGE,
LOCATION,
NAVIGATOR,
PERFORMANCE,
SESSION_STORAGE,
SPEECH_SYNTHESIS,
],
deps: [DOCUMENT, [new Optional(), SSR_LOCATION], [new Optional(), SSR_USER_AGENT]],
useFactory: windowFactory,
};

0 comments on commit 8ec86c4

Please sign in to comment.