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

Commit

Permalink
feat: provide typesafe mocks for SSR_USER_AGENT & SSR_LOCATION (#41)
Browse files Browse the repository at this point in the history
* feat: provide typesafe mocks for `SSR_USER_AGENT` & `SSR_LOCATION`

* chore: move mock-fallbacks (for user-agent & location) to another places
  • Loading branch information
nsbarsukov committed Dec 14, 2022
1 parent df7569c commit ee98d1b
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 20 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ from requests. Use the following helpers to harvest that info:
**server.ts:**

```typescript
import {provideLocation, provideUserAgent} from '@ng-web-apis/universal';
// ...
app.get('/**/*', (req: Request, res: Response) => {
res.render('../dist/index', {
req,
Expand Down
9 changes: 9 additions & 0 deletions projects/universal/src/classes/dom-string-list-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export class DOMStringListMock extends Array<string> implements DOMStringList {
contains(): boolean {
return false;
}

item(): null {
return null;
}
}
21 changes: 21 additions & 0 deletions projects/universal/src/classes/location-mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {emptyFunction} from '../utils/functions';
import {DOMStringListMock} from './dom-string-list-mock';

export class LocationMock implements Location {
readonly ancestorOrigins = new DOMStringListMock();
hash = '';
host = '';
hostname = '';
href = '';
readonly origin = '';
pathname = '';
port = '';
protocol = '';
search = '';

assign = emptyFunction;

reload = emptyFunction;

replace = emptyFunction;
}
9 changes: 5 additions & 4 deletions projects/universal/src/constants/universal-location.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {FactoryProvider} from '@angular/core';
import {FactoryProvider, Optional} from '@angular/core';
import {LOCATION} from '@ng-web-apis/common';

import {LocationMock} from '../classes/location-mock';
import {SSR_LOCATION} from '../tokens/ssr-location';
import {identity} from '../utils/functions';

export const UNIVERSAL_LOCATION: FactoryProvider = {
provide: LOCATION,
deps: [SSR_LOCATION],
useFactory: identity,
deps: [[new Optional(), SSR_LOCATION]],
useFactory: (location: Location | null) => location || new LocationMock(),
};
8 changes: 4 additions & 4 deletions projects/universal/src/constants/universal-user-agent.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {FactoryProvider} from '@angular/core';
import {FactoryProvider, Optional} from '@angular/core';
import {USER_AGENT} from '@ng-web-apis/common';

import {SSR_USER_AGENT} from '../tokens/ssr-user-agent';
import {identity} from '../utils/functions';

export const UNIVERSAL_USER_AGENT: FactoryProvider = {
provide: USER_AGENT,
deps: [SSR_USER_AGENT],
useFactory: identity,
deps: [[new Optional(), SSR_USER_AGENT]],
useFactory: (userAgent: string | null) => userAgent || '',
};
8 changes: 5 additions & 3 deletions projects/universal/src/constants/universal-window.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import {DOCUMENT} from '@angular/common';
import {FactoryProvider, Optional} from '@angular/core';
import {WINDOW} from '@ng-web-apis/common';

import {BlobMock} from '../classes/blob-mock';
import {LocationMock} from '../classes/location-mock';
import {StorageMock} from '../classes/storage-mock';
import {SSR_LOCATION} from '../tokens/ssr-location';
import {SSR_USER_AGENT} from '../tokens/ssr-user-agent';
Expand Down Expand Up @@ -65,14 +67,14 @@ const WINDOW_HANDLER: ProxyHandler<Window> = {

export function windowFactory(
document: Document,
location: Location,
userAgent: string,
location: Location | null,
userAgent: string | null,
): Window {
const windowMock: Window = {
...EVENT_TARGET,
document,
localStorage: new StorageMock(),
location: location || {},
location: location || new LocationMock(),
navigator: {...NAVIGATOR_MOCK, userAgent: userAgent || ''},
performance: performanceFactory(),
sessionStorage: new StorageMock(),
Expand Down
12 changes: 3 additions & 9 deletions projects/universal/src/utils/provide-location.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {ValueProvider} from '@angular/core';
import {IncomingMessage} from 'http';

import {DOMStringListMock} from '../classes/dom-string-list-mock';
import {SSR_LOCATION} from '../tokens/ssr-location';
import {emptyFunction} from './functions';

Expand All @@ -10,15 +12,7 @@ export function provideLocation(req: IncomingMessage): ValueProvider {
url.assign = emptyFunction;
url.reload = emptyFunction;
url.replace = emptyFunction;
url.ancestorOrigins = new (class extends Array<string> implements DOMStringList {
contains(): boolean {
return false;
}

item(): null {
return null;
}
})();
url.ancestorOrigins = new DOMStringListMock();

return {
provide: SSR_LOCATION,
Expand Down

0 comments on commit ee98d1b

Please sign in to comment.