2017-09-11 03:18:55 -04:00
|
|
|
/**
|
|
|
|
* @license
|
2020-05-19 15:08:49 -04:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2017-09-11 03:18:55 -04:00
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2019-03-11 20:20:40 -04:00
|
|
|
import {DOCUMENT} from '@angular/common';
|
2017-09-11 03:18:55 -04:00
|
|
|
import {TestBed} from '@angular/core/testing';
|
|
|
|
import {BrowserModule, BrowserTransferStateModule, TransferState} from '@angular/platform-browser';
|
2021-03-16 15:10:32 -04:00
|
|
|
import {escapeHtml, makeStateKey, unescapeHtml} from '@angular/platform-browser/src/browser/transfer_state';
|
2017-09-11 03:18:55 -04:00
|
|
|
|
2017-12-17 18:10:54 -05:00
|
|
|
(function() {
|
2020-04-13 19:40:21 -04:00
|
|
|
function removeScriptTag(doc: Document, id: string) {
|
|
|
|
const existing = doc.getElementById(id);
|
|
|
|
if (existing) {
|
|
|
|
doc.body.removeChild(existing);
|
2017-09-11 03:18:55 -04:00
|
|
|
}
|
2020-04-13 19:40:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function addScriptTag(doc: Document, appId: string, data: {}) {
|
|
|
|
const script = doc.createElement('script');
|
|
|
|
const id = appId + '-state';
|
|
|
|
script.id = id;
|
|
|
|
script.setAttribute('type', 'application/json');
|
|
|
|
script.textContent = escapeHtml(JSON.stringify(data));
|
|
|
|
|
|
|
|
// Remove any stale script tags.
|
|
|
|
removeScriptTag(doc, id);
|
|
|
|
|
|
|
|
doc.body.appendChild(script);
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('TransferState', () => {
|
|
|
|
const APP_ID = 'test-app';
|
|
|
|
let doc: Document;
|
|
|
|
|
|
|
|
const TEST_KEY = makeStateKey<number>('test');
|
2021-03-16 15:10:32 -04:00
|
|
|
const BOOLEAN_KEY = makeStateKey<boolean>('boolean');
|
2020-04-13 19:40:21 -04:00
|
|
|
const DELAYED_KEY = makeStateKey<string>('delayed');
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
TestBed.configureTestingModule({
|
|
|
|
imports: [
|
|
|
|
BrowserModule.withServerTransition({appId: APP_ID}),
|
|
|
|
BrowserTransferStateModule,
|
|
|
|
]
|
2017-09-11 03:18:55 -04:00
|
|
|
});
|
2020-04-13 19:40:21 -04:00
|
|
|
doc = TestBed.inject(DOCUMENT);
|
|
|
|
});
|
2017-09-11 03:18:55 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
afterEach(() => {
|
|
|
|
removeScriptTag(doc, APP_ID + '-state');
|
|
|
|
});
|
2017-09-11 03:18:55 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
it('is initialized from script tag', () => {
|
|
|
|
addScriptTag(doc, APP_ID, {test: 10});
|
|
|
|
const transferState: TransferState = TestBed.inject(TransferState);
|
|
|
|
expect(transferState.get(TEST_KEY, 0)).toBe(10);
|
|
|
|
});
|
2017-09-11 03:18:55 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
it('is initialized to empty state if script tag not found', () => {
|
|
|
|
const transferState: TransferState = TestBed.inject(TransferState);
|
|
|
|
expect(transferState.get(TEST_KEY, 0)).toBe(0);
|
|
|
|
});
|
2017-09-11 03:18:55 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
it('supports adding new keys using set', () => {
|
|
|
|
const transferState: TransferState = TestBed.inject(TransferState);
|
|
|
|
transferState.set(TEST_KEY, 20);
|
|
|
|
expect(transferState.get(TEST_KEY, 0)).toBe(20);
|
|
|
|
expect(transferState.hasKey(TEST_KEY)).toBe(true);
|
|
|
|
});
|
2017-09-11 03:18:55 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
it('supports setting and accessing value \'0\' via get', () => {
|
|
|
|
const transferState: TransferState = TestBed.inject(TransferState);
|
|
|
|
transferState.set(TEST_KEY, 0);
|
|
|
|
expect(transferState.get(TEST_KEY, 20)).toBe(0);
|
|
|
|
expect(transferState.hasKey(TEST_KEY)).toBe(true);
|
|
|
|
});
|
2018-02-12 16:03:53 -05:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
it('supports setting and accessing value \'false\' via get', () => {
|
|
|
|
const transferState: TransferState = TestBed.inject(TransferState);
|
2021-03-16 15:10:32 -04:00
|
|
|
transferState.set(BOOLEAN_KEY, false);
|
|
|
|
expect(transferState.get(BOOLEAN_KEY, true)).toBe(false);
|
|
|
|
expect(transferState.hasKey(BOOLEAN_KEY)).toBe(true);
|
2020-04-13 19:40:21 -04:00
|
|
|
});
|
2018-02-12 16:03:53 -05:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
it('supports setting and accessing value \'null\' via get', () => {
|
|
|
|
const transferState: TransferState = TestBed.inject(TransferState);
|
|
|
|
transferState.set(TEST_KEY, null);
|
|
|
|
expect(transferState.get(TEST_KEY, 20 as any)).toBe(null);
|
|
|
|
expect(transferState.hasKey(TEST_KEY)).toBe(true);
|
|
|
|
});
|
2018-02-12 16:03:53 -05:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
it('supports removing keys', () => {
|
|
|
|
const transferState: TransferState = TestBed.inject(TransferState);
|
|
|
|
transferState.set(TEST_KEY, 20);
|
|
|
|
transferState.remove(TEST_KEY);
|
|
|
|
expect(transferState.get(TEST_KEY, 0)).toBe(0);
|
|
|
|
expect(transferState.hasKey(TEST_KEY)).toBe(false);
|
|
|
|
});
|
2017-09-11 03:18:55 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
it('supports serialization using toJson()', () => {
|
|
|
|
const transferState: TransferState = TestBed.inject(TransferState);
|
|
|
|
transferState.set(TEST_KEY, 20);
|
|
|
|
expect(transferState.toJson()).toBe('{"test":20}');
|
|
|
|
});
|
2017-09-11 03:18:55 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
it('calls onSerialize callbacks when calling toJson()', () => {
|
|
|
|
const transferState: TransferState = TestBed.inject(TransferState);
|
|
|
|
transferState.set(TEST_KEY, 20);
|
2017-09-11 03:18:55 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
let value = 'initial';
|
|
|
|
transferState.onSerialize(DELAYED_KEY, () => value);
|
|
|
|
value = 'changed';
|
2017-09-11 03:18:55 -04:00
|
|
|
|
2020-04-13 19:40:21 -04:00
|
|
|
expect(transferState.toJson()).toBe('{"test":20,"delayed":"changed"}');
|
2017-09-11 03:18:55 -04:00
|
|
|
});
|
2020-04-13 19:40:21 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('escape/unescape', () => {
|
|
|
|
it('works with all escaped characters', () => {
|
|
|
|
const testString = '</script><script>alert(\'Hello&\' + "World");';
|
|
|
|
const testObj = {testString};
|
|
|
|
const escaped = escapeHtml(JSON.stringify(testObj));
|
|
|
|
expect(escaped).toBe(
|
|
|
|
'{&q;testString&q;:&q;&l;/script&g;&l;script&g;' +
|
|
|
|
'alert(&s;Hello&a;&s; + \\&q;World\\&q;);&q;}');
|
|
|
|
|
2021-02-04 17:17:17 -05:00
|
|
|
const unescapedObj = JSON.parse(unescapeHtml(escaped)) as {testString: string};
|
2020-04-13 19:40:21 -04:00
|
|
|
expect(unescapedObj['testString']).toBe(testString);
|
2017-09-11 03:18:55 -04:00
|
|
|
});
|
2020-04-13 19:40:21 -04:00
|
|
|
});
|
2017-10-24 07:54:08 -04:00
|
|
|
})();
|